166 lines
5.8 KiB
JavaScript
166 lines
5.8 KiB
JavaScript
import express from 'express'
|
|
import { users, roles, userRoles } from '../db/seedAuth.js'
|
|
import { organizations } from '../db/seedAuth.js'
|
|
import { nextId } from '../utils/id.js'
|
|
import { paginate } from '../utils/pagination.js'
|
|
import { sendOk, sendFail } from '../utils/response.js'
|
|
import { requireAuth } from '../middleware/requireAuth.js'
|
|
|
|
const router = express.Router()
|
|
router.use(requireAuth)
|
|
|
|
function orgName(organizationId) {
|
|
return organizations.find((o) => o.id === organizationId)?.organizationName || ''
|
|
}
|
|
|
|
function myRoleNames(userId) {
|
|
const roleIds = userRoles.filter((ur) => ur.userId === userId).map((ur) => ur.roleId)
|
|
return roles.filter((r) => roleIds.includes(r.id)).map((r) => r.roleName)
|
|
}
|
|
|
|
function toUserPageVo(user) {
|
|
return {
|
|
id: user.id,
|
|
username: user.username,
|
|
lastLoginTime: user.lastLoginTime,
|
|
email: user.email,
|
|
organizationName: orgName(user.organizationId),
|
|
roleNameList: myRoleNames(user.id),
|
|
phone: user.phone,
|
|
realName: user.realName,
|
|
organizationId: user.organizationId,
|
|
status: user.status
|
|
}
|
|
}
|
|
|
|
// 分页查询用户列表
|
|
router.post('/auth/user/page', (req, res) => {
|
|
const { organizationId, usernameLike, phone, status, roleId, page, pageSize } = req.body || {}
|
|
let list = users
|
|
if (organizationId) list = list.filter((u) => u.organizationId === organizationId)
|
|
if (usernameLike) list = list.filter((u) => u.username.includes(usernameLike))
|
|
if (phone) list = list.filter((u) => u.phone === phone)
|
|
if (status) list = list.filter((u) => u.status === status)
|
|
if (roleId) {
|
|
const userIdsWithRole = new Set(userRoles.filter((ur) => ur.roleId === roleId).map((ur) => ur.userId))
|
|
list = list.filter((u) => userIdsWithRole.has(u.id))
|
|
}
|
|
const result = paginate(list, page, pageSize)
|
|
sendOk(res, { ...result, records: result.records.map(toUserPageVo) })
|
|
})
|
|
|
|
// 创建用户
|
|
router.post('/auth/user/create', (req, res) => {
|
|
const { username, phone, realName, organizationId, status, email, userRoleBtoList } = req.body || {}
|
|
if (!username) return sendFail(res, '用户名不能为空', 400)
|
|
if (users.some((u) => u.username === username)) return sendFail(res, '用户名已存在', 400)
|
|
const id = nextId('user')
|
|
const user = {
|
|
id,
|
|
username,
|
|
password: 'Init@123456',
|
|
phone,
|
|
realName,
|
|
organizationId,
|
|
status: status || 'ACTIVE',
|
|
email,
|
|
userType: 'OPERATOR',
|
|
lastLoginTime: null,
|
|
forceChangePwd: true
|
|
}
|
|
users.push(user)
|
|
;(userRoleBtoList || []).forEach((item) => {
|
|
userRoles.push({ id: nextId('userRole'), userId: id, roleId: item.roleId })
|
|
})
|
|
sendOk(res, { id, status: user.status }, '创建成功')
|
|
})
|
|
|
|
// 更新用户(含角色重新绑定)
|
|
router.post('/user/update', (req, res) => {
|
|
const { id, username, phone, realName, organizationId, email, userRoleBtoList } = req.body || {}
|
|
const user = users.find((u) => u.id === id)
|
|
if (!user) return sendFail(res, '用户不存在', 404)
|
|
if (username !== undefined) user.username = username
|
|
if (phone !== undefined) user.phone = phone
|
|
if (realName !== undefined) user.realName = realName
|
|
if (organizationId !== undefined) user.organizationId = organizationId
|
|
if (email !== undefined) user.email = email
|
|
if (userRoleBtoList) {
|
|
for (let i = userRoles.length - 1; i >= 0; i -= 1) {
|
|
if (userRoles[i].userId === id) userRoles.splice(i, 1)
|
|
}
|
|
userRoleBtoList.forEach((item) => {
|
|
userRoles.push({ id: nextId('userRole'), userId: id, roleId: item.roleId })
|
|
})
|
|
}
|
|
sendOk(res, true, '更新成功')
|
|
})
|
|
|
|
router.post('/user/delete', (req, res) => {
|
|
const { userId } = req.body || {}
|
|
const index = users.findIndex((u) => u.id === userId)
|
|
if (index === -1) return sendFail(res, '用户不存在', 404)
|
|
if (users[index].username === 'admin') return sendFail(res, '内置管理员账号不可删除', 400)
|
|
users.splice(index, 1)
|
|
for (let i = userRoles.length - 1; i >= 0; i -= 1) {
|
|
if (userRoles[i].userId === userId) userRoles.splice(i, 1)
|
|
}
|
|
sendOk(res, true, '删除成功')
|
|
})
|
|
|
|
router.post('/user/lock', (req, res) => {
|
|
const { userId } = req.body || {}
|
|
const user = users.find((u) => u.id === userId)
|
|
if (!user) return sendFail(res, '用户不存在', 404)
|
|
user.status = 'LOCKED'
|
|
sendOk(res, true, '锁定成功')
|
|
})
|
|
|
|
router.post('/user/unlock', (req, res) => {
|
|
const { userId } = req.body || {}
|
|
const user = users.find((u) => u.id === userId)
|
|
if (!user) return sendFail(res, '用户不存在', 404)
|
|
user.status = 'ACTIVE'
|
|
sendOk(res, true, '解锁成功')
|
|
})
|
|
|
|
router.post('/user/reset-password', (req, res) => {
|
|
const { userId } = req.body || {}
|
|
const user = users.find((u) => u.id === userId)
|
|
if (!user) return sendFail(res, '用户不存在', 404)
|
|
user.password = 'Init@123456'
|
|
user.forceChangePwd = true
|
|
sendOk(res, true, '密码已重置为 Init@123456')
|
|
})
|
|
|
|
router.get('/auth/user-role/list-by-user', (req, res) => {
|
|
const userId = Number(req.query.userId)
|
|
const roleIds = userRoles.filter((ur) => ur.userId === userId).map((ur) => ur.roleId)
|
|
const list = roles
|
|
.filter((r) => roleIds.includes(r.id))
|
|
.map((r) => ({ id: r.id, roleName: r.roleName, organizationId: r.organizationId, isBuiltIn: r.isBuiltIn }))
|
|
sendOk(res, list)
|
|
})
|
|
|
|
router.post('/auth/user-role/bind', (req, res) => {
|
|
const { userId, roleIdList } = req.body || {}
|
|
;(roleIdList || []).forEach((roleId) => {
|
|
if (!userRoles.some((ur) => ur.userId === userId && ur.roleId === roleId)) {
|
|
userRoles.push({ id: nextId('userRole'), userId, roleId })
|
|
}
|
|
})
|
|
sendOk(res, true, '绑定成功')
|
|
})
|
|
|
|
router.post('/auth/user-role/unbind', (req, res) => {
|
|
const { userId, roleIdList } = req.body || {}
|
|
for (let i = userRoles.length - 1; i >= 0; i -= 1) {
|
|
if (userRoles[i].userId === userId && (roleIdList || []).includes(userRoles[i].roleId)) {
|
|
userRoles.splice(i, 1)
|
|
}
|
|
}
|
|
sendOk(res, true, '解绑成功')
|
|
})
|
|
|
|
export default router
|