feat: 添加操作日志、危险操作确认和业务关联功能
1. 新增操作日志记录功能,记录关键操作 2. 实现危险操作确认对话框,防止误删 3. 添加业务和库房管理模块 4. 支持设备标记为空闲状态 5. 完善API文档和健康检查 6. 优化前端删除操作的确认流程 7. 添加Swagger API文档支持 8. 实现设备与业务的关联功能 9. 改进设备模型,添加空闲相关字段 10. 优化用户、角色管理操作日志
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
const { sequelize } = require('./db');
|
||||
|
||||
async function addNewColumns() {
|
||||
try {
|
||||
console.log('开始添加新列...');
|
||||
|
||||
await sequelize.query(`
|
||||
ALTER TABLE devices ADD COLUMN isIdle INTEGER DEFAULT 0;
|
||||
`);
|
||||
console.log('已添加 isIdle 列');
|
||||
|
||||
await sequelize.query(`
|
||||
ALTER TABLE devices ADD COLUMN idleDate DATETIME;
|
||||
`);
|
||||
console.log('已添加 idleDate 列');
|
||||
|
||||
await sequelize.query(`
|
||||
ALTER TABLE devices ADD COLUMN idleReason TEXT;
|
||||
`);
|
||||
console.log('已添加 idleReason 列');
|
||||
|
||||
await sequelize.query(`
|
||||
ALTER TABLE devices ADD COLUMN warehouseId TEXT;
|
||||
`);
|
||||
console.log('已添加 warehouseId 列');
|
||||
|
||||
await sequelize.query(`
|
||||
ALTER TABLE devices ADD COLUMN sourceType TEXT DEFAULT 'rack';
|
||||
`);
|
||||
console.log('已添加 sourceType 列');
|
||||
|
||||
console.log('所有新列添加完成!');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
if (error.message.includes('duplicate column')) {
|
||||
console.log('列已存在,跳过');
|
||||
process.exit(0);
|
||||
}
|
||||
console.error('添加列失败:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
addNewColumns();
|
||||
@@ -172,8 +172,13 @@ async function initDeviceFields() {
|
||||
await DeviceField.create(field);
|
||||
console.log(`创建字段: ${field.displayName}`);
|
||||
} else {
|
||||
// 已存在的字段跳过,保留用户自定义配置
|
||||
console.log(`跳过已存在字段: ${field.displayName}`);
|
||||
// 如果字段已存在但缺少 options,则补充 options
|
||||
if (field.options && !existingField.options) {
|
||||
await existingField.update({ options: field.options });
|
||||
console.log(`更新字段 options: ${field.displayName}`);
|
||||
} else {
|
||||
console.log(`跳过已存在字段: ${field.displayName}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/tests/**/*.test.js'],
|
||||
collectCoverageFrom: [
|
||||
'models/**/*.js',
|
||||
'utils/**/*.js',
|
||||
'routes/**/*.js',
|
||||
'!models/ticketIndex.js'
|
||||
],
|
||||
coverageDirectory: 'coverage',
|
||||
verbose: true,
|
||||
testTimeout: 30000,
|
||||
setupFiles: ['./tests/setupEnv.js'],
|
||||
setupFilesAfterEnv: ['./tests/setup.js']
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
const Business = sequelize.define('Business', {
|
||||
businessId: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('active', 'offline'),
|
||||
defaultValue: 'active'
|
||||
},
|
||||
offlineDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
offlineReason: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
tableName: 'businesses',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{ fields: ['status'] },
|
||||
{ fields: ['name'] }
|
||||
]
|
||||
});
|
||||
|
||||
module.exports = Business;
|
||||
@@ -1,5 +1,7 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
const Rack = require('./Rack');
|
||||
const Warehouse = require('./Warehouse');
|
||||
|
||||
const Device = sequelize.define('Device', {
|
||||
deviceId: {
|
||||
@@ -47,6 +49,26 @@ const Device = sequelize.define('Device', {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: 'offline'
|
||||
},
|
||||
isIdle: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
},
|
||||
idleDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
idleReason: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
warehouseId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
sourceType: {
|
||||
type: DataTypes.ENUM('rack', 'warehouse'),
|
||||
defaultValue: 'rack'
|
||||
},
|
||||
purchaseDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
@@ -81,4 +103,7 @@ const Device = sequelize.define('Device', {
|
||||
]
|
||||
});
|
||||
|
||||
Device.belongsTo(Rack, { foreignKey: 'rackId' });
|
||||
Device.belongsTo(Warehouse, { foreignKey: 'warehouseId' });
|
||||
|
||||
module.exports = Device;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
const Device = require('./Device');
|
||||
const Business = require('./Business');
|
||||
|
||||
const DeviceBusiness = sequelize.define('DeviceBusiness', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
deviceId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: Device,
|
||||
key: 'deviceId'
|
||||
}
|
||||
},
|
||||
businessId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: Business,
|
||||
key: 'businessId'
|
||||
}
|
||||
},
|
||||
isPrimary: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'device_business',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{ fields: ['deviceId'] },
|
||||
{ fields: ['businessId'] },
|
||||
{ unique: true, fields: ['deviceId', 'businessId'] }
|
||||
]
|
||||
});
|
||||
|
||||
Device.belongsToMany(Business, {
|
||||
through: DeviceBusiness,
|
||||
foreignKey: 'deviceId',
|
||||
otherKey: 'businessId'
|
||||
});
|
||||
|
||||
Business.belongsToMany(Device, {
|
||||
through: DeviceBusiness,
|
||||
foreignKey: 'businessId',
|
||||
otherKey: 'deviceId'
|
||||
});
|
||||
|
||||
DeviceBusiness.belongsTo(Business, { foreignKey: 'businessId' });
|
||||
DeviceBusiness.belongsTo(Device, { foreignKey: 'deviceId' });
|
||||
|
||||
module.exports = DeviceBusiness;
|
||||
@@ -0,0 +1,89 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
const generateRecordId = () => {
|
||||
return `OPLOG_${Date.now().toString(36)}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
};
|
||||
|
||||
const OperationLog = sequelize.define('OperationLog', {
|
||||
recordId: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
module: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
comment: '模块:device/user/role/consumable/rack/room'
|
||||
},
|
||||
operationType: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
comment: '操作类型: create/update/delete/batch_delete/batch_update/status_change/move/permission_change'
|
||||
},
|
||||
operationDescription: {
|
||||
type: DataTypes.TEXT,
|
||||
comment: '操作描述'
|
||||
},
|
||||
targetId: {
|
||||
type: DataTypes.STRING,
|
||||
comment: '目标对象ID'
|
||||
},
|
||||
targetName: {
|
||||
type: DataTypes.STRING,
|
||||
comment: '目标对象名称(冗余便于展示)'
|
||||
},
|
||||
operatorId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
comment: '操作人ID'
|
||||
},
|
||||
operatorName: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
comment: '操作人姓名'
|
||||
},
|
||||
operatorRole: {
|
||||
type: DataTypes.STRING,
|
||||
comment: '操作人角色'
|
||||
},
|
||||
beforeState: {
|
||||
type: DataTypes.JSON,
|
||||
comment: '操作前状态'
|
||||
},
|
||||
afterState: {
|
||||
type: DataTypes.JSON,
|
||||
comment: '操作后状态'
|
||||
},
|
||||
result: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: 'success',
|
||||
comment: '操作结果: success/failed'
|
||||
},
|
||||
ipAddress: {
|
||||
type: DataTypes.STRING,
|
||||
comment: 'IP地址'
|
||||
},
|
||||
userAgent: {
|
||||
type: DataTypes.STRING,
|
||||
comment: '用户代理'
|
||||
},
|
||||
metadata: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
comment: '扩展字段'
|
||||
}
|
||||
}, {
|
||||
tableName: 'operation_logs',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{ fields: ['module'] },
|
||||
{ fields: ['operationType'] },
|
||||
{ fields: ['targetId'] },
|
||||
{ fields: ['operatorId'] },
|
||||
{ fields: ['createdAt'] }
|
||||
]
|
||||
});
|
||||
|
||||
module.exports = OperationLog;
|
||||
@@ -0,0 +1,41 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
const Warehouse = sequelize.define('Warehouse', {
|
||||
warehouseId: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
location: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
capacity: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
defaultValue: 100
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('active', 'inactive'),
|
||||
defaultValue: 'active'
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
tableName: 'warehouses',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{ fields: ['status'] },
|
||||
{ fields: ['name'] }
|
||||
]
|
||||
});
|
||||
|
||||
module.exports = Warehouse;
|
||||
Generated
+905
-1430
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,11 @@
|
||||
"lint": "eslint . --ext js --report-unused-disable-directives --max-warnings 0",
|
||||
"lint:fix": "eslint . --ext js --fix",
|
||||
"format": "prettier --write \"**/*.js\"",
|
||||
"format:check": "prettier --check \"**/*.js\""
|
||||
"format:check": "prettier --check \"**/*.js\"",
|
||||
"test": "jest --runInBand",
|
||||
"test:watch": "jest --watch",
|
||||
"test:coverage": "jest --coverage",
|
||||
"test:operation-logs": "jest tests/operationLog.model.test.js tests/operationLogger.test.js tests/operationLogs.api.test.js --runInBand"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.13.6",
|
||||
@@ -34,6 +38,8 @@
|
||||
"smb2": "^0.2.2",
|
||||
"sqlite3": "^5.1.7",
|
||||
"ssh2-sftp-client": "^9.1.0",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.1",
|
||||
"three": "^0.182.0",
|
||||
"webdav": "^5.3.1",
|
||||
"winston": "^3.19.0",
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { logDangerousOperation, getDangerousOperationsLogs, cleanOldLogs, DANGEROUS_OPERATION_TYPES, RISK_LEVELS, calculateRiskLevel } = require('../utils/dangerousOperationLogger');
|
||||
|
||||
router.post('/log', async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
operationType,
|
||||
operationName,
|
||||
targetType,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
metadata = {},
|
||||
success = true,
|
||||
errorMessage = null,
|
||||
} = req.body;
|
||||
|
||||
if (!operationType || !operationName) {
|
||||
return res.status(400).json({ error: '缺少必需参数 operationType 或 operationName' });
|
||||
}
|
||||
|
||||
const riskLevel = metadata.riskLevel || calculateRiskLevel(operationType, metadata.itemCount || 1, {
|
||||
hasRelatedData: metadata.relatedDataCount > 0,
|
||||
isSystemLevel: metadata.isSystemLevel,
|
||||
});
|
||||
|
||||
await logDangerousOperation(req, {
|
||||
operationType,
|
||||
operationName,
|
||||
targetType,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
metadata: {
|
||||
...metadata,
|
||||
riskLevel,
|
||||
},
|
||||
success,
|
||||
errorMessage,
|
||||
});
|
||||
|
||||
res.json({ success: true, riskLevel });
|
||||
} catch (error) {
|
||||
console.error('Failed to log dangerous operation:', error);
|
||||
res.status(500).json({ error: '日志记录失败' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/logs', async (req, res) => {
|
||||
try {
|
||||
const { operationType, targetType, success, startDate, endDate, username, riskLevel, page = 1, pageSize = 50 } = req.query;
|
||||
|
||||
const filters = {
|
||||
operationType,
|
||||
targetType,
|
||||
success: success !== undefined ? success === 'true' : undefined,
|
||||
startDate,
|
||||
endDate,
|
||||
username,
|
||||
riskLevel,
|
||||
};
|
||||
|
||||
const allLogs = getDangerousOperationsLogs(filters);
|
||||
const total = allLogs.length;
|
||||
const startIndex = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const endIndex = startIndex + parseInt(pageSize);
|
||||
const logs = allLogs.slice(startIndex, endIndex);
|
||||
|
||||
res.json({
|
||||
logs,
|
||||
total,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
totalPages: Math.ceil(total / parseInt(pageSize)),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to get dangerous operations logs:', error);
|
||||
res.status(500).json({ error: '获取日志失败' });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/logs/clean', async (req, res) => {
|
||||
try {
|
||||
const { daysToKeep = 90 } = req.query;
|
||||
|
||||
if (!req.user || req.user.role !== 'admin') {
|
||||
return res.status(403).json({ error: '只有管理员才能清理日志' });
|
||||
}
|
||||
|
||||
const result = await cleanOldLogs(parseInt(daysToKeep));
|
||||
|
||||
await logDangerousOperation(req, {
|
||||
operationType: DANGEROUS_OPERATION_TYPES.PURGE,
|
||||
operationName: '清理危险操作日志',
|
||||
targetType: 'operation_logs',
|
||||
targetId: null,
|
||||
targetName: `清理 ${daysToKeep} 天前的日志`,
|
||||
metadata: {
|
||||
riskLevel: RISK_LEVELS.MEDIUM,
|
||||
deletedCount: result.deletedCount,
|
||||
remainingCount: result.remainingCount,
|
||||
daysToKeep: parseInt(daysToKeep),
|
||||
},
|
||||
success: true,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: `已清理 ${result.deletedCount} 条过期日志,保留 ${result.remainingCount} 条日志`,
|
||||
deletedCount: result.deletedCount,
|
||||
remainingCount: result.remainingCount,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to clean logs:', error);
|
||||
res.status(500).json({ error: '清理日志失败' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/risk-assessment', async (req, res) => {
|
||||
try {
|
||||
const { operationType, itemCount, hasRelatedData, isSystemLevel } = req.query;
|
||||
|
||||
const riskLevel = calculateRiskLevel(
|
||||
operationType,
|
||||
parseInt(itemCount) || 1,
|
||||
{
|
||||
hasRelatedData: hasRelatedData === 'true',
|
||||
isSystemLevel: isSystemLevel === 'true',
|
||||
}
|
||||
);
|
||||
|
||||
const riskDescriptions = {
|
||||
[RISK_LEVELS.EXTREME]: '极高风险操作,需要输入确认关键词才能执行',
|
||||
[RISK_LEVELS.HIGH]: '高风险操作,需要详细确认信息',
|
||||
[RISK_LEVELS.MEDIUM]: '中等风险操作,需要明确确认',
|
||||
[RISK_LEVELS.LOW]: '低风险操作,使用标准确认即可',
|
||||
};
|
||||
|
||||
res.json({
|
||||
riskLevel,
|
||||
description: riskDescriptions[riskLevel],
|
||||
requiresKeyword: riskLevel === RISK_LEVELS.EXTREME,
|
||||
confirmationLevel: riskLevel === RISK_LEVELS.EXTREME ? 'KEYWORD' : riskLevel === RISK_LEVELS.HIGH ? 'ENHANCED' : 'STANDARD',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to assess risk:', error);
|
||||
res.status(500).json({ error: '风险评估失败' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
+207
-37
@@ -16,6 +16,7 @@ const DevicePort = require('../models/DevicePort');
|
||||
const Cable = require('../models/Cable');
|
||||
const NetworkCard = require('../models/NetworkCard');
|
||||
const InventoryRecord = require('../models/InventoryRecord');
|
||||
const { logDeviceOperation } = require('../utils/operationLogger');
|
||||
const { validateBody, validateQuery } = require('../middleware/validation');
|
||||
const {
|
||||
createDeviceSchema,
|
||||
@@ -469,7 +470,7 @@ router.post('/import-preview', async (req, res) => {
|
||||
|
||||
router.get('/', validateQuery(queryDeviceSchema), async (req, res) => {
|
||||
try {
|
||||
const { keyword, status, type, rackId, roomId, page = 1, pageSize = 10 } = req.query;
|
||||
const { keyword, status, type, rackId, roomId, page = 1, pageSize = 10, isIdle } = req.query;
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
// 构建查询条件
|
||||
@@ -544,6 +545,11 @@ router.get('/', validateQuery(queryDeviceSchema), async (req, res) => {
|
||||
where['$Rack.roomId$'] = roomId;
|
||||
}
|
||||
|
||||
// 空闲设备筛选
|
||||
if (isIdle !== undefined && isIdle !== '') {
|
||||
where.isIdle = isIdle === 'true' || isIdle === true;
|
||||
}
|
||||
|
||||
// 执行查询 - 优化:使用 JOIN 避免 N+1 查询问题
|
||||
const { count, rows } = await Device.findAndCountAll({
|
||||
where,
|
||||
@@ -626,14 +632,31 @@ router.post('/', validateBody(createDeviceSchema), async (req, res) => {
|
||||
}
|
||||
|
||||
const device = await Device.create(deviceData);
|
||||
|
||||
|
||||
const rack = await Rack.findByPk(deviceData.rackId);
|
||||
if (rack) {
|
||||
await rack.update({
|
||||
currentPower: rack.currentPower + deviceData.powerConsumption
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const createDetails = [
|
||||
`设备名称: ${device.name}`,
|
||||
`设备编号: ${device.deviceId}`,
|
||||
`设备类型: ${device.type}`,
|
||||
`所属机柜: ${rack ? rack.name : '未分配'}`,
|
||||
`安装位置: U${device.position}`,
|
||||
`功耗: ${device.powerConsumption}W`
|
||||
].join(';');
|
||||
|
||||
await logDeviceOperation('create', `创建设备【${device.name}】`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
afterState: device.toJSON(),
|
||||
req,
|
||||
metadata: { deviceType: device.type, rackName: rack?.name, powerConsumption: device.powerConsumption }
|
||||
});
|
||||
|
||||
res.status(201).json(device);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
@@ -1363,32 +1386,32 @@ router.put('/batch-offline', validateBody(batchDeviceIdsSchema), async (req, res
|
||||
router.put('/batch-status', async (req, res) => {
|
||||
try {
|
||||
const { deviceIds, status } = req.body;
|
||||
|
||||
|
||||
if (!deviceIds || !Array.isArray(deviceIds) || deviceIds.length === 0) {
|
||||
return res.status(400).json({ error: '请提供有效的设备ID列表' });
|
||||
}
|
||||
|
||||
|
||||
// 检查数据库中是否存在这些设备
|
||||
const existingDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
attributes: ['deviceId']
|
||||
});
|
||||
|
||||
|
||||
// 检查是否有不存在的设备
|
||||
const existingIds = existingDevices.map(d => d.deviceId);
|
||||
const missingIds = deviceIds.filter(id => !existingIds.includes(id));
|
||||
|
||||
|
||||
if (missingIds.length > 0) {
|
||||
return res.status(404).json({ error: `设备不存在: ${missingIds.join(', ')}` });
|
||||
}
|
||||
|
||||
|
||||
const validStatus = ['running', 'maintenance', 'offline', 'fault'];
|
||||
if (!validStatus.includes(status)) {
|
||||
return res.status(400).json({
|
||||
error: `状态值无效,有效值为:${validStatus.join('、')}`
|
||||
return res.status(400).json({
|
||||
error: `状态值无效,有效值为:${validStatus.join('、')}`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 状态映射
|
||||
const statusText = {
|
||||
running: '运行中',
|
||||
@@ -1396,13 +1419,30 @@ router.put('/batch-status', async (req, res) => {
|
||||
offline: '离线',
|
||||
fault: '故障'
|
||||
};
|
||||
|
||||
|
||||
const beforeDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } }
|
||||
});
|
||||
|
||||
const deviceNames = beforeDevices.map(d => d.name);
|
||||
|
||||
// 更新设备状态
|
||||
const [affectedCount] = await Device.update(
|
||||
{ status },
|
||||
{ where: { deviceId: { [Op.in]: deviceIds } } }
|
||||
);
|
||||
|
||||
|
||||
const statusChangeDesc = `批量变更${affectedCount}台设备状态:${deviceNames.join('、')} → ${statusText[status]}`;
|
||||
|
||||
await logDeviceOperation('status_change', statusChangeDesc, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${affectedCount}台设备`,
|
||||
beforeState: beforeDevices.map(d => ({ deviceId: d.deviceId, name: d.name, status: d.status })),
|
||||
afterState: beforeDevices.map(d => ({ deviceId: d.deviceId, name: d.name, status })),
|
||||
req,
|
||||
metadata: { status, statusText: statusText[status], count: affectedCount, deviceNames }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `批量状态变更成功,已将 ${affectedCount} 个设备状态变更为"${statusText[status]}"`,
|
||||
affectedCount,
|
||||
@@ -1433,9 +1473,16 @@ router.put('/batch-move', async (req, res) => {
|
||||
|
||||
const devicesToMove = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
attributes: ['deviceId', 'position', 'height']
|
||||
attributes: ['deviceId', 'name', 'rackId', 'position', 'height']
|
||||
});
|
||||
|
||||
const beforeMoveState = devicesToMove.map(d => ({
|
||||
deviceId: d.deviceId,
|
||||
name: d.name,
|
||||
rackId: d.rackId,
|
||||
position: d.position
|
||||
}));
|
||||
|
||||
const deviceHeightMap = new Map(devicesToMove.map(d => [d.deviceId, d.height || 1]));
|
||||
|
||||
if (startPosition) {
|
||||
@@ -1511,6 +1558,20 @@ router.put('/batch-move', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const deviceNames = devicesToMove.map(d => d.name);
|
||||
const moveDesc = startPosition
|
||||
? `批量移动${movedCount}台设备到机柜【${targetRack.name}】:${deviceNames.join('、')} → U${startPosition}起`
|
||||
: `批量移动${movedCount}台设备到机柜【${targetRack.name}】:${deviceNames.join('、')}`;
|
||||
|
||||
await logDeviceOperation('move', moveDesc, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${movedCount}台设备`,
|
||||
beforeState: beforeMoveState,
|
||||
afterState: { targetRackId, targetRackName: targetRack.name, startPosition },
|
||||
req,
|
||||
metadata: { count: movedCount, targetRackId, targetRackName: targetRack.name, startPosition, deviceNames }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `批量移动成功,已将 ${movedCount} 个设备移动到机柜 ${targetRackId}`,
|
||||
movedCount
|
||||
@@ -1542,6 +1603,61 @@ router.get('/:deviceId', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 将设备标记为空闲
|
||||
router.put('/:deviceId/to-idle', async (req, res) => {
|
||||
const t = await sequelize.transaction();
|
||||
try {
|
||||
const { idleReason } = req.body;
|
||||
const { deviceId } = req.params;
|
||||
|
||||
const device = await Device.findByPk(deviceId, { transaction: t });
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
if (device.isIdle) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '设备已经标记为空闲设备' });
|
||||
}
|
||||
|
||||
await device.update({
|
||||
isIdle: true,
|
||||
idleDate: new Date(),
|
||||
idleReason: idleReason || `从设备管理转入`
|
||||
}, { transaction: t });
|
||||
|
||||
if (device.rackId) {
|
||||
const rack = await Rack.findByPk(device.rackId, { transaction: t });
|
||||
if (rack) {
|
||||
await rack.update({
|
||||
currentPower: Math.max(0, rack.currentPower - (device.powerConsumption || 0))
|
||||
}, { transaction: t });
|
||||
}
|
||||
}
|
||||
|
||||
await t.commit();
|
||||
|
||||
await logDeviceOperation('to_idle', `设备【${device.name}】转入空闲设备`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState: { ...device.toJSON(), isIdle: false },
|
||||
afterState: { ...device.toJSON(), isIdle: true },
|
||||
req,
|
||||
metadata: { idleReason, type: 'device_to_idle' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: '设备已转入空闲设备',
|
||||
device: device.toJSON()
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('设备转入空闲设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取设备的工单列表
|
||||
router.get('/:deviceId/tickets', async (req, res) => {
|
||||
try {
|
||||
@@ -1583,18 +1699,19 @@ router.get('/:deviceId/tickets', async (req, res) => {
|
||||
// 更新设备
|
||||
router.put('/:deviceId', validateBody(updateDeviceSchema), async (req, res) => {
|
||||
try {
|
||||
// 获取旧设备信息以更新功率
|
||||
const oldDevice = await Device.findByPk(req.params.deviceId);
|
||||
if (!oldDevice) {
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
|
||||
const beforeState = oldDevice.toJSON();
|
||||
const changedFields = {};
|
||||
|
||||
const [updated] = await Device.update(req.body, {
|
||||
where: { deviceId: req.params.deviceId }
|
||||
});
|
||||
|
||||
|
||||
if (updated) {
|
||||
// 更新机柜当前功率
|
||||
const rack = await Rack.findByPk(oldDevice.rackId);
|
||||
if (rack) {
|
||||
const powerDiff = req.body.powerConsumption - oldDevice.powerConsumption;
|
||||
@@ -1602,7 +1719,7 @@ router.put('/:deviceId', validateBody(updateDeviceSchema), async (req, res) => {
|
||||
currentPower: rack.currentPower + powerDiff
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const updatedDevice = await Device.findByPk(req.params.deviceId, {
|
||||
include: [
|
||||
{
|
||||
@@ -1613,6 +1730,38 @@ router.put('/:deviceId', validateBody(updateDeviceSchema), async (req, res) => {
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const afterState = updatedDevice.toJSON();
|
||||
for (const key of Object.keys(req.body)) {
|
||||
if (JSON.stringify(beforeState[key]) !== JSON.stringify(afterState[key])) {
|
||||
changedFields[key] = { from: beforeState[key], to: afterState[key] };
|
||||
}
|
||||
}
|
||||
|
||||
const changeDetails = Object.entries(changedFields).map(([field, values]) => {
|
||||
const fieldNames = {
|
||||
name: '名称', deviceId: '设备编号', type: '类型', model: '型号',
|
||||
manufacturer: '制造商', serialNumber: '序列号', status: '状态',
|
||||
position: '安装位置(U)', height: '占用高度(U)', powerConsumption: '功耗(W)',
|
||||
ipAddress: 'IP地址', macAddress: 'MAC地址', managementIp: '管理IP'
|
||||
};
|
||||
const displayName = fieldNames[field] || field;
|
||||
return `${displayName}: ${values.from ?? '空'} → ${values.to ?? '空'}`;
|
||||
}).join(';');
|
||||
|
||||
const operationDesc = changeDetails
|
||||
? `更新设备【${updatedDevice.name}】:${changeDetails}`
|
||||
: `更新设备【${updatedDevice.name}】`;
|
||||
|
||||
await logDeviceOperation('update', operationDesc, {
|
||||
targetId: updatedDevice.deviceId,
|
||||
targetName: updatedDevice.name,
|
||||
beforeState,
|
||||
afterState,
|
||||
req,
|
||||
metadata: { changedFields }
|
||||
});
|
||||
|
||||
res.json(updatedDevice);
|
||||
} else {
|
||||
res.status(404).json({ error: '设备不存在' });
|
||||
@@ -1627,17 +1776,19 @@ router.post('/batch-delete', validateBody(batchDeviceIdsSchema), async (req, res
|
||||
const t = await sequelize.transaction();
|
||||
try {
|
||||
const { deviceIds } = req.body;
|
||||
|
||||
|
||||
if (!deviceIds || !Array.isArray(deviceIds) || deviceIds.length === 0) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '请提供有效的设备 ID 列表' });
|
||||
}
|
||||
|
||||
|
||||
const devices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
const deviceNames = devices.map(d => d.name).join(', ');
|
||||
|
||||
// 1. 删除相关端口 (必须在网卡之前删除,因为端口依赖网卡)
|
||||
await DevicePort.destroy({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
@@ -1660,19 +1811,19 @@ router.post('/batch-delete', validateBody(batchDeviceIdsSchema), async (req, res
|
||||
},
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
// 4. 解除工单关联
|
||||
await Ticket.update(
|
||||
{ deviceId: null },
|
||||
{ where: { deviceId: { [Op.in]: deviceIds } }, transaction: t }
|
||||
);
|
||||
|
||||
|
||||
// 5. 删除盘点记录
|
||||
await InventoryRecord.destroy({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
// 6. 更新机柜功率
|
||||
for (const device of devices) {
|
||||
if (device.rackId) {
|
||||
@@ -1684,7 +1835,7 @@ router.post('/batch-delete', validateBody(batchDeviceIdsSchema), async (req, res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 7. 删除设备
|
||||
const deletedCount = await Device.destroy({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
@@ -1692,7 +1843,15 @@ router.post('/batch-delete', validateBody(batchDeviceIdsSchema), async (req, res
|
||||
});
|
||||
|
||||
await t.commit();
|
||||
|
||||
|
||||
await logDeviceOperation('batch_delete', `批量删除${deletedCount}台设备:${deviceNames}`, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${deletedCount}台设备`,
|
||||
beforeState: devices.map(d => d.toJSON()),
|
||||
req,
|
||||
metadata: { count: deletedCount, deviceNames }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `批量删除成功,已删除 ${deletedCount} 个设备`,
|
||||
deletedCount
|
||||
@@ -1789,20 +1948,23 @@ router.delete('/:deviceId', async (req, res) => {
|
||||
const t = await sequelize.transaction();
|
||||
try {
|
||||
const { deviceId } = req.params;
|
||||
|
||||
|
||||
// 获取设备信息以更新功率
|
||||
const device = await Device.findByPk(deviceId, { transaction: t });
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
|
||||
const deviceName = device.name;
|
||||
const beforeState = device.toJSON();
|
||||
|
||||
// 1. 删除相关端口 (必须在网卡之前删除,因为端口依赖网卡)
|
||||
const deletedPorts = await DevicePort.destroy({
|
||||
where: { deviceId: deviceId },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
// 2. 删除相关网卡
|
||||
const deletedNetworkCards = await NetworkCard.destroy({
|
||||
where: { deviceId: deviceId },
|
||||
@@ -1826,13 +1988,13 @@ router.delete('/:deviceId', async (req, res) => {
|
||||
{ deviceId: null },
|
||||
{ where: { deviceId: deviceId }, transaction: t }
|
||||
);
|
||||
|
||||
|
||||
// 5. 删除盘点记录
|
||||
await InventoryRecord.destroy({
|
||||
where: { deviceId: deviceId },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
// 6. 更新机柜功率 (必须在删除设备之前)
|
||||
if (device.rackId) {
|
||||
try {
|
||||
@@ -1847,21 +2009,29 @@ router.delete('/:deviceId', async (req, res) => {
|
||||
throw err; // 重新抛出错误,触发事务回滚
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 7. 删除设备 (Delete Device)
|
||||
await Device.destroy({
|
||||
where: { deviceId: deviceId },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
|
||||
// 提交事务
|
||||
await t.commit();
|
||||
|
||||
|
||||
if (deletedCables > 0) {
|
||||
console.log(`已删除 ${deletedCables} 条相关接线`);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
|
||||
await logDeviceOperation('delete', `删除设备【${deviceName}】(编号:${deviceId},类型:${device.type},关联删除:${deletedCables}条接线、${deletedPorts}个端口、${deletedNetworkCards}张网卡)`, {
|
||||
targetId: deviceId,
|
||||
targetName: deviceName,
|
||||
beforeState,
|
||||
req,
|
||||
metadata: { deletedCables, deletedPorts, deletedNetworkCards, deviceType: device.type }
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
message: '删除成功',
|
||||
deviceId: deviceId,
|
||||
deletedCablesCount: deletedCables,
|
||||
|
||||
@@ -0,0 +1,862 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { Op } = require('sequelize');
|
||||
const Device = require('../models/Device');
|
||||
const Rack = require('../models/Rack');
|
||||
const Room = require('../models/Room');
|
||||
const { logDeviceOperation } = require('../utils/operationLogger');
|
||||
|
||||
async function generateIdleDeviceId() {
|
||||
const devices = await Device.findAll({
|
||||
where: {
|
||||
deviceId: {
|
||||
[Op.like]: 'DEV%'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let maxNumber = 0;
|
||||
devices.forEach(device => {
|
||||
const match = device.deviceId.match(/^DEV(\d+)$/);
|
||||
if (match) {
|
||||
const num = parseInt(match[1], 10);
|
||||
if (num > maxNumber) {
|
||||
maxNumber = num;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const newNumber = maxNumber + 1;
|
||||
return `DEV${String(newNumber).padStart(4, '0')}`;
|
||||
}
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const { keyword, sourceType, idleReason, page = 1, pageSize = 10 } = req.query;
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const where = { isIdle: true };
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ deviceId: { [Op.like]: `%${keyword}%` } },
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ serialNumber: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (sourceType && sourceType !== 'all') {
|
||||
where.sourceType = sourceType;
|
||||
}
|
||||
|
||||
if (idleReason) {
|
||||
where.idleReason = { [Op.like]: `%${idleReason}%` };
|
||||
}
|
||||
|
||||
const { count, rows } = await Device.findAndCountAll({
|
||||
where,
|
||||
include: [
|
||||
{
|
||||
model: Rack,
|
||||
attributes: ['rackId', 'name', 'roomId'],
|
||||
include: [{
|
||||
model: Room,
|
||||
attributes: ['roomId', 'name']
|
||||
}]
|
||||
}
|
||||
],
|
||||
offset: parseInt(offset),
|
||||
limit: parseInt(pageSize),
|
||||
order: [['idleDate', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
total: count,
|
||||
idleDevices: rows,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取空闲设备列表失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:deviceId', async (req, res) => {
|
||||
try {
|
||||
const device = await Device.findOne({
|
||||
where: { deviceId: req.params.deviceId, isIdle: true },
|
||||
include: [
|
||||
{
|
||||
model: Rack,
|
||||
attributes: ['rackId', 'name', 'roomId'],
|
||||
include: [{
|
||||
model: Room,
|
||||
attributes: ['roomId', 'name']
|
||||
}]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
return res.status(404).json({ error: '空闲设备不存在' });
|
||||
}
|
||||
|
||||
res.json(device);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, type, model, serialNumber, powerConsumption, idleReason, warehouseId, description, rackId, position } = req.body;
|
||||
|
||||
let { deviceId } = req.body;
|
||||
|
||||
if (!deviceId || deviceId.trim() === '') {
|
||||
deviceId = await generateIdleDeviceId();
|
||||
}
|
||||
|
||||
const existingDevice = await Device.findByPk(deviceId);
|
||||
if (existingDevice) {
|
||||
return res.status(400).json({ error: '设备ID已存在' });
|
||||
}
|
||||
|
||||
if (rackId) {
|
||||
const rack = await Rack.findByPk(rackId);
|
||||
if (!rack) {
|
||||
return res.status(404).json({ error: '机柜不存在' });
|
||||
}
|
||||
}
|
||||
|
||||
const device = await Device.create({
|
||||
deviceId,
|
||||
name: name || '',
|
||||
type: type || 'other',
|
||||
model: model || '',
|
||||
serialNumber: serialNumber || '',
|
||||
powerConsumption: powerConsumption || 0,
|
||||
status: 'offline',
|
||||
isIdle: true,
|
||||
idleDate: new Date(),
|
||||
idleReason: idleReason || '',
|
||||
warehouseId: warehouseId || null,
|
||||
rackId: rackId || null,
|
||||
position: position || null,
|
||||
sourceType: warehouseId ? 'warehouse' : (rackId ? 'rack' : 'rack'),
|
||||
description: description || ''
|
||||
});
|
||||
|
||||
await logDeviceOperation('create', `新增空闲设备【${device.name || deviceId}】`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
afterState: device.toJSON(),
|
||||
req,
|
||||
metadata: { sourceType: device.sourceType, type: 'idle_device_create' }
|
||||
});
|
||||
|
||||
res.status(201).json(device);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/from-device/:deviceId', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { idleReason } = req.body;
|
||||
const { deviceId } = req.params;
|
||||
|
||||
const device = await Device.findByPk(deviceId, { transaction: t });
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '设备不存在' });
|
||||
}
|
||||
|
||||
if (device.isIdle) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '设备已经标记为空闲设备' });
|
||||
}
|
||||
|
||||
await device.update({
|
||||
isIdle: true,
|
||||
idleDate: new Date(),
|
||||
idleReason: idleReason || `从设备管理转入`,
|
||||
sourceType: 'rack'
|
||||
}, { transaction: t });
|
||||
|
||||
await t.commit();
|
||||
|
||||
await logDeviceOperation('to_idle', `设备【${device.name}】转入空闲设备`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState: { ...device.toJSON(), isIdle: false },
|
||||
afterState: { ...device.toJSON(), isIdle: true },
|
||||
req,
|
||||
metadata: { idleReason, type: 'device_to_idle' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: '设备已转入空闲设备',
|
||||
device: device.toJSON()
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('设备转入空闲设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/batch-from-devices', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { deviceIds, idleReason } = req.body;
|
||||
|
||||
if (!deviceIds || !Array.isArray(deviceIds) || deviceIds.length === 0) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '请提供有效的设备ID列表' });
|
||||
}
|
||||
|
||||
const devices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
const notIdleDevices = devices.filter(d => !d.isIdle);
|
||||
const alreadyIdleDevices = devices.filter(d => d.isIdle);
|
||||
|
||||
if (notIdleDevices.length > 0) {
|
||||
await Device.update(
|
||||
{
|
||||
isIdle: true,
|
||||
idleDate: new Date(),
|
||||
idleReason: idleReason || `批量转入`
|
||||
},
|
||||
{
|
||||
where: { deviceId: { [Op.in]: notIdleDevices.map(d => d.deviceId) } },
|
||||
transaction: t
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
await t.commit();
|
||||
|
||||
await logDeviceOperation('batch_to_idle', `批量将 ${notIdleDevices.length} 台设备转入空闲设备`, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${notIdleDevices.length}台设备`,
|
||||
req,
|
||||
metadata: { idleReason, type: 'batch_device_to_idle' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `成功将 ${notIdleDevices.length} 台设备转入空闲设备`,
|
||||
total: devices.length,
|
||||
updated: notIdleDevices.length,
|
||||
skipped: alreadyIdleDevices.length
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('批量转入空闲设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/batch-restore', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { devices } = req.body;
|
||||
|
||||
console.log('========== batch-restore 开始 ==========');
|
||||
console.log('原始请求 body:', JSON.stringify(req.body));
|
||||
console.log('devices 参数:', devices);
|
||||
|
||||
if (!devices || !Array.isArray(devices) || devices.length === 0) {
|
||||
await t.rollback();
|
||||
console.log('错误: devices 参数无效');
|
||||
return res.status(400).json({ error: '请提供有效的设备列表' });
|
||||
}
|
||||
|
||||
const deviceIds = devices.map(d => d.deviceId).filter(Boolean);
|
||||
console.log('提取的 deviceIds:', deviceIds);
|
||||
|
||||
if (deviceIds.length === 0) {
|
||||
await t.rollback();
|
||||
console.log('错误: deviceIds 为空');
|
||||
return res.status(400).json({ error: '设备ID不能为空' });
|
||||
}
|
||||
|
||||
console.log('开始查询设备,条件:', { deviceId: { [Op.in]: deviceIds }, isIdle: true });
|
||||
|
||||
const idleDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds }, isIdle: true },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
console.log('查询到的空闲设备数量:', idleDevices.length);
|
||||
if (idleDevices.length > 0) {
|
||||
console.log('查询到的设备ID:', idleDevices.map(d => d.deviceId));
|
||||
}
|
||||
|
||||
if (idleDevices.length === 0) {
|
||||
console.log('没有找到空闲设备,检查设备是否存在:');
|
||||
const allDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
transaction: t
|
||||
});
|
||||
console.log('设备表中存在的设备数量:', allDevices.length);
|
||||
if (allDevices.length > 0) {
|
||||
console.log('存在的设备及其 isIdle 状态:', allDevices.map(d => ({ deviceId: d.deviceId, isIdle: d.isIdle })));
|
||||
}
|
||||
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '没有找到空闲设备' });
|
||||
}
|
||||
|
||||
let restoredCount = 0;
|
||||
const results = [];
|
||||
|
||||
for (const device of idleDevices) {
|
||||
const deviceConfig = devices.find(d => d.deviceId === device.deviceId);
|
||||
if (!deviceConfig) continue;
|
||||
|
||||
const targetRackId = deviceConfig.targetRackId;
|
||||
const targetPosition = deviceConfig.targetPosition;
|
||||
|
||||
if (!targetRackId) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'skipped',
|
||||
reason: '未指定目标机柜'
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const targetRack = await Rack.findByPk(targetRackId, { transaction: t });
|
||||
if (!targetRack) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'failed',
|
||||
reason: '目标机柜不存在'
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const height = device.height || 1;
|
||||
const position = targetPosition || 1;
|
||||
|
||||
const checkResult = await checkPositionAvailable(targetRackId, position, height, null, t);
|
||||
if (!checkResult.available) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'failed',
|
||||
reason: `U位${position}已被占用`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
await device.update({
|
||||
isIdle: false,
|
||||
idleDate: null,
|
||||
idleReason: null,
|
||||
rackId: targetRackId,
|
||||
position: position,
|
||||
warehouseId: null,
|
||||
sourceType: 'rack',
|
||||
status: 'offline'
|
||||
}, { transaction: t });
|
||||
|
||||
await targetRack.update({
|
||||
currentPower: targetRack.currentPower + (device.powerConsumption || 0)
|
||||
}, { transaction: t });
|
||||
|
||||
restoredCount++;
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'success',
|
||||
targetRack: targetRack.name,
|
||||
targetPosition: position
|
||||
});
|
||||
}
|
||||
|
||||
await t.commit();
|
||||
|
||||
const successCount = results.filter(r => r.status === 'success').length;
|
||||
const failedCount = results.filter(r => r.status === 'failed').length;
|
||||
const skippedCount = results.filter(r => r.status === 'skipped').length;
|
||||
|
||||
await logDeviceOperation('batch_restore', `批量上架 ${successCount} 台空闲设备`, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${successCount}台设备`,
|
||||
req,
|
||||
metadata: { results, type: 'batch_idle_device_restore' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `成功上架 ${successCount} 台设备`,
|
||||
total: idleDevices.length,
|
||||
restored: successCount,
|
||||
failed: failedCount,
|
||||
skipped: skippedCount,
|
||||
details: results
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('批量上架设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:deviceId/shelve', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { deviceId } = req.params;
|
||||
const { name, type, model, serialNumber, height, powerConsumption, rackId, position, description } = req.body;
|
||||
|
||||
const device = await Device.findOne({
|
||||
where: { deviceId, isIdle: true },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '空闲设备不存在' });
|
||||
}
|
||||
|
||||
if (!rackId) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '请选择目标机柜' });
|
||||
}
|
||||
|
||||
const targetRack = await Rack.findByPk(rackId, { transaction: t });
|
||||
if (!targetRack) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '目标机柜不存在' });
|
||||
}
|
||||
|
||||
const deviceHeight = height || device.height || 1;
|
||||
const positionCheck = await checkPositionAvailable(rackId, position, deviceHeight, deviceId, t);
|
||||
if (!positionCheck.available) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: positionCheck.reason });
|
||||
}
|
||||
|
||||
const beforeState = device.toJSON();
|
||||
|
||||
await device.update({
|
||||
name: name || device.name,
|
||||
type: type || device.type,
|
||||
model: model || device.model,
|
||||
serialNumber: serialNumber || device.serialNumber,
|
||||
height: deviceHeight,
|
||||
powerConsumption: powerConsumption || device.powerConsumption || 0,
|
||||
rackId: rackId,
|
||||
position: position,
|
||||
description: description || device.description,
|
||||
isIdle: false,
|
||||
idleDate: null,
|
||||
idleReason: null,
|
||||
warehouseId: null,
|
||||
sourceType: 'rack',
|
||||
status: 'running'
|
||||
}, { transaction: t });
|
||||
|
||||
await targetRack.update({
|
||||
currentPower: targetRack.currentPower + (powerConsumption || device.powerConsumption || 0)
|
||||
}, { transaction: t });
|
||||
|
||||
await t.commit();
|
||||
|
||||
const updatedDevice = await Device.findByPk(deviceId, {
|
||||
include: [
|
||||
{ model: Rack, include: [Room] }
|
||||
]
|
||||
});
|
||||
|
||||
await logDeviceOperation('shelve', `空闲设备【${device.name}】上架到机柜【${targetRack.name}】U${position}`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState: { ...beforeState, isIdle: true },
|
||||
afterState: updatedDevice.toJSON(),
|
||||
req,
|
||||
metadata: { rackId, position, type: 'idle_device_shelve' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: '设备上架成功',
|
||||
device: updatedDevice
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('设备上架失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:deviceId', async (req, res) => {
|
||||
try {
|
||||
const device = await Device.findOne({
|
||||
where: { deviceId: req.params.deviceId, isIdle: true }
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
return res.status(404).json({ error: '空闲设备不存在' });
|
||||
}
|
||||
|
||||
const beforeState = device.toJSON();
|
||||
const allowedFields = ['name', 'type', 'model', 'idleReason', 'description', 'powerConsumption'];
|
||||
|
||||
allowedFields.forEach(field => {
|
||||
if (req.body[field] !== undefined) {
|
||||
device[field] = req.body[field];
|
||||
}
|
||||
});
|
||||
|
||||
if (req.body.warehouseId !== undefined) {
|
||||
device.warehouseId = req.body.warehouseId || null;
|
||||
device.sourceType = req.body.warehouseId ? 'warehouse' : 'rack';
|
||||
}
|
||||
|
||||
if (req.body.rackId !== undefined) {
|
||||
if (req.body.rackId) {
|
||||
const rack = await Rack.findByPk(req.body.rackId);
|
||||
if (!rack) {
|
||||
return res.status(404).json({ error: '机柜不存在' });
|
||||
}
|
||||
}
|
||||
device.rackId = req.body.rackId || null;
|
||||
if (!req.body.warehouseId) {
|
||||
device.sourceType = 'rack';
|
||||
}
|
||||
}
|
||||
|
||||
if (req.body.position !== undefined) {
|
||||
device.position = req.body.position || null;
|
||||
}
|
||||
|
||||
await device.save();
|
||||
|
||||
await logDeviceOperation('update', `更新空闲设备【${device.name}】`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState,
|
||||
afterState: device.toJSON(),
|
||||
req,
|
||||
metadata: { type: 'idle_device_update' }
|
||||
});
|
||||
|
||||
res.json(device);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:deviceId/restore', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { targetRackId, targetPosition } = req.body;
|
||||
const { deviceId } = req.params;
|
||||
|
||||
const device = await Device.findOne({
|
||||
where: { deviceId, isIdle: true },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '空闲设备不存在' });
|
||||
}
|
||||
|
||||
if (!targetRackId || !targetPosition) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: '恢复设备需要指定目标机柜和位置' });
|
||||
}
|
||||
|
||||
const targetRack = await Rack.findByPk(targetRackId, { transaction: t });
|
||||
if (!targetRack) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '目标机柜不存在' });
|
||||
}
|
||||
|
||||
const positionCheck = await checkPositionAvailable(targetRackId, targetPosition, device.height || 1, deviceId, t);
|
||||
if (!positionCheck.available) {
|
||||
await t.rollback();
|
||||
return res.status(400).json({ error: positionCheck.reason });
|
||||
}
|
||||
|
||||
await device.update({
|
||||
isIdle: false,
|
||||
idleDate: null,
|
||||
idleReason: null,
|
||||
rackId: targetRackId,
|
||||
position: targetPosition,
|
||||
warehouseId: null,
|
||||
sourceType: 'rack',
|
||||
status: 'offline'
|
||||
}, { transaction: t });
|
||||
|
||||
await targetRack.update({
|
||||
currentPower: targetRack.currentPower + (device.powerConsumption || 0)
|
||||
}, { transaction: t });
|
||||
|
||||
await t.commit();
|
||||
|
||||
const updatedDevice = await Device.findByPk(deviceId, {
|
||||
include: [
|
||||
{ model: Rack, include: [Room] }
|
||||
]
|
||||
});
|
||||
|
||||
await logDeviceOperation('restore', `空闲设备【${device.name}】恢复到机柜【${targetRack.name}】U${targetPosition}`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState: { ...device.toJSON(), isIdle: true },
|
||||
afterState: updatedDevice.toJSON(),
|
||||
req,
|
||||
metadata: { targetRackId, targetPosition, type: 'idle_device_restore' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: '设备已恢复到设备管理',
|
||||
device: updatedDevice
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('恢复设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/batch-restore', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const { devices } = req.body;
|
||||
|
||||
console.log('========== batch-restore 开始 ==========');
|
||||
console.log('原始请求 body:', JSON.stringify(req.body));
|
||||
console.log('devices 参数:', devices);
|
||||
|
||||
if (!devices || !Array.isArray(devices) || devices.length === 0) {
|
||||
await t.rollback();
|
||||
console.log('错误: devices 参数无效');
|
||||
return res.status(400).json({ error: '请提供有效的设备列表' });
|
||||
}
|
||||
|
||||
const deviceIds = devices.map(d => d.deviceId).filter(Boolean);
|
||||
console.log('提取的 deviceIds:', deviceIds);
|
||||
console.log('deviceIds 类型:', typeof deviceIds, Array.isArray(deviceIds));
|
||||
|
||||
if (deviceIds.length === 0) {
|
||||
await t.rollback();
|
||||
console.log('错误: deviceIds 为空');
|
||||
return res.status(400).json({ error: '设备ID不能为空' });
|
||||
}
|
||||
|
||||
console.log('开始查询设备,条件:', { deviceId: { [Op.in]: deviceIds }, isIdle: true });
|
||||
|
||||
const idleDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds }, isIdle: true },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
console.log('查询到的空闲设备数量:', idleDevices.length);
|
||||
if (idleDevices.length > 0) {
|
||||
console.log('查询到的设备ID:', idleDevices.map(d => d.deviceId));
|
||||
}
|
||||
|
||||
if (idleDevices.length === 0) {
|
||||
console.log('没有找到空闲设备,检查设备是否存在:');
|
||||
const allDevices = await Device.findAll({
|
||||
where: { deviceId: { [Op.in]: deviceIds } },
|
||||
transaction: t
|
||||
});
|
||||
console.log('设备表中存在的设备数量:', allDevices.length);
|
||||
if (allDevices.length > 0) {
|
||||
console.log('存在的设备及其 isIdle 状态:', allDevices.map(d => ({ deviceId: d.deviceId, isIdle: d.isIdle })));
|
||||
}
|
||||
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '没有找到空闲设备' });
|
||||
}
|
||||
|
||||
let restoredCount = 0;
|
||||
const results = [];
|
||||
|
||||
for (const device of idleDevices) {
|
||||
const deviceConfig = devices.find(d => d.deviceId === device.deviceId);
|
||||
if (!deviceConfig) continue;
|
||||
|
||||
const targetRackId = deviceConfig.targetRackId;
|
||||
const targetPosition = deviceConfig.targetPosition;
|
||||
|
||||
if (!targetRackId) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'skipped',
|
||||
reason: '未指定目标机柜'
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const targetRack = await Rack.findByPk(targetRackId, { transaction: t });
|
||||
if (!targetRack) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'failed',
|
||||
reason: '目标机柜不存在'
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const height = device.height || 1;
|
||||
const position = targetPosition || 1;
|
||||
|
||||
const checkResult = await checkPositionAvailable(targetRackId, position, height, null, t);
|
||||
if (!checkResult.available) {
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'failed',
|
||||
reason: `U位${position}已被占用`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
await device.update({
|
||||
isIdle: false,
|
||||
idleDate: null,
|
||||
idleReason: null,
|
||||
rackId: targetRackId,
|
||||
position: position,
|
||||
warehouseId: null,
|
||||
sourceType: 'rack',
|
||||
status: 'offline'
|
||||
}, { transaction: t });
|
||||
|
||||
await targetRack.update({
|
||||
currentPower: targetRack.currentPower + (device.powerConsumption || 0)
|
||||
}, { transaction: t });
|
||||
|
||||
restoredCount++;
|
||||
results.push({
|
||||
deviceId: device.deviceId,
|
||||
name: device.name,
|
||||
status: 'success',
|
||||
targetRack: targetRack.name,
|
||||
targetPosition: position
|
||||
});
|
||||
}
|
||||
|
||||
await t.commit();
|
||||
|
||||
const successCount = results.filter(r => r.status === 'success').length;
|
||||
const failedCount = results.filter(r => r.status === 'failed').length;
|
||||
const skippedCount = results.filter(r => r.status === 'skipped').length;
|
||||
|
||||
await logDeviceOperation('batch_restore', `批量上架 ${successCount} 台空闲设备`, {
|
||||
targetId: deviceIds.join(','),
|
||||
targetName: `${successCount}台设备`,
|
||||
req,
|
||||
metadata: { results, type: 'batch_idle_device_restore' }
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `成功上架 ${successCount} 台设备`,
|
||||
total: idleDevices.length,
|
||||
restored: successCount,
|
||||
failed: failedCount,
|
||||
skipped: skippedCount,
|
||||
details: results
|
||||
});
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('批量上架设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:deviceId', async (req, res) => {
|
||||
const t = await require('../db').sequelize.transaction();
|
||||
try {
|
||||
const device = await Device.findOne({
|
||||
where: { deviceId: req.params.deviceId, isIdle: true },
|
||||
transaction: t
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
await t.rollback();
|
||||
return res.status(404).json({ error: '空闲设备不存在' });
|
||||
}
|
||||
|
||||
const beforeState = device.toJSON();
|
||||
|
||||
await device.destroy({ transaction: t });
|
||||
|
||||
await t.commit();
|
||||
|
||||
await logDeviceOperation('delete', `删除空闲设备【${device.name || device.deviceId}】`, {
|
||||
targetId: device.deviceId,
|
||||
targetName: device.name,
|
||||
beforeState,
|
||||
req,
|
||||
metadata: { type: 'idle_device_delete' }
|
||||
});
|
||||
|
||||
res.json({ message: '空闲设备删除成功' });
|
||||
} catch (error) {
|
||||
await t.rollback();
|
||||
console.error('删除空闲设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
async function checkPositionAvailable(rackId, position, height, excludeDeviceId = null, transaction = null) {
|
||||
if (!position || position <= 0) {
|
||||
return { available: true, reason: null };
|
||||
}
|
||||
|
||||
const deviceHeight = height || 1;
|
||||
const startU = position;
|
||||
const endU = position + deviceHeight - 1;
|
||||
|
||||
const queryOptions = {
|
||||
where: {
|
||||
rackId: rackId,
|
||||
position: { [Op.ne]: null },
|
||||
isIdle: false
|
||||
},
|
||||
attributes: ['deviceId', 'position', 'height']
|
||||
};
|
||||
|
||||
if (transaction) {
|
||||
queryOptions.transaction = transaction;
|
||||
}
|
||||
|
||||
const existingDevices = await Device.findAll(queryOptions);
|
||||
|
||||
for (const d of existingDevices) {
|
||||
if (excludeDeviceId && d.deviceId === excludeDeviceId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const existStart = d.position;
|
||||
const existEnd = d.position + (d.height || 1) - 1;
|
||||
|
||||
if (!(endU < existStart || startU > existEnd)) {
|
||||
return {
|
||||
available: false,
|
||||
reason: `U位冲突:机柜中已有设备 ${d.deviceId} 占用 U${existStart}${existEnd !== existStart ? '-' + existEnd : ''}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { available: true, reason: null };
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,263 @@
|
||||
const express = require('express');
|
||||
const { Op } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
const { authMiddleware } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
module,
|
||||
operationType,
|
||||
targetId,
|
||||
operatorId,
|
||||
keyword,
|
||||
startDate,
|
||||
endDate,
|
||||
result
|
||||
} = req.query;
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const limit = Math.min(parseInt(pageSize), 100);
|
||||
|
||||
const where = {};
|
||||
|
||||
if (module && module !== 'all') {
|
||||
where.module = module;
|
||||
}
|
||||
|
||||
if (operationType && operationType !== 'all') {
|
||||
where.operationType = operationType;
|
||||
}
|
||||
|
||||
if (targetId) {
|
||||
where.targetId = { [Op.like]: `%${targetId}%` };
|
||||
}
|
||||
|
||||
if (operatorId) {
|
||||
where.operatorId = operatorId;
|
||||
}
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ operationDescription: { [Op.like]: `%${keyword}%` } },
|
||||
{ targetName: { [Op.like]: `%${keyword}%` } },
|
||||
{ operatorName: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (result && result !== 'all') {
|
||||
where.result = result;
|
||||
}
|
||||
|
||||
if (startDate || endDate) {
|
||||
where.createdAt = {};
|
||||
if (startDate) {
|
||||
where.createdAt[Op.gte] = new Date(startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
const endOfDay = new Date(endDate);
|
||||
endOfDay.setHours(23, 59, 59, 999);
|
||||
where.createdAt[Op.lte] = endOfDay;
|
||||
}
|
||||
}
|
||||
|
||||
const { count, rows: logs } = await OperationLog.findAndCountAll({
|
||||
where,
|
||||
order: [['createdAt', 'DESC']],
|
||||
offset,
|
||||
limit
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
logs
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/modules', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const modules = await OperationLog.findAll({
|
||||
attributes: ['module'],
|
||||
group: ['module']
|
||||
});
|
||||
|
||||
const moduleList = modules.map(m => ({
|
||||
value: m.module,
|
||||
label: getModuleName(m.module)
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: moduleList
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取模块列表失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取模块列表失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/types', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { module } = req.query;
|
||||
|
||||
const where = {};
|
||||
if (module && module !== 'all') {
|
||||
where.module = module;
|
||||
}
|
||||
|
||||
const types = await OperationLog.findAll({
|
||||
where,
|
||||
attributes: ['operationType'],
|
||||
group: ['operationType']
|
||||
});
|
||||
|
||||
const typeList = types.map(t => ({
|
||||
value: t.operationType,
|
||||
label: getOperationTypeName(t.operationType)
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: typeList
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作类型列表失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作类型列表失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/statistics', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { startDate, endDate } = req.query;
|
||||
|
||||
const where = {};
|
||||
if (startDate || endDate) {
|
||||
where.createdAt = {};
|
||||
if (startDate) {
|
||||
where.createdAt[Op.gte] = new Date(startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
const endOfDay = new Date(endDate);
|
||||
endOfDay.setHours(23, 59, 59, 999);
|
||||
where.createdAt[Op.lte] = endOfDay;
|
||||
}
|
||||
}
|
||||
|
||||
const [moduleStats, typeStats, dailyStats] = await Promise.all([
|
||||
OperationLog.findAll({
|
||||
where,
|
||||
attributes: ['module', [sequelize.fn('COUNT', sequelize.col('module')), 'count']],
|
||||
group: ['module']
|
||||
}),
|
||||
OperationLog.findAll({
|
||||
where,
|
||||
attributes: ['operationType', [sequelize.fn('COUNT', sequelize.col('operationType')), 'count']],
|
||||
group: ['operationType']
|
||||
}),
|
||||
OperationLog.findAll({
|
||||
where,
|
||||
attributes: [
|
||||
[sequelize.fn('DATE', sequelize.col('createdAt')), 'date'],
|
||||
[sequelize.fn('COUNT', '*'), 'count']
|
||||
],
|
||||
group: [sequelize.fn('DATE', sequelize.col('createdAt'))],
|
||||
order: [[sequelize.fn('DATE', sequelize.col('createdAt')), 'DESC']],
|
||||
limit: 30
|
||||
})
|
||||
]);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
byModule: moduleStats.map(s => ({ module: s.module, count: s.get('count') })),
|
||||
byType: typeStats.map(s => ({ type: s.operationType, count: s.get('count') })),
|
||||
byDay: dailyStats.map(s => ({ date: s.get('date'), count: s.get('count') }))
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志统计失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志统计失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:recordId', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const log = await OperationLog.findByPk(req.params.recordId);
|
||||
|
||||
if (!log) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '日志记录不存在'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: log
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志详情失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志详情失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function getModuleName(module) {
|
||||
const moduleNames = {
|
||||
device: '设备管理',
|
||||
user: '用户管理',
|
||||
role: '角色管理',
|
||||
consumable: '耗材管理',
|
||||
rack: '机柜管理',
|
||||
room: '机房管理',
|
||||
ticket: '工单管理',
|
||||
backup: '备份管理'
|
||||
};
|
||||
return moduleNames[module] || module;
|
||||
}
|
||||
|
||||
function getOperationTypeName(type) {
|
||||
const typeNames = {
|
||||
create: '创建',
|
||||
update: '更新',
|
||||
delete: '删除',
|
||||
batch_delete: '批量删除',
|
||||
batch_update: '批量更新',
|
||||
status_change: '状态变更',
|
||||
move: '移动',
|
||||
permission_change: '权限变更',
|
||||
import: '导入',
|
||||
export: '导出'
|
||||
};
|
||||
return typeNames[type] || type;
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
@@ -4,6 +4,7 @@ const Permission = require('../models/Permission');
|
||||
const UserRole = require('../models/UserRole');
|
||||
const User = require('../models/User');
|
||||
const { authMiddleware } = require('../middleware/auth');
|
||||
const { logRoleOperation } = require('../utils/operationLogger');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -134,6 +135,18 @@ router.post('/', authMiddleware, async (req, res) => {
|
||||
sort: sort || 0
|
||||
});
|
||||
|
||||
const permissionNames = permissions && permissions.length > 0
|
||||
? permissions.join('、')
|
||||
: '无';
|
||||
|
||||
await logRoleOperation('create', `创建角色【${roleName}】(编码:${roleCode},权限:${permissionNames})`, {
|
||||
targetId: role.roleId,
|
||||
targetName: roleName,
|
||||
afterState: role.toJSON(),
|
||||
req,
|
||||
metadata: { roleCode, permissions, permissionNames }
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: '创建成功',
|
||||
@@ -160,6 +173,8 @@ router.put('/:roleId', authMiddleware, async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const beforeState = role.toJSON();
|
||||
|
||||
if (roleName !== undefined) role.roleName = roleName;
|
||||
if (description !== undefined) role.description = description;
|
||||
if (permissions !== undefined) role.permissions = permissions;
|
||||
@@ -168,6 +183,53 @@ router.put('/:roleId', authMiddleware, async (req, res) => {
|
||||
|
||||
await role.save();
|
||||
|
||||
const afterState = role.toJSON();
|
||||
const changedFields = {};
|
||||
|
||||
if (roleName !== undefined && beforeState.roleName !== roleName) {
|
||||
changedFields.roleName = { from: beforeState.roleName, to: roleName };
|
||||
}
|
||||
if (description !== undefined && beforeState.description !== description) {
|
||||
changedFields.description = { from: beforeState.description, to: description };
|
||||
}
|
||||
if (permissions !== undefined) {
|
||||
const oldPerms = (beforeState.permissions || []).sort().join(',');
|
||||
const newPerms = (permissions || []).sort().join(',');
|
||||
if (oldPerms !== newPerms) {
|
||||
changedFields.permissions = { from: beforeState.permissions, to: permissions };
|
||||
}
|
||||
}
|
||||
if (status !== undefined && beforeState.status !== status) {
|
||||
const statusText = { active: '启用', inactive: '禁用' };
|
||||
changedFields.status = { from: beforeState.status, to: status, fromText: statusText[beforeState.status], toText: statusText[status] };
|
||||
}
|
||||
|
||||
const changeDetails = Object.entries(changedFields).map(([field, values]) => {
|
||||
const fieldNames = { roleName: '角色名称', description: '描述', permissions: '权限', status: '状态' };
|
||||
const displayName = fieldNames[field] || field;
|
||||
|
||||
if (field === 'permissions') {
|
||||
return `权限: ${(values.from || []).join('、') || '无'} → ${(values.to || []).join('、') || '无'}`;
|
||||
}
|
||||
if (field === 'status') {
|
||||
return `状态: ${values.fromText} → ${values.toText}`;
|
||||
}
|
||||
return `${displayName}: ${values.from ?? '空'} → ${values.to ?? '空'}`;
|
||||
}).join(';');
|
||||
|
||||
const updateDesc = changeDetails
|
||||
? `更新角色【${role.roleName}】:${changeDetails}`
|
||||
: `更新角色【${role.roleName}】`;
|
||||
|
||||
await logRoleOperation('update', updateDesc, {
|
||||
targetId: role.roleId,
|
||||
targetName: role.roleName,
|
||||
beforeState,
|
||||
afterState,
|
||||
req,
|
||||
metadata: { changedFields, oldRoleName: beforeState.roleName, oldPermissions: beforeState.permissions }
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '更新成功',
|
||||
@@ -208,8 +270,20 @@ router.delete('/:roleId', authMiddleware, async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const roleName = role.roleName;
|
||||
const roleCode = role.roleCode;
|
||||
const beforeState = role.toJSON();
|
||||
|
||||
await role.destroy();
|
||||
|
||||
await logRoleOperation('delete', `删除角色【${roleName}】(编码:${roleCode},权限:${(role.permissions || []).join('、') || '无'})`, {
|
||||
targetId: req.params.roleId,
|
||||
targetName: roleName,
|
||||
beforeState,
|
||||
req,
|
||||
metadata: { roleCode, userCount, permissions: role.permissions }
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除成功'
|
||||
|
||||
+13
-3
@@ -8,10 +8,20 @@ const { createRoomSchema, updateRoomSchema } = require('../validation/roomSchema
|
||||
// 获取所有机房
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const rooms = await Room.findAll({
|
||||
include: Rack
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const pageSize = parseInt(req.query.pageSize) || 100;
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const { count, rows } = await Room.findAndCountAll({
|
||||
include: [{ model: Rack, attributes: ['rackId', 'name'] }],
|
||||
offset: offset,
|
||||
limit: pageSize
|
||||
});
|
||||
|
||||
res.json({
|
||||
rooms: rows,
|
||||
total: count
|
||||
});
|
||||
res.json(rooms);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
|
||||
+123
-7
@@ -7,6 +7,7 @@ const Role = require('../models/Role');
|
||||
const UserRole = require('../models/UserRole');
|
||||
const { authMiddleware } = require('../middleware/auth');
|
||||
const { SALT_ROUNDS, PASSWORD_MIN_LENGTH, FILE_UPLOAD, PAGINATION } = require('../config');
|
||||
const { logUserOperation } = require('../utils/operationLogger');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -16,22 +17,30 @@ const generateId = () => {
|
||||
|
||||
const getWhereClause = (query) => {
|
||||
const where = {};
|
||||
|
||||
|
||||
if (query.username) {
|
||||
where.username = { [Op.like]: `%${query.username}%` };
|
||||
}
|
||||
|
||||
|
||||
if (query.status) {
|
||||
where.status = query.status;
|
||||
}
|
||||
|
||||
|
||||
if (query.realName) {
|
||||
where.realName = { [Op.like]: `%${query.realName}%` };
|
||||
}
|
||||
|
||||
|
||||
return where;
|
||||
};
|
||||
|
||||
const getUserRoleIds = async (userId) => {
|
||||
const userRoles = await UserRole.findAll({
|
||||
where: { UserId: userId },
|
||||
attributes: ['RoleId']
|
||||
});
|
||||
return userRoles.map(ur => ur.RoleId);
|
||||
};
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
router.get('/', authMiddleware, async (req, res) => {
|
||||
@@ -204,6 +213,23 @@ router.post('/', authMiddleware, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const roleNames = roleIds && roleIds.length > 0
|
||||
? (await Role.findAll({ where: { roleId: { [Op.in]: roleIds } } })).map(r => r.roleName).join('、')
|
||||
: '未分配角色';
|
||||
|
||||
await logUserOperation('create', `创建用户【${username}】(姓名:${realName || '未填写'},邮箱:${email || '未填写'},角色:${roleNames})`, {
|
||||
targetId: user.userId,
|
||||
targetName: username,
|
||||
afterState: {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
realName: user.realName,
|
||||
status: user.status
|
||||
},
|
||||
req,
|
||||
metadata: { roleIds, roleNames }
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: '创建成功',
|
||||
@@ -236,9 +262,20 @@ router.put('/:userId', authMiddleware, async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const beforeState = {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
phone: user.phone,
|
||||
realName: user.realName,
|
||||
status: user.status,
|
||||
remark: user.remark
|
||||
};
|
||||
|
||||
const oldRoleIds = roleIds !== undefined ? null : await getUserRoleIds(user.userId);
|
||||
|
||||
if (username !== undefined && username !== user.username) {
|
||||
const existingUser = await User.findOne({
|
||||
where: { username, userId: { [Op.ne]: user.userId } }
|
||||
const existingUser = await User.findOne({
|
||||
where: { username, userId: { [Op.ne]: user.userId } }
|
||||
});
|
||||
if (existingUser) {
|
||||
return res.status(400).json({
|
||||
@@ -261,21 +298,82 @@ router.put('/:userId', authMiddleware, async (req, res) => {
|
||||
|
||||
await user.save();
|
||||
|
||||
let permissionChanged = false;
|
||||
let oldRoleNames = [];
|
||||
let newRoleNames = [];
|
||||
|
||||
if (roleIds !== undefined) {
|
||||
const oldRoles = await Role.findAll({ where: { roleId: { [Op.in]: oldRoleIds || [] } } });
|
||||
oldRoleNames = oldRoles.map(r => r.roleName);
|
||||
|
||||
await UserRole.destroy({ where: { UserId: user.userId } });
|
||||
|
||||
|
||||
for (const roleId of roleIds) {
|
||||
await UserRole.create({
|
||||
UserId: user.userId,
|
||||
RoleId: roleId
|
||||
});
|
||||
}
|
||||
|
||||
const newRoles = await Role.findAll({ where: { roleId: { [Op.in]: roleIds } } });
|
||||
newRoleNames = newRoles.map(r => r.roleName);
|
||||
permissionChanged = true;
|
||||
}
|
||||
|
||||
const updatedUser = await User.findByPk(req.params.userId, {
|
||||
attributes: { exclude: ['password'] }
|
||||
});
|
||||
|
||||
if (permissionChanged) {
|
||||
const roleChangeDesc = `变更用户【${updatedUser.username}】的角色:${oldRoleNames.join('、') || '无'} → ${newRoleNames.join('、') || '无'}`;
|
||||
await logUserOperation('permission_change', roleChangeDesc, {
|
||||
targetId: updatedUser.userId,
|
||||
targetName: updatedUser.username,
|
||||
beforeState: { ...beforeState, roleIds: oldRoleIds, roleNames: oldRoleNames },
|
||||
afterState: { ...beforeState, roleIds, roleNames: newRoleNames },
|
||||
req,
|
||||
metadata: { oldRoleIds, newRoleIds: roleIds, oldRoleNames, newRoleNames }
|
||||
});
|
||||
} else {
|
||||
const afterState = {
|
||||
username: updatedUser.username,
|
||||
email: updatedUser.email,
|
||||
phone: updatedUser.phone,
|
||||
realName: updatedUser.realName,
|
||||
status: updatedUser.status,
|
||||
remark: updatedUser.remark
|
||||
};
|
||||
|
||||
const changedFields = {};
|
||||
for (const key of Object.keys(beforeState)) {
|
||||
if (JSON.stringify(beforeState[key]) !== JSON.stringify(afterState[key])) {
|
||||
changedFields[key] = { from: beforeState[key], to: afterState[key] };
|
||||
}
|
||||
}
|
||||
|
||||
const changeDetails = Object.entries(changedFields).map(([field, values]) => {
|
||||
const fieldNames = {
|
||||
username: '用户名', email: '邮箱', phone: '电话', realName: '姓名',
|
||||
status: '状态', remark: '备注'
|
||||
};
|
||||
const displayName = fieldNames[field] || field;
|
||||
return `${displayName}: ${values.from ?? '空'} → ${values.to ?? '空'}`;
|
||||
}).join(';');
|
||||
|
||||
const updateDesc = changeDetails
|
||||
? `更新用户【${updatedUser.username}】:${changeDetails}`
|
||||
: `更新用户【${updatedUser.username}】`;
|
||||
|
||||
await logUserOperation('update', updateDesc, {
|
||||
targetId: updatedUser.userId,
|
||||
targetName: updatedUser.username,
|
||||
beforeState,
|
||||
afterState,
|
||||
req,
|
||||
metadata: { changedFields }
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '更新成功',
|
||||
@@ -343,9 +441,27 @@ router.delete('/:userId', authMiddleware, async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const userName = user.username;
|
||||
const userRealName = user.realName;
|
||||
const userEmail = user.email;
|
||||
const beforeState = {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
realName: user.realName,
|
||||
status: user.status
|
||||
};
|
||||
|
||||
await UserRole.destroy({ where: { UserId: user.userId } });
|
||||
await user.destroy();
|
||||
|
||||
await logUserOperation('delete', `删除用户【${userName}】(姓名:${userRealName || '未填写'},邮箱:${userEmail || '未填写'})`, {
|
||||
targetId: req.params.userId,
|
||||
targetName: userName,
|
||||
beforeState,
|
||||
req,
|
||||
metadata: { deletedUsername: userName, realName: userRealName, email: userEmail }
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除成功'
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { Op } = require('sequelize');
|
||||
const Warehouse = require('../models/Warehouse');
|
||||
const Device = require('../models/Device');
|
||||
const { logDeviceOperation } = require('../utils/operationLogger');
|
||||
|
||||
async function generateWarehouseId() {
|
||||
const warehouses = await Warehouse.findAll({
|
||||
where: {
|
||||
warehouseId: { [Op.like]: 'WH%' }
|
||||
}
|
||||
});
|
||||
|
||||
let maxNumber = 0;
|
||||
warehouses.forEach(wh => {
|
||||
const match = wh.warehouseId.match(/^WH(\d+)$/);
|
||||
if (match) {
|
||||
const num = parseInt(match[1], 10);
|
||||
if (num > maxNumber) {
|
||||
maxNumber = num;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const newNumber = maxNumber + 1;
|
||||
return `WH${String(newNumber).padStart(3, '0')}`;
|
||||
}
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const { keyword, status, page = 1, pageSize = 10 } = req.query;
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const where = {};
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ warehouseId: { [Op.like]: `%${keyword}%` } },
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ location: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (status && status !== 'all') {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
const { count, rows } = await Warehouse.findAndCountAll({
|
||||
where,
|
||||
offset: parseInt(offset),
|
||||
limit: parseInt(pageSize),
|
||||
order: [['createdAt', 'DESC']]
|
||||
});
|
||||
|
||||
const warehousesWithCount = await Promise.all(
|
||||
rows.map(async (warehouse) => {
|
||||
const deviceCount = await Device.count({
|
||||
where: { warehouseId: warehouse.warehouseId, isIdle: true }
|
||||
});
|
||||
return {
|
||||
...warehouse.toJSON(),
|
||||
deviceCount
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
res.json({
|
||||
total: count,
|
||||
warehouses: warehousesWithCount,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取库房列表失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:warehouseId', async (req, res) => {
|
||||
try {
|
||||
const warehouse = await Warehouse.findByPk(req.params.warehouseId);
|
||||
if (!warehouse) {
|
||||
return res.status(404).json({ error: '库房不存在' });
|
||||
}
|
||||
|
||||
const deviceCount = await Device.count({
|
||||
where: { warehouseId: warehouse.warehouseId, isIdle: true }
|
||||
});
|
||||
|
||||
res.json({
|
||||
...warehouse.toJSON(),
|
||||
deviceCount
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:warehouseId/devices', async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10 } = req.query;
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const warehouse = await Warehouse.findByPk(req.params.warehouseId);
|
||||
if (!warehouse) {
|
||||
return res.status(404).json({ error: '库房不存在' });
|
||||
}
|
||||
|
||||
const { count, rows } = await Device.findAndCountAll({
|
||||
where: { warehouseId: req.params.warehouseId, isIdle: true },
|
||||
offset: parseInt(offset),
|
||||
limit: parseInt(pageSize),
|
||||
order: [['idleDate', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
total: count,
|
||||
devices: rows,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取库房设备失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, location, capacity, description } = req.body;
|
||||
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: '库房名称不能为空' });
|
||||
}
|
||||
|
||||
const warehouseId = await generateWarehouseId();
|
||||
|
||||
const warehouse = await Warehouse.create({
|
||||
warehouseId,
|
||||
name,
|
||||
location: location || '',
|
||||
capacity: capacity || 100,
|
||||
status: 'active',
|
||||
description: description || ''
|
||||
});
|
||||
|
||||
await logDeviceOperation('create', `创建库房【${name}】`, {
|
||||
targetId: warehouse.warehouseId,
|
||||
targetName: name,
|
||||
afterState: warehouse.toJSON(),
|
||||
req,
|
||||
metadata: { type: 'warehouse_create' }
|
||||
});
|
||||
|
||||
res.status(201).json(warehouse);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:warehouseId', async (req, res) => {
|
||||
try {
|
||||
const warehouse = await Warehouse.findByPk(req.params.warehouseId);
|
||||
if (!warehouse) {
|
||||
return res.status(404).json({ error: '库房不存在' });
|
||||
}
|
||||
|
||||
const beforeState = warehouse.toJSON();
|
||||
const { name, location, capacity, status, description } = req.body;
|
||||
|
||||
if (name) warehouse.name = name;
|
||||
if (location !== undefined) warehouse.location = location;
|
||||
if (capacity !== undefined) warehouse.capacity = capacity;
|
||||
if (status) warehouse.status = status;
|
||||
if (description !== undefined) warehouse.description = description;
|
||||
|
||||
await warehouse.save();
|
||||
|
||||
await logDeviceOperation('update', `更新库房【${warehouse.name}】`, {
|
||||
targetId: warehouse.warehouseId,
|
||||
targetName: warehouse.name,
|
||||
beforeState,
|
||||
afterState: warehouse.toJSON(),
|
||||
req,
|
||||
metadata: { type: 'warehouse_update' }
|
||||
});
|
||||
|
||||
res.json(warehouse);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:warehouseId', async (req, res) => {
|
||||
try {
|
||||
const warehouse = await Warehouse.findByPk(req.params.warehouseId);
|
||||
if (!warehouse) {
|
||||
return res.status(404).json({ error: '库房不存在' });
|
||||
}
|
||||
|
||||
const idleDeviceCount = await Device.count({
|
||||
where: { warehouseId: req.params.warehouseId, isIdle: true }
|
||||
});
|
||||
|
||||
if (idleDeviceCount > 0) {
|
||||
return res.status(400).json({
|
||||
error: `库房中还有 ${idleDeviceCount} 台空闲设备,请先处理后再删除`
|
||||
});
|
||||
}
|
||||
|
||||
const warehouseName = warehouse.name;
|
||||
await warehouse.destroy();
|
||||
|
||||
await logDeviceOperation('delete', `删除库房【${warehouseName}】`, {
|
||||
targetId: req.params.warehouseId,
|
||||
targetName: warehouseName,
|
||||
req,
|
||||
metadata: { type: 'warehouse_delete' }
|
||||
});
|
||||
|
||||
res.json({ message: '库房删除成功' });
|
||||
} catch (error) {
|
||||
console.error('删除库房失败:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,73 +0,0 @@
|
||||
require('dotenv').config();
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
async function migrate() {
|
||||
try {
|
||||
console.log('开始 MySQL 数据库迁移...');
|
||||
console.log('数据库类型:', process.env.DB_TYPE);
|
||||
|
||||
// 检查列是否存在(MySQL 方式)
|
||||
const [columns] = await sequelize.query(`
|
||||
SELECT COLUMN_NAME
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = 'deviceFields'
|
||||
AND COLUMN_NAME = 'isSystem'
|
||||
AND TABLE_SCHEMA = '${process.env.MYSQL_DATABASE || 'it_assest'}'
|
||||
`);
|
||||
|
||||
if (columns.length === 0) {
|
||||
// 添加 isSystem 列
|
||||
await sequelize.query(`
|
||||
ALTER TABLE deviceFields
|
||||
ADD COLUMN isSystem BOOLEAN DEFAULT 0
|
||||
COMMENT '是否为系统字段,系统字段不可删除'
|
||||
`);
|
||||
console.log('✓ isSystem 列添加成功');
|
||||
} else {
|
||||
console.log('✓ isSystem 列已存在');
|
||||
}
|
||||
|
||||
// 更新系统字段标记
|
||||
const systemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry'
|
||||
];
|
||||
|
||||
for (const fieldName of systemFields) {
|
||||
await sequelize.query(`
|
||||
UPDATE deviceFields SET isSystem = 1 WHERE fieldName = '${fieldName}'
|
||||
`);
|
||||
console.log(`✓ 标记系统字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
// 验证结果
|
||||
const [results] = await sequelize.query(`
|
||||
SELECT fieldName, displayName, isSystem
|
||||
FROM deviceFields
|
||||
ORDER BY isSystem DESC, fieldName
|
||||
`);
|
||||
|
||||
console.log('\n========== 迁移结果 ==========');
|
||||
console.log('字段总数:', results.length);
|
||||
console.log('系统字段数:', results.filter(r => r.isSystem).length);
|
||||
console.log('\n系统字段列表:');
|
||||
results.filter(r => r.isSystem).forEach(r => {
|
||||
console.log(` 🔒 ${r.displayName} (${r.fieldName})`);
|
||||
});
|
||||
console.log('\n可选字段列表:');
|
||||
results.filter(r => !r.isSystem).forEach(r => {
|
||||
console.log(` ✏️ ${r.displayName} (${r.fieldName})`);
|
||||
});
|
||||
console.log('==============================\n');
|
||||
|
||||
console.log('✅ 迁移完成!');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ 迁移失败:', error.message);
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
migrate();
|
||||
@@ -1,49 +0,0 @@
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
async function migrate() {
|
||||
try {
|
||||
console.log('开始 SQLite 数据库迁移...');
|
||||
|
||||
// 检查列是否存在(SQLite 使用 PRAGMA)
|
||||
const tableInfo = await sequelize.query(
|
||||
"PRAGMA table_info(deviceFields)",
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
const hasIsSystemColumn = tableInfo.some(col => col.name === 'isSystem');
|
||||
|
||||
if (!hasIsSystemColumn) {
|
||||
// 添加 isSystem 列
|
||||
await sequelize.query(
|
||||
"ALTER TABLE deviceFields ADD COLUMN isSystem BOOLEAN DEFAULT 0",
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log('isSystem 列添加成功');
|
||||
} else {
|
||||
console.log('isSystem 列已存在');
|
||||
}
|
||||
|
||||
// 更新系统字段标记
|
||||
const systemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry'
|
||||
];
|
||||
|
||||
for (const fieldName of systemFields) {
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 1 WHERE fieldName = '${fieldName}'`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(`标记系统字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
console.log('迁移完成!');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('迁移失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
migrate();
|
||||
@@ -84,6 +84,16 @@ const migrations = [
|
||||
name: '暂存设备自定义字段',
|
||||
description: '为 pending_devices 表添加 customFields 字段,支持自定义字段存储',
|
||||
migrate: migratePendingDeviceCustomFields
|
||||
},
|
||||
{
|
||||
name: '空闲设备与业务关联',
|
||||
description: '创建 businesses、warehouses、device_business 表,为 devices 添加空闲设备字段',
|
||||
migrate: migrateIdleDeviceAndBusiness
|
||||
},
|
||||
{
|
||||
name: '设备字段系统标记',
|
||||
description: '为 deviceFields 表添加 isSystem 字段,标记系统字段不可删除',
|
||||
migrate: migrateDeviceFieldsIsSystem
|
||||
}
|
||||
];
|
||||
|
||||
@@ -565,6 +575,134 @@ async function migratePendingDeviceCustomFields() {
|
||||
await addColumnIfNotExists('pending_devices', 'customFields', columnDef);
|
||||
}
|
||||
|
||||
async function migrateDeviceFieldsIsSystem() {
|
||||
const dialect = sequelize.getDialect();
|
||||
|
||||
if (!(await tableExists('deviceFields'))) {
|
||||
console.log(' deviceFields 表不存在,跳过');
|
||||
return;
|
||||
}
|
||||
|
||||
if (dialect === 'mysql') {
|
||||
const [columns] = await sequelize.query(`
|
||||
SELECT COLUMN_NAME
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = 'deviceFields'
|
||||
AND COLUMN_NAME = 'isSystem'
|
||||
AND TABLE_SCHEMA = '${process.env.MYSQL_DATABASE || 'it_assest'}'
|
||||
`);
|
||||
|
||||
if (columns.length === 0) {
|
||||
await sequelize.query(`
|
||||
ALTER TABLE deviceFields
|
||||
ADD COLUMN isSystem BOOLEAN DEFAULT 0
|
||||
COMMENT '是否为系统字段,系统字段不可删除'
|
||||
`);
|
||||
console.log(' deviceFields 表添加 isSystem 字段成功');
|
||||
} else {
|
||||
console.log(' deviceFields 表 isSystem 字段已存在,跳过');
|
||||
}
|
||||
} else {
|
||||
const columns = await getTableColumns('deviceFields');
|
||||
if (!columns.includes('isSystem')) {
|
||||
await sequelize.query(
|
||||
"ALTER TABLE deviceFields ADD COLUMN isSystem BOOLEAN DEFAULT 0",
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(' deviceFields 表添加 isSystem 字段成功');
|
||||
} else {
|
||||
console.log(' deviceFields 表 isSystem 字段已存在,跳过');
|
||||
}
|
||||
}
|
||||
|
||||
const systemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry'
|
||||
];
|
||||
|
||||
for (const fieldName of systemFields) {
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 1 WHERE fieldName = ?`,
|
||||
{ replacements: [fieldName], type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(` 标记系统字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
console.log(' 设备字段系统标记迁移完成');
|
||||
}
|
||||
|
||||
async function migrateIdleDeviceAndBusiness() {
|
||||
const queryInterface = sequelize.getQueryInterface();
|
||||
const dialect = sequelize.getDialect();
|
||||
|
||||
if (dialect === 'sqlite') {
|
||||
await sequelize.query('PRAGMA foreign_keys = OFF');
|
||||
}
|
||||
|
||||
try {
|
||||
if (!(await tableExists('businesses'))) {
|
||||
await queryInterface.createTable('businesses', {
|
||||
businessId: { type: sequelize.Sequelize.STRING, primaryKey: true, allowNull: false, unique: true },
|
||||
name: { type: sequelize.Sequelize.STRING, allowNull: false },
|
||||
description: { type: sequelize.Sequelize.TEXT },
|
||||
status: { type: sequelize.Sequelize.ENUM('active', 'offline'), defaultValue: 'active' },
|
||||
offlineDate: { type: sequelize.Sequelize.DATE },
|
||||
offlineReason: { type: sequelize.Sequelize.STRING },
|
||||
createdAt: { type: sequelize.Sequelize.DATE, allowNull: false },
|
||||
updatedAt: { type: sequelize.Sequelize.DATE, allowNull: false }
|
||||
});
|
||||
console.log(' businesses 表创建成功');
|
||||
} else {
|
||||
console.log(' businesses 表已存在,跳过');
|
||||
}
|
||||
|
||||
if (!(await tableExists('warehouses'))) {
|
||||
await queryInterface.createTable('warehouses', {
|
||||
warehouseId: { type: sequelize.Sequelize.STRING, primaryKey: true, allowNull: false, unique: true },
|
||||
name: { type: sequelize.Sequelize.STRING, allowNull: false },
|
||||
location: { type: sequelize.Sequelize.STRING },
|
||||
capacity: { type: sequelize.Sequelize.INTEGER, defaultValue: 100 },
|
||||
status: { type: sequelize.Sequelize.ENUM('active', 'inactive'), defaultValue: 'active' },
|
||||
description: { type: sequelize.Sequelize.TEXT },
|
||||
createdAt: { type: sequelize.Sequelize.DATE, allowNull: false },
|
||||
updatedAt: { type: sequelize.Sequelize.DATE, allowNull: false }
|
||||
});
|
||||
console.log(' warehouses 表创建成功');
|
||||
} else {
|
||||
console.log(' warehouses 表已存在,跳过');
|
||||
}
|
||||
|
||||
if (!(await tableExists('device_business'))) {
|
||||
await queryInterface.createTable('device_business', {
|
||||
id: { type: sequelize.Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
|
||||
deviceId: { type: sequelize.Sequelize.STRING, allowNull: false },
|
||||
businessId: { type: sequelize.Sequelize.STRING, allowNull: false },
|
||||
isPrimary: { type: sequelize.Sequelize.BOOLEAN, defaultValue: false },
|
||||
createdAt: { type: sequelize.Sequelize.DATE, allowNull: false },
|
||||
updatedAt: { type: sequelize.Sequelize.DATE, allowNull: false }
|
||||
});
|
||||
console.log(' device_business 表创建成功');
|
||||
} else {
|
||||
console.log(' device_business 表已存在,跳过');
|
||||
}
|
||||
|
||||
if (await tableExists('devices')) {
|
||||
await addColumnIfNotExists('devices', 'isIdle', 'BOOLEAN DEFAULT 0');
|
||||
await addColumnIfNotExists('devices', 'idleDate', 'DATETIME');
|
||||
await addColumnIfNotExists('devices', 'idleReason', 'TEXT');
|
||||
await addColumnIfNotExists('devices', 'warehouseId', 'VARCHAR(255)');
|
||||
await addColumnIfNotExists('devices', 'sourceType', "TEXT DEFAULT 'rack'");
|
||||
}
|
||||
|
||||
console.log(' 空闲设备与业务关联迁移完成');
|
||||
} finally {
|
||||
if (dialect === 'sqlite') {
|
||||
await sequelize.query('PRAGMA foreign_keys = ON');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 执行迁移
|
||||
runMigrations().catch(error => {
|
||||
console.error('迁移执行失败:', error);
|
||||
|
||||
+58
-2
@@ -1,4 +1,5 @@
|
||||
require('dotenv').config();
|
||||
const path = require('path');
|
||||
const { ensureJwtSecret } = require('./initConfig');
|
||||
ensureJwtSecret();
|
||||
|
||||
@@ -84,6 +85,23 @@ async function syncBackupLogModel() {
|
||||
console.log('备份日志模型同步完成');
|
||||
}
|
||||
|
||||
async function syncOperationLogModel() {
|
||||
const OperationLog = require('./models/OperationLog');
|
||||
await OperationLog.sync();
|
||||
console.log('操作日志模型同步完成');
|
||||
}
|
||||
|
||||
async function syncBusinessModels() {
|
||||
const Business = require('./models/Business');
|
||||
const DeviceBusiness = require('./models/DeviceBusiness');
|
||||
const Warehouse = require('./models/Warehouse');
|
||||
|
||||
await Business.sync({ alter: true });
|
||||
await DeviceBusiness.sync({ alter: true });
|
||||
await Warehouse.sync({ alter: true });
|
||||
console.log('业务/设备关联/库房模型同步完成');
|
||||
}
|
||||
|
||||
async function initDefaultSystemSettings() {
|
||||
console.log('开始初始化系统设置默认值...');
|
||||
const { initDefaultSettings } = require('./routes/systemSettings');
|
||||
@@ -144,6 +162,8 @@ async function initializeApp() {
|
||||
await syncConsumableModels();
|
||||
await syncInventoryModels();
|
||||
await syncBackupLogModel();
|
||||
await syncOperationLogModel();
|
||||
await syncBusinessModels();
|
||||
await initDefaultSystemSettings();
|
||||
await initFaultCategories();
|
||||
await initAutoBackupScheduler();
|
||||
@@ -155,6 +175,9 @@ async function initializeApp() {
|
||||
}
|
||||
}
|
||||
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const { specs, customCSS } = require('./swagger');
|
||||
|
||||
initializeApp();
|
||||
|
||||
const deviceRoutes = require('./routes/devices');
|
||||
@@ -178,6 +201,10 @@ const networkCardRoutes = require('./routes/networkCards');
|
||||
const inventoryRoutes = require('./routes/inventory');
|
||||
const backupRoutes = require('./routes/backup');
|
||||
const statisticsRoutes = require('./routes/statistics');
|
||||
const operationLogsRoutes = require('./routes/operationLogs');
|
||||
const idleDeviceRoutes = require('./routes/idleDevices');
|
||||
const warehouseRoutes = require('./routes/warehouses');
|
||||
const dangerousOperationsRoutes = require('./routes/dangerousOperations');
|
||||
|
||||
app.use('/api/devices', deviceRoutes);
|
||||
app.use('/api/racks', rackRoutes);
|
||||
@@ -200,9 +227,34 @@ app.use('/api/network-cards', networkCardRoutes);
|
||||
app.use('/api/inventory', inventoryRoutes);
|
||||
app.use('/api/backup', backupRoutes);
|
||||
app.use('/api/statistics', statisticsRoutes);
|
||||
app.use('/api/operation-logs', operationLogsRoutes);
|
||||
app.use('/api/idle-devices', idleDeviceRoutes);
|
||||
app.use('/api/warehouses', warehouseRoutes);
|
||||
app.use('/api/dangerous-operations', dangerousOperationsRoutes);
|
||||
|
||||
app.use('/uploads', express.static('uploads'));
|
||||
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs, {
|
||||
customCss: customCSS,
|
||||
customSiteTitle: 'IDC设备管理系统 API文档',
|
||||
swaggerOptions: {
|
||||
persistAuthorization: true,
|
||||
displayRequestDuration: true,
|
||||
docExpansion: 'none',
|
||||
deepLinking: true,
|
||||
defaultModelsExpandDepth: -1,
|
||||
defaultModelExpandDepth: 2
|
||||
}
|
||||
}));
|
||||
|
||||
app.get('/api-docs', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'swagger_index.html'));
|
||||
});
|
||||
|
||||
app.get('/api-docs.json', (req, res) => {
|
||||
res.json(specs);
|
||||
});
|
||||
|
||||
app.get('/api', (req, res) => {
|
||||
res.json({
|
||||
name: 'IDC设备管理系统 API',
|
||||
@@ -234,8 +286,12 @@ app.get('/api', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({ status: 'ok', message: 'IDC设备管理系统后端服务正常运行' });
|
||||
const { performHealthCheck } = require('./utils/healthCheck');
|
||||
|
||||
app.get('/health', async (req, res) => {
|
||||
const health = await performHealthCheck();
|
||||
const statusCode = health.status === 'error' ? 503 : health.status === 'warning' ? 200 : 200;
|
||||
res.status(statusCode).json(health);
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
|
||||
@@ -0,0 +1,575 @@
|
||||
const swaggerJsdoc = require('swagger-jsdoc');
|
||||
|
||||
const customCSS = `
|
||||
/* ========================================
|
||||
IDC设备管理系统 - Swagger UI 翠竹绿风格定制
|
||||
======================================== */
|
||||
|
||||
/* 隐藏默认顶部栏 */
|
||||
.swagger-ui .topbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* 全局字体设置 */
|
||||
.swagger-ui {
|
||||
font-family: 'Inter', 'PingFang SC', 'Microsoft YaHei', sans-serif;
|
||||
}
|
||||
|
||||
/* 主容器背景 - 淡绿灰渐变 */
|
||||
.swagger-ui .swagger-container {
|
||||
background: linear-gradient(180deg, #f8fafc 0%, #f1f5f9 50%, #e2e8e0 100%);
|
||||
}
|
||||
|
||||
/* 标题区域 */
|
||||
.swagger-ui .title {
|
||||
font-size: 24px !important;
|
||||
font-weight: 700 !important;
|
||||
color: #166534 !important;
|
||||
text-shadow: 0 2px 8px rgba(22, 101, 52, 0.1);
|
||||
}
|
||||
|
||||
/* 描述文字 */
|
||||
.swagger-ui .title small {
|
||||
background: linear-gradient(135deg, #22c55e, #16a34a) !important;
|
||||
border-radius: 4px;
|
||||
padding: 4px 8px;
|
||||
font-size: 12px !important;
|
||||
color: #fff !important;
|
||||
}
|
||||
|
||||
/* 信息容器 */
|
||||
.swagger-ui .info {
|
||||
margin: 30px 0 !important;
|
||||
padding: 24px !important;
|
||||
background: rgba(255, 255, 255, 0.9) !important;
|
||||
border-radius: 16px !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.2) !important;
|
||||
box-shadow: 0 4px 24px rgba(22, 101, 52, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .info .title {
|
||||
color: #166534 !important;
|
||||
font-size: 28px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .info p {
|
||||
color: #64748b !important;
|
||||
line-height: 1.7 !important;
|
||||
}
|
||||
|
||||
/* 服务器选择器 */
|
||||
.swagger-ui .servers {
|
||||
margin: 20px 0 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .servers > label {
|
||||
color: #64748b !important;
|
||||
font-size: 12px !important;
|
||||
font-weight: 600 !important;
|
||||
text-transform: uppercase !important;
|
||||
letter-spacing: 1px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .servers select {
|
||||
background: rgba(255, 255, 255, 0.95) !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.3) !important;
|
||||
border-radius: 8px !important;
|
||||
color: #166534 !important;
|
||||
padding: 10px 16px !important;
|
||||
font-size: 14px !important;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .servers select:hover {
|
||||
border-color: rgba(34, 197, 94, 0.6) !important;
|
||||
box-shadow: 0 0 12px rgba(34, 197, 94, 0.15) !important;
|
||||
}
|
||||
|
||||
/* 标签页导航 */
|
||||
.swagger-ui .opblock-tag {
|
||||
background: rgba(255, 255, 255, 0.8) !important;
|
||||
border: none !important;
|
||||
border-bottom: 2px solid transparent !important;
|
||||
margin: 0 !important;
|
||||
padding: 16px 20px !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock-tag:hover {
|
||||
background: rgba(34, 197, 94, 0.08) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock-tag .tag-header {
|
||||
color: #1e293b !important;
|
||||
font-size: 15px !important;
|
||||
font-weight: 600 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock-tag .tag-header span {
|
||||
color: #94a3b8 !important;
|
||||
font-size: 13px !important;
|
||||
font-weight: 400 !important;
|
||||
}
|
||||
|
||||
/* 展开的API块 */
|
||||
.swagger-ui .opblock {
|
||||
background: rgba(255, 255, 255, 0.85) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
border-radius: 12px !important;
|
||||
margin: 8px 0 !important;
|
||||
box-shadow: 0 2px 8px rgba(22, 101, 52, 0.04) !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock:hover {
|
||||
border-color: rgba(34, 197, 94, 0.3) !important;
|
||||
box-shadow: 0 4px 16px rgba(34, 197, 94, 0.1) !important;
|
||||
transform: translateY(-1px) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock.opblock-post {
|
||||
border-left: 4px solid #22c55e !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock.opblock-get {
|
||||
border-left: 4px solid #3b82f6 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock.opblock-put {
|
||||
border-left: 4px solid #f59e0b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock.opblock-delete {
|
||||
border-left: 4px solid #ef4444 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock.opblock-patch {
|
||||
border-left: 4px solid #8b5cf6 !important;
|
||||
}
|
||||
|
||||
/* 操作标题栏 */
|
||||
.swagger-ui .opblock .opblock-summary {
|
||||
padding: 12px 16px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-path {
|
||||
color: #1e293b !important;
|
||||
font-size: 14px !important;
|
||||
font-weight: 500 !important;
|
||||
font-family: 'SF Mono', 'Consolas', monospace !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-path:hover {
|
||||
color: #166534 !important;
|
||||
}
|
||||
|
||||
/* HTTP方法标签 */
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-method {
|
||||
border-radius: 6px !important;
|
||||
font-size: 12px !important;
|
||||
font-weight: 700 !important;
|
||||
min-width: 60px !important;
|
||||
padding: 6px 10px !important;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-method span {
|
||||
font-size: 12px !important;
|
||||
}
|
||||
|
||||
/* GET 方法 - 静谧蓝 */
|
||||
.swagger-ui .opblock-get .opblock-summary-method {
|
||||
background: linear-gradient(135deg, #60a5fa, #3b82f6) !important;
|
||||
border: none !important;
|
||||
box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3) !important;
|
||||
}
|
||||
|
||||
/* POST 方法 - 翠竹绿 */
|
||||
.swagger-ui .opblock-post .opblock-summary-method {
|
||||
background: linear-gradient(135deg, #4ade80, #22c55e) !important;
|
||||
border: none !important;
|
||||
box-shadow: 0 2px 8px rgba(34, 197, 94, 0.3) !important;
|
||||
}
|
||||
|
||||
/* PUT 方法 - 暖阳橙 */
|
||||
.swagger-ui .opblock-put .opblock-summary-method {
|
||||
background: linear-gradient(135deg, #fbbf24, #f59e0b) !important;
|
||||
border: none !important;
|
||||
box-shadow: 0 2px 8px rgba(245, 158, 11, 0.3) !important;
|
||||
}
|
||||
|
||||
/* DELETE 方法 - 胭脂红 */
|
||||
.swagger-ui .opblock-delete .opblock-summary-method {
|
||||
background: linear-gradient(135deg, #f87171, #ef4444) !important;
|
||||
border: none !important;
|
||||
box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3) !important;
|
||||
}
|
||||
|
||||
/* PATCH 方法 - 梦幻紫 */
|
||||
.swagger-ui .opblock-patch .opblock-summary-method {
|
||||
background: linear-gradient(135deg, #a78bfa, #8b5cf6) !important;
|
||||
border: none !important;
|
||||
box-shadow: 0 2px 8px rgba(139, 92, 246, 0.3) !important;
|
||||
}
|
||||
|
||||
/* 参数区域 */
|
||||
.swagger-ui .opblock .opblock-section-header {
|
||||
background: rgba(248, 250, 252, 0.8) !important;
|
||||
border-bottom: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
padding: 12px 16px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .opblock .opblock-section-header h4 {
|
||||
color: #64748b !important;
|
||||
font-size: 12px !important;
|
||||
font-weight: 600 !important;
|
||||
text-transform: uppercase !important;
|
||||
letter-spacing: 1px !important;
|
||||
}
|
||||
|
||||
/* 参数框 */
|
||||
.swagger-ui .parameters .parameter {
|
||||
padding: 12px 0 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .parameters .parameter .parameter__name {
|
||||
color: #1e293b !important;
|
||||
font-weight: 500 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .parameters .parameter .parameter__name.required:after {
|
||||
color: #ef4444 !important;
|
||||
content: " *";
|
||||
}
|
||||
|
||||
.swagger-ui .parameters .parameter input,
|
||||
.swagger-ui .parameters .parameter textarea {
|
||||
background: rgba(255, 255, 255, 0.95) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.1) !important;
|
||||
border-radius: 8px !important;
|
||||
color: #1e293b !important;
|
||||
padding: 10px 14px !important;
|
||||
font-size: 14px !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .parameters .parameter input:focus,
|
||||
.swagger-ui .parameters .parameter textarea:focus {
|
||||
border-color: #22c55e !important;
|
||||
box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1) !important;
|
||||
outline: none !important;
|
||||
}
|
||||
|
||||
/* 请求体编辑器 */
|
||||
.swagger-ui .body-edit-area {
|
||||
background: rgba(255, 255, 255, 0.95) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.1) !important;
|
||||
border-radius: 8px !important;
|
||||
color: #1e293b !important;
|
||||
font-family: 'SF Mono', 'Consolas', monospace !important;
|
||||
}
|
||||
|
||||
.swagger-ui .body-edit-area:focus {
|
||||
border-color: #22c55e !important;
|
||||
box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1) !important;
|
||||
}
|
||||
|
||||
/* 执行按钮 - 翠竹绿 */
|
||||
.swagger-ui .btn {
|
||||
border-radius: 8px !important;
|
||||
font-weight: 600 !important;
|
||||
font-size: 14px !important;
|
||||
padding: 10px 20px !important;
|
||||
transition: all 0.3s ease !important;
|
||||
text-transform: uppercase !important;
|
||||
letter-spacing: 0.5px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .btn.execute {
|
||||
background: linear-gradient(135deg, #4ade80, #22c55e) !important;
|
||||
border: none !important;
|
||||
color: #fff !important;
|
||||
box-shadow: 0 4px 16px rgba(34, 197, 94, 0.3) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .btn.execute:hover {
|
||||
background: linear-gradient(135deg, #22c55e, #16a34a) !important;
|
||||
box-shadow: 0 6px 24px rgba(34, 197, 94, 0.4) !important;
|
||||
transform: translateY(-1px) !important;
|
||||
}
|
||||
|
||||
/* 响应区域 */
|
||||
.swagger-ui .responses-wrapper {
|
||||
background: rgba(248, 250, 252, 0.6) !important;
|
||||
border-top: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .response-col_status {
|
||||
color: #64748b !important;
|
||||
font-weight: 600 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .response-col_status.success {
|
||||
color: #22c55e !important;
|
||||
}
|
||||
|
||||
.swagger-ui .response-col_status.error {
|
||||
color: #ef4444 !important;
|
||||
}
|
||||
|
||||
/* 认证区域 */
|
||||
.swagger-ui .auth-wrapper {
|
||||
background: rgba(255, 255, 255, 0.9) !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.2) !important;
|
||||
border-radius: 12px !important;
|
||||
padding: 16px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .auth-wrapper .authorize {
|
||||
background: rgba(34, 197, 94, 0.1) !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.3) !important;
|
||||
border-radius: 8px !important;
|
||||
color: #166534 !important;
|
||||
padding: 8px 16px !important;
|
||||
font-weight: 600 !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .auth-wrapper .authorize:hover {
|
||||
background: rgba(34, 197, 94, 0.2) !important;
|
||||
box-shadow: 0 0 12px rgba(34, 197, 94, 0.15) !important;
|
||||
}
|
||||
|
||||
/* 滚动条美化 */
|
||||
.swagger-ui ::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
.swagger-ui ::-webkit-scrollbar-track {
|
||||
background: rgba(0, 0, 0, 0.04) !important;
|
||||
}
|
||||
|
||||
.swagger-ui ::-webkit-scrollbar-thumb {
|
||||
background: rgba(34, 197, 94, 0.3) !important;
|
||||
border-radius: 4px !important;
|
||||
}
|
||||
|
||||
.swagger-ui ::-webkit-scrollbar-thumb:hover {
|
||||
background: rgba(34, 197, 94, 0.5) !important;
|
||||
}
|
||||
|
||||
/* 模型区域 */
|
||||
.swagger-ui .model-container {
|
||||
background: rgba(255, 255, 255, 0.8) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
border-radius: 8px !important;
|
||||
padding: 16px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .model {
|
||||
color: #1e293b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .model .model-title {
|
||||
color: #166534 !important;
|
||||
font-weight: 600 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .model .prop {
|
||||
color: #64748b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .model .prop-type {
|
||||
color: #f59e0b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .model .prop-primitive {
|
||||
color: #22c55e !important;
|
||||
}
|
||||
|
||||
/* Markdown 内容 */
|
||||
.swagger-ui .markdown p,
|
||||
.swagger-ui .markdown li {
|
||||
color: #64748b !important;
|
||||
line-height: 1.7 !important;
|
||||
}
|
||||
|
||||
.swagger-ui .markdown code {
|
||||
background: rgba(34, 197, 94, 0.1) !important;
|
||||
border-radius: 4px !important;
|
||||
color: #166534 !important;
|
||||
padding: 2px 6px !important;
|
||||
font-size: 13px !important;
|
||||
}
|
||||
|
||||
/* Loading 状态 */
|
||||
.swagger-ui .loading-container .loading {
|
||||
background: rgba(255, 255, 255, 0.95) !important;
|
||||
}
|
||||
|
||||
.swagger-ui .loading-container .loading::after {
|
||||
border-color: #22c55e transparent transparent transparent !important;
|
||||
}
|
||||
|
||||
/* 展开/折叠箭头 */
|
||||
.swagger-ui .expand-operations {
|
||||
background: rgba(255, 255, 255, 0.8) !important;
|
||||
border-radius: 8px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .expand-operations button {
|
||||
color: #64748b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .expand-operations button:hover {
|
||||
color: #166534 !important;
|
||||
}
|
||||
|
||||
/* 过滤器 */
|
||||
.swagger-ui .filter-container .filter-wrapper {
|
||||
background: rgba(255, 255, 255, 0.9) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
border-radius: 8px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .filter-container input {
|
||||
background: rgba(255, 255, 255, 0.95) !important;
|
||||
border: none !important;
|
||||
color: #1e293b !important;
|
||||
padding: 8px 12px !important;
|
||||
border-radius: 6px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .filter-container input::placeholder {
|
||||
color: #94a3b8 !important;
|
||||
}
|
||||
|
||||
/* 版本信息 */
|
||||
.swagger-ui .version-stamp {
|
||||
background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(22, 163, 74, 0.15)) !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.3) !important;
|
||||
border-radius: 6px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .version-stamp span {
|
||||
color: #166534 !important;
|
||||
font-weight: 600 !important;
|
||||
}
|
||||
|
||||
/* 图标颜色 */
|
||||
.swagger-ui .svg_assets {
|
||||
color: #64748b !important;
|
||||
}
|
||||
|
||||
/* 角标/徽章 */
|
||||
.swagger-ui .badge {
|
||||
border-radius: 4px !important;
|
||||
font-size: 11px !important;
|
||||
padding: 3px 8px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .badge--deprecated {
|
||||
background: rgba(245, 158, 11, 0.15) !important;
|
||||
color: #d97706 !important;
|
||||
border: 1px solid rgba(245, 158, 11, 0.3) !important;
|
||||
}
|
||||
|
||||
/* Try it out 按钮 */
|
||||
.swagger-ui .try-out-btn {
|
||||
background: rgba(34, 197, 94, 0.1) !important;
|
||||
border: 1px solid rgba(34, 197, 94, 0.3) !important;
|
||||
border-radius: 6px !important;
|
||||
color: #166534 !important;
|
||||
font-size: 12px !important;
|
||||
padding: 6px 12px !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.swagger-ui .try-out-btn:hover {
|
||||
background: rgba(34, 197, 94, 0.2) !important;
|
||||
}
|
||||
|
||||
/* Copy 按钮 */
|
||||
.swagger-ui .copy-to-clipboard {
|
||||
background: rgba(255, 255, 255, 0.9) !important;
|
||||
border: 1px solid rgba(0, 0, 0, 0.06) !important;
|
||||
border-radius: 6px !important;
|
||||
}
|
||||
|
||||
.swagger-ui .copy-to-clipboard button {
|
||||
color: #64748b !important;
|
||||
}
|
||||
|
||||
.swagger-ui .copy-to-clipboard button:hover {
|
||||
color: #166534 !important;
|
||||
}
|
||||
`;
|
||||
|
||||
const options = {
|
||||
definition: {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'IDC设备管理系统 API',
|
||||
version: '1.0.0',
|
||||
description: '数据中心设备管理平台后端服务 API 文档',
|
||||
contact: {
|
||||
name: 'API Support'
|
||||
}
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: 'http://localhost:8000',
|
||||
description: '开发环境服务器'
|
||||
}
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
bearerAuth: {
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT',
|
||||
description: '输入 JWT token'
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [{
|
||||
bearerAuth: []
|
||||
}],
|
||||
tags: [
|
||||
{ name: 'health', description: '健康检查' },
|
||||
{ name: 'auth', description: '认证接口' },
|
||||
{ name: 'rooms', description: '机房管理' },
|
||||
{ name: 'racks', description: '机柜管理' },
|
||||
{ name: 'devices', description: '设备管理' },
|
||||
{ name: 'deviceFields', description: '设备字段' },
|
||||
{ name: 'device-ports', description: '设备端口' },
|
||||
{ name: 'network-cards', description: '网卡管理' },
|
||||
{ name: 'cables', description: '线缆管理' },
|
||||
{ name: 'tickets', description: '工单管理' },
|
||||
{ name: 'ticket-categories', description: '工单分类' },
|
||||
{ name: 'ticket-fields', description: '工单字段' },
|
||||
{ name: 'consumables', description: '耗材管理' },
|
||||
{ name: 'consumable-categories', description: '耗材分类' },
|
||||
{ name: 'consumable-records', description: '耗材记录' },
|
||||
{ name: 'users', description: '用户管理' },
|
||||
{ name: 'roles', description: '角色管理' },
|
||||
{ name: 'system-settings', description: '系统设置' },
|
||||
{ name: 'background', description: '背景配置' },
|
||||
{ name: 'inventory', description: '盘点管理' },
|
||||
{ name: 'statistics', description: '统计接口' },
|
||||
{ name: 'operation-logs', description: '操作日志' },
|
||||
{ name: 'backup', description: '备份管理' }
|
||||
]
|
||||
},
|
||||
apis: ['./routes/*.js', './swagger_docs.yaml']
|
||||
};
|
||||
|
||||
const specs = swaggerJsdoc(options);
|
||||
|
||||
module.exports = { specs, customCSS };
|
||||
|
||||
// 如需独立使用CSS文件,可导出:
|
||||
// module.exports = { specs, customCSS };
|
||||
// 并在server.js中引入外部CSS文件
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,143 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>IDC设备管理系统 API文档</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.10.5/swagger-ui.css">
|
||||
<style>
|
||||
.swagger-ui .topbar { display: none }
|
||||
.swagger-ui .title { font-size: 24px; font-weight: 700; color: #166534; font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; }
|
||||
.swagger-ui .swagger-container { background: linear-gradient(180deg, #f8fafc 0%, #f1f5f9 50%, #e2e8e0 100%); min-height: 100vh; }
|
||||
.swagger-ui .info { margin: 30px 0; padding: 24px; background: rgba(255, 255, 255, 0.9); border-radius: 16px; border: 1px solid rgba(34, 197, 94, 0.2); box-shadow: 0 4px 24px rgba(22, 101, 52, 0.08); }
|
||||
.swagger-ui .info .title { color: #166534; font-size: 28px; }
|
||||
.swagger-ui .info p { color: #64748b; }
|
||||
.swagger-ui .opblock-tag { background: rgba(255, 255, 255, 0.8); border: none; border-bottom: 2px solid transparent; padding: 16px 20px; transition: all 0.3s; }
|
||||
.swagger-ui .opblock-tag:hover { background: rgba(34, 197, 94, 0.08); }
|
||||
.swagger-ui .opblock-tag .tag-header { color: #1e293b; font-size: 15px; font-weight: 600; }
|
||||
.swagger-ui .opblock { background: rgba(255, 255, 255, 0.85); border: 1px solid rgba(0, 0, 0, 0.06); border-radius: 12px; margin: 8px 0; transition: all 0.3s; }
|
||||
.swagger-ui .opblock:hover { border-color: rgba(34, 197, 94, 0.3); transform: translateY(-1px); }
|
||||
.swagger-ui .opblock.opblock-post { border-left: 4px solid #22c55e; }
|
||||
.swagger-ui .opblock.opblock-get { border-left: 4px solid #3b82f6; }
|
||||
.swagger-ui .opblock.opblock-put { border-left: 4px solid #f59e0b; }
|
||||
.swagger-ui .opblock.opblock-delete { border-left: 4px solid #ef4444; }
|
||||
.swagger-ui .opblock.opblock-patch { border-left: 4px solid #8b5cf6; }
|
||||
.swagger-ui .opblock .opblock-summary { padding: 12px 16px; }
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-method { border-radius: 6px; font-size: 12px; font-weight: 700; min-width: 60px; padding: 6px 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
||||
.swagger-ui .opblock-get .opblock-summary-method { background: linear-gradient(135deg, #60a5fa, #3b82f6); color: white; }
|
||||
.swagger-ui .opblock-post .opblock-summary-method { background: linear-gradient(135deg, #4ade80, #22c55e); color: white; }
|
||||
.swagger-ui .opblock-put .opblock-summary-method { background: linear-gradient(135deg, #fbbf24, #f59e0b); color: white; }
|
||||
.swagger-ui .opblock-delete .opblock-summary-method { background: linear-gradient(135deg, #f87171, #ef4444); color: white; }
|
||||
.swagger-ui .opblock-patch .opblock-summary-method { background: linear-gradient(135deg, #a78bfa, #8b5cf6); color: white; }
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-path { color: #1e293b; font-family: 'SF Mono', Consolas, monospace; }
|
||||
.swagger-ui .opblock .opblock-summary .opblock-summary-path:hover { color: #166534; }
|
||||
.swagger-ui .parameters .parameter input, .swagger-ui .parameters .parameter textarea { background: rgba(255, 255, 255, 0.95); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; color: #1e293b; padding: 10px 14px; }
|
||||
.swagger-ui .parameters .parameter input:focus, .swagger-ui .parameters .parameter textarea:focus { border-color: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1); outline: none; }
|
||||
.swagger-ui .btn.execute { background: linear-gradient(135deg, #4ade80, #22c55e); border: none; color: white; border-radius: 8px; font-weight: 600; padding: 10px 20px; text-transform: uppercase; box-shadow: 0 4px 16px rgba(34, 197, 94, 0.3); }
|
||||
.swagger-ui .btn.execute:hover { background: linear-gradient(135deg, #22c55e, #16a34a); box-shadow: 0 6px 24px rgba(34, 197, 94, 0.4); }
|
||||
.swagger-ui .try-out-btn { background: rgba(34, 197, 94, 0.1); border: 1px solid rgba(34, 197, 94, 0.3); border-radius: 6px; color: #166534; font-size: 12px; padding: 6px 12px; }
|
||||
.swagger-ui .try-out-btn:hover { background: rgba(34, 197, 94, 0.2); }
|
||||
.swagger-ui .auth-wrapper { background: rgba(255, 255, 255, 0.9); border: 1px solid rgba(34, 197, 94, 0.2); border-radius: 12px; padding: 16px; }
|
||||
.swagger-ui .auth-wrapper .authorize { background: rgba(34, 197, 94, 0.1); border: 1px solid rgba(34, 197, 94, 0.3); border-radius: 8px; color: #166534; padding: 8px 16px; font-weight: 600; }
|
||||
.swagger-ui .auth-wrapper .authorize:hover { background: rgba(34, 197, 94, 0.2); }
|
||||
.swagger-ui .servers select { background: rgba(255, 255, 255, 0.95); border: 1px solid rgba(34, 197, 94, 0.3); border-radius: 8px; color: #166534; padding: 10px 16px; cursor: pointer; }
|
||||
.swagger-ui .model-container { background: rgba(255, 255, 255, 0.8); border: 1px solid rgba(0, 0, 0, 0.06); border-radius: 8px; padding: 16px; }
|
||||
.swagger-ui .model .model-title { color: #166534; font-weight: 600; }
|
||||
.swagger-ui .model .prop-type { color: #f59e0b; }
|
||||
.swagger-ui .model .prop-primitive { color: #22c55e; }
|
||||
.swagger-ui ::-webkit-scrollbar { width: 8px; height: 8px; }
|
||||
.swagger-ui ::-webkit-scrollbar-track { background: rgba(0, 0, 0, 0.04); }
|
||||
.swagger-ui ::-webkit-scrollbar-thumb { background: rgba(34, 197, 94, 0.3); border-radius: 4px; }
|
||||
.swagger-ui ::-webkit-scrollbar-thumb:hover { background: rgba(34, 197, 94, 0.5); }
|
||||
.swagger-ui .version-stamp { background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(22, 163, 74, 0.15)); border: 1px solid rgba(34, 197, 94, 0.3); border-radius: 6px; }
|
||||
.swagger-ui .version-stamp span { color: #166534; font-weight: 600; }
|
||||
.swagger-ui .filter-container input { background: rgba(255, 255, 255, 0.95); border: none; color: #1e293b; padding: 8px 12px; border-radius: 6px; }
|
||||
.swagger-ui .copy-to-clipboard { background: rgba(255, 255, 255, 0.9); border: 1px solid rgba(0, 0, 0, 0.06); border-radius: 6px; }
|
||||
.swagger-ui .copy-to-clipboard button { color: #64748b; }
|
||||
.swagger-ui .markdown code { background: rgba(34, 197, 94, 0.1); border-radius: 4px; color: #166534; padding: 2px 6px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5.10.5/swagger-ui-bundle.js"></script>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5.10.5/swagger-ui-standalone-preset.js"></script>
|
||||
<script>
|
||||
window.addEventListener('load', function() {
|
||||
var translations = {
|
||||
"Operations":"接口列表","Operation":"操作","Parameters":"参数列表","Parameter":"参数",
|
||||
"Headers":"请求头","Request_Body":"请求体","Body":"请求体","Responses":"响应列表",
|
||||
"Response":"响应","Try it out":"在线测试","Execute":"执行","Cancel":"取消",
|
||||
"Copy":"复制","Copied":"已复制","reset":"重置","Authorize":"授权","Log Out":"退出",
|
||||
"Logout":"退出","Welcome":"欢迎","Authorized":"已授权","Explore":"浏览",
|
||||
"Filter":"过滤","Search":"搜索","Collapse":"收起","Expand":"展开",
|
||||
"Loading":"加载中...","Fetch error":"获取失败","No parameters":"无参数",
|
||||
"No response":"无响应","No content":"无内容","schema":"结构","Model Schema":"模型结构","Model":"模型",
|
||||
"Example":"示例","Examples":"示例","Value":"值","Type":"类型",
|
||||
"Description":"描述","Required":"必填","Deprecated":"已弃用","Version":"版本",
|
||||
"Host":"主机","Base URL":"基础地址","Schemes":"协议","Info":"信息",
|
||||
"External Documentation":"外部文档","Response Class":"响应类型","Status Codes":"状态码",
|
||||
"Success":"成功","Error":"错误","Server":"服务器","Servers":"服务器列表",
|
||||
"Security":"安全设置","Actions":"操作","DELETE":"删除","POST":"创建",
|
||||
"GET":"查询","PUT":"更新","PATCH":"部分更新","OPTIONS":"选项","HEAD":"头信息",
|
||||
"userId":"用户ID","Device ID":"设备ID","Room ID":"机房ID","Rack ID":"机柜ID","Ticket ID":"工单ID",
|
||||
"Created at":"创建时间","Updated at":"更新时间","Created":"创建于","Updated":"更新于",
|
||||
"Status":"状态","Name":"名称","ID":"ID","type":"类型","status":"状态",
|
||||
"page":"页码","pageSize":"每页数量","total":"总数","keyword":"关键词",
|
||||
"Filter by":"筛选","Sort by":"排序","Showing":"显示",
|
||||
"Authorizations":"授权","Available authorizations":"可用授权",
|
||||
"Close":"关闭","OK":"确定","Yes":"是","No":"否",
|
||||
"Path Parameters":"路径参数","Query Parameters":"查询参数","Request Body":"请求体",
|
||||
"Response Headers":"响应头","Response Body":"响应体",
|
||||
"Implementation notes":"实现说明","Response messages":"响应消息",
|
||||
"Default":"默认","Example Value":"示例值","Model":"模型",
|
||||
"Request Headers":"请求头","Response Code":"响应码","Response Description":"响应描述",
|
||||
"page":"页码","pageSize":"每页数量","total":"总数",
|
||||
"password":"密码","username":"用户名","email":"邮箱","phone":"电话",
|
||||
"Authorization":"授权","Bearer":"令牌","access_token":"访问令牌","token_type":"令牌类型",
|
||||
"expires_in":"过期时间","scope":"作用域","grant_type":"授权类型",
|
||||
"client_id":"客户端ID","client_secret":"客户端密钥",
|
||||
"undefined":"未定义","None":"无","object":"对象","string":"字符串","integer":"整数",
|
||||
"number":"数字","boolean":"布尔值","array":"数组"
|
||||
};
|
||||
|
||||
window.ui = SwaggerUIBundle({
|
||||
url: "/api-docs.json",
|
||||
dom_id: "#swagger-ui",
|
||||
deepLinking: true,
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
|
||||
layout: "BaseLayout",
|
||||
docExpansion: "none",
|
||||
displayRequestDuration: true,
|
||||
defaultModelsExpandDepth: -1,
|
||||
defaultModelExpandDepth: 2,
|
||||
onComplete: function() {
|
||||
translatePage();
|
||||
setInterval(translatePage, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
function translatePage() {
|
||||
var els = document.querySelectorAll('.swagger-ui');
|
||||
els.forEach(function(el) {
|
||||
var nodes = el.querySelectorAll('*');
|
||||
nodes.forEach(function(node) {
|
||||
if (node.childNodes.length === 1 && node.childNodes[0].nodeType === 3) {
|
||||
var text = node.nodeValue.trim();
|
||||
for (var key in translations) {
|
||||
if (text === key && node.nodeValue === key) {
|
||||
node.nodeValue = translations[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,545 @@
|
||||
const request = require('supertest');
|
||||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const bodyParser = require('body-parser');
|
||||
const { sequelize } = require('../db');
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
const Device = require('../models/Device');
|
||||
const Rack = require('../models/Rack');
|
||||
const Room = require('../models/Room');
|
||||
const User = require('../models/User');
|
||||
const Role = require('../models/Role');
|
||||
const UserRole = require('../models/UserRole');
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';
|
||||
|
||||
describe('设备/用户/角色操作日志集成测试', () => {
|
||||
let app;
|
||||
let authToken;
|
||||
let adminUser;
|
||||
let testRack;
|
||||
let testRoom;
|
||||
|
||||
beforeAll(async () => {
|
||||
await sequelize.sync({ force: true });
|
||||
|
||||
adminUser = await User.create({
|
||||
userId: 'admin_int_test',
|
||||
username: 'admin',
|
||||
password: '$2a$10$test',
|
||||
realName: '管理员',
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
testRoom = await Room.create({
|
||||
roomId: 'ROOM_INT_TEST',
|
||||
name: '测试机房',
|
||||
location: '测试位置',
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
testRack = await Rack.create({
|
||||
rackId: 'RACK_INT_TEST',
|
||||
name: '测试机柜',
|
||||
roomId: testRoom.roomId,
|
||||
totalPower: 10000,
|
||||
currentPower: 0,
|
||||
totalUnits: 48,
|
||||
usedUnits: 0,
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
app = createTestApp();
|
||||
authToken = jwt.sign(
|
||||
{ userId: adminUser.userId, username: adminUser.username, realName: adminUser.realName, roleName: '管理员' },
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '24h' }
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await OperationLog.destroy({ where: {} });
|
||||
await Device.destroy({ where: {} });
|
||||
});
|
||||
|
||||
const createTestApp = () => {
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
return res.status(401).json({ success: false, message: '未授权' });
|
||||
}
|
||||
try {
|
||||
const decoded = jwt.verify(token, JWT_SECRET);
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(401).json({ success: false, message: '无效的令牌' });
|
||||
}
|
||||
};
|
||||
|
||||
const devicesRouter = require('../routes/devices');
|
||||
const usersRouter = require('../routes/users');
|
||||
const rolesRouter = require('../routes/roles');
|
||||
|
||||
app.use('/api/devices', authMiddleware, devicesRouter);
|
||||
app.use('/api/users', authMiddleware, usersRouter);
|
||||
app.use('/api/roles', authMiddleware, rolesRouter);
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
describe('设备操作日志记录', () => {
|
||||
test('创建设备应该记录操作日志', async () => {
|
||||
const deviceData = {
|
||||
name: '集成测试设备',
|
||||
deviceId: `DEV_INT_${Date.now()}`,
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 1,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/devices')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(deviceData)
|
||||
.expect(201);
|
||||
|
||||
expect(response.body.name).toBe(deviceData.name);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
targetId: response.body.deviceId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].operationDescription).toContain('创建设备');
|
||||
expect(logs[0].operatorId).toBe(adminUser.userId);
|
||||
expect(logs[0].result).toBe('success');
|
||||
});
|
||||
|
||||
test('更新设备应该记录操作日志', async () => {
|
||||
const device = await Device.create({
|
||||
deviceId: `DEV_UPD_INT_${Date.now()}`,
|
||||
name: '更新前设备',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 5,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/devices/${device.deviceId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ name: '更新后设备', status: 'running' })
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.name).toBe('更新后设备');
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.name).toBe('更新前设备');
|
||||
expect(logs[0].afterState.name).toBe('更新后设备');
|
||||
});
|
||||
|
||||
test('删除设备应该记录操作日志', async () => {
|
||||
const device = await Device.create({
|
||||
deviceId: `DEV_DEL_INT_${Date.now()}`,
|
||||
name: '待删除设备',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 10,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.delete(`/api/devices/${device.deviceId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'delete',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.name).toBe('待删除设备');
|
||||
});
|
||||
|
||||
test('批量删除设备应该记录操作日志', async () => {
|
||||
const device1 = await Device.create({
|
||||
deviceId: `DEV_BATCH1_${Date.now()}`,
|
||||
name: '批量设备1',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 15,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
});
|
||||
|
||||
const device2 = await Device.create({
|
||||
deviceId: `DEV_BATCH2_${Date.now()}`,
|
||||
name: '批量设备2',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 20,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/devices/batch-delete')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ deviceIds: [device1.deviceId, device2.deviceId] })
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'batch_delete' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.count).toBe(2);
|
||||
});
|
||||
|
||||
test('批量变更设备状态应该记录操作日志', async () => {
|
||||
const device1 = await Device.create({
|
||||
deviceId: `DEV_STAT1_${Date.now()}`,
|
||||
name: '状态设备1',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 25,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
});
|
||||
|
||||
const device2 = await Device.create({
|
||||
deviceId: `DEV_STAT2_${Date.now()}`,
|
||||
name: '状态设备2',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 30,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.put('/api/devices/batch-status')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ deviceIds: [device1.deviceId, device2.deviceId], status: 'running' })
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'status_change' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.status).toBe('running');
|
||||
expect(logs[0].metadata.count).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('用户操作日志记录', () => {
|
||||
let testRole;
|
||||
|
||||
beforeAll(async () => {
|
||||
testRole = await Role.create({
|
||||
roleId: `ROLE_INT_TEST_${Date.now()}`,
|
||||
roleName: '测试角色',
|
||||
roleCode: `test_role_${Date.now()}`,
|
||||
permissions: ['read', 'write'],
|
||||
status: 'active'
|
||||
});
|
||||
});
|
||||
|
||||
test('创建用户应该记录操作日志', async () => {
|
||||
const userData = {
|
||||
username: `int_test_user_${Date.now()}`,
|
||||
password: 'Password123!',
|
||||
realName: '集成测试用户',
|
||||
email: `test_${Date.now()}@example.com`,
|
||||
roleIds: [testRole.roleId]
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/users')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(userData)
|
||||
.expect(201);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
targetName: userData.username
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].operationDescription).toContain(`创建用户 ${userData.username}`);
|
||||
expect(logs[0].metadata.roleIds).toContain(testRole.roleId);
|
||||
|
||||
await User.destroy({ where: { userId: response.body.userId } });
|
||||
});
|
||||
|
||||
test('更新用户应该记录操作日志', async () => {
|
||||
const user = await User.create({
|
||||
userId: `USER_UPD_INT_${Date.now()}`,
|
||||
username: `old_name_${Date.now()}`,
|
||||
password: 'Password123!',
|
||||
realName: '旧名称用户',
|
||||
email: `old_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/users/${user.userId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ realName: '新名称用户' })
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'update',
|
||||
targetId: user.userId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.realName).toBe('旧名称用户');
|
||||
expect(logs[0].afterState.realName).toBe('新名称用户');
|
||||
|
||||
await User.destroy({ where: { userId: user.userId } });
|
||||
});
|
||||
|
||||
test('变更用户角色应该记录权限变更日志', async () => {
|
||||
const user = await User.create({
|
||||
userId: `USER_ROLE_INT_${Date.now()}`,
|
||||
username: `role_test_${Date.now()}`,
|
||||
password: 'Password123!',
|
||||
realName: '角色测试用户',
|
||||
email: `role_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
const newRole = await Role.create({
|
||||
roleId: `ROLE_NEW_INT_${Date.now()}`,
|
||||
roleName: '新测试角色',
|
||||
roleCode: `new_role_${Date.now()}`,
|
||||
permissions: ['admin'],
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
await UserRole.create({
|
||||
UserId: user.userId,
|
||||
RoleId: testRole.roleId
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/users/${user.userId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ roleIds: [newRole.roleId] })
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'permission_change',
|
||||
targetId: user.userId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.oldRoleIds).toContain(testRole.roleId);
|
||||
expect(logs[0].metadata.newRoleIds).toContain(newRole.roleId);
|
||||
|
||||
await UserRole.destroy({ where: { UserId: user.userId } });
|
||||
await User.destroy({ where: { userId: user.userId } });
|
||||
await Role.destroy({ where: { roleId: newRole.roleId } });
|
||||
});
|
||||
|
||||
test('删除用户应该记录操作日志', async () => {
|
||||
const user = await User.create({
|
||||
userId: `USER_DEL_INT_${Date.now()}`,
|
||||
username: `del_test_${Date.now()}`,
|
||||
password: 'Password123!',
|
||||
realName: '删除测试用户',
|
||||
email: `del_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.delete(`/api/users/${user.userId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'delete',
|
||||
targetId: user.userId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.username).toBe(user.username);
|
||||
});
|
||||
});
|
||||
|
||||
describe('角色操作日志记录', () => {
|
||||
test('创建角色应该记录操作日志', async () => {
|
||||
const roleData = {
|
||||
roleName: `集成测试角色_${Date.now()}`,
|
||||
roleCode: `int_test_role_${Date.now()}`,
|
||||
description: '集成测试用角色',
|
||||
permissions: ['read', 'write'],
|
||||
status: 'active'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/roles')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(roleData)
|
||||
.expect(201);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'create',
|
||||
targetId: response.body.roleId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].operationDescription).toContain(`创建角色 ${roleData.roleName}`);
|
||||
expect(logs[0].metadata.roleCode).toBe(roleData.roleCode);
|
||||
|
||||
await Role.destroy({ where: { roleId: response.body.roleId } });
|
||||
});
|
||||
|
||||
test('更新角色应该记录操作日志', async () => {
|
||||
const role = await Role.create({
|
||||
roleId: `ROLE_UPD_INT_${Date.now()}`,
|
||||
roleName: `旧角色名_${Date.now()}`,
|
||||
roleCode: `old_role_${Date.now()}`,
|
||||
description: '旧描述',
|
||||
permissions: ['read'],
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/roles/${role.roleId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({
|
||||
roleName: `新角色名_${Date.now()}`,
|
||||
permissions: ['read', 'write', 'delete']
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'update',
|
||||
targetId: role.roleId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.roleName).toContain('旧角色名');
|
||||
expect(logs[0].afterState.roleName).toContain('新角色名');
|
||||
|
||||
await Role.destroy({ where: { roleId: role.roleId } });
|
||||
});
|
||||
|
||||
test('删除角色应该记录操作日志', async () => {
|
||||
const role = await Role.create({
|
||||
roleId: `ROLE_DEL_INT_${Date.now()}`,
|
||||
roleName: `待删除角色_${Date.now()}`,
|
||||
roleCode: `del_role_${Date.now()}`,
|
||||
description: '待删除',
|
||||
permissions: ['read'],
|
||||
status: 'active'
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.delete(`/api/roles/${role.roleId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'delete',
|
||||
targetId: role.roleId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.roleName).toContain('待删除角色');
|
||||
});
|
||||
});
|
||||
|
||||
describe('日志查询验证', () => {
|
||||
test('通过 API 应该能够查询到刚记录的操作日志', async () => {
|
||||
const device = await Device.create({
|
||||
deviceId: `DEV_QUERY_INT_${Date.now()}`,
|
||||
name: '查询测试设备',
|
||||
type: 'server',
|
||||
rackId: testRack.rackId,
|
||||
position: 40,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.put(`/api/devices/${device.deviceId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({ status: 'maintenance' })
|
||||
.expect(200);
|
||||
|
||||
const logsResponse = await request(app)
|
||||
.get('/api/devices')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.status).toBe('running');
|
||||
expect(logs[0].afterState.status).toBe('maintenance');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,277 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
|
||||
describe('OperationLog 模型测试', () => {
|
||||
beforeAll(async () => {
|
||||
await sequelize.sync({ force: true });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await OperationLog.destroy({ where: {} });
|
||||
});
|
||||
|
||||
describe('模型定义', () => {
|
||||
test('OperationLog 模型应该正确定义', () => {
|
||||
expect(OperationLog).toBeDefined();
|
||||
expect(typeof OperationLog.create).toBe('function');
|
||||
expect(typeof OperationLog.findAll).toBe('function');
|
||||
expect(typeof OperationLog.findOne).toBe('function');
|
||||
});
|
||||
|
||||
test('模型应该有正确的表名', () => {
|
||||
expect(OperationLog.options.tableName).toBe('operation_logs');
|
||||
});
|
||||
});
|
||||
|
||||
describe('创建日志记录', () => {
|
||||
test('应该能够创建基本的操作日志', async () => {
|
||||
const logData = {
|
||||
recordId: 'OPLOG_TEST_001',
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
operationDescription: '创建设备 TEST_SERVER',
|
||||
targetId: 'DEV001',
|
||||
targetName: 'TEST_SERVER',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户',
|
||||
operatorRole: '管理员',
|
||||
result: 'success'
|
||||
};
|
||||
|
||||
const log = await OperationLog.create(logData);
|
||||
|
||||
expect(log.recordId).toBe(logData.recordId);
|
||||
expect(log.module).toBe(logData.module);
|
||||
expect(log.operationType).toBe(logData.operationType);
|
||||
expect(log.operationDescription).toBe(logData.operationDescription);
|
||||
expect(log.targetId).toBe(logData.targetId);
|
||||
expect(log.targetName).toBe(logData.targetName);
|
||||
expect(log.operatorId).toBe(logData.operatorId);
|
||||
expect(log.operatorName).toBe(logData.operatorName);
|
||||
expect(log.result).toBe(logData.result);
|
||||
expect(log.createdAt).toBeDefined();
|
||||
expect(log.updatedAt).toBeDefined();
|
||||
});
|
||||
|
||||
test('应该能够创建带有 beforeState 和 afterState 的日志', async () => {
|
||||
const beforeState = { name: '旧名称', status: 'offline' };
|
||||
const afterState = { name: '新名称', status: 'running' };
|
||||
|
||||
const log = await OperationLog.create({
|
||||
recordId: 'OPLOG_TEST_002',
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
operationDescription: '更新设备 TEST_DEVICE',
|
||||
targetId: 'DEV002',
|
||||
targetName: 'TEST_DEVICE',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户',
|
||||
beforeState,
|
||||
afterState,
|
||||
result: 'success'
|
||||
});
|
||||
|
||||
expect(log.beforeState).toEqual(beforeState);
|
||||
expect(log.afterState).toEqual(afterState);
|
||||
});
|
||||
|
||||
test('应该能够创建带有 metadata 的日志', async () => {
|
||||
const metadata = {
|
||||
count: 5,
|
||||
source: 'batch_operation',
|
||||
extraInfo: '额外信息'
|
||||
};
|
||||
|
||||
const log = await OperationLog.create({
|
||||
recordId: 'OPLOG_TEST_003',
|
||||
module: 'device',
|
||||
operationType: 'batch_delete',
|
||||
operationDescription: '批量删除设备',
|
||||
targetId: 'DEV001,DEV002,DEV003',
|
||||
targetName: '3台设备',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户',
|
||||
metadata,
|
||||
result: 'success'
|
||||
});
|
||||
|
||||
expect(log.metadata).toEqual(metadata);
|
||||
});
|
||||
|
||||
test('应该能够创建带有 IP 地址和 UserAgent 的日志', async () => {
|
||||
const log = await OperationLog.create({
|
||||
recordId: 'OPLOG_TEST_004',
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
operationDescription: '创建用户 test_user',
|
||||
targetId: 'user_test',
|
||||
targetName: 'test_user',
|
||||
operatorId: 'admin_user',
|
||||
operatorName: '管理员',
|
||||
ipAddress: '192.168.1.100',
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
||||
result: 'success'
|
||||
});
|
||||
|
||||
expect(log.ipAddress).toBe('192.168.1.100');
|
||||
expect(log.userAgent).toBe('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
|
||||
});
|
||||
|
||||
test('result 字段默认为 success', async () => {
|
||||
const log = await OperationLog.create({
|
||||
recordId: 'OPLOG_TEST_005',
|
||||
module: 'device',
|
||||
operationType: 'delete',
|
||||
operationDescription: '删除设备',
|
||||
targetId: 'DEV005',
|
||||
targetName: 'TEST_DEVICE',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户'
|
||||
});
|
||||
|
||||
expect(log.result).toBe('success');
|
||||
});
|
||||
|
||||
test('metadata 字段默认为空对象', async () => {
|
||||
const log = await OperationLog.create({
|
||||
recordId: 'OPLOG_TEST_006',
|
||||
module: 'role',
|
||||
operationType: 'create',
|
||||
operationDescription: '创建角色',
|
||||
targetId: 'role_test',
|
||||
targetName: '测试角色',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户'
|
||||
});
|
||||
|
||||
expect(log.metadata).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('查询日志记录', () => {
|
||||
beforeEach(async () => {
|
||||
await OperationLog.bulkCreate([
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_001',
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
operationDescription: '创建设备 设备A',
|
||||
targetId: 'DEV_A',
|
||||
targetName: '设备A',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'success'
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_002',
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
operationDescription: '更新设备 设备B',
|
||||
targetId: 'DEV_B',
|
||||
targetName: '设备B',
|
||||
operatorId: 'user_002',
|
||||
operatorName: '用户B',
|
||||
result: 'success'
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_003',
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
operationDescription: '创建用户 用户C',
|
||||
targetId: 'user_C',
|
||||
targetName: '用户C',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'success'
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_004',
|
||||
module: 'device',
|
||||
operationType: 'delete',
|
||||
operationDescription: '删除设备 设备D',
|
||||
targetId: 'DEV_D',
|
||||
targetName: '设备D',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'failed'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('应该能够按 module 查询', async () => {
|
||||
const deviceLogs = await OperationLog.findAll({
|
||||
where: { module: 'device' }
|
||||
});
|
||||
expect(deviceLogs.length).toBe(3);
|
||||
});
|
||||
|
||||
test('应该能够按 operationType 查询', async () => {
|
||||
const createLogs = await OperationLog.findAll({
|
||||
where: { operationType: 'create' }
|
||||
});
|
||||
expect(createLogs.length).toBe(2);
|
||||
});
|
||||
|
||||
test('应该能够按 operatorId 查询', async () => {
|
||||
const user001Logs = await OperationLog.findAll({
|
||||
where: { operatorId: 'user_001' }
|
||||
});
|
||||
expect(user001Logs.length).toBe(3);
|
||||
});
|
||||
|
||||
test('应该能够按 result 查询', async () => {
|
||||
const failedLogs = await OperationLog.findAll({
|
||||
where: { result: 'failed' }
|
||||
});
|
||||
expect(failedLogs.length).toBe(1);
|
||||
});
|
||||
|
||||
test('应该能够按 targetId 模糊查询', async () => {
|
||||
const { Op } = require('sequelize');
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
targetId: { [Op.like]: '%DEV%' }
|
||||
}
|
||||
});
|
||||
expect(logs.length).toBe(3);
|
||||
});
|
||||
|
||||
test('应该支持分页查询', async () => {
|
||||
const { count, rows } = await OperationLog.findAndCountAll({
|
||||
limit: 2,
|
||||
offset: 0,
|
||||
order: [['createdAt', 'DESC']]
|
||||
});
|
||||
expect(count).toBe(4);
|
||||
expect(rows.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('索引测试', () => {
|
||||
test('应该有 module 索引', () => {
|
||||
const indexes = OperationLog.options.indexes;
|
||||
expect(indexes.some(idx => idx.fields.includes('module'))).toBe(true);
|
||||
});
|
||||
|
||||
test('应该有 operationType 索引', () => {
|
||||
const indexes = OperationLog.options.indexes;
|
||||
expect(indexes.some(idx => idx.fields.includes('operationType'))).toBe(true);
|
||||
});
|
||||
|
||||
test('应该有 targetId 索引', () => {
|
||||
const indexes = OperationLog.options.indexes;
|
||||
expect(indexes.some(idx => idx.fields.includes('targetId'))).toBe(true);
|
||||
});
|
||||
|
||||
test('应该有 operatorId 索引', () => {
|
||||
const indexes = OperationLog.options.indexes;
|
||||
expect(indexes.some(idx => idx.fields.includes('operatorId'))).toBe(true);
|
||||
});
|
||||
|
||||
test('应该有 createdAt 索引', () => {
|
||||
const indexes = OperationLog.options.indexes;
|
||||
expect(indexes.some(idx => idx.fields.includes('createdAt'))).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,477 @@
|
||||
const { sequelize } = require('../db');
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
const {
|
||||
logOperation,
|
||||
logDeviceOperation,
|
||||
logUserOperation,
|
||||
logRoleOperation
|
||||
} = require('../utils/operationLogger');
|
||||
|
||||
describe('operationLogger 工具函数测试', () => {
|
||||
beforeAll(async () => {
|
||||
await sequelize.sync({ force: true });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await OperationLog.destroy({ where: {} });
|
||||
});
|
||||
|
||||
describe('logOperation 基础函数', () => {
|
||||
test('应该能够记录基本操作日志', async () => {
|
||||
const mockReq = {
|
||||
user: {
|
||||
userId: 'user_test_001',
|
||||
realName: '测试用户',
|
||||
roleName: '管理员'
|
||||
},
|
||||
headers: {
|
||||
'x-forwarded-for': '192.168.1.100',
|
||||
'user-agent': 'Mozilla/5.0 Test Browser'
|
||||
}
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
operationDescription: '测试创建设备',
|
||||
targetId: 'DEV_TEST_001',
|
||||
targetName: '测试设备',
|
||||
beforeState: null,
|
||||
afterState: { name: '测试设备', status: 'running' },
|
||||
result: 'success',
|
||||
req: mockReq
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_TEST_001' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].module).toBe('device');
|
||||
expect(logs[0].operationType).toBe('create');
|
||||
expect(logs[0].operationDescription).toBe('测试创建设备');
|
||||
expect(logs[0].targetId).toBe('DEV_TEST_001');
|
||||
expect(logs[0].targetName).toBe('测试设备');
|
||||
expect(logs[0].operatorId).toBe('user_test_001');
|
||||
expect(logs[0].operatorName).toBe('测试用户');
|
||||
expect(logs[0].operatorRole).toBe('管理员');
|
||||
expect(logs[0].ipAddress).toBe('192.168.1.100');
|
||||
expect(logs[0].userAgent).toBe('Mozilla/5.0 Test Browser');
|
||||
expect(logs[0].result).toBe('success');
|
||||
});
|
||||
|
||||
test('应该能够处理没有用户信息的请求', async () => {
|
||||
const mockReq = {
|
||||
user: null,
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
module: 'system',
|
||||
operationType: 'batch_update',
|
||||
operationDescription: '系统批量更新',
|
||||
targetId: 'SYSTEM',
|
||||
targetName: '系统',
|
||||
result: 'success',
|
||||
req: mockReq
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'SYSTEM' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].operatorId).toBe('system');
|
||||
expect(logs[0].operatorName).toBe('系统');
|
||||
});
|
||||
|
||||
test('应该能够处理没有请求对象的场景', async () => {
|
||||
await logOperation({
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
operationDescription: '无请求更新',
|
||||
targetId: 'DEV_NO_REQ',
|
||||
targetName: '无请求设备',
|
||||
result: 'success',
|
||||
req: null
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_NO_REQ' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].ipAddress).toBeNull();
|
||||
expect(logs[0].userAgent).toBeNull();
|
||||
});
|
||||
|
||||
test('应该能够记录失败的操作', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_fail', realName: '失败用户' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
module: 'device',
|
||||
operationType: 'delete',
|
||||
operationDescription: '删除设备失败',
|
||||
targetId: 'DEV_FAIL',
|
||||
targetName: '失败设备',
|
||||
result: 'failed',
|
||||
req: mockReq,
|
||||
metadata: { errorMessage: '设备不存在' }
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_FAIL' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].result).toBe('failed');
|
||||
});
|
||||
|
||||
test('应该使用提供的 metadata', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_meta', realName: '元数据用户' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
const customMetadata = {
|
||||
batchCount: 10,
|
||||
source: 'import',
|
||||
duration: 5000
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
module: 'device',
|
||||
operationType: 'batch_update',
|
||||
operationDescription: '批量更新设备',
|
||||
targetId: 'DEV_BATCH',
|
||||
targetName: '批量设备',
|
||||
result: 'success',
|
||||
req: mockReq,
|
||||
metadata: customMetadata
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_BATCH' }
|
||||
});
|
||||
|
||||
expect(logs[0].metadata).toEqual(customMetadata);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logDeviceOperation 设备操作日志', () => {
|
||||
test('应该记录设备创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_dev', realName: '设备管理员' },
|
||||
headers: { 'x-forwarded-for': '10.0.0.1' }
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'create',
|
||||
'创建设备 测试服务器 (DEV001)',
|
||||
{
|
||||
targetId: 'DEV001',
|
||||
targetName: '测试服务器',
|
||||
afterState: { deviceId: 'DEV001', name: '测试服务器', status: 'running' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'device', operationType: 'create' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].module).toBe('device');
|
||||
expect(logs[0].operationType).toBe('create');
|
||||
expect(logs[0].targetId).toBe('DEV001');
|
||||
expect(logs[0].targetName).toBe('测试服务器');
|
||||
});
|
||||
|
||||
test('应该记录设备更新日志并包含状态变更', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_upd', realName: '更新操作员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
const beforeState = { name: '旧名称', status: 'offline' };
|
||||
const afterState = { name: '新名称', status: 'running' };
|
||||
|
||||
await logDeviceOperation(
|
||||
'update',
|
||||
'更新设备 DEV002',
|
||||
{
|
||||
targetId: 'DEV002',
|
||||
targetName: 'DEV002',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV002', operationType: 'update' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState).toEqual(beforeState);
|
||||
expect(logs[0].afterState).toEqual(afterState);
|
||||
});
|
||||
|
||||
test('应该记录设备删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_del', realName: '删除操作员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'delete',
|
||||
'删除设备 DEV003 (测试服务器)',
|
||||
{
|
||||
targetId: 'DEV003',
|
||||
targetName: '测试服务器',
|
||||
beforeState: { deviceId: 'DEV003', name: '测试服务器' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV003', operationType: 'delete' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
});
|
||||
|
||||
test('应该记录批量删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_batch', realName: '批量操作员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'batch_delete',
|
||||
'批量删除设备 3台 (DEV_A,DEV_B,DEV_C)',
|
||||
{
|
||||
targetId: 'DEV_A,DEV_B,DEV_C',
|
||||
targetName: '3台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_A', name: '设备A' },
|
||||
{ deviceId: 'DEV_B', name: '设备B' },
|
||||
{ deviceId: 'DEV_C', name: '设备C' }
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { count: 3 }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'batch_delete' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.count).toBe(3);
|
||||
});
|
||||
|
||||
test('应该记录状态变更日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_status', realName: '状态管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'status_change',
|
||||
'批量变更设备状态为"运行中"',
|
||||
{
|
||||
targetId: 'DEV_STATUS_1,DEV_STATUS_2',
|
||||
targetName: '2台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'offline' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'maintenance' }
|
||||
],
|
||||
afterState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'running' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'running' }
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { status: 'running', count: 2 }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'status_change' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.status).toBe('running');
|
||||
});
|
||||
});
|
||||
|
||||
describe('logUserOperation 用户操作日志', () => {
|
||||
test('应该记录用户创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '系统管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'create',
|
||||
'创建用户 new_user',
|
||||
{
|
||||
targetId: 'user_new',
|
||||
targetName: 'new_user',
|
||||
afterState: { username: 'new_user', email: 'new@example.com' },
|
||||
req: mockReq,
|
||||
metadata: { roleIds: ['role_admin'] }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'user', operationType: 'create' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].module).toBe('user');
|
||||
expect(logs[0].targetName).toBe('new_user');
|
||||
});
|
||||
|
||||
test('应该记录权限变更日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'permission_change',
|
||||
'变更用户 test_user 的角色',
|
||||
{
|
||||
targetId: 'user_test',
|
||||
targetName: 'test_user',
|
||||
beforeState: { roleIds: ['role_viewer'] },
|
||||
afterState: { roleIds: ['role_admin'] },
|
||||
req: mockReq,
|
||||
metadata: { oldRoleIds: ['role_viewer'], newRoleIds: ['role_admin'] }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'permission_change' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.oldRoleIds).toEqual(['role_viewer']);
|
||||
expect(logs[0].metadata.newRoleIds).toEqual(['role_admin']);
|
||||
});
|
||||
|
||||
test('应该记录用户删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'delete',
|
||||
'删除用户 deleted_user',
|
||||
{
|
||||
targetId: 'user_deleted',
|
||||
targetName: 'deleted_user',
|
||||
beforeState: { username: 'deleted_user', email: 'deleted@example.com' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'user', operationType: 'delete' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logRoleOperation 角色操作日志', () => {
|
||||
test('应该记录角色创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logRoleOperation(
|
||||
'create',
|
||||
'创建角色 测试角色',
|
||||
{
|
||||
targetId: 'role_test',
|
||||
targetName: '测试角色',
|
||||
afterState: { roleName: '测试角色', permissions: ['read', 'write'] },
|
||||
req: mockReq,
|
||||
metadata: { roleCode: 'test_role', permissions: ['read', 'write'] }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'create' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].targetName).toBe('测试角色');
|
||||
expect(logs[0].metadata.roleCode).toBe('test_role');
|
||||
});
|
||||
|
||||
test('应该记录角色更新日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
const beforeState = { roleName: '旧角色', permissions: ['read'] };
|
||||
const afterState = { roleName: '新角色', permissions: ['read', 'write', 'delete'] };
|
||||
|
||||
await logRoleOperation(
|
||||
'update',
|
||||
'更新角色 角色A',
|
||||
{
|
||||
targetId: 'role_a',
|
||||
targetName: '角色A',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq,
|
||||
metadata: { oldRoleName: '旧角色', oldPermissions: ['read'] }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'update' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].beforeState.roleName).toBe('旧角色');
|
||||
expect(logs[0].afterState.roleName).toBe('新角色');
|
||||
});
|
||||
|
||||
test('应该记录角色删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
};
|
||||
|
||||
await logRoleOperation(
|
||||
'delete',
|
||||
'删除角色 测试角色B',
|
||||
{
|
||||
targetId: 'role_b',
|
||||
targetName: '测试角色B',
|
||||
beforeState: { roleName: '测试角色B', userCount: 0 },
|
||||
req: mockReq,
|
||||
metadata: { userCount: 0 }
|
||||
}
|
||||
);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'delete' }
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].metadata.userCount).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,348 @@
|
||||
process.env.JWT_SECRET = 'test-secret-key-for-jest-testing-minimum-32-chars-long';
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.DB_DIALECT = 'sqlite';
|
||||
process.env.DB_STORAGE = ':memory:';
|
||||
|
||||
const request = require('supertest');
|
||||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { Op } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
|
||||
const JWT_SECRET = 'test-secret-key-for-jest-testing-minimum-32-chars-long';
|
||||
|
||||
const createTestApp = () => {
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
return res.status(401).json({ success: false, message: '未授权' });
|
||||
}
|
||||
try {
|
||||
const decoded = jwt.verify(token, JWT_SECRET);
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(401).json({ success: false, message: '无效的令牌' });
|
||||
}
|
||||
};
|
||||
|
||||
const operationLogsRouter = express.Router();
|
||||
|
||||
operationLogsRouter.get('/', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
module,
|
||||
operationType,
|
||||
targetId,
|
||||
operatorId,
|
||||
keyword,
|
||||
startDate,
|
||||
endDate,
|
||||
result
|
||||
} = req.query;
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const limit = Math.min(parseInt(pageSize), 100);
|
||||
|
||||
const where = {};
|
||||
|
||||
if (module && module !== 'all') {
|
||||
where.module = module;
|
||||
}
|
||||
|
||||
if (operationType && operationType !== 'all') {
|
||||
where.operationType = operationType;
|
||||
}
|
||||
|
||||
if (targetId) {
|
||||
where.targetId = { [Op.like]: `%${targetId}%` };
|
||||
}
|
||||
|
||||
if (operatorId) {
|
||||
where.operatorId = operatorId;
|
||||
}
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ operationDescription: { [Op.like]: `%${keyword}%` } },
|
||||
{ targetName: { [Op.like]: `%${keyword}%` } },
|
||||
{ operatorName: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (result && result !== 'all') {
|
||||
where.result = result;
|
||||
}
|
||||
|
||||
if (startDate || endDate) {
|
||||
where.createdAt = {};
|
||||
if (startDate) {
|
||||
where.createdAt[Op.gte] = new Date(startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
const endOfDay = new Date(endDate);
|
||||
endOfDay.setHours(23, 59, 59, 999);
|
||||
where.createdAt[Op.lte] = endOfDay;
|
||||
}
|
||||
}
|
||||
|
||||
const { count, rows: logs } = await OperationLog.findAndCountAll({
|
||||
where,
|
||||
order: [['createdAt', 'DESC']],
|
||||
offset,
|
||||
limit
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
logs
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
operationLogsRouter.get('/:recordId', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const log = await OperationLog.findByPk(req.params.recordId);
|
||||
|
||||
if (!log) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '日志记录不存在'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: log
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志详情失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志详情失败'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.use('/api/operation-logs', operationLogsRouter);
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
describe('OperationLogs API 路由测试', () => {
|
||||
let app;
|
||||
let authToken;
|
||||
const testUser = {
|
||||
userId: 'test_user_001',
|
||||
username: 'testuser',
|
||||
realName: '测试用户',
|
||||
roleName: '管理员'
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
await sequelize.sync({ force: true });
|
||||
app = createTestApp();
|
||||
authToken = jwt.sign(testUser, JWT_SECRET, { expiresIn: '24h' });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await OperationLog.destroy({ where: {} });
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await sequelize.close();
|
||||
});
|
||||
|
||||
const createTestLog = async (data = {}) => {
|
||||
const defaultData = {
|
||||
recordId: `OPLOG_API_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
operationDescription: '测试日志',
|
||||
targetId: 'DEV_TEST',
|
||||
targetName: '测试设备',
|
||||
operatorId: testUser.userId,
|
||||
operatorName: testUser.realName,
|
||||
operatorRole: testUser.roleName,
|
||||
result: 'success'
|
||||
};
|
||||
return await OperationLog.create({ ...defaultData, ...data });
|
||||
};
|
||||
|
||||
describe('GET /api/operation-logs', () => {
|
||||
test('未授权访问应该返回 401', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.expect(401);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
|
||||
test('应该能够获取日志列表', async () => {
|
||||
await createTestLog({ recordId: 'OPLOG_LST_001' });
|
||||
await createTestLog({ recordId: 'OPLOG_LST_002' });
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(true);
|
||||
expect(response.body.data.logs).toHaveLength(2);
|
||||
expect(response.body.data.total).toBe(2);
|
||||
});
|
||||
|
||||
test('应该支持分页参数', async () => {
|
||||
for (let i = 0; i < 15; i++) {
|
||||
await createTestLog({ recordId: `OPLOG_PAGE_${i}` });
|
||||
}
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ page: 1, pageSize: 10 })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(true);
|
||||
expect(response.body.data.logs).toHaveLength(10);
|
||||
expect(response.body.data.total).toBe(15);
|
||||
expect(response.body.data.page).toBe(1);
|
||||
expect(response.body.data.pageSize).toBe(10);
|
||||
});
|
||||
|
||||
test('应该支持按 module 筛选', async () => {
|
||||
await createTestLog({ module: 'device', recordId: 'OPLOG_MOD_1' });
|
||||
await createTestLog({ module: 'user', recordId: 'OPLOG_MOD_2' });
|
||||
await createTestLog({ module: 'role', recordId: 'OPLOG_MOD_3' });
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ module: 'device' })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(1);
|
||||
expect(response.body.data.logs[0].module).toBe('device');
|
||||
});
|
||||
|
||||
test('应该支持按 operationType 筛选', async () => {
|
||||
await createTestLog({ operationType: 'create', recordId: 'OPLOG_TYPE_1' });
|
||||
await createTestLog({ operationType: 'update', recordId: 'OPLOG_TYPE_2' });
|
||||
await createTestLog({ operationType: 'delete', recordId: 'OPLOG_TYPE_3' });
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ operationType: 'create' })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(1);
|
||||
expect(response.body.data.logs[0].operationType).toBe('create');
|
||||
});
|
||||
|
||||
test('应该支持按 keyword 搜索', async () => {
|
||||
await createTestLog({ operationDescription: '创建设备 SERVER_A', recordId: 'OPLOG_KW_1', targetName: '服务器A' });
|
||||
await createTestLog({ operationDescription: '更新设备 SERVER_B', recordId: 'OPLOG_KW_2', targetName: '服务器B' });
|
||||
await createTestLog({ operationDescription: '删除用户 USER_C', recordId: 'OPLOG_KW_3', targetName: '用户C' });
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ keyword: 'SERVER' })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(2);
|
||||
expect(response.body.data.logs.every(log => log.operationDescription.includes('SERVER'))).toBe(true);
|
||||
});
|
||||
|
||||
test('应该支持按 result 筛选', async () => {
|
||||
await createTestLog({ result: 'success', recordId: 'OPLOG_RES_1' });
|
||||
await createTestLog({ result: 'failed', recordId: 'OPLOG_RES_2' });
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ result: 'failed' })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(1);
|
||||
expect(response.body.data.logs[0].result).toBe('failed');
|
||||
});
|
||||
|
||||
test('应该支持多条件组合筛选', async () => {
|
||||
await createTestLog({
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_1'
|
||||
});
|
||||
await createTestLog({
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_2'
|
||||
});
|
||||
await createTestLog({
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_3'
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.query({ module: 'device', operationType: 'create' })
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(1);
|
||||
expect(response.body.data.logs[0].module).toBe('device');
|
||||
expect(response.body.data.logs[0].operationType).toBe('create');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/operation-logs/:recordId', () => {
|
||||
test('应该能够获取单个日志详情', async () => {
|
||||
const log = await createTestLog({
|
||||
recordId: 'OPLOG_DETAIL_001',
|
||||
beforeState: { name: '旧名称' },
|
||||
afterState: { name: '新名称' },
|
||||
metadata: { customField: '自定义值' }
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs/OPLOG_DETAIL_001')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(true);
|
||||
expect(response.body.data.recordId).toBe('OPLOG_DETAIL_001');
|
||||
});
|
||||
|
||||
test('不存在的日志应该返回 404', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs/NON_EXISTENT_LOG')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(404);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
let isClosed = false;
|
||||
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
beforeAll(async () => {
|
||||
try {
|
||||
if (!isClosed) {
|
||||
await sequelize.sync({ force: true });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Setup beforeAll error:', error);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
afterAll(async () => {
|
||||
try {
|
||||
if (!isClosed) {
|
||||
isClosed = true;
|
||||
await sequelize.close();
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore close errors
|
||||
}
|
||||
}, 30000);
|
||||
@@ -0,0 +1,4 @@
|
||||
process.env.JWT_SECRET = 'test-secret-key-for-jest-testing-minimum-32-chars-long';
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.DB_DIALECT = 'sqlite';
|
||||
process.env.DB_STORAGE = ':memory:';
|
||||
@@ -0,0 +1,204 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const LOG_DIR = path.join(__dirname, '../../logs');
|
||||
const DANGEROUS_OPERATIONS_LOG = path.join(LOG_DIR, 'dangerous-operations.log');
|
||||
|
||||
const ensureLogDir = () => {
|
||||
if (!fs.existsSync(LOG_DIR)) {
|
||||
fs.mkdirSync(LOG_DIR, { recursive: true });
|
||||
}
|
||||
};
|
||||
|
||||
const formatLogEntry = (entry) => {
|
||||
const timestamp = new Date().toISOString();
|
||||
return JSON.stringify({
|
||||
timestamp,
|
||||
...entry,
|
||||
}) + '\n';
|
||||
};
|
||||
|
||||
const logDangerousOperation = async (req, {
|
||||
operationType,
|
||||
operationName,
|
||||
targetType,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
metadata = {},
|
||||
success = true,
|
||||
errorMessage = null,
|
||||
}) => {
|
||||
ensureLogDir();
|
||||
|
||||
const clientIp = req?.ip || req?.connection?.remoteAddress || 'unknown';
|
||||
const userAgent = req?.get?.('User-Agent') || 'unknown';
|
||||
const userId = req?.user?.userId || req?.session?.userId || 'anonymous';
|
||||
const username = req?.user?.username || req?.session?.username || 'anonymous';
|
||||
|
||||
const logEntry = {
|
||||
operationType,
|
||||
operationName,
|
||||
targetType,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState: beforeState ? JSON.stringify(beforeState) : null,
|
||||
metadata,
|
||||
success,
|
||||
errorMessage,
|
||||
clientIp,
|
||||
userAgent,
|
||||
userId,
|
||||
username,
|
||||
riskLevel: metadata.riskLevel || 'UNKNOWN',
|
||||
relatedDataCount: metadata.relatedDataCount || 0,
|
||||
itemCount: metadata.itemCount || 1,
|
||||
};
|
||||
|
||||
try {
|
||||
fs.appendFileSync(DANGEROUS_OPERATIONS_LOG, formatLogEntry(logEntry));
|
||||
console.log(`[DANGEROUS-OP] ${logEntry.operationName} by ${logEntry.username} - ${success ? 'SUCCESS' : 'FAILED'}`);
|
||||
} catch (error) {
|
||||
console.error('Failed to write dangerous operation log:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const getDangerousOperationsLogs = (filters = {}) => {
|
||||
ensureLogDir();
|
||||
|
||||
if (!fs.existsSync(DANGEROUS_OPERATIONS_LOG)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(DANGEROUS_OPERATIONS_LOG, 'utf-8');
|
||||
const lines = content.split('\n').filter(line => line.trim());
|
||||
|
||||
let logs = lines.map(line => {
|
||||
try {
|
||||
return JSON.parse(line);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}).filter(log => log !== null);
|
||||
|
||||
if (filters.operationType) {
|
||||
logs = logs.filter(log => log.operationType === filters.operationType);
|
||||
}
|
||||
|
||||
if (filters.targetType) {
|
||||
logs = logs.filter(log => log.targetType === filters.targetType);
|
||||
}
|
||||
|
||||
if (filters.success !== undefined) {
|
||||
logs = logs.filter(log => log.success === filters.success);
|
||||
}
|
||||
|
||||
if (filters.startDate) {
|
||||
logs = logs.filter(log => new Date(log.timestamp) >= new Date(filters.startDate));
|
||||
}
|
||||
|
||||
if (filters.endDate) {
|
||||
logs = logs.filter(log => new Date(log.timestamp) <= new Date(filters.endDate));
|
||||
}
|
||||
|
||||
if (filters.username) {
|
||||
logs = logs.filter(log => log.username?.toLowerCase().includes(filters.username.toLowerCase()));
|
||||
}
|
||||
|
||||
if (filters.riskLevel) {
|
||||
logs = logs.filter(log => log.riskLevel === filters.riskLevel);
|
||||
}
|
||||
|
||||
return logs.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
|
||||
} catch (error) {
|
||||
console.error('Failed to read dangerous operations log:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const cleanOldLogs = (daysToKeep = 90) => {
|
||||
ensureLogDir();
|
||||
|
||||
if (!fs.existsSync(DANGEROUS_OPERATIONS_LOG)) {
|
||||
return { deletedCount: 0 };
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(DANGEROUS_OPERATIONS_LOG, 'utf-8');
|
||||
const lines = content.split('\n').filter(line => line.trim());
|
||||
|
||||
const cutoffDate = new Date();
|
||||
cutoffDate.setDate(cutoffDate.getDate() - daysToKeep);
|
||||
|
||||
const remainingLogs = [];
|
||||
let deletedCount = 0;
|
||||
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const logEntry = JSON.parse(line);
|
||||
if (new Date(logEntry.timestamp) >= cutoffDate) {
|
||||
remainingLogs.push(line);
|
||||
} else {
|
||||
deletedCount++;
|
||||
}
|
||||
} catch {
|
||||
deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(DANGEROUS_OPERATIONS_LOG, remainingLogs.join('\n') + '\n');
|
||||
|
||||
return { deletedCount, remainingCount: remainingLogs.length };
|
||||
} catch (error) {
|
||||
console.error('Failed to clean old logs:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const DANGEROUS_OPERATION_TYPES = {
|
||||
DELETE_SINGLE: 'DELETE_SINGLE',
|
||||
DELETE_BATCH: 'DELETE_BATCH',
|
||||
DELETE_ALL: 'DELETE_ALL',
|
||||
UPDATE_BATCH: 'UPDATE_BATCH',
|
||||
RESTORE: 'RESTORE',
|
||||
PURGE: 'PURGE',
|
||||
BATCH_RESTORE: 'BATCH_RESTORE',
|
||||
};
|
||||
|
||||
const RISK_LEVELS = {
|
||||
EXTREME: 'EXTREME',
|
||||
HIGH: 'HIGH',
|
||||
MEDIUM: 'MEDIUM',
|
||||
LOW: 'LOW',
|
||||
};
|
||||
|
||||
const calculateRiskLevel = (operationType, itemCount, options = {}) => {
|
||||
const { hasRelatedData = false, isSystemLevel = false } = options;
|
||||
|
||||
if (operationType === DANGEROUS_OPERATION_TYPES.DELETE_ALL || isSystemLevel) {
|
||||
return RISK_LEVELS.EXTREME;
|
||||
}
|
||||
|
||||
if (operationType === DANGEROUS_OPERATION_TYPES.DELETE_BATCH) {
|
||||
if (itemCount > 10) {
|
||||
return RISK_LEVELS.EXTREME;
|
||||
}
|
||||
return itemCount > 3 ? RISK_LEVELS.HIGH : RISK_LEVELS.MEDIUM;
|
||||
}
|
||||
|
||||
if (hasRelatedData) {
|
||||
return RISK_LEVELS.MEDIUM;
|
||||
}
|
||||
|
||||
return RISK_LEVELS.LOW;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
logDangerousOperation,
|
||||
getDangerousOperationsLogs,
|
||||
cleanOldLogs,
|
||||
DANGEROUS_OPERATION_TYPES,
|
||||
RISK_LEVELS,
|
||||
calculateRiskLevel,
|
||||
};
|
||||
@@ -0,0 +1,158 @@
|
||||
const { sequelize, DB_TYPE, dbDialect } = require('../db');
|
||||
|
||||
const checkDatabase = async () => {
|
||||
const result = {
|
||||
status: 'ok',
|
||||
type: dbDialect,
|
||||
message: '数据库连接正常'
|
||||
};
|
||||
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
result.status = 'ok';
|
||||
} catch (error) {
|
||||
result.status = 'error';
|
||||
result.message = `数据库连接失败: ${error.message}`;
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
await sequelize.query('SELECT 1');
|
||||
result.status = 'ok';
|
||||
} catch (error) {
|
||||
result.status = 'error';
|
||||
result.message = `数据库查询失败: ${error.message}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const checkCriticalConfig = () => {
|
||||
const checks = [];
|
||||
|
||||
const jwtSecret = process.env.JWT_SECRET;
|
||||
if (!jwtSecret) {
|
||||
checks.push({
|
||||
key: 'JWT_SECRET',
|
||||
status: 'error',
|
||||
message: 'JWT_SECRET 未配置'
|
||||
});
|
||||
} else if (jwtSecret.length < 32) {
|
||||
checks.push({
|
||||
key: 'JWT_SECRET',
|
||||
status: 'warning',
|
||||
message: 'JWT_SECRET 长度不足,建议至少 32 字符'
|
||||
});
|
||||
} else {
|
||||
checks.push({
|
||||
key: 'JWT_SECRET',
|
||||
status: 'ok',
|
||||
message: 'JWT_SECRET 已配置'
|
||||
});
|
||||
}
|
||||
|
||||
const port = process.env.PORT;
|
||||
checks.push({
|
||||
key: 'PORT',
|
||||
status: port ? 'ok' : 'warning',
|
||||
message: port ? `服务端口: ${port}` : '使用默认端口 8000'
|
||||
});
|
||||
|
||||
const dbType = process.env.DB_TYPE || 'sqlite';
|
||||
checks.push({
|
||||
key: 'DB_TYPE',
|
||||
status: 'ok',
|
||||
message: `数据库类型: ${dbType}`
|
||||
});
|
||||
|
||||
if (dbType === 'mysql') {
|
||||
const mysqlHost = process.env.MYSQL_HOST;
|
||||
const mysqlDb = process.env.MYSQL_DATABASE;
|
||||
if (!mysqlHost || !mysqlDb) {
|
||||
checks.push({
|
||||
key: 'MYSQL_CONFIG',
|
||||
status: 'warning',
|
||||
message: 'MySQL 配置不完整'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const overallStatus = checks.every(c => c.status === 'ok')
|
||||
? 'ok'
|
||||
: checks.some(c => c.status === 'error')
|
||||
? 'error'
|
||||
: 'warning';
|
||||
|
||||
return {
|
||||
status: overallStatus,
|
||||
checks
|
||||
};
|
||||
};
|
||||
|
||||
const getSystemInfo = () => {
|
||||
const memUsage = process.memoryUsage();
|
||||
const uptime = process.uptime();
|
||||
|
||||
const formatUptime = (seconds) => {
|
||||
const days = Math.floor(seconds / 86400);
|
||||
const hours = Math.floor((seconds % 86400) / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
|
||||
const parts = [];
|
||||
if (days > 0) parts.push(`${days}天`);
|
||||
if (hours > 0) parts.push(`${hours}小时`);
|
||||
if (minutes > 0) parts.push(`${minutes}分钟`);
|
||||
if (secs > 0 || parts.length === 0) parts.push(`${secs}秒`);
|
||||
|
||||
return parts.join(' ');
|
||||
};
|
||||
|
||||
return {
|
||||
nodeVersion: process.version,
|
||||
platform: process.platform,
|
||||
memory: {
|
||||
heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024 * 100) / 100,
|
||||
heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024 * 100) / 100,
|
||||
rss: Math.round(memUsage.rss / 1024 / 1024 * 100) / 100,
|
||||
unit: 'MB'
|
||||
},
|
||||
uptime: formatUptime(uptime),
|
||||
uptimeSeconds: Math.round(uptime)
|
||||
};
|
||||
};
|
||||
|
||||
const performHealthCheck = async () => {
|
||||
const [dbCheck] = await Promise.all([checkDatabase()]);
|
||||
const configCheck = checkCriticalConfig();
|
||||
const systemInfo = getSystemInfo();
|
||||
|
||||
const allChecks = [
|
||||
{ name: 'database', ...dbCheck },
|
||||
{ name: 'config', ...configCheck }
|
||||
];
|
||||
|
||||
const overallStatus = allChecks.every(c => c.status === 'ok')
|
||||
? 'ok'
|
||||
: allChecks.some(c => c.status === 'error')
|
||||
? 'error'
|
||||
: 'warning';
|
||||
|
||||
return {
|
||||
status: overallStatus,
|
||||
timestamp: new Date().toISOString(),
|
||||
service: {
|
||||
name: 'IDC设备管理系统',
|
||||
version: '1.0.0'
|
||||
},
|
||||
checks: allChecks,
|
||||
system: systemInfo
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
performHealthCheck,
|
||||
checkDatabase,
|
||||
checkCriticalConfig,
|
||||
getSystemInfo
|
||||
};
|
||||
@@ -0,0 +1,123 @@
|
||||
const OperationLog = require('../models/OperationLog');
|
||||
|
||||
const generateRecordId = () => {
|
||||
return `OPLOG_${Date.now().toString(36)}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
};
|
||||
|
||||
const getOperatorInfo = (req) => {
|
||||
if (!req || !req.user) {
|
||||
return {
|
||||
operatorId: 'system',
|
||||
operatorName: '系统',
|
||||
operatorRole: null
|
||||
};
|
||||
}
|
||||
return {
|
||||
operatorId: req.user.userId || req.user.id || 'unknown',
|
||||
operatorName: req.user.realName || req.user.username || '未知用户',
|
||||
operatorRole: req.user.roleName || req.user.role || null
|
||||
};
|
||||
};
|
||||
|
||||
const getClientInfo = (req) => {
|
||||
if (!req) {
|
||||
return { ipAddress: null, userAgent: null };
|
||||
}
|
||||
const ipAddress = req.headers['x-forwarded-for'] ||
|
||||
req.headers['x-real-ip'] ||
|
||||
req.connection?.remoteAddress ||
|
||||
req.ip ||
|
||||
null;
|
||||
const userAgent = req.headers['user-agent'] || null;
|
||||
return { ipAddress, userAgent };
|
||||
};
|
||||
|
||||
async function logOperation({
|
||||
module,
|
||||
operationType,
|
||||
operationDescription,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
afterState,
|
||||
result = 'success',
|
||||
req,
|
||||
metadata = {}
|
||||
}) {
|
||||
try {
|
||||
const operatorInfo = getOperatorInfo(req);
|
||||
const clientInfo = getClientInfo(req);
|
||||
|
||||
await OperationLog.create({
|
||||
recordId: generateRecordId(),
|
||||
module,
|
||||
operationType,
|
||||
operationDescription,
|
||||
targetId: targetId || null,
|
||||
targetName: targetName || null,
|
||||
operatorId: operatorInfo.operatorId,
|
||||
operatorName: operatorInfo.operatorName,
|
||||
operatorRole: operatorInfo.operatorRole,
|
||||
beforeState: beforeState || null,
|
||||
afterState: afterState || null,
|
||||
result,
|
||||
ipAddress: clientInfo.ipAddress,
|
||||
userAgent: clientInfo.userAgent,
|
||||
metadata
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('记录操作日志失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function logDeviceOperation(operationType, operationDescription, { targetId, targetName, beforeState, afterState, result, req, metadata = {} }) {
|
||||
return logOperation({
|
||||
module: 'device',
|
||||
operationType,
|
||||
operationDescription,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
afterState,
|
||||
result,
|
||||
req,
|
||||
metadata
|
||||
});
|
||||
}
|
||||
|
||||
async function logUserOperation(operationType, operationDescription, { targetId, targetName, beforeState, afterState, result, req, metadata = {} }) {
|
||||
return logOperation({
|
||||
module: 'user',
|
||||
operationType,
|
||||
operationDescription,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
afterState,
|
||||
result,
|
||||
req,
|
||||
metadata
|
||||
});
|
||||
}
|
||||
|
||||
async function logRoleOperation(operationType, operationDescription, { targetId, targetName, beforeState, afterState, result, req, metadata = {} }) {
|
||||
return logOperation({
|
||||
module: 'role',
|
||||
operationType,
|
||||
operationDescription,
|
||||
targetId,
|
||||
targetName,
|
||||
beforeState,
|
||||
afterState,
|
||||
result,
|
||||
req,
|
||||
metadata
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
logOperation,
|
||||
logDeviceOperation,
|
||||
logUserOperation,
|
||||
logRoleOperation
|
||||
};
|
||||
Reference in New Issue
Block a user