feat: 实现ID生成器并重构模型ID生成逻辑

refactor: 统一ID生成方式,使用新的idGenerator模块
refactor: 重构模型ID生成逻辑,允许空ID并在创建前自动生成
fix(roomSchema): 使机房ID字段变为可选并更新前端表单验证
style: 格式化代码并优化导入语句
test: 添加操作日志集成测试文件
docs: 添加错误处理模块文档
This commit is contained in:
zhang1106
2026-04-02 10:34:13 +08:00
parent ea67951dd4
commit 7f1df28858
28 changed files with 451 additions and 91 deletions
+4 -7
View File
@@ -12,6 +12,7 @@ const User = require('../models/User');
const PendingDevice = require('../models/PendingDevice');
const { authMiddleware, authorize } = require('../middleware/auth');
const { PAGINATION } = require('../config');
const { generateId } = require('../utils/idGenerator');
InventoryTask.belongsTo(InventoryPlan, { foreignKey: 'planId', as: 'Plan' });
InventoryPlan.hasMany(InventoryTask, { foreignKey: 'planId', as: 'Tasks' });
@@ -23,20 +24,16 @@ InventoryPlan.hasMany(InventoryRecord, { foreignKey: 'planId', as: 'Records' });
InventoryRecord.belongsTo(Device, { foreignKey: 'deviceId', as: 'Device' });
InventoryRecord.belongsTo(User, { foreignKey: 'checkedBy', as: 'Checker' });
function generateId(prefix) {
return `${prefix}${Date.now().toString(36).toUpperCase()}${Math.random().toString(36).substr(2, 6).toUpperCase()}`;
}
function generatePlanId() {
return `PLAN${Date.now().toString(36).toUpperCase()}${Math.random().toString(36).substr(2, 4).toUpperCase()}`;
return generateId({ prefix: 'PLAN' });
}
function generateTaskId() {
return `TASK${Date.now().toString(36).toUpperCase()}${Math.random().toString(36).substr(2, 4).toUpperCase()}`;
return generateId({ prefix: 'TASK' });
}
function generateRecordId() {
return `REC${Date.now().toString(36).toUpperCase()}${Math.random().toString(36).substr(2, 6).toUpperCase()}`;
return generateId({ prefix: 'REC' });
}
router.use(authMiddleware);