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 renderLabelCard = (item) => `
${escapeHtml(item?.customerName)}
${escapeHtml(item?.salesContractNo)}
${escapeHtml(item?.productName)}
${escapeHtml(item?.specification)}
${escapeHtml(item?.floorCode)}
`;
export const printSalesLabel = (rawList = []) => {
const list = Array.isArray(rawList) ? rawList : [];
if (!list.length) {
throw new Error("标签数据为空,无法打印");
}
const pageSize = 18; // 3 列 * 6 行(50x40mm)
const pages = splitByPage(list, pageSize);
const printWindow = window.open("", "_blank", "width=1200,height=900");
if (!printWindow) {
throw new Error("浏览器拦截了弹窗,请允许弹窗后重试");
}
const html = `
${pageList.map((item) => renderLabelCard(item)).join("")}
`
)
.join("")}
`;
printWindow.document.write(html);
printWindow.document.close();
printWindow.onload = () => {
setTimeout(() => {
printWindow.focus();
printWindow.print();
printWindow.close();
}, 300);
};
};