Files
SupplierSystem/miniprogram/utils/thermalPrint.js
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

137 lines
4.0 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);
}
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 rows = options.rows || [];
if (!rows.length) throw new Error(options.emptyText || '没有可打印的数据');
const 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');
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,
};