北斗应用技术培训考试管理系统
This commit is contained in:
272
app/service/User.js
Normal file
272
app/service/User.js
Normal file
@@ -0,0 +1,272 @@
|
||||
'use strict';
|
||||
|
||||
const Service = require('../core/base_service');
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
const moment = require('moment');
|
||||
|
||||
const fileName = "User";
|
||||
|
||||
class UserService extends Service {
|
||||
// 管理系统新增用户账号信息
|
||||
async addUser(form) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const result = await ctx.model.Examinee.bulkCreate(form, { ignoreDuplicates: true });
|
||||
return result.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "addUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 管理系统查看用户账号信息
|
||||
async showUser(uid, body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
let { offset, limit, searchOptions, sortOptions } = body;
|
||||
offset = (offset - 1) * limit;
|
||||
let attributes = ['uid', 'user_name', 'identity_card', 'real_check', 'workplace', 'address', 'phone', 'email', 'referrers', 'status', 'created_at', 'updated_at', 'login_status'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['uid', 'user_name', 'identity_card', 'workplace', 'address', 'phone', 'email', 'referrers'],
|
||||
key = ['real_check', 'status', 'login_status'],
|
||||
between = ['created_at', 'updated_at'];
|
||||
attributes.forEach(element => {
|
||||
if (searchOptions[element]) {
|
||||
if (searchOptions[element] == '*空') {
|
||||
if (joinSubstring.includes(element)) {
|
||||
where[element] = null;
|
||||
} else {
|
||||
where[element] = [null, ''];
|
||||
}
|
||||
} else if (joinSubstring.includes(element)) {
|
||||
let targetKeyArr = [];
|
||||
where[element] = targetKeyArr.length > 0 ? targetKeyArr : 'null';
|
||||
} else if (substring.includes(element)) {
|
||||
where[element] = { [Op.substring]: searchOptions[element] };
|
||||
} else if (key.includes(element)) {
|
||||
where[element] = searchOptions[element];
|
||||
} else if (between.includes(element)) {
|
||||
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
|
||||
}
|
||||
}
|
||||
if (sortOptions[element] == true) {
|
||||
order.push([element, 'ASC']);
|
||||
} else if (sortOptions[element] == false) {
|
||||
order.push([element, 'DESC']);
|
||||
}
|
||||
});
|
||||
dbOptions = { attributes, offset, limit, where, order };
|
||||
const result = await ctx.model.Examinee.findAndCountAll(dbOptions);
|
||||
return result.rows.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "showUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 管理系统编辑用户账号信息
|
||||
async editUser(uid, modelType, updateMsg, requestMsg) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const result = await ctx.model.Manager.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.role < 1) return false;
|
||||
let where = {};
|
||||
if (modelType == "single") {
|
||||
const { uid, user_name, identity_card, real_check, workplace, address, phone, email, status } = requestMsg;
|
||||
where = { uid, user_name, identity_card, real_check, workplace, address, phone, email, status };
|
||||
} else if (modelType == "multiple") {
|
||||
let uid = [], user_name = [], identity_card = [], real_check = [], workplace = [], address = [], phone = [], email = [], status = [];
|
||||
for (const key in requestMsg) {
|
||||
requestMsg[key].forEach(element => {
|
||||
uid.push(element.uid);
|
||||
user_name.push(element.user_name);
|
||||
identity_card.push(element.identity_card);
|
||||
real_check.push(element.real_check);
|
||||
workplace.push(element.workplace);
|
||||
address.push(element.address);
|
||||
phone.push(element.phone);
|
||||
email.push(element.email);
|
||||
status.push(element.status);
|
||||
});
|
||||
}
|
||||
where = { uid, user_name, identity_card, real_check, workplace, address, phone, email, status };
|
||||
}
|
||||
updateMsg.updated_at = new Date();
|
||||
const resultList = await ctx.model.Examinee.update(updateMsg, { where });
|
||||
return resultList[0] == 0 ? false : resultList;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "editUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 管理系统删除用户账号信息
|
||||
async deleteUser(uid, modelType, password, requestMsg) {
|
||||
const { ctx } = this;
|
||||
const systemMonitoring = {
|
||||
operate: 2,
|
||||
operate_msg: '删除用户账号信息',
|
||||
type: 0,
|
||||
status: 0,
|
||||
affect_entries: 0,
|
||||
user: uid,
|
||||
created_at: new Date()
|
||||
};
|
||||
try {
|
||||
const result = await ctx.model.Manager.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 2) {
|
||||
if (!result) systemMonitoring.status = 0;
|
||||
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemMonitoring.status = 1;
|
||||
else if (result.dataValues.role < 2) systemMonitoring.status = 2;
|
||||
await ctx.model.Monitoring.create(systemMonitoring);
|
||||
return false;
|
||||
};
|
||||
let where = {};
|
||||
if (modelType == "single") {
|
||||
const { uid, user_name, identity_card, real_check, workplace, address, phone, email, status } = requestMsg;
|
||||
where = { uid, user_name, identity_card, real_check, workplace, address, phone, email, status };
|
||||
await ctx.model.Audit.destroy({ where : { user_uid: uid } });
|
||||
await ctx.model.Application.destroy({ where : { user_uid: uid } });
|
||||
await ctx.model.Grades.destroy({ where: { user_uid: uid } });
|
||||
} else if (modelType == "multiple") {
|
||||
let uid = [], user_name = [], identity_card = [], real_check = [], workplace = [], address = [], phone = [], email = [], status = [];
|
||||
for (const key in requestMsg) {
|
||||
requestMsg[key].forEach(element => {
|
||||
uid.push(element.uid);
|
||||
user_name.push(element.user_name);
|
||||
identity_card.push(element.identity_card);
|
||||
real_check.push(element.real_check);
|
||||
workplace.push(element.workplace);
|
||||
address.push(element.address);
|
||||
phone.push(element.phone);
|
||||
email.push(element.email);
|
||||
status.push(element.status);
|
||||
});
|
||||
}
|
||||
where = { uid, user_name, identity_card, real_check, workplace, address, phone, email, status };
|
||||
await ctx.model.Audit.destroy({ where : { user_uid: uid } });
|
||||
await ctx.model.Application.destroy({ where : { user_uid: uid } });
|
||||
await ctx.model.Grades.destroy({ where: { user_uid: uid } });
|
||||
}
|
||||
const resultList = await ctx.model.Examinee.destroy({ where });
|
||||
systemMonitoring.status = resultList == 0 ? 3 : 4;
|
||||
systemMonitoring.affect_entries = resultList;
|
||||
await ctx.model.Monitoring.create(systemMonitoring);
|
||||
return resultList == 0 ? false : true;
|
||||
} catch (error) {
|
||||
systemMonitoring.status = 5;
|
||||
await ctx.model.Monitoring.create(systemMonitoring);
|
||||
const blockName = fileName + "--" + "deleteUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 管理系统导出用户账号信息数据
|
||||
async downloadUserData(uid, searchMsg) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const user = await ctx.model.Manager.findOne({ where: { uid } });
|
||||
if (!user || user.dataValues.role < 1) return false;
|
||||
let { searchOptions, sortOptions } = searchMsg;
|
||||
let attributes = ['uid', 'user_name', 'identity_card', 'real_check', 'workplace', 'address', 'phone', 'email', 'status', 'created_at', 'updated_at', 'login_status'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['uid', 'user_name', 'identity_card', 'workplace', 'address', 'phone', 'email'],
|
||||
key = ['real_check', 'status', 'login_status'],
|
||||
between = ['created_at', 'updated_at'];
|
||||
attributes.forEach(element => {
|
||||
if (searchOptions[element]) {
|
||||
if (searchOptions[element] == '*空') {
|
||||
if (joinSubstring.includes(element)) {
|
||||
where[element] = null;
|
||||
} else {
|
||||
where[element] = [null, ''];
|
||||
}
|
||||
} else if (joinSubstring.includes(element)) {
|
||||
let targetKeyArr = [];
|
||||
where[element] = targetKeyArr.length > 0 ? targetKeyArr : 'null';
|
||||
} else if (substring.includes(element)) {
|
||||
where[element] = { [Op.substring]: searchOptions[element] };
|
||||
} else if (key.includes(element)) {
|
||||
where[element] = searchOptions[element];
|
||||
} else if (between.includes(element)) {
|
||||
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
|
||||
}
|
||||
}
|
||||
if (sortOptions[element] == true) {
|
||||
order.push([element, 'ASC']);
|
||||
} else if (sortOptions[element] == false) {
|
||||
order.push([element, 'DESC']);
|
||||
}
|
||||
});
|
||||
dbOptions = { attributes, where, order };
|
||||
const result = await ctx.model.Examinee.findAll(dbOptions);
|
||||
if (result.length == 0) return { code: false };
|
||||
let data = [];
|
||||
const realCheckOptions = [
|
||||
{
|
||||
value: "0",
|
||||
label: "未核验"
|
||||
},
|
||||
{
|
||||
value: "1",
|
||||
label: "核验通过"
|
||||
},
|
||||
{
|
||||
value: "2",
|
||||
label: "核验失败"
|
||||
},
|
||||
];
|
||||
const statusOptions = [
|
||||
{
|
||||
value: "0",
|
||||
label: "默认状态"
|
||||
},
|
||||
{
|
||||
value: "1",
|
||||
label: "正常使用"
|
||||
},
|
||||
{
|
||||
value: "2",
|
||||
label: "已被封禁"
|
||||
},
|
||||
];
|
||||
result.forEach(element => {
|
||||
realCheckOptions.forEach((item) => {
|
||||
if (item.value == element.dataValues["real_check"]) {
|
||||
element.dataValues["real_check"] = item.label;
|
||||
}
|
||||
});
|
||||
statusOptions.forEach((item) => {
|
||||
if (item.value == element.dataValues["status"]) {
|
||||
element.dataValues["status"] = item.label;
|
||||
}
|
||||
});
|
||||
element.dataValues.created_at = moment
|
||||
.utc(element.dataValues.created_at)
|
||||
.add(8, "hours")
|
||||
.format("YYYY年MM月DD日HH时mm分ss秒");
|
||||
element.dataValues.updated_at = moment
|
||||
.utc(element.dataValues.updated_at)
|
||||
.add(8, "hours")
|
||||
.format("YYYY年MM月DD日HH时mm分ss秒");
|
||||
delete element.dataValues.login_status;
|
||||
data.push(element.dataValues);
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "downloadUserData";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserService;
|
||||
Reference in New Issue
Block a user