新功能: 1. 部门管理功能 - 管理员可在系统设置中添加/删除部门 - 用户注册时从下拉框选择部门 - 新增Department数据库表和API 2. 文件上传优化 - 支持直接上传源文件(.max/.psd/.ai/.cad/.dwg/.c4d) - 自动识别文件类型并设置素材类型 - 压缩包需手动选择类型 - 优化拖拽上传体验 数据库变更: - 新增departments表 - User表新增department字段
65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import bcrypt from 'bcryptjs'
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const { email, username, department, password } = await req.json()
|
|
|
|
// Validation
|
|
if (!email || !username || !password) {
|
|
return NextResponse.json(
|
|
{ error: '所有字段都是必填的' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Check if user exists
|
|
const existingUser = await prisma.user.findFirst({
|
|
where: {
|
|
OR: [{ email }, { username }],
|
|
},
|
|
})
|
|
|
|
if (existingUser) {
|
|
return NextResponse.json(
|
|
{ error: '邮箱或用户名已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Hash password
|
|
const hashedPassword = await bcrypt.hash(password, 10)
|
|
|
|
// Create user
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
email,
|
|
username,
|
|
department: department || '',
|
|
password: hashedPassword,
|
|
},
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
username: true,
|
|
department: true,
|
|
role: true,
|
|
createdAt: true,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(
|
|
{ message: '注册成功', user },
|
|
{ status: 201 }
|
|
)
|
|
} catch (error) {
|
|
console.error('Registration error:', error)
|
|
return NextResponse.json(
|
|
{ error: '注册失败,请稍后再试' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|