不再走 print_jobs → 安卓称重终端链路。小程序分拣面板每行恢复「打印」按钮,改为 BLE 热敏直连(复用 utils/printHistory,thermalPrint 新增 label 模式);Web 明细弹层每行加「打印标签」,走浏览器打印通道(复用 printHtmlViaHiddenIframe),打到 58/80mm 热敏或小票机。 标签字段沿用旧秤端:商品名/数量/单位/订单号/客户/日期/条码。旧链路与 PICKING_SCALE_TERMINAL_ENABLED 隐藏态不动;无后端改动、无迁移。
181 lines
5.3 KiB
JavaScript
181 lines
5.3 KiB
JavaScript
const { getCanvasNode } = require('./a4HistoryPrint');
|
|
|
|
const T_W = 384;
|
|
const MAX_HEIGHT = 2400;
|
|
const MARGIN = 12;
|
|
const LINE = 28;
|
|
|
|
function concatBytes(parts) {
|
|
let len = 0;
|
|
for (let i = 0; i < parts.length; i += 1) len += parts[i].length;
|
|
const out = new Uint8Array(len);
|
|
let offset = 0;
|
|
for (let i = 0; i < parts.length; i += 1) {
|
|
out.set(parts[i], offset);
|
|
offset += parts[i].length;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function initEscpos() {
|
|
return Uint8Array.of(0x1b, 0x40);
|
|
}
|
|
|
|
function feedAndCut() {
|
|
return Uint8Array.of(0x1d, 0x56, 0x00);
|
|
}
|
|
|
|
function gsRaster(width, height, bitmap) {
|
|
const xL = width & 0xff;
|
|
const xH = (width >> 8) & 0xff;
|
|
const yL = height & 0xff;
|
|
const yH = (height >> 8) & 0xff;
|
|
const header = Uint8Array.of(0x1d, 0x76, 0x30, 0x00, xL, xH, yL, yH);
|
|
return concatBytes([header, bitmap]);
|
|
}
|
|
|
|
function imageDataToRaster(imageData) {
|
|
const { width, height, data } = imageData;
|
|
const bytesPerRow = Math.ceil(width / 8);
|
|
const out = new Uint8Array(bytesPerRow * height);
|
|
for (let y = 0; y < height; y += 1) {
|
|
for (let x = 0; x < width; x += 1) {
|
|
const i = (y * width + x) * 4;
|
|
const lum = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
|
|
if (lum < 160) {
|
|
out[y * bytesPerRow + (x >> 3)] |= 0x80 >> (x & 7);
|
|
}
|
|
}
|
|
}
|
|
return { width: bytesPerRow, height, bitmap: out };
|
|
}
|
|
|
|
function ellipsize(ctx, text, maxWidth) {
|
|
const value = String(text || '');
|
|
if (ctx.measureText(value).width <= maxWidth) return value;
|
|
let s = value;
|
|
while (s.length && ctx.measureText(`${s}…`).width > maxWidth) s = s.slice(0, -1);
|
|
return s ? `${s}…` : '';
|
|
}
|
|
|
|
function sumAmounts(rows) {
|
|
let total = 0;
|
|
let count = 0;
|
|
for (const row of rows || []) {
|
|
const amount = Number(row[5]);
|
|
if (Number.isFinite(amount)) total += amount;
|
|
count += 1;
|
|
}
|
|
return { count, total: Math.round(total * 100) / 100 };
|
|
}
|
|
|
|
function neededHeight(rowCount) {
|
|
return Math.min(MAX_HEIGHT, 90 + rowCount * LINE + 50);
|
|
}
|
|
|
|
const LABEL_HEIGHT = 300;
|
|
|
|
/** 单张分拣标签:商品名(居中)→ 数量+单位 → 分隔线 → 订单号/客户/日期/条码;无「合计」页脚。 */
|
|
function drawLabel(ctx, w, h, label) {
|
|
ctx.fillStyle = '#fff';
|
|
ctx.fillRect(0, 0, w, h);
|
|
ctx.fillStyle = '#000';
|
|
ctx.textBaseline = 'top';
|
|
|
|
ctx.textAlign = 'center';
|
|
ctx.font = 'bold 26px sans-serif';
|
|
ctx.fillText(ellipsize(ctx, label.itemName || '分拣标签', w - MARGIN * 2), w / 2, 14);
|
|
ctx.font = 'bold 34px sans-serif';
|
|
ctx.fillText(`${label.qty || ''}${label.unit || ''}`, w / 2, 54);
|
|
|
|
ctx.beginPath();
|
|
ctx.moveTo(MARGIN, 104);
|
|
ctx.lineTo(w - MARGIN, 104);
|
|
ctx.stroke();
|
|
|
|
ctx.textAlign = 'left';
|
|
ctx.font = '18px sans-serif';
|
|
const lines = [];
|
|
if (label.orderCode) lines.push(`订单号 ${label.orderCode}`);
|
|
if (label.companyName) lines.push(`客户 ${label.companyName}`);
|
|
if (label.date) lines.push(`日期 ${label.date}`);
|
|
if (label.barcode) lines.push(`条码 ${label.barcode}`);
|
|
let y = 116;
|
|
for (let i = 0; i < lines.length; i += 1) {
|
|
ctx.fillText(ellipsize(ctx, lines[i], w - MARGIN * 2), MARGIN, y);
|
|
y += 26;
|
|
}
|
|
}
|
|
|
|
function drawThermal(ctx, w, h, options) {
|
|
ctx.fillStyle = '#fff';
|
|
ctx.fillRect(0, 0, w, h);
|
|
ctx.fillStyle = '#000';
|
|
ctx.textBaseline = 'top';
|
|
ctx.textAlign = 'center';
|
|
ctx.font = 'bold 22px sans-serif';
|
|
ctx.fillText(options.title || '鲜配达', w / 2, 8);
|
|
ctx.font = '16px sans-serif';
|
|
ctx.fillText(options.subtitle || '', w / 2, 36);
|
|
ctx.textAlign = 'left';
|
|
ctx.beginPath();
|
|
ctx.moveTo(MARGIN, 62);
|
|
ctx.lineTo(w - MARGIN, 62);
|
|
ctx.stroke();
|
|
let y = 70;
|
|
ctx.font = '16px sans-serif';
|
|
for (const row of options.rows || []) {
|
|
const name = `${row[0] || ''} ${row[1] || ''}`.trim();
|
|
ctx.fillText(ellipsize(ctx, name, w - MARGIN * 2), MARGIN, y);
|
|
y += 18;
|
|
const line2 = `${row[2] || ''} ${row[3] || ''} ${row[4] || ''} ${row[5] || ''}`;
|
|
ctx.fillText(ellipsize(ctx, line2, w - MARGIN * 2), MARGIN, y);
|
|
y += LINE - 18;
|
|
if (y > h - 48) break;
|
|
}
|
|
ctx.beginPath();
|
|
ctx.moveTo(MARGIN, h - 40);
|
|
ctx.lineTo(w - MARGIN, h - 40);
|
|
ctx.stroke();
|
|
ctx.font = 'bold 16px sans-serif';
|
|
const { count, total } = sumAmounts(options.rows);
|
|
ctx.fillText(`合计 ${count} 项 ¥${total}`, MARGIN, h - 32);
|
|
}
|
|
|
|
async function buildThermalBytes(component, options) {
|
|
const label = options.label;
|
|
let height;
|
|
if (label) {
|
|
height = LABEL_HEIGHT;
|
|
} else {
|
|
const rows = options.rows || [];
|
|
if (!rows.length) throw new Error(options.emptyText || '没有可打印的数据');
|
|
height = neededHeight(rows.length);
|
|
if (90 + rows.length * LINE + 50 > MAX_HEIGHT) {
|
|
throw new Error('行数过多,请缩小日期范围后再打印');
|
|
}
|
|
}
|
|
const canvas = await getCanvasNode(component, options.selector || '#printCanvas');
|
|
canvas.width = T_W;
|
|
canvas.height = height;
|
|
const ctx = canvas.getContext('2d');
|
|
if (label) {
|
|
drawLabel(ctx, T_W, height, label);
|
|
} else {
|
|
drawThermal(ctx, T_W, height, options);
|
|
}
|
|
let imageData;
|
|
try {
|
|
imageData = ctx.getImageData(0, 0, T_W, height);
|
|
} catch {
|
|
throw new Error('当前基础库无法生成热敏小票,请改用 A4 打印');
|
|
}
|
|
const raster = imageDataToRaster(imageData);
|
|
return concatBytes([initEscpos(), gsRaster(raster.width, raster.height, raster.bitmap), feedAndCut()]);
|
|
}
|
|
|
|
module.exports = {
|
|
buildThermalBytes,
|
|
T_W,
|
|
};
|