You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.4 KiB
204 lines
5.4 KiB
'use strict';
|
|
const Hex = require('crypto-js/enc-hex');
|
|
const MD5 = require('crypto-js/md5');
|
|
|
|
async function getUser(ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const userRes = await models.User.findAll({
|
|
where: {
|
|
delete: false
|
|
},
|
|
attributes: { exclude: ['password'] },
|
|
order: [['id', 'asc']],
|
|
})
|
|
|
|
ctx.status = 200;
|
|
ctx.body = userRes
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "获取用户信息失败"
|
|
}
|
|
}
|
|
}
|
|
|
|
async function creatUser(ctx, next) {
|
|
let errMsg = "新建用户失败"
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
|
|
let repeatUserNameCount = await models.User.count({
|
|
where: {
|
|
username: data.username,
|
|
delete: false
|
|
}
|
|
})
|
|
|
|
if (repeatUserNameCount) {
|
|
errMsg = '已有当前用户名'
|
|
throw errMsg
|
|
}
|
|
|
|
await models.User.create({
|
|
name: data.name,
|
|
username: data.username,
|
|
password: Hex.stringify(MD5(data.password)),
|
|
delete: false,
|
|
})
|
|
const userId = ctx.fs.api.userId;
|
|
await models.OperationLog.create({
|
|
time: new Date().getTime(),
|
|
clientType: ctx.header['user-agent'],
|
|
content: `新建用户:${data.name}`,
|
|
parameter: null,
|
|
userId,
|
|
})
|
|
|
|
ctx.status = 204;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": errMsg
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async function updateUser(ctx, next) {
|
|
let errMsg = "修改用户失败"
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
const { id } = ctx.params;
|
|
|
|
let repeatUserNameCount = await models.User.findOne({
|
|
where: {
|
|
username: data.username,
|
|
delete: false
|
|
},
|
|
raw: true,
|
|
})
|
|
|
|
if (repeatUserNameCount && repeatUserNameCount.id != id) {
|
|
errMsg = '已有当前用户名'
|
|
throw errMsg
|
|
}
|
|
|
|
await models.User.update({
|
|
name: data.name,
|
|
username: data.username,
|
|
delete: false,
|
|
}, {
|
|
where: { id }
|
|
});
|
|
|
|
const userId = ctx.fs.api.userId;
|
|
await models.OperationLog.create({
|
|
time: new Date().getTime(),
|
|
clientType: ctx.header['user-agent'],
|
|
content: `修改 (${data.name}) 用户信息`,
|
|
parameter: null,
|
|
userId,
|
|
})
|
|
|
|
ctx.status = 204;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": errMsg
|
|
}
|
|
}
|
|
}
|
|
|
|
async function deleteUser(ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { ids } = ctx.params;
|
|
const userIds = ids.split(',');
|
|
const delUser = await models.User.update({
|
|
delete: true,
|
|
}, {
|
|
where: {
|
|
id: { $in: userIds },
|
|
},
|
|
returning: true,
|
|
});
|
|
|
|
const userId = ctx.fs.api.userId;
|
|
const userName = delUser[1].map(item => item.name).join()
|
|
await models.OperationLog.create({
|
|
time: new Date().getTime(),
|
|
clientType: ctx.header['user-agent'],
|
|
content: `删除用户:${userName}`,
|
|
parameter: null,
|
|
userId,
|
|
})
|
|
|
|
ctx.status = 204;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "删除用户失败"
|
|
}
|
|
}
|
|
}
|
|
|
|
async function resetPwd(ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { id } = ctx.params;
|
|
const { password } = ctx.request.body;
|
|
if (!password) {
|
|
ctx.status = 400;
|
|
ctx.body = { "message": "请输入修改密码" };
|
|
return;
|
|
}
|
|
const userRes = await models.User.findOne({
|
|
where: { id },
|
|
attributes: ['id']
|
|
});
|
|
if (userRes) {
|
|
const updateUser = await models.User.update(
|
|
{ password: Hex.stringify(MD5(password)) },
|
|
{
|
|
where: { id },
|
|
returning: true,
|
|
},
|
|
);
|
|
const userId = ctx.fs.api.userId;
|
|
await models.OperationLog.create({
|
|
time: new Date().getTime(),
|
|
clientType: ctx.header['user-agent'],
|
|
content: `修改 (${updateUser[1][0].name}) 用户密码`,
|
|
parameter: null,
|
|
userId,
|
|
})
|
|
ctx.status = 204;
|
|
} else {
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "用户不存在"
|
|
}
|
|
}
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "修改用户密码失败"
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getUser,
|
|
creatUser,
|
|
updateUser,
|
|
deleteUser,
|
|
resetPwd,
|
|
}
|