fix(bpm): preserve detail row identity in captured snapshots

This commit is contained in:
abaishu0730 committed 2026-10-19 13:58:57 +08:00
1 parent cc1ade2261
commit 0a366f43d6
4 files changed
+70 -12

No files matched your search

+31 -8
View File
@@ -48,37 +48,60 @@ class BpmOrderProvider{
if(!response||response.status()!==200||page.url()!==url.href)throw fail('BPM 表单读取失败或登录已失效,请续登后重试。',401);
const snapshot=await page.evaluate(tables=>{
const copy=document.documentElement.cloneNode(true),fields=[],seen=new Map();
const hydrated=new Map();
const hydrated=new Map(),resolved=new Map(),bindings=[...document.querySelectorAll('[xdatabind]')],detailTables=new Set();
const blocked=()=>({blocker:'明细多行表单的行数、字段或表格关系不明确,暂不支持此快照;请核验表单接入后重试。'});
for(const table of tables){
const prefix=(table.DataSource?table.DataSource+':':'')+table.TableName;
const prefixKey=prefix.toLowerCase(),bound=bindings.filter(el=>{const key=el.getAttribute('xdatabind')?.toLowerCase();return key?.startsWith(prefixKey+'.')||key?.startsWith(prefixKey+'[');});
for(const [index,row]of table.Rows.entries())for(const [column,value]of Object.entries(row)){
if(value!==null&&!['string','number','boolean'].includes(typeof value))continue;
const path=prefix+'.'+column,key=path.toLowerCase(),item={path:table.Rows.length===1?path:prefix+'['+index+'].'+column,value:value===null?'':String(value)};
const list=hydrated.get(key)||[];list.push(item);hydrated.set(key,list);
}
if(!bound.length)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
// flatten a column into every occurrence. Other layouts need an adapter.
const rows=new Map(),keys=new Set(bound.map(el=>el.getAttribute('xdatabind').toLowerCase()));let container,section;
for(const el of bound){
const row=el.closest('tr'),host=el.closest('table'),cell=el.closest('td,th'),key=el.getAttribute('xdatabind').toLowerCase(),values=hydrated.get(key);
if(!row||!host||row.closest('table')!==host||!cell||cell.rowSpan!==1||el.parentElement?.closest('[xdatabind]')||!values||values.length!==table.Rows.length)return blocked();
if(container&&(container!==host||section!==row.parentElement))return blocked();container=host;section=row.parentElement;
const columns=rows.get(row)||new Map();if(columns.has(key))return blocked();columns.set(key,el);rows.set(row,columns);
}
if(rows.size!==table.Rows.length||detailTables.has(container))return blocked();detailTables.add(container);
for(const [index,columns]of [...rows.values()].entries()){
if(columns.size!==keys.size||[...keys].some(key=>!columns.has(key)))return blocked();
for(const [key,el]of columns)resolved.set(el,hydrated.get(key)[index]);
}
}else{
for(const el of bound){const values=hydrated.get(el.getAttribute('xdatabind').toLowerCase());if(values?.length===1)resolved.set(el,values[0]);}
}
}
for(const el of document.querySelectorAll('[xdatabind]')){
for(const el of bindings){
const key=el.getAttribute('xdatabind');if(!key||key.length>250)continue;
const values=hydrated.get(key.toLowerCase()),label=el.getAttribute('aria-label')||el.closest('td')?.previousElementSibling?.textContent?.trim()||key;
if(values){for(const item of values){if(!seen.has(item.path)){seen.set(item.path,item.value);fields.push({...item,label:label.slice(0,200)});}}continue;}
const item=resolved.get(el),label=el.getAttribute('aria-label')||el.closest('td')?.previousElementSibling?.textContent?.trim()||key;
if(item){if(!seen.has(item.path)){seen.set(item.path,item.value);fields.push({...item,label:label.slice(0,200)});}continue;}
if(['radio','checkbox'].includes(el.type)&&!el.checked)continue;
const value='value'in el?el.value:el.textContent.trim();
if(seen.has(key)&&seen.get(key)!==value){seen.set(key,null);continue;}seen.set(key,value);fields.push({path:key,label:label.slice(0,200),value});
}
const originals=[...document.querySelectorAll('input,textarea,select')];
for(const el of copy.querySelectorAll('[xdatabind]')){
for(const [index,el]of [...copy.querySelectorAll('[xdatabind]')].entries()){
if(el.matches('input,textarea,select'))continue;
const values=hydrated.get(el.getAttribute('xdatabind')?.toLowerCase());if(values)el.textContent=values.map(v=>v.value).join('\n');
const item=resolved.get(bindings[index]);if(item)el.textContent=item.value;
}
for(const [index,el]of [...copy.querySelectorAll('input,textarea,select')].entries()){
const original=originals[index],span=document.createElement('span');
const values=hydrated.get(original?.getAttribute('xdatabind')?.toLowerCase());
span.textContent=values?['radio','checkbox'].includes(original.type)?values.some(v=>v.value===original.value)?'☑':'☐':values.map(v=>v.value).join('\n'):original?['radio','checkbox'].includes(original.type)?original.checked?'☑':'☐':original.tagName==='SELECT'?original.selectedOptions?.[0]?.textContent||original.value:original.value||'':'';
const item=resolved.get(original);
span.textContent=item?['radio','checkbox'].includes(original.type)?item.value===original.value?'☑':'☐':item.value:original?['radio','checkbox'].includes(original.type)?original.checked?'☑':'☐':original.tagName==='SELECT'?original.selectedOptions?.[0]?.textContent||original.value:original.value||'':'';
if(original?.type==='hidden')span.hidden=true;el.replaceWith(span);
}
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);
if(snapshot.blocker)throw fail(snapshot.blocker);
if(snapshot.html.length>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||'');