320 lines
7.1 KiB
JavaScript
320 lines
7.1 KiB
JavaScript
|
|
const { Op } = require('sequelize');
|
|||
|
|
const Role = require('../models/Role');
|
|||
|
|
const { logOperation } = require('../utils/logger');
|
|||
|
|
|
|||
|
|
// 获取角色列表
|
|||
|
|
exports.getRoles = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { page = 1, pageSize = 10, name, status } = req.query;
|
|||
|
|
|
|||
|
|
const where = { isDeleted: 0 };
|
|||
|
|
if (name) where.name = { [Op.like]: `%${name}%` };
|
|||
|
|
if (status) where.status = status;
|
|||
|
|
|
|||
|
|
const { count, rows: roles } = await Role.findAndCountAll({
|
|||
|
|
where,
|
|||
|
|
limit: parseInt(pageSize),
|
|||
|
|
offset: (parseInt(page) - 1) * parseInt(pageSize),
|
|||
|
|
order: [['id', 'DESC']]
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '获取角色列表成功',
|
|||
|
|
data: {
|
|||
|
|
list: roles,
|
|||
|
|
total: count,
|
|||
|
|
page: parseInt(page),
|
|||
|
|
pageSize: parseInt(pageSize)
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 记录操作日志
|
|||
|
|
await logOperation({
|
|||
|
|
userId: req.user.id,
|
|||
|
|
username: req.user.username,
|
|||
|
|
module: '角色管理',
|
|||
|
|
action: '查询',
|
|||
|
|
description: '获取角色列表',
|
|||
|
|
method: req.method,
|
|||
|
|
path: req.path,
|
|||
|
|
ip: req.ip,
|
|||
|
|
status: 'success'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取角色列表错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '获取角色列表失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 获取角色详情
|
|||
|
|
exports.getRoleById = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
|
|||
|
|
const role = await Role.findByPk(id, {
|
|||
|
|
where: { isDeleted: 0 }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!role) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
code: 404,
|
|||
|
|
message: '角色不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '获取角色详情成功',
|
|||
|
|
data: role
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 记录操作日志
|
|||
|
|
await logOperation({
|
|||
|
|
userId: req.user.id,
|
|||
|
|
username: req.user.username,
|
|||
|
|
module: '角色管理',
|
|||
|
|
action: '查询',
|
|||
|
|
description: `获取角色详情,ID: ${id}`,
|
|||
|
|
method: req.method,
|
|||
|
|
path: req.path,
|
|||
|
|
ip: req.ip,
|
|||
|
|
status: 'success'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取角色详情错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '获取角色详情失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 创建角色
|
|||
|
|
exports.createRole = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { name, code, description, permissions, status } = req.body;
|
|||
|
|
|
|||
|
|
// 参数验证
|
|||
|
|
if (!name || !code) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
code: 400,
|
|||
|
|
message: '角色名称和编码不能为空'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查角色编码是否已存在
|
|||
|
|
const existingRole = await Role.findOne({
|
|||
|
|
where: { code, isDeleted: 0 }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (existingRole) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
code: 400,
|
|||
|
|
message: '角色编码已存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const role = await Role.create({
|
|||
|
|
name,
|
|||
|
|
code,
|
|||
|
|
description,
|
|||
|
|
permissions,
|
|||
|
|
status: status || 'active',
|
|||
|
|
createBy: req.user.id,
|
|||
|
|
updateBy: req.user.id
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '创建角色成功',
|
|||
|
|
data: role
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 记录操作日志
|
|||
|
|
await logOperation({
|
|||
|
|
userId: req.user.id,
|
|||
|
|
username: req.user.username,
|
|||
|
|
module: '角色管理',
|
|||
|
|
action: '创建',
|
|||
|
|
description: `创建角色: ${name}`,
|
|||
|
|
method: req.method,
|
|||
|
|
path: req.path,
|
|||
|
|
ip: req.ip,
|
|||
|
|
status: 'success'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('创建角色错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '创建角色失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 更新角色
|
|||
|
|
exports.updateRole = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
const { name, code, description, permissions, status } = req.body;
|
|||
|
|
|
|||
|
|
// 查找角色
|
|||
|
|
const role = await Role.findByPk(id, {
|
|||
|
|
where: { isDeleted: 0 }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!role) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
code: 404,
|
|||
|
|
message: '角色不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查角色编码是否已存在(排除当前角色)
|
|||
|
|
if (code && code !== role.code) {
|
|||
|
|
const existingRole = await Role.findOne({
|
|||
|
|
where: { code, isDeleted: 0, id: { [Op.ne]: id } }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (existingRole) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
code: 400,
|
|||
|
|
message: '角色编码已存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await role.update({
|
|||
|
|
name: name || role.name,
|
|||
|
|
code: code || role.code,
|
|||
|
|
description: description !== undefined ? description : role.description,
|
|||
|
|
permissions: permissions !== undefined ? permissions : role.permissions,
|
|||
|
|
status: status || role.status,
|
|||
|
|
updateBy: req.user.id
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '更新角色成功',
|
|||
|
|
data: role
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 记录操作日志
|
|||
|
|
await logOperation({
|
|||
|
|
userId: req.user.id,
|
|||
|
|
username: req.user.username,
|
|||
|
|
module: '角色管理',
|
|||
|
|
action: '更新',
|
|||
|
|
description: `更新角色: ${role.name}`,
|
|||
|
|
method: req.method,
|
|||
|
|
path: req.path,
|
|||
|
|
ip: req.ip,
|
|||
|
|
status: 'success'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('更新角色错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '更新角色失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 删除角色
|
|||
|
|
exports.deleteRole = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const { id } = req.params;
|
|||
|
|
|
|||
|
|
// 查找角色
|
|||
|
|
const role = await Role.findByPk(id, {
|
|||
|
|
where: { isDeleted: 0 }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (!role) {
|
|||
|
|
return res.status(404).json({
|
|||
|
|
code: 404,
|
|||
|
|
message: '角色不存在'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查是否有用户使用此角色
|
|||
|
|
const userCount = await role.countUsers({
|
|||
|
|
where: { isDeleted: 0 }
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (userCount > 0) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
code: 400,
|
|||
|
|
message: '该角色下还有用户,无法删除'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 软删除
|
|||
|
|
await role.update({
|
|||
|
|
isDeleted: 1,
|
|||
|
|
updateBy: req.user.id
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '删除角色成功'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 记录操作日志
|
|||
|
|
await logOperation({
|
|||
|
|
userId: req.user.id,
|
|||
|
|
username: req.user.username,
|
|||
|
|
module: '角色管理',
|
|||
|
|
action: '删除',
|
|||
|
|
description: `删除角色: ${role.name}`,
|
|||
|
|
method: req.method,
|
|||
|
|
path: req.path,
|
|||
|
|
ip: req.ip,
|
|||
|
|
status: 'success'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('删除角色错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '删除角色失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 获取所有角色(用于下拉选择)
|
|||
|
|
exports.getAllRoles = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
const roles = await Role.findAll({
|
|||
|
|
where: { isDeleted: 0, status: 'active' },
|
|||
|
|
attributes: ['id', 'name', 'code']
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
res.json({
|
|||
|
|
code: 200,
|
|||
|
|
message: '获取角色列表成功',
|
|||
|
|
data: roles
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取角色列表错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
code: 500,
|
|||
|
|
message: '获取角色列表失败',
|
|||
|
|
error: error.message
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|