提交正式官网
This commit is contained in:
@@ -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)
|
||||
})
|
||||
Reference in New Issue
Block a user