feat(分拣): 标签打印改走蓝牙直连与浏览器打印
不再走 print_jobs → 安卓称重终端链路。小程序分拣面板每行恢复「打印」按钮,改为 BLE 热敏直连(复用 utils/printHistory,thermalPrint 新增 label 模式);Web 明细弹层每行加「打印标签」,走浏览器打印通道(复用 printHtmlViaHiddenIframe),打到 58/80mm 热敏或小票机。 标签字段沿用旧秤端:商品名/数量/单位/订单号/客户/日期/条码。旧链路与 PICKING_SCALE_TERMINAL_ENABLED 隐藏态不动;无后端改动、无迁移。
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
# 分拣标签打印:改蓝牙直连(小程序)+ 浏览器直连(Web)
|
||||
|
||||
> 立项 2026-10-13。开发者口径(原话):
|
||||
> ① 「当前小程序的打印还有 web 的打印这个解封 我们当前需要这个打印功能」;
|
||||
> ② 「小程序上的打印修改一下 变为蓝牙打印 相当于连接蓝牙打印机 直接打印或者就是 web 界面上他连接那种小票打印机打印
|
||||
> 而不是走我们的 之前的打印链路了」;
|
||||
> ③ 打印内容确认=**A 分拣标签**(就是这个商品的分拣标签,非整单小票);Web 通道确认=**浏览器打印**(打热敏/小票机)。
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
恢复分拣里的「打印标签」,但**换通道**:小程序改为 **BLE 热敏直连**(连蓝牙打印机直接打),
|
||||
Web 改为**浏览器打印**(当前页宿主 + `window.print()`,打到热敏/小票机)。
|
||||
**不再走**旧链路(`print_jobs` → 安卓称重终端 USB 标签机)。
|
||||
|
||||
## 1. 背景:为什么现在没有分拣打印
|
||||
|
||||
- 2026-10-12(`a5c953c`)开发者要求「小程序不需要打印」⇒ 小程序分拣面板**移除每行「打印」按钮**,
|
||||
并去掉确认时的自动下发;`fetchPickingWeighDevices` / `createPickingPrintJobs` 一并摘除
|
||||
(`server/tests/webPickingRemotePrint.test.ts` 有反向断言钉住)。
|
||||
- 2026-10-13(`docs/design/20261013-分拣完成改按钮触发与隐藏秤打印.md`)开发者要求隐藏分拣里的
|
||||
秤与打印 ⇒ Web 侧新增常量 `PICKING_SCALE_TERMINAL_ENABLED = false`(代码保留),隐藏了
|
||||
称重阈值 / 称重终端 / 标签打印 / 待打印任务 等 7 处。
|
||||
- 旧通道本体:`print_jobs` 表 + `weigh_devices` 心跳 + 扇出打印 → 安卓称重终端 `PrintJobPoller.java` USB 标签机。
|
||||
|
||||
⇒ 现在两条直连通道其实**都已具备**,只是没接到分拣界面:
|
||||
|
||||
| 侧 | 已有基建 | 分拣页现状(改前) |
|
||||
|---|---|---|
|
||||
| 小程序 | `miniprogram/utils/printHistory.js` + `blePrinter.js` + `thermalPrint.js`,「备货历史」「采购历史」在用 | 无打印按钮 |
|
||||
| Web | `src/utils/deliveryNotePrint.ts` 的 `printHtmlViaHiddenIframe`(配送单浏览器打印通道)、`thermalPrintService.ts`(QZ,本次**不用**) | 无标签打印入口 |
|
||||
|
||||
## 2. 方案要点
|
||||
|
||||
### 2.1 标签内容(沿用旧秤端标签字段)
|
||||
|
||||
每商品行一张标签,字段与旧 `print_jobs.payload` 对齐:
|
||||
|
||||
| 字段 | 来源(小程序) | 来源(Web) |
|
||||
|---|---|---|
|
||||
| 商品名 | `item.display_name` | `item.item_name` |
|
||||
| 数量 | 结算量(`settlement_qty`,无则 `required_qty`) | `settlementQtyOf(item.id, item)` |
|
||||
| 单位 | `item.unit` | `item.unit` |
|
||||
| 订单号 | `task.order_code_short \|\| task.order_code` | `task.order_code` |
|
||||
| 客户/公司 | `item.company_name \|\| task.company_name` | `task.company_name` |
|
||||
| 日期 | `task.delivery_date` | `task.delivery_date` |
|
||||
| 条码 | `item.barcode \|\| item.sku_code` | `item.barcode` |
|
||||
|
||||
> 条码本期以**文本**呈现(不做 Code128 光栅)。旧链路由秤端渲染条码图形,本次不迁移该能力。
|
||||
|
||||
### 2.2 小程序:BLE 热敏直连(每行一个「打印」按钮)
|
||||
|
||||
- `miniprogram/utils/thermalPrint.js` 增**标签模式**:`buildThermalBytes(component, { label })` 时画单张标签
|
||||
(商品名居中加粗 → 数量+单位 → 分隔线 → 订单号/客户/日期/条码若干行),无「合计」页脚。
|
||||
**默认(无 `label`)行为一字不改**,现网两个面板不受影响。
|
||||
- 新增 `miniprogram/utils/pickingLabelPrint.js`:`buildPickingLabel(task, item)` 组装字段;
|
||||
`printPickingLabel(component, task, item, pickDevice)` → `printThermal(...)`(热敏专用,不弹 A4 选项)。
|
||||
- `picking-panel`:恢复每行「打印」按钮(wxss 里 `.btn-print-row` 样式仍在,直接复用),
|
||||
挂 `printCurrentPick` → `printPickingLabel`;新增 `<canvas id="printCanvas">` + `<ble-printer-sheet id="blePicker">`,
|
||||
`pickBleDevice()` 复用 `ble-printer-sheet` 的 `pick()`(与 `preparation-panel` 同款)。
|
||||
- 复用既有 BLE 存储键 `canteen.blePrinter`(已在用),无需新绑定入口。
|
||||
|
||||
### 2.3 Web:浏览器打印(每行一个「打印标签」按钮)
|
||||
|
||||
- 新增 `src/utils/pickingLabelPrint.ts`:`buildPickingLabelHtml(labels, { paperWidthMm })` 生成
|
||||
`@page { size: 58mm auto }` 的标签页(多标签一签一页);`printPickingLabels(labels)` 调
|
||||
**既有** `printHtmlViaHiddenIframe(html)`(当前页宿主 + `window.print()`,与配送单通道同一机制,**不改该函数**)。
|
||||
- `PickingManagement.tsx`:新增常量 `PICKING_LABEL_PRINT_ENABLED = true`(**与冻结的 `PICKING_SCALE_TERMINAL_ENABLED`
|
||||
各管各**);明细弹层商品格内加「打印标签」小按钮,点击即打该行一张标签。
|
||||
- 文案**不新增 key**:复用既有 `picking.modal.printLabel`(`打印标签` / `Print labels` / `Cetak label`)。
|
||||
|
||||
## 3. 不动的(冻结面)
|
||||
|
||||
- **旧链路**:`print_jobs` / `weigh_devices` / `printJobService` / `mobilePickingPrintService` /
|
||||
`PICKING_SCALE_TERMINAL_ENABLED` 隐藏态 —— **保持现状,不恢复、不删除**。
|
||||
- **分拣冻结**(`picking-freeze`):不动 `confirmPickingComplete` 写订单量、不动状态链 / progress / complete 语义;
|
||||
打印不写库、不推进状态。
|
||||
- **配送单浏览器打印通道**(`delivery-note-print.mdc`):只是**复用** `printHtmlViaHiddenIframe`,**不改**该函数与契约。
|
||||
- 后端**零改动**:不新增接口、不改表、无迁移。
|
||||
- 小程序分拣面板**不引用** `fetchPickingWeighDevices` / `createPickingPrintJobs`(保持 `webPickingRemotePrint.test.ts` 反向断言成立)。
|
||||
|
||||
## 4. 改动面
|
||||
|
||||
| 文件 | 新增/改 | 意图 |
|
||||
|---|---|---|
|
||||
| `miniprogram/utils/thermalPrint.js` | 改(增量) | 增 `label` 模式(默认行为不变) |
|
||||
| `miniprogram/utils/pickingLabelPrint.js` | 新增 | 组装分拣标签 + 走 BLE 热敏 |
|
||||
| `miniprogram/components/picking-panel/index.js` | 改 | `printing` 态 + `printCurrentPick` / `pickBleDevice` |
|
||||
| `miniprogram/components/picking-panel/index.wxml` | 改 | 行内「打印」按钮 + canvas + ble-printer-sheet |
|
||||
| `miniprogram/components/picking-panel/index.wxss` | 改 | `.print-canvas`(复用既有 `.btn-print-row`) |
|
||||
| `miniprogram/components/picking-panel/index.json` | 改 | 注册 `ble-printer-sheet` |
|
||||
| `src/utils/pickingLabelPrint.ts` | 新增 | 生成标签 HTML + 走浏览器打印 |
|
||||
| `src/pages/picking/PickingManagement.tsx` | 改 | 常量 + 行内「打印标签」按钮 + handler |
|
||||
|
||||
## 5. 验收标准
|
||||
|
||||
- [ ] 小程序分拣面板每行出现「打印」按钮;点按能连已绑定 BLE 热敏机打出该商品标签(商品名/数量/单位/订单号/客户/日期/条码)。
|
||||
- [ ] 未绑定打印机时点「打印」弹出 `ble-printer-sheet` 选择设备并保存。
|
||||
- [ ] Web 分拣明细弹层每行出现「打印标签」;点按生成 58mm 标签页并弹浏览器打印对话框。
|
||||
- [ ] 多行连打 = 多页标签(一签一页)。
|
||||
- [ ] 小程序「备货历史 / 采购历史」打印**行为与产物不变**(`thermalPrint` 默认分支未改)。
|
||||
- [ ] 旧链路(`print_jobs`、`PICKING_SCALE_TERMINAL_ENABLED`)状态不变;`webPickingRemotePrint.test.ts` 仍绿。
|
||||
- [ ] 前端 `npm run typecheck` 通过;小程序 `node --check` 通过。
|
||||
- [ ] 不写库、不推进订单状态、无迁移。
|
||||
|
||||
## 6. 风险与回滚
|
||||
|
||||
| 风险 | 处置 |
|
||||
|---|---|
|
||||
| 小程序 `thermalPrint` 改动误伤现网两个面板 | 标签走独立 `label` 分支,默认分支零改;`node --check` + 代码审阅 |
|
||||
| 浏览器直接 `window.print()` 让用户误以为「没反应」 | 与配送单通道同表现(弹系统打印对话框),一致即可 |
|
||||
| 标签打在 A4 上(未在打印对话框选好小票机) | 属用户端设置;`@page size: 58mm auto` 已声明纸宽,选对打印机即可 |
|
||||
| 回滚 | 纯前端:还原上述 8 个文件即可;无迁移、无数据、无接口 |
|
||||
|
||||
## 7. 修订记录
|
||||
|
||||
| 版本 | 日期 | 变更 |
|
||||
|---|---|---|
|
||||
| v1 | 2026-10-13 | 分拣标签打印换通道:小程序 BLE 热敏 + Web 浏览器打印;不走旧 `print_jobs` 链路 |
|
||||
@@ -70,6 +70,7 @@ AI 提示词清理已停用条目(7 条归档不删、清单过滤 archived、
|
||||
主系统**骑手上门取货单**收口时**补扣库存**(改前:扣库点只有「装车核货/发车」,而这类单不发车、结案又 `skipInventorySettle` ⇒ **库存永不扣**;现改:`picked_up → 90` 的同一事务里按既有出库口径**软扣**一次 `delivery_loading_out`,直送/免入库/加菜行自动跳过、`orderHasOutboundOnClient` 幂等)见 [20261013-骑手上门取货单出库时点.md](./20261013-骑手上门取货单出库时点.md)。
|
||||
订单详情打开后**丢失「主系统单」标记**(`openOrderDetail` 直连 `orders.select('*')` 重建 `selectedOrder`,而 `is_main_system_order` 是列表接口的 `EXISTS` 别名、**不是表列** ⇒ 秒级窗口后被覆盖为 undefined ⇒ 主系统单偶发出现「添加」、「缺货」消失);改为**以列表行为基底、真实列用最新值覆盖**)见 [20261013-订单详情打开丢主系统标记.md](./20261013-订单详情打开丢主系统标记.md)。
|
||||
小程序分拣**「按区域」看不到区域**(`taskPickerLabel` 只处理 `category`/`sku`、`zone` 落到兜底显示**订单号** ⇒ 一个订单拆出的多张区域拣货单在选择器里长得一样;`tab_title` 那处改动在 wxml 里未渲染)见 [20261013-小程序分拣区域标签缺失.md](./20261013-小程序分拣区域标签缺失.md)。
|
||||
分拣**标签打印换通道**(不再走 `print_jobs` → 安卓称重终端 USB:小程序改 **BLE 热敏直连**、Web 改**浏览器打印**打热敏/小票机;标签字段沿用旧秤端;不清旧链路、无迁移、无后端改动)见 [20261013-分拣标签打印改蓝牙与浏览器直连.md](./20261013-分拣标签打印改蓝牙与浏览器直连.md)。
|
||||
|
||||
## 全量对齐与改造方案(2026-09-28)
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ const {
|
||||
const { buildUnitPicker, ensureUnitDictionary, getUnitDictionaryLabels, isWeightUnit } = require('../../utils/units');
|
||||
const { formatMpOrderCode, formatMpOrderCodeList } = require('../../utils/orderCodeDisplay');
|
||||
const { fmtLocalDate } = require('../../utils/purchaseToday');
|
||||
const { printPickingLabel } = require('../../utils/pickingLabelPrint');
|
||||
|
||||
const DATE_FIELD_OPTIONS = [
|
||||
{ label: '下单日', value: 'created_at' },
|
||||
@@ -206,6 +207,7 @@ Component({
|
||||
workMode: 'manual',
|
||||
loading: false,
|
||||
saving: false,
|
||||
printing: false,
|
||||
/** 出库拍照(可选,商品级):按拣货明细 id 存「已上传到 delivery-photos 桶的对象路径 + 本地临时图预览」 */
|
||||
rowPhotos: {},
|
||||
photoUploadingId: '',
|
||||
@@ -922,6 +924,37 @@ Component({
|
||||
});
|
||||
},
|
||||
|
||||
pickBleDevice() {
|
||||
const sheet = this.selectComponent('#blePicker');
|
||||
if (!sheet || typeof sheet.pick !== 'function') {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
return sheet.pick();
|
||||
},
|
||||
|
||||
/** 打印当前商品行的一张分拣标签(BLE 热敏直连;不走旧 print_jobs / 称重终端链路) */
|
||||
async printCurrentPick(e) {
|
||||
if (this.data.printing) return;
|
||||
const id = e.currentTarget.dataset.id;
|
||||
const item = this.findItemById(id);
|
||||
if (!item) {
|
||||
wx.showToast({ title: '未找到该商品行', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
this.setData({ printing: true });
|
||||
try {
|
||||
await printPickingLabel(this, this.data.activeTask || {}, item, () => this.pickBleDevice());
|
||||
wx.showToast({ title: '已发送到打印机', icon: 'success' });
|
||||
} catch (err) {
|
||||
wx.hideLoading();
|
||||
if (err && err.message !== '已取消') {
|
||||
wx.showToast({ title: err.message || '打印失败', icon: 'none' });
|
||||
}
|
||||
} finally {
|
||||
this.setData({ printing: false });
|
||||
}
|
||||
},
|
||||
|
||||
buildConfirmRows(list) {
|
||||
const rows = [];
|
||||
for (const target of list) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"usingComponents": {
|
||||
"van-icon": "@vant/weapp/icon/index",
|
||||
"van-empty": "@vant/weapp/empty/index",
|
||||
"van-loading": "@vant/weapp/loading/index"
|
||||
"van-loading": "@vant/weapp/loading/index",
|
||||
"ble-printer-sheet": "/components/ble-printer-sheet/index"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,6 +119,12 @@
|
||||
<block wx:elif="{{item.line_pick_status >= 30 && !item.can_pick}}">
|
||||
<text class="qty-meta">已拣 {{item.picked_qty_num || 0}}{{item.unit || ''}}</text>
|
||||
</block>
|
||||
<view
|
||||
class="btn-print-row {{saving || printing ? 'is-disabled' : ''}}"
|
||||
data-pick="{{item.pick_code || activePickCode}}"
|
||||
data-id="{{item.id}}"
|
||||
catchtap="printCurrentPick"
|
||||
>{{printing ? '打印中' : '打印'}}</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{batchesMap[item.id]}}" class="batch-line">
|
||||
@@ -213,4 +219,7 @@
|
||||
</view>
|
||||
</view>
|
||||
</root-portal>
|
||||
|
||||
<canvas type="2d" id="printCanvas" class="print-canvas"></canvas>
|
||||
<ble-printer-sheet id="blePicker" />
|
||||
</view>
|
||||
|
||||
@@ -615,6 +615,17 @@
|
||||
border: 2rpx solid #059669;
|
||||
}
|
||||
|
||||
.print-canvas {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
.btn-stage {
|
||||
background: #fff;
|
||||
color: #059669;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 分拣标签打印(小程序 · BLE 热敏直连)。
|
||||
* 设计:docs/design/20261013-分拣标签打印改蓝牙与浏览器直连.md
|
||||
* 不走旧链路(print_jobs / 称重终端);只组装字段并复用 utils/printHistory 的热敏通道。
|
||||
*/
|
||||
const { printThermal } = require('./printHistory');
|
||||
const { displayText } = require('./pickingAssembly');
|
||||
|
||||
function formatQty(value) {
|
||||
const num = Number(value);
|
||||
if (!Number.isFinite(num)) return '';
|
||||
return String(Math.round(num * 1000) / 1000);
|
||||
}
|
||||
|
||||
/** 结算量优先(已确认实拣),否则落回需求量 */
|
||||
function labelQty(item) {
|
||||
const settlement = Number(item && item.settlement_qty);
|
||||
if (Number.isFinite(settlement) && settlement > 0) return settlement;
|
||||
return Number(item && item.required_qty) || 0;
|
||||
}
|
||||
|
||||
/** 组装单张分拣标签(字段沿用旧秤端标签:商品名/数量/单位/订单号/客户/日期/条码) */
|
||||
function buildPickingLabel(task, item) {
|
||||
const orderCode =
|
||||
displayText(task && (task.order_code_short || task.order_code)) ||
|
||||
displayText(item && item.order_code);
|
||||
return {
|
||||
itemName: displayText(item && item.display_name, displayText(item && item.item_name)),
|
||||
qty: formatQty(labelQty(item)),
|
||||
unit: displayText(item && item.unit),
|
||||
orderCode,
|
||||
companyName: displayText(item && item.company_name, displayText(task && task.company_name)),
|
||||
date: displayText(task && task.delivery_date),
|
||||
barcode: displayText(item && (item.barcode || item.sku_code)),
|
||||
};
|
||||
}
|
||||
|
||||
/** 打印一张分拣标签;pickDevice 用于首次绑定 BLE 打印机 */
|
||||
async function printPickingLabel(component, task, item, pickDevice) {
|
||||
const label = buildPickingLabel(task, item);
|
||||
await printThermal(component, { label }, pickDevice);
|
||||
return label;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildPickingLabel,
|
||||
printPickingLabel,
|
||||
};
|
||||
@@ -73,6 +73,40 @@ 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);
|
||||
@@ -109,17 +143,27 @@ function drawThermal(ctx, w, h, options) {
|
||||
}
|
||||
|
||||
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 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');
|
||||
drawThermal(ctx, T_W, height, options);
|
||||
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);
|
||||
|
||||
@@ -60,6 +60,7 @@ import {
|
||||
type PickingTaskRow,
|
||||
} from '../../utils/picking';
|
||||
import { buildRoleLabelResolver } from '../../utils/roleLabel';
|
||||
import { printPickingLabels } from '../../utils/pickingLabelPrint';
|
||||
import type { WeighMatchToleranceMode, WeighDeviceRow, PrintJobRow, PickingOutboundBatches } from '../../utils/workflowApi';
|
||||
import { useCategoryRows } from '../../hooks/useCategoryRows';
|
||||
import type { DateRangeValue } from '../../components/ui/DateRangeFilterButton';
|
||||
@@ -130,6 +131,13 @@ const PRINT_COPIES_STORAGE_KEY = 'picking-remote-print-copies';
|
||||
*/
|
||||
const PICKING_SCALE_TERMINAL_ENABLED: boolean = false;
|
||||
|
||||
/**
|
||||
* 分拣**标签打印新通道**(2026-10-13 开发者要求):小程序 BLE 热敏 + Web 浏览器打印,
|
||||
* **不走**旧 `print_jobs` → 安卓称重终端链路。与上面的 `PICKING_SCALE_TERMINAL_ENABLED` 各管各。
|
||||
* 设计:docs/design/20261013-分拣标签打印改蓝牙与浏览器直连.md
|
||||
*/
|
||||
const PICKING_LABEL_PRINT_ENABLED: boolean = true;
|
||||
|
||||
// 待分拣用琥珀,勿用主题 neutral(晨曦主题下会偏绿,像已完成)
|
||||
const STATUS_COLOR: Record<number, string> = {
|
||||
10: 'bg-amber-100 text-amber-800 dark:bg-amber-950/60 dark:text-amber-300',
|
||||
@@ -1582,6 +1590,28 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
}
|
||||
};
|
||||
|
||||
/** 打印该商品行的一张分拣标签(浏览器打印 → 热敏/小票机) */
|
||||
const handlePrintItemLabel = async (item: PickingItemRow) => {
|
||||
if (!selectedTask) return;
|
||||
try {
|
||||
await printPickingLabels([
|
||||
{
|
||||
itemName: item.item_name || t('picking.productFallback'),
|
||||
qty: formatQty(settlementQtyOf(item.id, item)),
|
||||
unit: item.unit || '',
|
||||
orderCode: selectedTask.order_code || '',
|
||||
companyName: selectedTask.company_name || '',
|
||||
date: selectedTask.delivery_date
|
||||
? formatPickingTaskCalendarDay(selectedTask.delivery_date)
|
||||
: '',
|
||||
barcode: item.barcode || '',
|
||||
},
|
||||
]);
|
||||
} catch (error: unknown) {
|
||||
notify.error(error instanceof Error ? error.message : t('picking.toast.printFail'));
|
||||
}
|
||||
};
|
||||
|
||||
const pendingPickCount = useMemo(() => {
|
||||
if (!taskItems.length) return 0;
|
||||
return taskItems.filter((item) => {
|
||||
@@ -2742,6 +2772,16 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
<td key={col.id} className="px-4 py-3">
|
||||
<div className="app-product-name">{item.item_name || t('picking.productFallback')}</div>
|
||||
<div className="text-xs text-gray-500">{item.barcode || t('picking.modal.noBarcode')}</div>
|
||||
{PICKING_LABEL_PRINT_ENABLED ? (
|
||||
<button
|
||||
type="button"
|
||||
className="mt-1 inline-flex items-center gap-1 text-xs text-emerald-700 hover:underline"
|
||||
onClick={() => void handlePrintItemLabel(item)}
|
||||
>
|
||||
<Printer className="w-3 h-3" />
|
||||
{t('picking.modal.printLabel')}
|
||||
</button>
|
||||
) : null}
|
||||
{PICKING_SCALE_TERMINAL_ENABLED && job ? (
|
||||
<div
|
||||
className={`text-xs mt-1 ${
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { buildPickingLabelHtml } from './pickingLabelPrint';
|
||||
|
||||
describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
|
||||
it('默认 58mm 纸宽,一签一分区', () => {
|
||||
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: '10', unit: '斤' }]);
|
||||
expect(html).toContain('@page { size: 58mm auto; margin: 0; }');
|
||||
expect(html).toContain('class="pick-label"');
|
||||
});
|
||||
|
||||
it('可切 80mm 纸宽', () => {
|
||||
const html = buildPickingLabelHtml([{ itemName: 'a', qty: '1' }], { paperWidthMm: 80 });
|
||||
expect(html).toContain('@page { size: 80mm auto; margin: 0; }');
|
||||
});
|
||||
|
||||
it('多张标签 = 多页(一签一页)', () => {
|
||||
const html = buildPickingLabelHtml([
|
||||
{ itemName: 'a', qty: '1' },
|
||||
{ itemName: 'b', qty: '2' },
|
||||
]);
|
||||
expect((html.match(/class="pick-label"/g) || []).length).toBe(2);
|
||||
});
|
||||
|
||||
it('元字段按有值才输出,且 HTML 转义', () => {
|
||||
const html = buildPickingLabelHtml([
|
||||
{ itemName: '<b>x</b>', qty: '1', barcode: 'a&b' },
|
||||
]);
|
||||
expect(html).not.toContain('<b>x</b>');
|
||||
expect(html).toContain('<b>');
|
||||
expect(html).toContain('a&b');
|
||||
// 无值字段不输出对应行
|
||||
expect(html).not.toContain('订单号');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* 分拣标签打印(Web · 浏览器打印)。
|
||||
* 设计:docs/design/20261013-分拣标签打印改蓝牙与浏览器直连.md
|
||||
* 复用配送单浏览器打印通道(当前页宿主 + window.print()),**不改**该通道本身。
|
||||
*/
|
||||
import { printHtmlViaHiddenIframe } from './deliveryNotePrint';
|
||||
|
||||
export interface PickingLabel {
|
||||
itemName: string;
|
||||
qty: string;
|
||||
unit?: string | null;
|
||||
orderCode?: string | null;
|
||||
companyName?: string | null;
|
||||
date?: string | null;
|
||||
barcode?: string | null;
|
||||
}
|
||||
|
||||
function esc(value: unknown): string {
|
||||
return String(value == null ? '' : value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"');
|
||||
}
|
||||
|
||||
/** 生成 58/80mm 热敏标签页;多张标签 = 多页(一签一页)。 */
|
||||
export function buildPickingLabelHtml(
|
||||
labels: PickingLabel[],
|
||||
options?: { paperWidthMm?: 58 | 80 }
|
||||
): string {
|
||||
const width = options?.paperWidthMm === 80 ? 80 : 58;
|
||||
const pages = (labels || [])
|
||||
.map((label) => {
|
||||
const meta: Array<[string, string]> = [];
|
||||
if (label.orderCode) meta.push(['订单号', String(label.orderCode)]);
|
||||
if (label.companyName) meta.push(['客户', String(label.companyName)]);
|
||||
if (label.date) meta.push(['日期', String(label.date)]);
|
||||
if (label.barcode) meta.push(['条码', String(label.barcode)]);
|
||||
const metaHtml = meta
|
||||
.map(
|
||||
([key, value]) =>
|
||||
`<div class="row"><span class="k">${esc(key)}</span><span class="v">${esc(value)}</span></div>`
|
||||
)
|
||||
.join('');
|
||||
return `<section class="pick-label"><div class="name">${esc(
|
||||
label.itemName || '分拣标签'
|
||||
)}</div><div class="qty">${esc(label.qty || '')}${esc(label.unit || '')}</div><div class="hr"></div>${metaHtml}</section>`;
|
||||
})
|
||||
.join('');
|
||||
return `<!doctype html><html><head><meta charset="utf-8"><title>分拣标签</title><style>
|
||||
@page { size: ${width}mm auto; margin: 0; }
|
||||
html, body { margin: 0; padding: 0; }
|
||||
.pick-label { width: ${width}mm; box-sizing: border-box; padding: 3mm; color: #111; font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; page-break-after: always; }
|
||||
.pick-label:last-child { page-break-after: auto; }
|
||||
.pick-label .name { font-size: 15px; font-weight: 700; text-align: center; word-break: break-all; }
|
||||
.pick-label .qty { font-size: 20px; font-weight: 700; text-align: center; margin-top: 4px; }
|
||||
.pick-label .hr { border-top: 1px solid #111; margin: 8px 0; }
|
||||
.pick-label .row { display: flex; font-size: 12px; line-height: 1.5; }
|
||||
.pick-label .row .k { width: 3.6em; flex: none; color: #333; }
|
||||
.pick-label .row .v { flex: 1; word-break: break-all; }
|
||||
</style></head><body>${pages}</body></html>`;
|
||||
}
|
||||
|
||||
export async function printPickingLabels(
|
||||
labels: PickingLabel[],
|
||||
options?: { paperWidthMm?: 58 | 80 }
|
||||
): Promise<void> {
|
||||
const list = (labels || []).filter(Boolean);
|
||||
if (!list.length) return;
|
||||
await printHtmlViaHiddenIframe(buildPickingLabelHtml(list, options));
|
||||
}
|
||||
Reference in New Issue
Block a user