feat: 实现ID生成器并重构模型ID生成逻辑
refactor: 统一ID生成方式,使用新的idGenerator模块 refactor: 重构模型ID生成逻辑,允许空ID并在创建前自动生成 fix(roomSchema): 使机房ID字段变为可选并更新前端表单验证 style: 格式化代码并优化导入语句 test: 添加操作日志集成测试文件 docs: 添加错误处理模块文档
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
|
||||
const DEFAULT_CONFIG = {
|
||||
prefix: '',
|
||||
timestampBase: 36,
|
||||
timestampUpperCase: true,
|
||||
randomLength: 4,
|
||||
randomBase: 36,
|
||||
randomUpperCase: true,
|
||||
};
|
||||
|
||||
function generateId(config = {}) {
|
||||
const {
|
||||
prefix,
|
||||
timestampBase,
|
||||
timestampUpperCase,
|
||||
randomLength,
|
||||
randomBase,
|
||||
randomUpperCase,
|
||||
} = { ...DEFAULT_CONFIG, ...config };
|
||||
|
||||
const timestamp = Date.now().toString(timestampBase);
|
||||
const formattedTimestamp = timestampUpperCase ? timestamp.toUpperCase() : timestamp;
|
||||
|
||||
let randomStr = '';
|
||||
for (let i = 0; i < randomLength; i++) {
|
||||
randomStr += Math.floor(Math.random() * randomBase).toString(randomBase);
|
||||
}
|
||||
const formattedRandom = randomUpperCase ? randomStr.toUpperCase() : randomStr;
|
||||
|
||||
return `${prefix}${formattedTimestamp}${formattedRandom}`;
|
||||
}
|
||||
|
||||
function generateTicketId() {
|
||||
return generateId({ prefix: 'TKT', randomLength: 4 });
|
||||
}
|
||||
|
||||
function generateRoleId() {
|
||||
return generateId({
|
||||
prefix: 'role_',
|
||||
timestampUpperCase: false,
|
||||
randomLength: 9,
|
||||
randomUpperCase: false
|
||||
});
|
||||
}
|
||||
|
||||
function generatePlanId() {
|
||||
return generateId({ prefix: 'PLAN', randomLength: 4 });
|
||||
}
|
||||
|
||||
function generateTaskId() {
|
||||
return generateId({ prefix: 'TASK', randomLength: 4 });
|
||||
}
|
||||
|
||||
function generateRecordId() {
|
||||
return generateId({ prefix: 'REC', randomLength: 6 });
|
||||
}
|
||||
|
||||
function generatePendingId() {
|
||||
return generateId({ prefix: 'PEND', randomLength: 4 });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateId,
|
||||
generateTicketId,
|
||||
generateRoleId,
|
||||
generatePlanId,
|
||||
generateTaskId,
|
||||
generateRecordId,
|
||||
generatePendingId,
|
||||
DEFAULT_CONFIG,
|
||||
};
|
||||
Reference in New Issue
Block a user