打印状态移至备注后并在点击打印时自动记录
This commit is contained in:
1 parent
846a30cded
commit
c12f2157b4
6 files changed
+80
-40
No files matched your search
+36
-19
@@ -53,27 +53,44 @@
|
||||
window.addEventListener('beforeprint',()=>{printing=true;try{preparePrint();document.body.classList.remove('fit-error');}catch{failed();}});
|
||||
window.addEventListener('afterprint',()=>{printing=false;fit();});
|
||||
printMedia.addEventListener('change',event=>{if(!event.matches){printing=false;fit();}});
|
||||
button.addEventListener('click',async()=>{await ready();if(fit())window.print();});
|
||||
button.addEventListener('click',async()=>{await ready();if(fit()){window.recordDocumentPrint?.();window.print();}});
|
||||
})();
|
||||
|
||||
// 浏览器不能确认纸张是否输出,必须由操作人确认,不监听 afterprint 写入。
|
||||
// 以点击页面“打印”为已打印依据;仅预览、加载或关闭系统打印窗口不写状态。
|
||||
(()=>{
|
||||
const label=document.getElementById('document-print-state'),button=document.getElementById('confirm-document-print'),error=document.getElementById('document-print-error');
|
||||
if(!label?.dataset?.state||!button)return;
|
||||
let state=JSON.parse(label.dataset.state),pending=null;
|
||||
const render=()=>{label.textContent=state.printed?'✓ 已打印':'未打印(未登记)';button.textContent=state.printed?'改为未打印':'确认已打印';};
|
||||
button.addEventListener('click',async()=>{
|
||||
button.disabled=true;error.textContent='';
|
||||
const url='/api/print-status/'+state.kind+'/'+state.id;
|
||||
if(!pending)pending={key:crypto.randomUUID(),body:JSON.stringify({printed:!state.printed,revision:state.revision,sourceRevision:state.sourceRevision})};
|
||||
const label=document.getElementById('document-print-state'),retry=document.getElementById('retry-document-print'),error=document.getElementById('document-print-error');
|
||||
if(!label?.dataset?.state||!retry||!error)return;
|
||||
let state=JSON.parse(label.dataset.state),pending=null,saving=false;
|
||||
const url='/api/print-status/'+state.kind+'/'+state.id;
|
||||
function render(){label.textContent=state.printed?'✓ 已打印':'未打印';}
|
||||
function notify(){try{localStorage.setItem('ledger-print-status-changed',JSON.stringify({user:state.user,kind:state.kind,id:state.id,time:Date.now()}));}catch{}}
|
||||
async function save(){
|
||||
if(saving)return;
|
||||
if(state.printed&&!pending){notify();return;}
|
||||
saving=true;retry.hidden=true;retry.disabled=true;error.textContent='正在保存打印记录…';
|
||||
if(!pending)pending={key:crypto.randomUUID(),body:JSON.stringify({printed:true,revision:state.revision,sourceRevision:state.sourceRevision})};
|
||||
try{
|
||||
const response=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json','X-Requested-With':'maimai-ledger','X-Ledger-User':state.user,'Idempotency-Key':pending.key},body:pending.body,signal:AbortSignal.timeout(15000)});
|
||||
const result=await response.json();
|
||||
if(!response.ok){if(response.status===409){pending=null;button.hidden=true;error.textContent=(result.error||'状态已变化')+',请刷新打印页。';return;}throw new Error(result.error||'保存失败,请重试');}
|
||||
state={...state,...result,kind:state.kind,id:state.id};pending=null;render();
|
||||
// 同源内部页面收到通知后只刷新状态,不触碰正在填写的表单。
|
||||
try{localStorage.setItem('ledger-print-status-changed',JSON.stringify({user:state.user,kind:state.kind,id:state.id,time:Date.now()}));}catch{}
|
||||
}catch(err){error.textContent=(err.name==='TimeoutError'?'网络超时':err.message)+';请重试确认,不会重复记账。';}
|
||||
finally{button.disabled=false;}
|
||||
});
|
||||
for(let attempt=0;attempt<2;attempt++){
|
||||
try{
|
||||
const response=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json','X-Requested-With':'maimai-ledger','X-Ledger-User':state.user,'Idempotency-Key':pending.key},body:pending.body,signal:AbortSignal.timeout(15000)});
|
||||
const result=await response.json();
|
||||
if(!response.ok){
|
||||
if(response.status===409){
|
||||
if(result.code!=='ACCOUNT_CHANGED'){
|
||||
const latestResponse=await fetch(url,{headers:{'X-Ledger-User':state.user},signal:AbortSignal.timeout(15000)});
|
||||
const latest=await latestResponse.json();
|
||||
if(latestResponse.ok&&latest.sourceRevision===state.sourceRevision&&latest.printed){state={...state,...latest};pending=null;render();notify();error.textContent='';return;}
|
||||
}
|
||||
pending=null;error.textContent=(result.error||'单据或账号已变化')+',请刷新打印页后重新打印。';return;
|
||||
}
|
||||
throw Object.assign(new Error(result.error||'保存失败'),{status:response.status});
|
||||
}
|
||||
state={...state,...result};pending=null;render();notify();error.textContent='';return;
|
||||
}catch(err){if(attempt===1||err.status&&err.status<500)throw err;}
|
||||
}
|
||||
}catch(err){error.textContent=(err.name==='TimeoutError'?'网络超时':err.message)+';打印记录尚未确认,请重试保存。';retry.hidden=false;}
|
||||
finally{saving=false;retry.disabled=false;}
|
||||
}
|
||||
window.recordDocumentPrint=save;
|
||||
retry.addEventListener('click',()=>{if(pending)save();});
|
||||
})();
|
||||
Reference in new issue
Block a user