修复分拣标签二维码使用订单号的问题

分拣标签二维码改用服务端提交回包的 container_code,不再使用 order_code;取不到容器码时不打印,避免生成配送小程序无法识别的标签。\n\n同时修正标签日期/时间等字段折行、空单位与市斤的重量口径、TSPL/ZPL 文本清洗,并补充 v1.0.64 回归用例。
This commit is contained in:
雨天
2026-11-09 15:11:18 +08:00
parent e38ae2b88c
commit 50caf99993
9 changed files with 163 additions and 31 deletions
+38 -7
View File
@@ -1984,6 +1984,12 @@ class FlowEngine:
except Exception:
pass
container_code = self._container_code_of(resp)
if self.station == 'picking':
if container_code:
self.emit('log', DEV + '分拣容器码:%s' % container_code)
else:
self.emit('log', DEV + '分拣回包没有容器码(container_code),'
'本次标签将不打印')
if container_code:
# 落盘留档:结果不明/重试时还能从 journal 取回容器码重打标签
self.journal.update(op, containerCode=container_code)
@@ -2663,7 +2669,7 @@ class FlowEngine:
这里递归找一遍。找不到就返回空串,绝不拿订单号顶替。
"""
keys = ('container_code', 'containerCode', 'container_no',
'containerNo')
'containerNo', 'container')
seen = set()
def walk(node, depth=0):
@@ -2683,8 +2689,19 @@ class FlowEngine:
seen.add(oid)
for k in keys:
v = node.get(k)
if v not in (None, ''):
return str(v).strip()
if isinstance(v, (str, int)):
txt = str(v).strip()
if txt and txt != '0':
return txt
elif k == 'container' and isinstance(v, dict):
# 少数接口把容器写成 {"container": {"code": "..."}}
for ck in ('code', 'container_code', 'containerCode',
'no', 'container_no'):
cv = v.get(ck)
if isinstance(cv, (str, int)):
txt = str(cv).strip()
if txt and txt != '0':
return txt
for v in node.values():
if isinstance(v, (dict, list)):
got = walk(v, depth + 1)
@@ -2706,7 +2723,7 @@ class FlowEngine:
兜底只用于临时救急,日志会明确写出“扫码可能失败”。
"""
code = str(container_code or '').strip()
if code:
if code and code != '0':
return code, 'container_code'
code = self._container_code_of(row or {})
if code:
@@ -2753,7 +2770,8 @@ class FlowEngine:
self.emit('log', msg)
self.emit('print_result', (False, msg))
return
if op and op in self._printed_ops:
printed_ops = getattr(self, '_printed_ops', None)
if op and printed_ops is not None and op in printed_ops:
self.emit('log', DEV + '操作号 %s 已打印过标签,跳过重复打印' % op)
return
qr, qr_src = '', ''
@@ -2789,8 +2807,8 @@ class FlowEngine:
self._print_q.put({'row': dict(row), 'business': float(business),
'kg': float(kg or 0.0), 'station': self.station,
'qr': qr, 'qr_source': qr_src, 'op': str(op or '')})
if op:
self._printed_ops.add(op)
if op and printed_ops is not None:
printed_ops.add(op)
self.emit('log', '标签已排队(%s)' % name)
def _print_worker(self):
@@ -2851,6 +2869,19 @@ class FlowEngine:
self.emit('log', '标签按秤上实际重量印:%.2f %s(记账 %.2f %s)'
% (qty, unit, float(business or 0.0), unit))
try:
try:
if float(qty or 0.0) <= 0:
msg = ('标签数量为 0,本次未打印:%s'
% (row.get('item_name') or ''))
self.emit('log', msg)
self.emit('print_result', (False, msg))
return
except (TypeError, ValueError):
msg = ('标签数量无效,本次未打印:%s'
% (row.get('item_name') or ''))
self.emit('log', msg)
self.emit('print_result', (False, msg))
return
if station == 'picking':
if not qr:
msg = ('标签缺少容器码,本次未打印:%s'
+25 -11
View File
@@ -238,7 +238,7 @@ def plan_label(cfg, title, fields, qr_data=''):
# 三档退让,优先保版面整齐:
# ① 全部单行
# ② 只让品名折两行
# ③ 都被逼到墙角了才允许所有字段折行
# ③ 都被逼到墙角了才允许客户/供应商也折(日期/时间/重量/数量/订单号不折)
# 为什么不让所有字段随时可折:32 号字会把「客户: 张三水果批发店」劈成
# 「客户: 张三水果」+「批发店」两行,比小一号字难看得多。
avail_h = h - my * 2
@@ -348,6 +348,8 @@ def _page_dots(cfg):
def build_picking_label_tspl(cfg, customer, item_name, weight, unit, date_str,
order_code=''):
# 参数名保留 order_code 兼容旧调用;v1.0.64 起传的是服务端容器码
# (container_code),二维码只能用这个,不能用订单号。
p = plan_label(cfg, '分拣标签',
['客户: %s' % customer,
'品名: %s' % item_name,
@@ -369,16 +371,27 @@ def build_inspection_label_tspl(cfg, order_code, item_name, weight, unit,
def build_zpl(cfg, title, fields, qr_data=None):
w, h = _page_dots(cfg)
out = ['^XA^CI28^PW%d^LL%d^LH0,0' % (w, h),
'^FO20,20^A0N,36,36^FD%s^FS' % _zpl_text(title)]
y = 80
for line in fields:
out.append('^FO20,%d^A0N,34,34^FD%s^FS' % (y, _zpl_text(line)))
y += 55
if qr_data:
out.append('^FO%d,20^BQN,2,4^FDQA,%s^FS'
% (max(w - 130, 20), _zpl_text(qr_data)))
"""ZPL 也走同一份 plan_label 版面(v1.0.64)
以前 ZPL 是单独一套写死的 36/34 号字、55 点行距、二维码固定在
w-130:50×50 以外的纸型会溢出/裁字,而且和 TSPL 预览对不上。
现在复用 TSPL 的自适应结果(折行、原子串不拆、二维码位置),
只把 TEXT/QRCODE 换成 ZPL 的 ^FO/^A0N/^BQN。
"""
p = plan_label(cfg, title, fields, qr_data or '')
g = p['geom']
out = ['^XA^CI28^PW%d^LL%d^LH0,0' % (g['w'], g['h'])]
for ln in p['lines']:
out.append('^FO%d,%d^A0N,%d,%d^FD%s^FS'
% (ln['x'], ln['y'], ln['font'], ln['font'],
_zpl_text(ln['text'])))
if p['qr']:
q = p['qr']
# ZPL 的 ^BQN 第三个参数是放大倍数(1~10),正好对应 cell
mag = max(1, min(10, int(q['cell'])))
out.append('^FO%d,%d^BQN,2,%d^FDQA,%s^FS'
% (q['x'], q['y'], mag,
_zpl_text(p.get('qr_data') or '')))
out.append('^XZ')
return ''.join(out).encode('utf-8')
@@ -479,6 +492,7 @@ class LabelPrinter:
return dict(self.cfg)
def print_picking(self, customer, item_name, weight, unit, order_code=''):
# order_code 为兼容旧调用保留的名字;调用方实际传容器码
date_str = datetime.datetime.now().strftime('%m-%d %H:%M')
if self.cfg.get('type') == 'zpl':
data = build_zpl(self.cfg, '分拣标签',
+1 -1
View File
@@ -1,6 +1,6 @@
; ============================================================
; 入库分拣工作站 · 安装包脚本(Inno Setup 6)
; 编译:iscc installer.iss → dist\入库分拣工作站_安装程序_v1.0.63.exe
; 编译:iscc installer.iss → dist\入库分拣工作站_安装程序_v1.0.64.exe
; 说明:源目录为 build_exe.py 产出的 dist\入库分拣工作站(onedir)
; ============================================================
+2 -2
View File
@@ -121,7 +121,7 @@ class FakeApi:
eng.journal = FakeJournal()
eng.api = FakeApi()
eng._running = True
eng._print_async = lambda r, q, kg=None: None
eng._print_async = lambda r, q, kg=None, **k: None
eng._record = lambda *a, **k: None
eng._set_hold = lambda *a, **k: None
eng._set_state = lambda s: setattr(eng, 'state', s)
@@ -168,7 +168,7 @@ eng.state = 'SUBMIT'
eng._plan = [(ROWS[1], 1.5), (ROWS[2], 10.0)]
started = []
eng._start_submit = lambda r, q, s: started.append((r['id'], q, s))
eng._print_async = lambda r, q, kg=None: None
eng._print_async = lambda r, q, kg=None, **k: None
eng._record = lambda *a, **k: None
eng._set_hold = lambda *a, **k: None
eng._frozen_kg = 14.5
+2 -2
View File
@@ -108,7 +108,7 @@ eng.refresh_pending = lambda initial=False: None
eng.journal = FakeJournal()
eng.api = FakeApi()
eng._running = True
eng._print_async = lambda r, q: None
eng._print_async = lambda r, q, **k: None
eng._record = lambda *a, **k: None
print('=== 1. 独立入库查重:同 SKU + 同重量 → 拒绝 ===')
@@ -143,7 +143,7 @@ eng2.refresh_pending = lambda initial=False: None
eng2.journal = FakeJournal()
eng2.api = FakeApi()
eng2._running = True
eng2._print_async = lambda r, q: None
eng2._print_async = lambda r, q, **k: None
eng2._record = lambda *a, **k: None
eng2.rows = [row('r6', '藕', 13, indep=True, sku='SKU-OU'),
row('r8', '大白菜', 30, indep=True, sku='SKU-BC')]
+1 -1
View File
@@ -70,7 +70,7 @@ eng._running = True
eng.station = 'picking'
eng.rows = []
eng._start_submit = lambda row, q, src: submits.append((row, q, src))
eng._print_async = lambda row, biz, kg=None: prints.append(
eng._print_async = lambda row, biz, kg=None, **k: prints.append(
(row.get('item_name'), float(biz)))
eng._learn_sample = lambda *a, **k: None
eng._abort_cycle = lambda msg: aborts.append(msg)
+66
View File
@@ -88,6 +88,9 @@ check('根节点 container_code',
check('data.result.container_code',
e._container_code_of(
{'data': {'result': {'container_code': 'CT-N'}}}) == 'CT-N')
check('container.code',
e._container_code_of(
{'data': {'container': {'code': 'CT-C'}}}) == 'CT-C')
check('行上的 container_code',
e._container_code_of(ROW) == 'CT20261108000123')
@@ -139,6 +142,19 @@ text = data.decode('gbk', 'replace')
check('QRCODE 里是容器码', 'QRCODE' in text and 'CT20261108000123' in text, text)
check('没有把订单号打进二维码', 'ORD_WEB' not in text, text)
print('=== 6b. ZPL 也走同一份版面与容器码 ===')
zpl = LB.build_zpl(
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
'分拣标签',
['客户: 李府家宴', '品名: 大葱', '重量: 35.22 斤', '日期: 11-06 10:00'],
'CT20261108000123').decode('utf-8')
check('ZPL 里有容器码', 'CT20261108000123' in zpl, zpl)
check('ZPL 里没有订单号', 'ORD_WEB' not in zpl, zpl)
check('ZPL 日期没被拆开',
'日期: 11-06 10:00' in zpl
and '^FD0:00^FS' not in zpl
and '11-06 1^FS' not in zpl, zpl)
print('=== 7. 50×50mm 带二维码:日期必须单行 ===')
p = LB.plan_label(
{'width_mm': 50, 'height_mm': 50, 'dpi': 203, 'gap_mm': 2},
@@ -176,6 +192,56 @@ check('队列里只有 1 张', e._print_q.qsize() == 1)
check('第二次跳过有日志',
any('已打印过标签' in x for x in logs()), logs())
print('=== 11. 全链路:提交回包 → 打印队列 ===')
class FakeJournal:
def __init__(self):
self.rec = {}
def begin(self, op, kind, target, qty, weight, unit):
self.rec[op] = {'operationId': op}
return self.rec[op]
def update(self, op, **kw):
self.rec.setdefault(op, {}).update(kw)
return True
def conclude(self, op, state, msg=''):
self.rec.setdefault(op, {})['state'] = state
return True
def has_unresolved(self):
return False
class FakeApi:
user_id = 'u'
base_url = 'http://x'
def commit_picking(self, row, qty, weight, op, photo_expected):
return {'operation_status': 'committed_by_this_operation',
'client_operation_id': op,
'data': {'pick_status': 20,
'container_code': 'CT-E2E'}}
e = new_eng()
e.journal = FakeJournal()
e.api = FakeApi()
e._record = lambda *a, **k: None
e.frames.capture_confirmed = lambda: {
'qualified': False, 'path': None, 'sha256': '', 'reason': 'test'}
e._frozen_kg = 1.0
e._frozen_row = dict(ROW, container_code='')
row_no_ct = dict(ROW, container_code='')
e._submit_worker(row_no_ct, 1.0, 'AI')
it = e._print_q.get_nowait()
check('提交回包里的 container_code 进了打印队列',
it['qr'] == 'CT-E2E', it)
check('来源标记 container_code', it['qr_source'] == 'container_code', it)
check('没有退化成订单号', 'ORD_WEB' not in it['qr'], it['qr'])
print()
print('%d 项通过,%d 项失败' % (ok, fail))
sys.exit(1 if fail else 0)
+6 -4
View File
@@ -3735,9 +3735,9 @@ class App:
"""异步打印回来的结果:成功淡淡标一下,失败必须显眼(要手工补打)"""
ok, msg = pair
self.lbl_print.configure(
text=('✔ 标签已打印' if ok else '⚠ 标签未打印:%s' % msg),
text=('✔ 标签已发送打印' if ok else '⚠ 标签未打印:%s' % msg),
fg='#0f6e56' if ok else '#a32d2d')
self.log(('打印成功: ' if ok else '打印失败: ') + msg)
self.log(('打印已发送: ' if ok else '打印失败: ') + msg)
if not ok:
try:
self.root.bell()
@@ -5996,7 +5996,7 @@ class App:
self.engine.frames.pause_preview()
try:
ok, msg = self.engine.printer.print_picking(
'测试客户', '测试商品', 1.00, 'kg', 'TEST-PRINT')
'测试客户', '测试商品', 1.00, 'kg', 'CT-TEST-PRINT')
except Exception as e: # noqa: BLE001
ok, msg = False, str(e)
finally:
@@ -6114,7 +6114,7 @@ class App:
['客户: 张三水果批发店',
'品名: 农夫山泉饮用天然水 550ml×24',
'数量: 1.00 桶', '日期: 11-04 19:02'],
qr_data='PK2026110400001234')
qr_data='CT2026110400001234')
g = p['geom']
cw = max(cv.winfo_width(), 560)
ch = max(cv.winfo_height(), 320)
@@ -6366,6 +6366,8 @@ class App:
add('打印前释放USB(只老USB直连打印机才开)',
'printer.release_usb', choice=['true', 'false'])
add('打印指令类型', 'printer.type', choice=['tspl', 'zpl'])
add('标签二维码兜底字段(留空=只认容器码;临时救急可填 pick_code)',
'printer.qr_fallback')
add('标签宽度(mm)', 'printer.width_mm')
add('标签高度(mm)', 'printer.height_mm')
add('标签间距(mm,0=连续纸/无间隙)', 'printer.gap_mm')
+22 -3
View File
@@ -25,7 +25,26 @@
build_assets/ 打包素材(VC++ 运行库 vc_redist.x64.exe)
打包.bat 双击打包
dist/ 产物:dist\入库分拣工作站\(绿色版)
dist\入库分拣工作站_安装程序_v1.0.63.exe(安装包)
dist\入库分拣工作站_安装程序_v1.0.64.exe(安装包)
【v1.0.64 分拣标签二维码修复(2026-11-09)】
◆ 二维码改打「容器码」,不再打订单号
· 此前分拣标签二维码 = 订单号(ORD_WEB_...),配送小程序按「商品/容器」
查库,会提示「未找到条码对应商品或容器:ORD_WEB_...」。现在改用服务端
分拣提交回包里的 data.container_code(容器码)。
· 提交回包和待办行都取不到容器码时,本次不打印标签(宁可不打,也不打
一张扫不出来的标签),界面会红字提示;data/app.log 里的
「标签二维码」一行能看到实际打的码和来源。
· 入库默认不打标签,本次行为不变。
◆ 版面/字段校正
· 50×50mm 带二维码时,日期/时间/重量/数量/订单号不再被折成两行
(以前可能出现「日期: 11-06 1」+「0:00」)。
· 空单位/市斤按「重量」打印;不再出现「数量: 5.00 斤」。
· 商品名/客户名里有英文双引号或换行时会被清洗,不会把整张标签指令截断。
· 高级设置新增「标签二维码兜底字段」(printer.qr_fallback),默认空 =
只认容器码。只有确认配送小程序能识别 pick_code 时,才临时填 pick_code。
【离线演示模式(v1.0.63,现场当前已开启)】
@@ -1104,8 +1123,8 @@
【标签打印:异步,不挡取货(分拣模式)】
· 提交成功后标签立刻进队列,界面直接变「DONE」可拿走、放下一件;
打印在后台单线程串行做,多件连打不会交错、顺序与提交顺序一致。
· 打印结果回报在待办卡右上角:
✔ 标签已打印 → 正常
· 打印结果回报在左侧状态卡:
✔ 标签已发送打印 → 正常(已交给 Windows 打印队列)
⚠ 标签未打印:原因 → 红字 + 提示音,最近一行提示"请手工补打"
不自动重打(铁律:不重复打印),失败一律人工补。
· 没选打印机时只提醒一次(标签落成 data/last_label.prn),不会每件弹红字。