来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
222 lines
6.4 KiB
TypeScript
222 lines
6.4 KiB
TypeScript
import JsBarcode from 'jsbarcode';
|
||
import type { BarcodePrintOptions } from './printTemplates';
|
||
|
||
/** 默认用 SKU 编码作为条码内容(Code128) */
|
||
export function defaultSkuBarcode(skuCode: string): string {
|
||
return String(skuCode ?? '').trim();
|
||
}
|
||
|
||
export function renderBarcodeSvg(value: string, options?: { width?: number; height?: number }): string {
|
||
const text = value.trim();
|
||
if (!text) return '';
|
||
|
||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||
JsBarcode(svg, text, {
|
||
format: 'CODE128',
|
||
displayValue: true,
|
||
fontSize: options?.height && options.height <= 28 ? 10 : 12,
|
||
height: options?.height ?? 40,
|
||
width: options?.width ?? 1.6,
|
||
margin: 4,
|
||
textMargin: 2,
|
||
});
|
||
return new XMLSerializer().serializeToString(svg);
|
||
}
|
||
|
||
export function renderBarcodeDataUrl(value: string, barcodeHeight = 40): string {
|
||
const canvas = document.createElement('canvas');
|
||
JsBarcode(canvas, value.trim(), {
|
||
format: 'CODE128',
|
||
displayValue: true,
|
||
fontSize: barcodeHeight <= 28 ? 10 : 12,
|
||
height: barcodeHeight,
|
||
width: 1.6,
|
||
margin: 4,
|
||
textMargin: 2,
|
||
});
|
||
return canvas.toDataURL('image/png');
|
||
}
|
||
|
||
export interface SkuBarcodeLabel {
|
||
skuCode: string;
|
||
skuName: string;
|
||
barcode: string;
|
||
spec?: string;
|
||
unit?: string;
|
||
/** 订单明细打印时展示数量 */
|
||
quantity?: number;
|
||
}
|
||
|
||
const DEFAULT_BARCODE_PRINT_OPTIONS: BarcodePrintOptions = {
|
||
pageWidthMm: 60,
|
||
pageHeightMm: 40,
|
||
marginMm: 2,
|
||
showSkuName: true,
|
||
showSkuCode: true,
|
||
showBarcode: true,
|
||
showQuantity: true,
|
||
showSpec: true,
|
||
showUnit: true,
|
||
};
|
||
|
||
function resolveBarcodeHeight(labelHeightMm: number): number {
|
||
if (labelHeightMm <= 30) return 22;
|
||
if (labelHeightMm <= 40) return 30;
|
||
if (labelHeightMm <= 50) return 38;
|
||
return 46;
|
||
}
|
||
|
||
function resolveNameFontSize(labelHeightMm: number): number {
|
||
if (labelHeightMm <= 30) return 8;
|
||
if (labelHeightMm <= 40) return 9;
|
||
if (labelHeightMm <= 50) return 10;
|
||
return 11;
|
||
}
|
||
|
||
function resolveMetaFontSize(labelHeightMm: number): number {
|
||
if (labelHeightMm <= 30) return 7;
|
||
if (labelHeightMm <= 40) return 8;
|
||
return 9;
|
||
}
|
||
|
||
export function buildBarcodePrintHtml(
|
||
labels: SkuBarcodeLabel[],
|
||
options?: { title?: string; printOptions?: Partial<BarcodePrintOptions>; autoPrint?: boolean }
|
||
): string {
|
||
if (!labels.length) {
|
||
throw new Error('没有可预览的条码标签');
|
||
}
|
||
|
||
const opts = { ...DEFAULT_BARCODE_PRINT_OPTIONS, ...options?.printOptions };
|
||
const pageW = opts.pageWidthMm;
|
||
const pageH = opts.pageHeightMm;
|
||
const margin = opts.marginMm ?? 2;
|
||
const innerW = pageW - margin * 2;
|
||
const innerH = pageH - margin * 2;
|
||
const docTitle = options?.title || '商品条码打印';
|
||
const barcodeHeight = resolveBarcodeHeight(pageH);
|
||
const nameFont = resolveNameFontSize(pageH);
|
||
const metaFont = resolveMetaFontSize(pageH);
|
||
const autoPrintScript =
|
||
options?.autoPrint === false ? '' : '<script>window.onload=function(){window.print();}</script>';
|
||
|
||
return `<!DOCTYPE html>
|
||
<html><head><meta charset="utf-8"/><title>${escapeHtml(docTitle)}</title>
|
||
<style>
|
||
@page { size: ${pageW}mm ${pageH}mm; margin: 0; }
|
||
* { box-sizing: border-box; }
|
||
html, body {
|
||
margin: 0; padding: 0;
|
||
width: ${pageW}mm;
|
||
font-family: "Microsoft YaHei", Arial, sans-serif;
|
||
}
|
||
.label-page {
|
||
width: ${pageW}mm;
|
||
height: ${pageH}mm;
|
||
padding: ${margin}mm;
|
||
page-break-after: always;
|
||
page-break-inside: avoid;
|
||
break-inside: avoid;
|
||
overflow: hidden;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
.label-page:last-child { page-break-after: auto; }
|
||
.label {
|
||
width: ${innerW}mm;
|
||
height: ${innerH}mm;
|
||
max-height: ${innerH}mm;
|
||
page-break-inside: avoid;
|
||
break-inside: avoid;
|
||
overflow: hidden;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
text-align: center;
|
||
gap: 0.8mm;
|
||
}
|
||
.name {
|
||
font-size: ${nameFont}px;
|
||
font-weight: 700;
|
||
line-height: 1.25;
|
||
max-width: 100%;
|
||
word-break: break-all;
|
||
display: -webkit-box;
|
||
-webkit-line-clamp: 2;
|
||
-webkit-box-orient: vertical;
|
||
overflow: hidden;
|
||
}
|
||
.meta {
|
||
font-size: ${metaFont}px;
|
||
color: #333;
|
||
line-height: 1.3;
|
||
max-width: 100%;
|
||
word-break: break-all;
|
||
}
|
||
img, .barcode-svg {
|
||
max-width: 100%;
|
||
max-height: ${Math.max(12, innerH * 0.45)}mm;
|
||
width: auto;
|
||
height: auto;
|
||
object-fit: contain;
|
||
page-break-inside: avoid;
|
||
break-inside: avoid;
|
||
}
|
||
@media print {
|
||
html, body { width: ${pageW}mm; }
|
||
.label-page { width: ${pageW}mm; height: ${pageH}mm; }
|
||
}
|
||
</style></head><body>
|
||
${labels
|
||
.map((label) => {
|
||
// 用矢量 SVG 而不是 canvas→PNG base64:大订单几百张标签时光栅编码会卡死主线程
|
||
const barcodeSvg = renderBarcodeSvg(label.barcode, { height: barcodeHeight });
|
||
const metaParts: string[] = [];
|
||
if (opts.showSkuCode) metaParts.push(label.skuCode);
|
||
if (opts.showQuantity && label.quantity != null && label.quantity > 0) {
|
||
const qtyText = Number.isInteger(label.quantity) ? String(label.quantity) : String(label.quantity);
|
||
metaParts.push(`数量 ${qtyText}${opts.showUnit && label.unit ? label.unit : ''}`);
|
||
} else if (opts.showSpec && label.spec) {
|
||
metaParts.push(label.spec);
|
||
} else if (opts.showUnit && label.unit) {
|
||
metaParts.push(label.unit);
|
||
}
|
||
if (opts.showSpec && label.spec && !metaParts.includes(label.spec)) {
|
||
metaParts.push(label.spec);
|
||
}
|
||
const meta = metaParts.join(' · ');
|
||
return `<div class="label-page"><div class="label">
|
||
${opts.showSkuName ? `<div class="name">${escapeHtml(label.skuName)}</div>` : ''}
|
||
${opts.showBarcode ? `<div class="barcode-svg">${barcodeSvg}</div>` : ''}
|
||
${meta ? `<div class="meta">${escapeHtml(meta)}</div>` : ''}
|
||
</div></div>`;
|
||
})
|
||
.join('')}
|
||
${autoPrintScript}
|
||
</body></html>`;
|
||
}
|
||
|
||
export function openBarcodePrintWindow(
|
||
labels: SkuBarcodeLabel[],
|
||
options?: { title?: string; printOptions?: Partial<BarcodePrintOptions> }
|
||
) {
|
||
if (!labels.length) return;
|
||
const html = buildBarcodePrintHtml(labels, { ...options, autoPrint: true });
|
||
const win = window.open('', '_blank', 'width=420,height=640');
|
||
if (!win) {
|
||
throw new Error('无法打开打印窗口,请允许弹窗');
|
||
}
|
||
win.document.write(html);
|
||
win.document.close();
|
||
}
|
||
|
||
function escapeHtml(text: string) {
|
||
return text
|
||
.replace(/&/g, '&')
|
||
.replace(/</g, '<')
|
||
.replace(/>/g, '>')
|
||
.replace(/"/g, '"');
|
||
}
|