const PRINT_TITLE = "销售标签"; const escapeHtml = (value) => String(value ?? "") .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); const splitByPage = (items, pageSize) => { const list = Array.isArray(items) ? items : []; if (list.length === 0) return [[]]; const pages = []; for (let i = 0; i < list.length; i += pageSize) { pages.push(list.slice(i, i + pageSize)); } return pages; }; const buildMainSpec = (item) => { const value = item?.specificationModel ?? item?.productSpecModel ?? item?.productName ?? ""; return String(value ?? "").trim(); }; const buildDetailSpec = (item) => { const value = item?.specification ?? item?.spec ?? item?.sizeSpec ?? item?.productSpec ?? ""; return String(value ?? "").trim(); }; const renderLabelCard = (item) => `
${escapeHtml(item?.customerName)}
${escapeHtml(item?.salesContractNo)}
${escapeHtml(buildMainSpec(item))}
${escapeHtml(buildDetailSpec(item))}
${escapeHtml(item?.floorCode)}
`; export const printSalesLabel = (rawList = []) => { const list = Array.isArray(rawList) ? rawList : []; if (!list.length) { throw new Error("标签数据为空,无法打印"); } const pageSize = 6; // 2 列 * 3 行(50x40mm) const pages = splitByPage(list, pageSize); const printWindow = window.open("", "_blank", "width=1200,height=900"); if (!printWindow) { throw new Error("浏览器拦截了弹窗,请允许弹窗后重试"); } const html = ` ${PRINT_TITLE} ${pages .map( (pageList) => `
${pageList.map((item) => renderLabelCard(item)).join("")}
` ) .join("")} `; printWindow.document.write(html); printWindow.document.close(); printWindow.onload = () => { setTimeout(() => { printWindow.focus(); printWindow.print(); printWindow.close(); }, 300); }; };