瞪眼法测试通过
This commit is contained in:
@@ -118,6 +118,21 @@ async function info(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/user/list —— 简易用户列表(用于下拉选择负责人,仅需登录)
|
||||
async function simpleList(req, res) {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT u.id, u.username, e.name AS real_name
|
||||
FROM users u LEFT JOIN employees e ON u.employee_id = e.id
|
||||
WHERE u.is_active = 1 ORDER BY u.id`
|
||||
)
|
||||
res.json({ code: 0, message: 'ok', data: rows })
|
||||
} catch (e) {
|
||||
console.error('[user simpleList] error:', e)
|
||||
res.status(500).json({ code: 500, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/user/logout —— 登出(需要 token,仅做应答)
|
||||
async function logout(req, res) {
|
||||
res.json({ code: 0, message: 'ok' })
|
||||
@@ -162,7 +177,7 @@ async function changePassword(req, res) {
|
||||
async function list(req, res) {
|
||||
try {
|
||||
const { page, pageSize, offset } = pagination(req.query)
|
||||
const { username, role_id, is_active } = req.query
|
||||
const { username, real_name, id, role_id, is_active } = req.query
|
||||
|
||||
let where = 'WHERE 1=1'
|
||||
const params = []
|
||||
@@ -171,6 +186,14 @@ async function list(req, res) {
|
||||
where += ' AND u.username LIKE ?'
|
||||
params.push(`%${username}%`)
|
||||
}
|
||||
if (real_name) {
|
||||
where += ' AND e.name LIKE ?'
|
||||
params.push(`%${real_name}%`)
|
||||
}
|
||||
if (id) {
|
||||
where += ' AND u.id = ?'
|
||||
params.push(Number(id))
|
||||
}
|
||||
if (role_id) {
|
||||
where += ' AND u.role_id = ?'
|
||||
params.push(Number(role_id))
|
||||
@@ -181,7 +204,9 @@ async function list(req, res) {
|
||||
}
|
||||
|
||||
const [[{ total }]] = await pool.query(
|
||||
`SELECT COUNT(*) AS total FROM users u ${where}`,
|
||||
`SELECT COUNT(*) AS total FROM users u
|
||||
LEFT JOIN employees e ON u.employee_id = e.id
|
||||
${where}`,
|
||||
params
|
||||
)
|
||||
|
||||
@@ -369,11 +394,20 @@ async function remove(req, res) {
|
||||
return res.status(400).json({ code: 400, message: '不能删除自己' })
|
||||
}
|
||||
|
||||
const [existing] = await pool.query('SELECT id FROM users WHERE id = ?', [id])
|
||||
const [existing] = await pool.query('SELECT id, role_id FROM users WHERE id = ?', [id])
|
||||
if (existing.length === 0) {
|
||||
return res.status(404).json({ code: 404, message: '用户不存在' })
|
||||
}
|
||||
|
||||
// 禁止删除最后一个管理员
|
||||
const [adminRole] = await pool.query('SELECT id FROM roles WHERE name = ?', ['admin'])
|
||||
if (adminRole.length > 0 && existing[0].role_id === adminRole[0].id) {
|
||||
const [[{ cnt }]] = await pool.query('SELECT COUNT(*) AS cnt FROM users WHERE role_id = ?', [adminRole[0].id])
|
||||
if (cnt <= 1) {
|
||||
return res.status(400).json({ code: 400, message: '不能删除最后一个管理员' })
|
||||
}
|
||||
}
|
||||
|
||||
await pool.query('DELETE FROM users WHERE id = ?', [id])
|
||||
res.json({ code: 0, message: 'ok' })
|
||||
} catch (e) {
|
||||
@@ -382,4 +416,4 @@ async function remove(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { login, info, logout, changePassword, list, detail, create, update, remove }
|
||||
module.exports = { login, info, simpleList, logout, changePassword, list, detail, create, update, remove }
|
||||
|
||||
Reference in New Issue
Block a user