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.
220 lines
5.7 KiB
220 lines
5.7 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 { depId } = ctx.params
|
|
const userRes = await models.User.findAll({
|
|
where: {
|
|
departmentId: parseInt(depId),
|
|
delete: false
|
|
},
|
|
attributes: { exclude: ['password', 'delete', 'username'] },
|
|
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 getUserAll (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const userRes = await models.User.findAll({
|
|
where: {
|
|
delete: false
|
|
},
|
|
attributes: { exclude: ['password', 'delete', 'username'] },
|
|
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) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
|
|
let repeatUserNameCount = await models.User.count({
|
|
where: {
|
|
phone: data.phone,
|
|
delete: false
|
|
}
|
|
})
|
|
|
|
if (repeatUserNameCount) {
|
|
throw '已有当前用户名'
|
|
}
|
|
|
|
await models.User.create({
|
|
name: data.name,
|
|
// username: data.username,
|
|
password: Hex.stringify(MD5(data.password)),
|
|
departmentId: data.departmentId,
|
|
email: data.email,
|
|
enable: data.enable,
|
|
delete: false,
|
|
phone: data.phone,
|
|
remark: 'th',
|
|
})
|
|
|
|
ctx.status = 204;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async function updateUser (ctx, next) {
|
|
let errMsg = "修改用户失败"
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const data = ctx.request.body;
|
|
const { userId } = ctx.params;
|
|
|
|
let repeatUserNameCount = await models.User.count({
|
|
where: {
|
|
phone: data.phone,
|
|
delete: false,
|
|
id: { $ne: userId }
|
|
}
|
|
})
|
|
|
|
if (repeatUserNameCount) {
|
|
throw '已有当前用户名'
|
|
}
|
|
|
|
await models.User.update({
|
|
name: data.name,
|
|
// username: data.username,
|
|
departmentId: data.departmentId,
|
|
email: data.email,
|
|
enable: data.enable,
|
|
delete: false,
|
|
phone: data.phone,
|
|
}, {
|
|
where: {
|
|
id: userId
|
|
}
|
|
});
|
|
|
|
ctx.status = 204;
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
message: typeof error == 'string' ? error : undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
async function deleteUser (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { userIds } = ctx.params;
|
|
const userIds_ = userIds.split(',');
|
|
await models.User.update({
|
|
delete: true,
|
|
}, {
|
|
where: {
|
|
id: { $in: userIds_ },
|
|
}
|
|
});
|
|
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 data = ctx.request.body;
|
|
// await models.User.update({
|
|
// password: Hex.stringify(MD5(data.password)),
|
|
// }, {
|
|
// where: {
|
|
// id: id,
|
|
// }
|
|
// });
|
|
// ctx.status = 204;
|
|
// } catch (error) {
|
|
// ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
// ctx.status = 400;
|
|
// ctx.body = {
|
|
// "message": "重置用户密码失败"
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
async function setPassword (ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { userId } = ctx.params;
|
|
const { password } = ctx.request.body;
|
|
if (!password) {
|
|
ctx.status = 400;
|
|
ctx.body = { "message": "请输入修改密码" };
|
|
return;
|
|
}
|
|
const userRes = await models.User.findOne({
|
|
where: {
|
|
id: userId
|
|
},
|
|
attributes: ['id']
|
|
});
|
|
if (userRes) {
|
|
await models.User.update({ password: Hex.stringify(MD5(password)) }, { where: { id: 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,
|
|
getUserAll,
|
|
creatUser,
|
|
updateUser,
|
|
deleteUser,
|
|
// resetPwd,
|
|
setPassword
|
|
}
|