2026-03-27 17:49:17 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const Service = require('../core/base_service');
|
|
|
|
|
|
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
|
|
2026-06-05 17:38:10 +08:00
|
|
|
const moment = require('moment');
|
|
|
|
|
|
2026-03-27 17:49:17 +08:00
|
|
|
const fileName = "Monitoring";
|
|
|
|
|
|
|
|
|
|
class MonitoringService extends Service {
|
2026-06-05 17:38:10 +08:00
|
|
|
// 查看危险操作记录
|
|
|
|
|
async showMonitoring(body) {
|
|
|
|
|
const { ctx } = this;
|
|
|
|
|
try {
|
|
|
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
|
|
|
|
offset = (offset - 1) * limit;
|
|
|
|
|
let attributes = ['operate', 'operate_msg', 'type', 'status', 'affect_entries', 'user', 'created_at'],
|
|
|
|
|
where = {}, order = [], dbOptions, joinSubstring = [],
|
|
|
|
|
substring = ['operate_msg', 'user'],
|
|
|
|
|
key = ['type', 'status', 'operate'],
|
|
|
|
|
between = ['created_at'];
|
|
|
|
|
attributes.forEach(element => {
|
|
|
|
|
if (searchOptions[element]) {
|
|
|
|
|
if (searchOptions[element] == '*空') {
|
|
|
|
|
if (joinSubstring.includes(element)) {
|
|
|
|
|
where[element] = null;
|
|
|
|
|
} else {
|
|
|
|
|
where[element] = [null, ''];
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
} 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']);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const insiders = await ctx.model3.Insiders.findAll({ attributes: ['uid', 'name'] });
|
|
|
|
|
if (searchOptions.name) {
|
|
|
|
|
if (searchOptions.name == '*空') {
|
|
|
|
|
where.user = [null, ''];
|
|
|
|
|
} else {
|
|
|
|
|
let uidArr = [];
|
|
|
|
|
insiders.filter(item => {
|
|
|
|
|
if (item.dataValues.name.includes(searchOptions.name)) {
|
|
|
|
|
uidArr.push(item.dataValues.uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
where.user = [...uidArr];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dbOptions = { attributes, offset, limit, where, order };
|
|
|
|
|
const result = await ctx.model3.Monitoring.findAndCountAll(dbOptions);
|
|
|
|
|
result.rows.forEach(element => {
|
|
|
|
|
element.dataValues.name = '';
|
|
|
|
|
insiders.filter(item => {
|
|
|
|
|
if (item.dataValues.uid == element.dataValues.user) {
|
|
|
|
|
element.dataValues.name = item.dataValues.name;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return result.rows.length == 0 ? false : result;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const blockName = fileName + "--" + "showMonitoring";
|
|
|
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
|
|
|
return 'error';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除危险操作记录
|
|
|
|
|
async deleteMonitoring(uid, modelType, password, requestMsg) {
|
|
|
|
|
const { ctx } = this;
|
|
|
|
|
const systemMonitoring = {
|
|
|
|
|
operate: 2,
|
|
|
|
|
operate_msg: '删除危险操作记录',
|
|
|
|
|
type: 2,
|
|
|
|
|
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 < 9) {
|
|
|
|
|
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 < 9) systemMonitoring.status = 2;
|
|
|
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
let where = {};
|
|
|
|
|
if (modelType == "single") {
|
|
|
|
|
const { user, created_at } = requestMsg;
|
|
|
|
|
where = { user, created_at };
|
|
|
|
|
} else if (modelType == "multiple") {
|
|
|
|
|
let user = [], created_at = [];
|
|
|
|
|
for (const key in requestMsg) {
|
|
|
|
|
requestMsg[key].forEach(element => {
|
|
|
|
|
user.push(element.user),
|
|
|
|
|
created_at.push(element.created_at)
|
|
|
|
|
});
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
where = { user, created_at };
|
|
|
|
|
}
|
|
|
|
|
const resultList = await ctx.model3.Monitoring.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 + "--" + "deleteMonitoring";
|
|
|
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
|
|
|
return 'error';
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
}
|
2026-03-27 17:49:17 +08:00
|
|
|
|
2026-06-05 17:38:10 +08:00
|
|
|
// 管理系统导出监控记录
|
|
|
|
|
async downloadMonitoring(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 = ['operate', 'operate_msg', 'type', 'status', 'affect_entries', 'user', 'created_at'],
|
|
|
|
|
where = {}, order = [], dbOptions,
|
|
|
|
|
joinSubstring = [],
|
|
|
|
|
substring = ['operate_msg', 'affect_entries', 'user'],
|
|
|
|
|
key = ['operate', 'type', 'status'],
|
|
|
|
|
between = ['created_at'];
|
|
|
|
|
attributes.forEach(element => {
|
|
|
|
|
if (searchOptions[element]) {
|
|
|
|
|
if (searchOptions[element] == '*空') {
|
|
|
|
|
if (joinSubstring.includes(element)) {
|
|
|
|
|
where[element] = null;
|
|
|
|
|
} else {
|
|
|
|
|
where[element] = [null, ''];
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
} 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.mode3.Monitoring.findAll(dbOptions);
|
|
|
|
|
if (result.length == 0) return { code: false };
|
|
|
|
|
let data = [];
|
|
|
|
|
const operateOptions = [
|
|
|
|
|
{
|
|
|
|
|
value: "0",
|
|
|
|
|
label: "新增数据"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "1",
|
|
|
|
|
label: "编辑数据"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "2",
|
|
|
|
|
label: "删除数据"
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
const typeOptions = [
|
|
|
|
|
{
|
|
|
|
|
value: "0",
|
|
|
|
|
label: "一般等级"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "1",
|
|
|
|
|
label: "重要等级"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "2",
|
|
|
|
|
label: "严重等级"
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
];
|
|
|
|
|
const statusOptions = [
|
|
|
|
|
{
|
|
|
|
|
value: "0",
|
|
|
|
|
label: "因无用户身份未成功执行"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "1",
|
|
|
|
|
label: "因密码错误未成功执行"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "2",
|
|
|
|
|
label: "因权限不足未成功执行"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "3",
|
|
|
|
|
label: "因查询条件错误未成功执行"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "4",
|
|
|
|
|
label: "成功执行"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "5",
|
|
|
|
|
label: "因程序错误未成功执行"
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
result.forEach(element => {
|
|
|
|
|
operateOptions.forEach((item) => {
|
|
|
|
|
if (item.value == element.dataValues["operate"]) {
|
|
|
|
|
element.dataValues["operate"] = item.label;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
typeOptions.forEach((item) => {
|
|
|
|
|
if (item.value == element.dataValues["type"]) {
|
|
|
|
|
element.dataValues["type"] = 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秒");
|
|
|
|
|
data.push(element.dataValues);
|
|
|
|
|
});
|
|
|
|
|
return { code: true, data };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const blockName = fileName + "--" + "downloadMonitoring";
|
|
|
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
|
|
|
return 'error';
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
2026-06-05 17:38:10 +08:00
|
|
|
}
|
2026-03-27 17:49:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = MonitoringService;
|