政务数据资源中心(Government data Resource center) 03专项3期主要建设内容
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.
 
 
 
 

164 lines
5.1 KiB

'use strict';
const Hex = require('crypto-js/enc-hex');
const MD5 = require('crypto-js/md5');
function getUserList(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
const { page, limit, name, role } = ctx.query;
const Op = ctx.fs.dc.ORM.Op;
let errMsg = { message: '获取用户失败' }
try {
let searchWhere = {
username: { $not: 'SuperAdmin' }
}
let option = {
where: searchWhere,
order: [["id", "desc"]],
attributes: { exclude: ['password'] },
}
if (name) {
searchWhere.name = { $like: '%' + name + '%' };
}
if (role) {
searchWhere.role = role;
}
option.where = searchWhere
let limit_ = limit || 10;
let page_ = page || 1;
let offset = (page_ - 1) * limit_;
if (limit && page) {
option.limit = limit_
option.offset = offset
}
const res = await models.User.findAndCount(option);
ctx.status = 200;
ctx.body = res;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = errMsg
}
}
}
// 新增用户
function addUser(opts) {
return async function (ctx, next) {
const models = ctx.fs.dc.models;
try {
const { username } = ctx.request.body
const checkName = await models.User.findOne({ where: { username } });
if (checkName) {
ctx.status = 400;
ctx.body = { message: "该用户名已存在" }
} else {
let rslt = ctx.request.body;
rslt.password = 'e10adc3949ba59abbe56e057f20f883e';
await models.User.create(Object.assign({}, rslt))
ctx.status = 204;
ctx.body = { message: '新建用户成功' }
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '新建用户失败' }
}
}
}
// 修改用户
function editUser(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const body = ctx.request.body;
if (body.oldpassword) {
const password = Hex.stringify(MD5(body.oldpassword));
const checkPwd = await models.User.findOne({ where: { id: id, password } });
if (!checkPwd) {
ctx.status = 400;
ctx.body = { message: '旧密码错误' }
} else {
await models.User.update(
{ password: Hex.stringify(MD5(body.password)) },
{ where: { id: id, } }
)
ctx.status = 204;
ctx.body = { message: '修改用户成功' }
}
} else {
const checkName = await models.User.findOne({ where: { id: { $not: id }, username: body.username } });
if (checkName) {
ctx.status = 400;
ctx.body = { message: '该用户名已存在' }
} else {
await models.User.update(
body,
{ where: { id: id, } }
)
ctx.status = 204;
ctx.body = { message: '修改用户成功' }
}
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '修改用户失败' }
}
}
}
// 删除用户
function deleteUser(opts) {
return async function (ctx, next) {
try {
const models = ctx.fs.dc.models;
const { id } = ctx.params;
const checkName1 = await models.MetadataDatabase.findOne({ where: { createBy: id } });
const checkName2 = await models.MetadataFile.findOne({ where: { createBy: id } });
const checkName3 = await models.MetadataRestapi.findOne({ where: { createBy: id } });
if (checkName1 || checkName2 || checkName3) {
ctx.status = 400;
ctx.body = { message: '该用户下存在依赖资源无法删除!' }
} else {
await models.User.destroy({
where: {
id: id
}
})
ctx.status = 204;
ctx.body = { message: '删除用户成功' }
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = { message: '删除用户失败' }
}
}
}
module.exports = {
getUserList,
addUser,
editUser,
deleteUser,
}