2026-01-29 12:44:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用户信息更新API
|
|
|
|
|
|
* 支持更新昵称、头像、手机号、微信号、支付宝、地址等
|
2026-02-06 18:34:02 +08:00
|
|
|
|
* 使用 Prisma ORM(安全,防SQL注入)
|
2026-01-29 12:44:29 +08:00
|
|
|
|
*/
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
2026-02-06 18:34:02 +08:00
|
|
|
|
import { prisma } from '@/lib/prisma'
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
2026-01-29 12:44:29 +08:00
|
|
|
|
export async function POST(request: NextRequest) {
|
2026-01-29 11:35:56 +08:00
|
|
|
|
try {
|
2026-01-29 12:44:29 +08:00
|
|
|
|
const body = await request.json()
|
|
|
|
|
|
const { userId, nickname, avatar, phone, wechat, alipay, address, autoWithdraw, withdrawAccount } = body
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
if (!userId) {
|
2026-01-29 12:44:29 +08:00
|
|
|
|
return NextResponse.json({ success: false, message: '缺少用户ID' }, { status: 400 })
|
2026-01-29 11:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-06 18:34:02 +08:00
|
|
|
|
// 构建 Prisma 更新数据对象
|
|
|
|
|
|
const updateData: any = { updated_at: new Date() }
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
2026-02-06 18:34:02 +08:00
|
|
|
|
if (nickname !== undefined) updateData.nickname = nickname
|
|
|
|
|
|
if (avatar !== undefined) updateData.avatar = avatar
|
|
|
|
|
|
if (phone !== undefined) updateData.phone = phone
|
|
|
|
|
|
if (wechat !== undefined) updateData.wechat_id = wechat // 映射到 wechat_id 字段
|
|
|
|
|
|
// 注意:alipay, address, auto_withdraw, withdraw_account 在 schema 中不存在,需要先添加字段或移除
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
2026-02-06 18:34:02 +08:00
|
|
|
|
if (Object.keys(updateData).length === 1) {
|
2026-01-29 12:44:29 +08:00
|
|
|
|
return NextResponse.json({ success: false, message: '没有需要更新的字段' }, { status: 400 })
|
|
|
|
|
|
}
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
2026-02-06 18:34:02 +08:00
|
|
|
|
// 执行更新(Prisma 自动防SQL注入)
|
|
|
|
|
|
await prisma.users.update({
|
|
|
|
|
|
where: { id: userId },
|
|
|
|
|
|
data: updateData
|
|
|
|
|
|
})
|
2026-01-29 11:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
success: true,
|
2026-01-29 12:44:29 +08:00
|
|
|
|
message: '更新成功'
|
2026-01-29 11:35:56 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[User Update] Error:', error)
|
2026-01-29 12:44:29 +08:00
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '更新失败',
|
|
|
|
|
|
error: String(error)
|
|
|
|
|
|
}, { status: 500 })
|
2026-01-29 11:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|