Files
2026-07-09 14:35:32 +08:00

49 lines
1.3 KiB
TypeScript

import { nowStr, requireAuth } from '~/server/utils/auth'
import { getUserById } from '~/server/utils/db'
import { execute } from '~/server/utils/mysql'
import { fail, ok } from '~/server/utils/response'
/** 当前登录用户更新个人资料 */
export default defineEventHandler(async (event) => {
const auth = requireAuth(event)
if (!auth) {
setResponseStatus(event, 401)
return fail('未授权,请先登录', 401)
}
const user = await getUserById(auth.userId)
if (!user) return fail('用户不存在')
const body = await readBody<{
name?: string
phone?: string
email?: string
avatar?: string
bio?: string
tags?: string
}>(event)
const now = nowStr()
await execute(
`UPDATE admin_users
SET name = ?, phone = ?, email = ?, avatar = ?, bio = ?, tags = ?, update_time = ?
WHERE id = ?`,
[
body.name ?? user.name ?? null,
body.phone ?? user.phone ?? null,
body.email ?? user.email ?? null,
body.avatar ?? user.avatar ?? null,
body.bio ?? user.bio ?? null,
body.tags ?? user.tags ?? null,
now,
auth.userId,
]
)
const updated = await getUserById(auth.userId)
if (!updated) return fail('用户不存在')
const { password: _password, ...safeUser } = updated
return ok(safeUser, '更新成功')
})