提交正式官网
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { md5, 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<{
|
||||
oldPassword?: string
|
||||
newPassword?: string
|
||||
confirmPassword?: string
|
||||
}>(event)
|
||||
|
||||
const { oldPassword, newPassword, confirmPassword } = body
|
||||
|
||||
if (!oldPassword || !newPassword || !confirmPassword) {
|
||||
return fail('旧密码、新密码和确认密码不能为空')
|
||||
}
|
||||
|
||||
if (newPassword.length < 6) {
|
||||
return fail('新密码长度至少6位')
|
||||
}
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
return fail('新密码和确认密码不一致')
|
||||
}
|
||||
|
||||
if (user.password !== md5(oldPassword)) {
|
||||
return fail('旧密码错误')
|
||||
}
|
||||
|
||||
const now = nowStr()
|
||||
await execute('UPDATE admin_users SET password = ?, update_time = ? WHERE id = ?', [
|
||||
md5(newPassword),
|
||||
now,
|
||||
auth.userId,
|
||||
])
|
||||
|
||||
return ok(null, '密码修改成功')
|
||||
})
|
||||
Reference in New Issue
Block a user