2026-04-20 06:43:09 +00:00
|
|
|
const { Op } = require('sequelize');
|
|
|
|
|
const OperationLog = require('../models/OperationLog');
|
|
|
|
|
const LoginLog = require('../models/LoginLog');
|
2026-04-22 06:48:32 +00:00
|
|
|
const response = require('../utils/response');
|
2026-04-20 06:43:09 +00:00
|
|
|
|
|
|
|
|
// 获取操作日志列表
|
|
|
|
|
exports.getOperationLogs = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { page = 1, pageSize = 20, username, module, action, status, startTime, endTime } = req.query;
|
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
const where = {};
|
|
|
|
|
if (username) {
|
|
|
|
|
where.username = { [Op.like]: `%${username}%` };
|
|
|
|
|
}
|
|
|
|
|
if (module) {
|
|
|
|
|
where.module = module;
|
|
|
|
|
}
|
|
|
|
|
if (action) {
|
|
|
|
|
where.action = action;
|
|
|
|
|
}
|
|
|
|
|
if (status) {
|
|
|
|
|
where.status = status;
|
|
|
|
|
}
|
|
|
|
|
if (startTime && endTime) {
|
|
|
|
|
where.createTime = {
|
|
|
|
|
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询日志列表
|
|
|
|
|
const { count, rows } = await OperationLog.findAndCountAll({
|
|
|
|
|
where,
|
|
|
|
|
order: [['createTime', 'DESC']],
|
|
|
|
|
offset: (page - 1) * pageSize,
|
|
|
|
|
limit: parseInt(pageSize)
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-22 06:48:32 +00:00
|
|
|
response.success(res, '获取成功', {
|
|
|
|
|
list: rows,
|
|
|
|
|
total: count,
|
|
|
|
|
page: parseInt(page),
|
|
|
|
|
pageSize: parseInt(pageSize)
|
2026-04-20 06:43:09 +00:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取操作日志错误:', error);
|
2026-04-22 06:48:32 +00:00
|
|
|
response.serverError(res, '获取操作日志失败', error);
|
2026-04-20 06:43:09 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 获取登录日志列表
|
|
|
|
|
exports.getLoginLogs = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { page = 1, pageSize = 20, username, loginType, status, startTime, endTime } = req.query;
|
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
const where = {};
|
|
|
|
|
if (username) {
|
|
|
|
|
where.username = { [Op.like]: `%${username}%` };
|
|
|
|
|
}
|
|
|
|
|
if (loginType) {
|
|
|
|
|
where.loginType = loginType;
|
|
|
|
|
}
|
|
|
|
|
if (status) {
|
|
|
|
|
where.status = status;
|
|
|
|
|
}
|
|
|
|
|
if (startTime && endTime) {
|
|
|
|
|
where.createTime = {
|
|
|
|
|
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询日志列表
|
|
|
|
|
const { count, rows } = await LoginLog.findAndCountAll({
|
|
|
|
|
where,
|
|
|
|
|
order: [['createTime', 'DESC']],
|
|
|
|
|
offset: (page - 1) * pageSize,
|
|
|
|
|
limit: parseInt(pageSize)
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-22 06:48:32 +00:00
|
|
|
response.success(res, '获取成功', {
|
|
|
|
|
list: rows,
|
|
|
|
|
total: count,
|
|
|
|
|
page: parseInt(page),
|
|
|
|
|
pageSize: parseInt(pageSize)
|
2026-04-20 06:43:09 +00:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取登录日志错误:', error);
|
2026-04-22 06:48:32 +00:00
|
|
|
response.serverError(res, '获取登录日志失败', error);
|
2026-04-20 06:43:09 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 清空操作日志
|
|
|
|
|
exports.clearOperationLogs = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { startTime, endTime } = req.body;
|
|
|
|
|
const where = {};
|
|
|
|
|
|
|
|
|
|
if (startTime && endTime) {
|
|
|
|
|
where.createTime = {
|
|
|
|
|
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await OperationLog.destroy({ where });
|
|
|
|
|
|
2026-04-22 06:48:32 +00:00
|
|
|
response.success(res, '清空成功');
|
2026-04-20 06:43:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('清空操作日志错误:', error);
|
2026-04-22 06:48:32 +00:00
|
|
|
response.serverError(res, '清空操作日志失败', error);
|
2026-04-20 06:43:09 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 清空登录日志
|
|
|
|
|
exports.clearLoginLogs = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { startTime, endTime } = req.body;
|
|
|
|
|
const where = {};
|
|
|
|
|
|
|
|
|
|
if (startTime && endTime) {
|
|
|
|
|
where.createTime = {
|
|
|
|
|
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await LoginLog.destroy({ where });
|
|
|
|
|
|
2026-04-22 06:48:32 +00:00
|
|
|
response.success(res, '清空成功');
|
2026-04-20 06:43:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('清空登录日志错误:', error);
|
2026-04-22 06:48:32 +00:00
|
|
|
response.serverError(res, '清空登录日志失败', error);
|
2026-04-20 06:43:09 +00:00
|
|
|
}
|
|
|
|
|
};
|