Initial team BPM workspace with managed connections, scanning and mail templates
This commit is contained in:
commit
34770ed338
101 files changed
+6499
No files matched your search
@@ -0,0 +1,99 @@
|
||||
const fs=require('node:fs');
|
||||
const path=require('node:path');
|
||||
const crypto=require('node:crypto');
|
||||
const copy=value=>structuredClone(value);
|
||||
const safeUser=({id,username,role,createdAt})=>({id,username,role,createdAt});
|
||||
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
|
||||
|
||||
function validateSettings(settings) {
|
||||
const schedule=settings.schedule;
|
||||
if(!schedule || !Array.isArray(schedule.times) || schedule.times.length!==2 || new Set(schedule.times).size!==2)fail('请设置两个不同的发送时间。');
|
||||
if(schedule.times.some(time=>typeof time!=='string' || !/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(time)))fail('时间必须为有效的 HH:MM。');
|
||||
if(typeof schedule.weekends!=='boolean' || !['product_line','order'].includes(schedule.mode) || !['next_window','skip'].includes(schedule.missed))fail('发送计划选项不正确。');
|
||||
if(schedule.timezone!=='Asia/Shanghai')fail('时区必须为 Asia/Shanghai。');
|
||||
if(!settings.templates || typeof settings.templates.subject!=='string' || typeof settings.templates.body!=='string' || settings.templates.subject.length>300 || settings.templates.body.length>20000)fail('模板标题最多 300 字,正文最多 20000 字。');
|
||||
if(!Array.isArray(settings.groups) || settings.groups.length>100)fail('收件群组格式不正确。');
|
||||
const names=new Set();
|
||||
for(const group of settings.groups) {
|
||||
if(!group || typeof group.productLine!=='string' || !group.productLine.trim() || group.productLine.length>100 || names.has(group.productLine.trim()))fail('产品线名称不能为空或重复。');
|
||||
names.add(group.productLine.trim());
|
||||
for(const field of ['to','cc']) {
|
||||
if(!Array.isArray(group[field]) || group[field].length>50 || group[field].some(email=>typeof email!=='string' || email.length>254 || !/^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/.test(email)))fail('请填写有效的收件邮箱,使用逗号分隔。');
|
||||
}
|
||||
if(group.to.length===0)fail('每个产品线至少需要一个收件人。');
|
||||
}
|
||||
}
|
||||
|
||||
class Store {
|
||||
constructor(dir) {
|
||||
fs.mkdirSync(dir,{recursive:true});this.file=path.join(dir,'state.json');
|
||||
try {this.data=JSON.parse(fs.readFileSync(this.file,'utf8'));if(this.data.schema!==1)fail('状态文件版本不支持。',500);}
|
||||
catch(error) {if(error.code!=='ENOENT')throw error;this.data={schema:1,users:[],settings:{version:1,schedule:{times:['09:00','17:00'],timezone:'Asia/Shanghai',weekends:false,mode:'product_line',missed:'next_window'},templates:{subject:'',body:''},groups:[]},jobs:[],audit:[]};}
|
||||
}
|
||||
commit(mutator) {
|
||||
const draft=copy(this.data);const result=mutator(draft);
|
||||
const temp=this.file+'.'+crypto.randomBytes(6).toString('hex')+'.tmp';
|
||||
try {fs.writeFileSync(temp,JSON.stringify(draft,null,2),{mode:0o600});fs.renameSync(temp,this.file);}
|
||||
catch(error){try{fs.unlinkSync(temp);}catch{}throw error;}
|
||||
this.data=draft;return copy(result);
|
||||
}
|
||||
addAudit(draft,actor,action,detail='') {draft.audit.unshift({id:crypto.randomUUID(),at:new Date().toISOString(),actor,action,detail});draft.audit=draft.audit.slice(0,2000);}
|
||||
settings(){return copy(this.data.settings);}
|
||||
saveSettings(patch,version,actor) {
|
||||
return this.commit(draft=>{
|
||||
if(version!==draft.settings.version)fail('配置已被其他人更新,请刷新后重试。',409);
|
||||
const updated={...draft.settings};
|
||||
for(const key of ['schedule','templates','groups'])if(Object.hasOwn(patch,key))updated[key]=copy(patch[key]);
|
||||
validateSettings(updated);
|
||||
updated.schedule={times:[...updated.schedule.times].sort(),timezone:'Asia/Shanghai',weekends:updated.schedule.weekends,mode:updated.schedule.mode,missed:updated.schedule.missed};
|
||||
updated.templates={subject:updated.templates.subject,body:updated.templates.body};
|
||||
updated.groups=updated.groups.map(group=>({productLine:group.productLine.trim(),to:group.to,cc:group.cc}));
|
||||
updated.version++;draft.settings=updated;
|
||||
this.addAudit(draft,actor,'保存配置','发送计划 / 模板 / 产品线群组');return updated;
|
||||
});
|
||||
}
|
||||
users(){return this.data.users.map(safeUser);}
|
||||
user(id){const user=this.data.users.find(user=>user.id===id);return user?safeUser(user):null;}
|
||||
makeUser(username,password,role) {
|
||||
if(typeof username!=='string' || !/^[a-zA-Z0-9_.-]{3,40}$/.test(username))fail('账号需为 3–40 位英文、数字、点、下划线或短横线。');
|
||||
if(typeof password!=='string' || password.length<12 || password.length>128)fail('密码长度需为 12–128 位。');
|
||||
if(!['admin','operator','viewer'].includes(role))fail('账号角色不正确。');
|
||||
if(this.data.users.some(user=>user.username.toLowerCase()===username.toLowerCase()))fail('账号已存在。',409);
|
||||
const salt=crypto.randomBytes(16).toString('hex');
|
||||
return {id:crypto.randomUUID(),username,role,createdAt:new Date().toISOString(),passwordHash:salt+':'+crypto.scryptSync(password,salt,32).toString('hex')};
|
||||
}
|
||||
bootstrap(username,password) {
|
||||
if(this.data.users.length)fail('管理员已经创建,请登录。',409);
|
||||
const user=this.makeUser(username,password,'admin');
|
||||
return this.commit(draft=>{draft.users.push(user);this.addAudit(draft,username,'创建管理员');return safeUser(user);});
|
||||
}
|
||||
addUser(input,actor) {
|
||||
const user=this.makeUser(input.username,input.password,input.role);
|
||||
return this.commit(draft=>{draft.users.push(user);this.addAudit(draft,actor,'创建账号',user.username+' / '+user.role);return safeUser(user);});
|
||||
}
|
||||
authenticate(username,password) {
|
||||
if(typeof username!=='string' || typeof password!=='string' || password.length>128)return null;
|
||||
const user=this.data.users.find(user=>user.username.toLowerCase()===username.toLowerCase());
|
||||
const [salt,hash]=(user?.passwordHash || '00000000000000000000000000000000:'+ '0'.repeat(64)).split(':');
|
||||
const actual=crypto.scryptSync(password,salt,32);
|
||||
if(!crypto.timingSafeEqual(actual,Buffer.from(hash,'hex')) || !user)return null;
|
||||
return safeUser(user);
|
||||
}
|
||||
recordJob(record) {
|
||||
return this.commit(draft=>{
|
||||
const index=draft.jobs.findIndex(job=>job.jobId===record.jobId);
|
||||
const prior=index>=0?draft.jobs[index]:{createdAt:new Date().toISOString()};
|
||||
const saved={...prior,...copy(record),updatedAt:new Date().toISOString()};
|
||||
if(index>=0)draft.jobs.splice(index,1);draft.jobs.unshift(saved);draft.jobs=draft.jobs.slice(0,5000);
|
||||
if(record.status!=='running')this.addAudit(draft,'浏览器导出',record.status==='success'?'PDF 导出完成':'PDF 导出失败',record.filename || record.error || '');
|
||||
return saved;
|
||||
});
|
||||
}
|
||||
jobs(){return copy(this.data.jobs);}
|
||||
audit(){return copy(this.data.audit);}
|
||||
recoverJobs() {
|
||||
if(!this.data.jobs.some(job=>job.status==='running'))return;
|
||||
this.commit(draft=>{for(const job of draft.jobs)if(job.status==='running'){job.status='interrupted';job.error='服务重启,请核对输出文件后重新导出。';}return null;});
|
||||
}
|
||||
}
|
||||
module.exports={Store};
|
||||
Reference in new issue
Block a user