feat: collect BPM field evidence and equip ZCode mapping assistant

This commit is contained in:
abaishu0730 committed 2026-10-20 10:40:52 +08:00
1 parent 24dbbb74c3
commit d4070e08f9
30 files changed
+453 -61

No files matched your search

+28 -9
View File
@@ -1,7 +1,7 @@
'use strict';
const {BpmTodoClient}=require('./todo-client.cjs');
const {loadPlaywright,browserOptions}=require('../../platform/browser.cjs');
const {safeEntry,loadForm,loadData}=require('./order-form.cjs');
const {safeEntry,historyURL,loadForm,loadData}=require('./order-form.cjs');
const fail=(message,status=409)=>Object.assign(new Error(message),{status});
function detailURL(value,allowLocal=false){
let url;try{url=new URL(value);}catch{throw fail('待办没有可读取的表单地址;请核验对应门户表单接入。');}
@@ -12,6 +12,11 @@ function detailURL(value,allowLocal=false){
class BpmOrderProvider{
constructor({manager,client=new BpmTodoClient(),browserFactory,allowLocal=false}={}){this.manager=manager;this.client=client;this.browserFactory=browserFactory||(()=>loadPlaywright().chromium.launch(browserOptions()));this.allowLocal=allowLocal;this.entries=new Map();this.checks={liveTodo:false,pdfExport:false,message:'尚未完成真实待办与表单采集检查。'};}
capabilities(){return {...this.checks};}
async scanHistory(connection,principal,names){
const rows=await this.saved(connection,principal,state=>new (require('./history-client.cjs').BpmHistoryClient)().scan(state,names));
this.historyEntries||=new Map();this.historyEntries.set(connection.id,new Map(rows.map(row=>[row.taskId,{url:row.detailUrl,row,at:Date.now()}])));
return rows.map(row=>({...row,detailUrl:safeEntry(row.detailUrl,{history:true}).target.href}));
}
async saved(connection,principal,work){return this.manager.run(connection?.id,async()=>{
this.manager.authorize(connection,principal);if(this.manager.sessions.has(connection.id))throw fail('此连接正在登录窗口中使用。');
const saved=await this.manager.vault().read(connection.id);if(!saved?.storageState)throw fail('BPM 会话未保存,请前往 BPM 连接完成登录核验。',401);
@@ -24,9 +29,11 @@ class BpmOrderProvider{
this.checks={...this.checks,liveTodo:true,checkedAt:new Date().toISOString(),message:'真实待办接口已读取;各类别表单仍需字段与归档现场核验。'};return safe;}
catch(error){this.checks={...this.checks,liveTodo:false,message:error.message};throw error;}
}
async capture(connection,principal,row){
captureFields(connection,principal,row){return this.capture(connection,principal,row,{fieldsOnly:true});}
async capture(connection,principal,row,{fieldsOnly=false}={}){
const history=row.source==='history';if(history&&!fieldsOnly)throw fail('历史样本仅用于字段配置,不能导出或执行。',403);
return this.saved(connection,principal,async storageState=>{
let entry=this.entries.get(connection.id)?.get(row.taskId);
let entry=(history?this.historyEntries:this.entries)?.get(connection.id)?.get(row.taskId);
const sameTask=candidate=>['taskId','instanceId','processName','nodeName','processVersion','revision'].every(key=>String(candidate[key]??'')===String(row[key]??''));
if(entry&&!sameTask(entry.row))throw fail('任务元数据已变化,请重新扫描核验原节点与版本。');
if((!entry||Date.now()-entry.at>5*60*1000)&&row.detailUrl?.includes('/YZSoft/Forms/Process.aspx')){
@@ -34,7 +41,7 @@ class BpmOrderProvider{
if(!fresh)throw fail('此任务已不在原节点或版本,请重新扫描核验。');entry={url:fresh.detailUrl,row:fresh,at:Date.now()};
}
const request=await loadPlaywright().request.newContext({storageState,timeout:20000});let loaded,tables;
try{loaded=await loadForm(request,entry?.url||row.detailUrl,value=>detailURL(value,this.allowLocal));tables=await loadData(request,loaded.url);}finally{await request.dispose();}
try{loaded=await loadForm(request,entry?.url||row.detailUrl,history?historyURL:value=>detailURL(value,this.allowLocal),{history});tables=await loadData(request,loaded.url,{history});}finally{await request.dispose();}
const url=loaded.url;
const browser=await this.browserFactory();try{
// Server-rendered V4 form only. No page scripts, approval requests or redirects.
@@ -46,7 +53,7 @@ class BpmOrderProvider{
const page=await context.newPage();let response;
try{response=await page.goto(url.href,{waitUntil:'domcontentloaded',timeout:20000});}catch{throw fail('BPM 只读表单解析失败。',502);}
if(!response||response.status()!==200||page.url()!==url.href)throw fail('BPM 表单读取失败或登录已失效,请续登后重试。',401);
const snapshot=await page.evaluate(tables=>{
const snapshot=await page.evaluate(({tables,fieldsOnly})=>{
const copy=document.documentElement.cloneNode(true),fields=[],seen=new Map();
const hydrated=new Map(),resolved=new Map(),bindings=[...document.querySelectorAll('[xdatabind]')],detailTables=new Set();
const blocked=()=>({blocker:'明细多行表单的行数、字段或表格关系不明确,暂不支持此快照;请核验表单接入后重试。'});
@@ -59,6 +66,7 @@ class BpmOrderProvider{
const list=hydrated.get(key)||[];list.push(item);hydrated.set(key,list);
}
if(!bound.length)continue;
if(fieldsOnly)continue;
if(table.Rows.length>1){
// Only a complete repeated row schema in one table section proves the
// source Rows order maps to DOM rows. Never expand a single template or
@@ -79,6 +87,17 @@ class BpmOrderProvider{
for(const el of bound){const values=hydrated.get(el.getAttribute('xdatabind').toLowerCase());if(values?.length===1)resolved.set(el,values[0]);}
}
}
if(fieldsOnly){
// Field evidence comes from actual server Rows; DOM layout is not a PDF
// proof. Index every detail row without assigning it to a template row.
for(const el of bindings){
const key=el.getAttribute('xdatabind'),items=hydrated.get(key?.toLowerCase());
if(!items)continue;
const label=el.getAttribute('aria-label')||el.closest('td')?.previousElementSibling?.textContent?.trim()||key;
for(const item of items)if(!seen.has(item.path)){seen.set(item.path,item.value);fields.push({...item,label:label.slice(0,200)});}
}
return {purpose:'field-mapping',fields};
}
for(const el of bindings){
const key=el.getAttribute('xdatabind');if(!key||key.length>250)continue;
const item=resolved.get(el),label=el.getAttribute('aria-label')||el.closest('td')?.previousElementSibling?.textContent?.trim()||key;
@@ -100,16 +119,16 @@ class BpmOrderProvider{
}
copy.querySelectorAll('script,iframe,object,embed,button,[printout="false"],#taskCommentForm').forEach(el=>el.remove());
return {html:copy.outerHTML,styles:Array.from(document.querySelectorAll('style')).map(e=>e.textContent),images:[],fields:fields.filter(f=>seen.get(f.path)!==null).filter((f,i,a)=>a.findIndex(x=>x.path===f.path)===i)};
},tables);
},{tables,fieldsOnly});
if(snapshot.blocker)throw fail(snapshot.blocker);
if(snapshot.html.length>12000000||snapshot.fields.length>4000)throw fail('表单超出采集上限。');
if((snapshot.html?.length||0)>12000000||snapshot.fields.length>4000)throw fail('表单超出采集上限。');
if(!snapshot.fields.some(f=>f.value!==''))throw fail('只读表单未取得实际字段值,需核验表单加载接口;禁止使用空表单继续执行。');
snapshot.sourceUrl=url.href;snapshot.processVersion=String(row.processVersion||'');
if(!snapshot.processVersion)throw fail('待办未提供流程版本,需要核验版本来源。');
const sample={...row,connectionId:connection.id,fields:Object.fromEntries(snapshot.fields.map(f=>[f.path,f.value])),brief:row.title};
this.checks={...this.checks,pdfExport:true,capturedAt:new Date().toISOString()};return {sample,snapshot};
if(!fieldsOnly)this.checks={...this.checks,pdfExport:true,capturedAt:new Date().toISOString()};return {sample,snapshot};
}finally{await browser.close();}
}).catch(error=>{this.checks={...this.checks,pdfExport:false,message:error.message};throw error;});
}).catch(error=>{if(!fieldsOnly)this.checks={...this.checks,pdfExport:false,message:error.message};throw error;});
}
}
module.exports={BpmOrderProvider,detailURL};