Files
account-manager/app/service/BSide.js
2026-06-05 17:38:10 +08:00

275 lines
10 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const moment = require('moment');
const fileName = "BSide";
class BSideService extends Service {
// 注册B端用户
async addBSide(form) {
const { ctx } = this;
try {
const result = await ctx.model.BSide.bulkCreate(form);
return result.length == 0 ? false : true;
} catch (error) {
const blockName = fileName + "--" + "addBSide";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 查看B端用户
async showBSide(body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['status', 'role', 'account_name', 'organization', 'user_name', 'phone', 'msg', 'created_at', 'updated_at', 'login_at', 'login_status'],
where = {}, order = [], dbOptions,
joinSubstring = ['organization'],
substring = ['account_name', 'user_name', 'phone', 'msg'],
key = ['status', 'role', 'login_status'],
between = ['created_at', 'updated_at', 'login_at'];
const Organization = await ctx.model3.Organization.findAll({ attributes: ['uid', 'organization_name'] });
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 = [];
Organization.forEach(item => {
if (item.dataValues.organization_name.includes(searchOptions[element])) {
targetKeyArr.push(item.dataValues.uid);
}
});
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.BSide.findAndCountAll(dbOptions);
result.rows.forEach(element => {
if (element.dataValues.organization !== null) {
element.dataValues.organization = Organization.find(item => item.dataValues.uid == element.dataValues.organization).dataValues.organization_name;
}
});
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "showBSide";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 编辑B端用户
async editBSide(uid, modelType, updateMsg, requestMsg) {
const { ctx } = this;
try {
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
if (!result || result.dataValues.role < 6) return false;
const Organization = await ctx.model3.Organization.findAll({ attributes: ['uid', 'organization_name'] });
let where = {};
if (modelType == "single") {
let { account_name, organization } = requestMsg;
if (organization !== null) {
organization = Organization.find(item => item.dataValues.organization_name == organization).dataValues.uid;
}
where = { account_name, organization };
} else if (modelType == "multiple") {
let account_name = [], organization_name = [], organization = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
account_name.push(element.account_name);
organization_name.push(element.organization);
});
}
organization_name.forEach(element => {
if (element !== null) {
organization.push(Organization.find(item => item.dataValues.organization_name == element).dataValues.uid);
}
});
where = { account_name, organization };
if (where.organization.length == 0) {
delete where.organization;
}
}
updateMsg.updated_at = new Date();
const resultList = await ctx.model.BSide.update(updateMsg, { where });
return resultList[0] == 0 ? false : resultList;
} catch (error) {
const blockName = fileName + "--" + "editBSide";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 删除B端用户
async deleteBSide(uid, modelType, password, requestMsg) {
const { ctx } = this;
const systemMonitoring = {
operate: 2,
operate_msg: '删除B端用户',
type: 0,
status: 0,
affect_entries: 0,
user: uid,
created_at: new Date()
};
try {
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 6) {
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 < 6) systemMonitoring.status = 2;
await ctx.model3.Monitoring.create(systemMonitoring);
return false;
};
let where = {};
if (modelType == "single") {
const { account_name, organization } = requestMsg;
where = { account_name, organization };
} else if (modelType == "multiple") {
let account_name = [], organization = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
account_name.push(element.account_name),
organization.push(element.organization)
});
}
where = { account_name, organization };
}
const resultList = await ctx.model.BSide.destroy({ where });
systemMonitoring.status = resultList == 0 ? 3 : 4;
systemMonitoring.affect_entries = resultList;
await ctx.model3.Monitoring.create(systemMonitoring);
return resultList == 0 ? false : true;
} catch (error) {
systemMonitoring.status = 5;
await ctx.model3.Monitoring.create(systemMonitoring);
const blockName = fileName + "--" + "deleteBSide";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统导出B端用户
async downloadBside(uid, searchMsg) {
const { ctx } = this;
try {
const user = await ctx.model3.Insiders.findOne({ where: { uid } });
if (!user || user.dataValues.role < 3) return false;
let { searchOptions, sortOptions } = searchMsg;
let attributes = ['uid', 'status', 'role', 'account_name', 'organization', 'user_name', 'phone', 'msg', 'created_at', 'updated_at', 'login_at', 'login_status'],
where = {}, order = [], dbOptions,
joinSubstring = [],
substring = ['uid', 'account_name', 'organization', 'user_name', 'phone', 'msg'],
key = ['status', 'role', 'login_status'],
between = ['created_at', 'updated_at', 'login_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.BSide.findAll(dbOptions);
if (result.length == 0) return { code: false };
let data = [];
const statusOptions = [
{
value: "0",
label: "默认"
},
{
value: "1",
label: "正在使用"
},
{
value: "2",
label: "封禁"
}
];
const roleOptions = [
{
value: "0",
label: "普通用户"
},
{
value: "1",
label: "单位管理员"
}
];
result.forEach(element => {
statusOptions.forEach((item) => {
if (item.value == element.dataValues["status"]) {
element.dataValues["status"] = item.label;
}
});
roleOptions.forEach((item) => {
if (item.value == element.dataValues["role"]) {
element.dataValues["role"] = 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秒");
element.dataValues.login_at = moment
.utc(element.dataValues.login_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 + "--" + "downloadBside";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = BSideService;