2026-10-17 19:50:24 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
(() => {
|
|
|
|
|
|
const button = document.getElementById('print'),
|
|
|
|
|
|
frame = document.querySelector('.print-frame'),
|
|
|
|
|
|
content = document.querySelector('.content'),
|
|
|
|
|
|
status = document.getElementById('fit-status');
|
2026-10-22 12:32:44 +08:00
|
|
|
|
const canvas = document.querySelector('.print-canvas'),
|
|
|
|
|
|
foreignObject = canvas.querySelector('foreignObject'),
|
|
|
|
|
|
printMedia = window.matchMedia('print');
|
|
|
|
|
|
let printing = false;
|
2026-10-22 12:59:56 +08:00
|
|
|
|
// 打印副本保留原始文字与表格。SVG 始终适配固定241×140mm内的内容框,不依赖 beforeprint 的屏幕尺寸或异步 resize 时序。
|
2026-10-22 12:32:44 +08:00
|
|
|
|
function preparePrint() {
|
|
|
|
|
|
const widthMm = Number(frame.dataset.layoutWidthMm),
|
|
|
|
|
|
heightMm = Number(frame.dataset.layoutHeightMm);
|
|
|
|
|
|
if (!(widthMm > 0 && heightMm > 0)) throw new Error('页面尺寸未就绪');
|
|
|
|
|
|
const measure = document.createElement('div'),
|
|
|
|
|
|
copy = content.cloneNode(true);
|
|
|
|
|
|
Object.assign(measure.style, {
|
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
|
left: '0',
|
|
|
|
|
|
top: '0',
|
|
|
|
|
|
visibility: 'hidden',
|
|
|
|
|
|
pointerEvents: 'none',
|
|
|
|
|
|
width: widthMm + 'mm',
|
|
|
|
|
|
height: heightMm + 'mm',
|
|
|
|
|
|
});
|
|
|
|
|
|
copy.style.transform = 'none';
|
|
|
|
|
|
copy.style.width = '100%';
|
|
|
|
|
|
copy.classList.remove('compact');
|
|
|
|
|
|
copy.removeAttribute('data-scale');
|
|
|
|
|
|
measure.appendChild(copy);
|
|
|
|
|
|
document.body.appendChild(measure);
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (copy.scrollHeight > measure.clientHeight) copy.classList.add('compact');
|
|
|
|
|
|
const width = Math.max(measure.clientWidth, copy.scrollWidth, copy.offsetWidth);
|
|
|
|
|
|
copy.style.width = width + 'px';
|
|
|
|
|
|
const height = Math.ceil(Math.max(copy.scrollHeight, copy.offsetHeight, copy.getBoundingClientRect().height)) + 1;
|
|
|
|
|
|
if (!(width > 0 && height > 1)) throw new Error('页面尺寸未就绪');
|
|
|
|
|
|
canvas.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
|
|
|
|
|
foreignObject.setAttribute('width', String(width));
|
|
|
|
|
|
foreignObject.setAttribute('height', String(height));
|
|
|
|
|
|
foreignObject.replaceChildren(copy);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
measure.remove();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-22 12:32:44 +08:00
|
|
|
|
function failed() {
|
|
|
|
|
|
document.body.classList.add('fit-error');
|
|
|
|
|
|
button.disabled = true;
|
|
|
|
|
|
status.textContent = '页面暂未排好,请刷新后再打印。';
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2026-10-22 13:51:39 +08:00
|
|
|
|
// 屏幕与打印统一使用模板的放大字号和紧凑留白;仍完整保留全部商品、附加项和尾部内容。
|
2026-10-17 19:50:24 +08:00
|
|
|
|
function fit() {
|
2026-10-22 12:32:44 +08:00
|
|
|
|
if (printing || printMedia.matches) return true;
|
2026-10-17 19:50:24 +08:00
|
|
|
|
try {
|
|
|
|
|
|
content.style.transform = 'none';
|
|
|
|
|
|
content.style.width = '100%';
|
|
|
|
|
|
content.classList.remove('compact');
|
|
|
|
|
|
if (content.scrollHeight > frame.clientHeight) content.classList.add('compact');
|
|
|
|
|
|
if (!content.scrollHeight || !frame.clientWidth || !frame.clientHeight) throw new Error('页面尺寸未就绪');
|
|
|
|
|
|
const availableWidth = frame.clientWidth - 1,
|
|
|
|
|
|
availableHeight = frame.clientHeight - 1;
|
|
|
|
|
|
const measure = scale => {
|
|
|
|
|
|
content.style.width = availableWidth / scale + 'px';
|
|
|
|
|
|
return (
|
|
|
|
|
|
Math.max(content.scrollHeight, content.offsetHeight) * scale <= availableHeight &&
|
|
|
|
|
|
Math.max(content.scrollWidth, content.offsetWidth) * scale <= availableWidth + 1
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
let scale = 1;
|
|
|
|
|
|
if (!measure(1)) {
|
|
|
|
|
|
let low = 0.0001,
|
|
|
|
|
|
high = 1;
|
|
|
|
|
|
if (!measure(low)) throw new Error('内容超出纸张可用范围');
|
|
|
|
|
|
for (let i = 0; i < 18; i++) {
|
|
|
|
|
|
const mid = (low + high) / 2;
|
|
|
|
|
|
if (measure(mid)) low = mid;
|
|
|
|
|
|
else high = mid;
|
|
|
|
|
|
}
|
|
|
|
|
|
scale = low;
|
|
|
|
|
|
measure(scale);
|
|
|
|
|
|
}
|
|
|
|
|
|
content.style.transform = 'scale(' + scale + ')';
|
|
|
|
|
|
content.dataset.scale = String(scale);
|
2026-10-22 12:32:44 +08:00
|
|
|
|
preparePrint();
|
|
|
|
|
|
document.body.classList.remove('fit-error');
|
|
|
|
|
|
button.disabled = false;
|
2026-10-22 12:59:56 +08:00
|
|
|
|
status.textContent = scale < 0.65 ? '内容较多,字号已缩小,请先查看打印预览。' : '已按241×140mm横版排为一页。';
|
2026-10-17 19:50:24 +08:00
|
|
|
|
return true;
|
2026-10-22 12:32:44 +08:00
|
|
|
|
} catch {
|
|
|
|
|
|
return failed();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-17 19:50:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
const ready = () => document.fonts?.ready || Promise.resolve();
|
2026-10-22 12:32:44 +08:00
|
|
|
|
ready().then(fit);
|
|
|
|
|
|
window.addEventListener('load', fit);
|
|
|
|
|
|
window.addEventListener('resize', fit);
|
|
|
|
|
|
window.addEventListener('beforeprint', () => {
|
|
|
|
|
|
printing = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
preparePrint();
|
|
|
|
|
|
document.body.classList.remove('fit-error');
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
failed();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-22 12:32:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
window.addEventListener('afterprint', () => {
|
|
|
|
|
|
printing = false;
|
|
|
|
|
|
fit();
|
|
|
|
|
|
});
|
|
|
|
|
|
printMedia.addEventListener('change', event => {
|
|
|
|
|
|
if (!event.matches) {
|
2026-12-04 03:20:49 +08:00
|
|
|
|
printing = false;
|
|
|
|
|
|
fit();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
});
|
|
|
|
|
|
button.addEventListener('click', async () => {
|
|
|
|
|
|
await ready();
|
|
|
|
|
|
if (fit()) {
|
|
|
|
|
|
window.recordDocumentPrint?.();
|
|
|
|
|
|
window.print();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-10-17 19:50:24 +08:00
|
|
|
|
})();
|
2026-12-03 17:46:23 +08:00
|
|
|
|
|
2026-12-04 03:20:49 +08:00
|
|
|
|
// 以点击页面“打印”为已打印依据;仅预览、加载或关闭系统打印窗口不写状态。
|
2026-12-03 17:46:23 +08:00
|
|
|
|
(() => {
|
2026-12-04 03:20:49 +08:00
|
|
|
|
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 {}
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
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 }),
|
|
|
|
|
|
};
|
2026-12-03 17:46:23 +08:00
|
|
|
|
try {
|
2026-12-04 03:20:49 +08:00
|
|
|
|
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 || '单据或账号已变化') + ',请刷新打印页后重新打印。';
|
2026-12-04 08:58:53 -08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
throw Object.assign(new Error(result.error || '保存失败'), { status: response.status });
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
state = { ...state, ...result };
|
|
|
|
|
|
pending = null;
|
|
|
|
|
|
render();
|
2026-12-04 08:58:53 -08:00
|
|
|
|
notify();
|
2026-12-04 03:20:49 +08:00
|
|
|
|
error.textContent = '';
|
|
|
|
|
|
return;
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
if (attempt === 1 || (err.status && err.status < 500)) throw err;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
error.textContent = (err.name === 'TimeoutError' ? '网络超时' : err.message) + ';打印记录尚未确认,请重试保存。';
|
|
|
|
|
|
retry.hidden = false;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
saving = false;
|
|
|
|
|
|
retry.disabled = false;
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-12-04 03:20:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
window.recordDocumentPrint = save;
|
|
|
|
|
|
retry.addEventListener('click', () => {
|
|
|
|
|
|
if (pending) save();
|
|
|
|
|
|
});
|
2026-12-03 17:46:23 +08:00
|
|
|
|
})();
|