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; 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 ? '' : ''; return ` ${escapeHtml(docTitle)} ${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 `
${opts.showSkuName ? `
${escapeHtml(label.skuName)}
` : ''} ${opts.showBarcode ? `
${barcodeSvg}
` : ''} ${meta ? `
${escapeHtml(meta)}
` : ''}
`; }) .join('')} ${autoPrintScript} `; } export function openBarcodePrintWindow( labels: SkuBarcodeLabel[], options?: { title?: string; printOptions?: Partial } ) { 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, '"'); }