提交正式官网
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
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 id = getRouterParam(event, 'id')
|
||||
if (!id) return fail('用户ID不能为空')
|
||||
|
||||
const user = await getUserById(id)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
return ok(user, '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,132 @@
|
||||
import { md5, nowStr, requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById, rowToUser, type AdminUserRow } from '~/server/utils/db'
|
||||
import { execute, query, queryOne } from '~/server/utils/mysql'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function authOrFail(event: Parameters<typeof requireAuth>[0]) {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return null
|
||||
}
|
||||
return auth
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = authOrFail(event)
|
||||
if (!auth) return fail('未授权,请先登录', 401)
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const queryParams = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(queryParams.pageSize || '10'), 10))
|
||||
const username = String(queryParams.username || '')
|
||||
const name = String(queryParams.name || '')
|
||||
const status = queryParams.status ? String(queryParams.status) : ''
|
||||
|
||||
let rows = await query<AdminUserRow>('SELECT * FROM admin_users ORDER BY create_time DESC')
|
||||
|
||||
if (username) rows = rows.filter((r) => r.username.includes(username))
|
||||
if (name) rows = rows.filter((r) => (r.name || '').includes(name))
|
||||
if (status) rows = rows.filter((r) => r.status === status)
|
||||
|
||||
const total = rows.length
|
||||
const list = rows.slice((page - 1) * pageSize, page * pageSize).map(rowToUser)
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'POST') {
|
||||
const body = await readBody<{
|
||||
username?: string
|
||||
password?: string
|
||||
name?: string
|
||||
phone?: string
|
||||
email?: string
|
||||
roleId?: string
|
||||
status?: 'active' | 'inactive'
|
||||
}>(event)
|
||||
|
||||
if (!body.username?.trim() || !body.password) {
|
||||
return fail('用户名和密码不能为空')
|
||||
}
|
||||
|
||||
const exists = await queryOne<{ id: number }>(
|
||||
'SELECT id FROM admin_users WHERE username = ?',
|
||||
[body.username.trim()]
|
||||
)
|
||||
if (exists) return fail('用户名已存在')
|
||||
|
||||
const now = nowStr()
|
||||
await execute(
|
||||
`INSERT INTO admin_users (username, password, name, phone, email, role_id, status, is_built_in, create_time, update_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?)`,
|
||||
[
|
||||
body.username.trim(),
|
||||
md5(body.password),
|
||||
body.name || null,
|
||||
body.phone || null,
|
||||
body.email || null,
|
||||
body.roleId ? Number(body.roleId) || null : null,
|
||||
body.status || 'active',
|
||||
now,
|
||||
now,
|
||||
]
|
||||
)
|
||||
|
||||
return ok(null, '创建成功')
|
||||
}
|
||||
|
||||
if (method === 'PUT') {
|
||||
const body = await readBody<{
|
||||
id?: string
|
||||
username?: string
|
||||
password?: string
|
||||
name?: string
|
||||
phone?: string
|
||||
email?: string
|
||||
roleId?: string
|
||||
status?: 'active' | 'inactive'
|
||||
}>(event)
|
||||
|
||||
if (!body.id) return fail('用户ID不能为空')
|
||||
const existing = await getUserById(body.id)
|
||||
if (!existing) return fail('用户不存在')
|
||||
|
||||
const now = nowStr()
|
||||
const password = body.password ? md5(body.password) : existing.password
|
||||
|
||||
await execute(
|
||||
`UPDATE admin_users
|
||||
SET username = ?, password = ?, name = ?, phone = ?, email = ?, role_id = ?, status = ?, update_time = ?
|
||||
WHERE id = ?`,
|
||||
[
|
||||
body.username?.trim() || existing.username,
|
||||
password,
|
||||
body.name ?? existing.name ?? null,
|
||||
body.phone ?? existing.phone ?? null,
|
||||
body.email ?? existing.email ?? null,
|
||||
body.roleId ? Number(body.roleId) || null : existing.roleId ? Number(existing.roleId) || null : null,
|
||||
body.status || existing.status,
|
||||
now,
|
||||
body.id,
|
||||
]
|
||||
)
|
||||
|
||||
return ok(null, '更新成功')
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<string[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的用户')
|
||||
|
||||
for (const id of ids) {
|
||||
await execute('DELETE FROM admin_users WHERE id = ? AND is_built_in = 0', [id])
|
||||
}
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
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('用户不存在')
|
||||
|
||||
return ok(user, '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,14 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
/** 记录登录日志(当前仅接收并返回成功,避免前端报错) */
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
await readBody(event).catch(() => null)
|
||||
return ok(null, '添加登录日志成功')
|
||||
})
|
||||
@@ -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, '密码修改成功')
|
||||
})
|
||||
@@ -0,0 +1,26 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getRoleById, getUserById } from '~/server/utils/db'
|
||||
import { getPermissionsForRole } from '~/server/utils/menus'
|
||||
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('用户不存在')
|
||||
|
||||
if (!user.roleId) {
|
||||
return ok({ menus: [], buttonPermissions: [] }, '获取成功')
|
||||
}
|
||||
|
||||
const role = await getRoleById(user.roleId)
|
||||
if (!role?.menuIds?.length) {
|
||||
return ok({ menus: [], buttonPermissions: [] }, '获取成功')
|
||||
}
|
||||
|
||||
return ok(getPermissionsForRole(role.menuIds), '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
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, '更新成功')
|
||||
})
|
||||
Reference in New Issue
Block a user