店管家面单打印数据(params)只有 10 分钟有效期,取号返回的 printinfo 存着不能用,
WMS 真正打印前必须换新的。新增对外方法:
method = deliveryorder.getprintdata
入参:{ caiNiaoAuthInfoIds: [6838859], waybillCodes: ["770357408441408"] }
也支持直传 { printHistoryIds: ["10000078656212"] }(命中即跳过查库)
出参:{ total, success, failed, expires_in: 600, results: [...] }
每单含 print_data / printer_config / document(documentID = 运单号)
实现:
- app/lib/waybillPrintData.js:按「店铺标识 + 运单号」查 waybill_fetch_records 定位打印记录,
同账号同分区合并成一次 PrintHistory/LoadPrintData(只读,不消耗面单)
- 取号记录补 platform_host(实际命中的订单域名):1688 分销单在 fxali、真实抖音单在 fxdd,
用被换算过的 source_platform_code 推域名会打错
- waybillGet 落 platform_host;迁移 20261107050000 加列
实测(只读):运单号 770357408441408 → 新 params 7888 字符,documentID 770357408441408,
printer 默认打印机;不存在的运单号返回「未找到该运单号的取号记录」。
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
64 lines
2.8 KiB
JavaScript
64 lines
2.8 KiB
JavaScript
'use strict';
|
||
|
||
const { assert } = require('egg-mock/bootstrap');
|
||
const printData = require('../../../app/lib/waybillPrintData');
|
||
|
||
describe('waybillPrintData 打印数据换取', () => {
|
||
const record = {
|
||
waybill_code: '770357408441408',
|
||
waybill_codes: JSON.stringify([ '770357408441408', '770357408441409' ]),
|
||
print_history_ids: JSON.stringify([
|
||
{ id: '10000078656212', pid: 'p1', waybill_code: '770357408441408', batch_index: 1 },
|
||
{ id: '10000078656213', pid: 'p2', waybill_code: '770357408441409', batch_index: 2 },
|
||
]),
|
||
};
|
||
|
||
it('入参归一化:单值/数组/逗号分隔都接受并去重', () => {
|
||
assert.deepStrictEqual(printData.toStringList('a, b'), [ 'a', 'b' ]);
|
||
assert.deepStrictEqual(printData.toStringList([ 'a', 'a' ]), [ 'a' ]);
|
||
assert.deepStrictEqual(printData.toStringList(null), []);
|
||
});
|
||
|
||
it('按运单号挑出对应的打印记录(多包裹不能串号)', () => {
|
||
const first = printData.pickPrintHistory(record, '770357408441408');
|
||
const second = printData.pickPrintHistory(record, '770357408441409');
|
||
console.log('打印记录:', first && first.id, second && second.id);
|
||
assert.strictEqual(first.id, '10000078656212');
|
||
assert.strictEqual(second.id, '10000078656213');
|
||
});
|
||
|
||
it('直传的打印记录ID命中时优先使用', () => {
|
||
const hit = printData.pickPrintHistory(record, '770357408441408', [ '10000078656213' ]);
|
||
assert.strictEqual(hit.id, '10000078656213');
|
||
});
|
||
|
||
it('历史数据缺 waybill_code 时按顺序兜底,找不到返回 null', () => {
|
||
const legacy = {
|
||
waybill_codes: JSON.stringify([ 'A', 'B' ]),
|
||
print_history_ids: JSON.stringify([{ id: '1' }, { id: '2' }]),
|
||
};
|
||
assert.strictEqual(printData.pickPrintHistory(legacy, 'B').id, '2');
|
||
assert.strictEqual(printData.pickPrintHistory(legacy, 'C'), null);
|
||
assert.strictEqual(printData.pickPrintHistory({}, 'A'), null);
|
||
});
|
||
|
||
it('解析平台返回的 params:拿到打印机配置与面单 document', () => {
|
||
const params = JSON.stringify({
|
||
id: null,
|
||
sendContent: '[1];\n',
|
||
printDataJson: JSON.stringify({
|
||
PrinterConfig: { cmd: 'setPrinterConfig', printer: { name: '默认打印机' } },
|
||
PrintDataList: {
|
||
cmd: 'print',
|
||
task: { printer: '默认打印机', documents: [{ documentID: '770357408441408', contents: [{ templateURL: 'https://x/t.xml' }] }] },
|
||
},
|
||
}),
|
||
});
|
||
const parsed = printData.parsePrintData(params);
|
||
console.log('documentID:', parsed.document.documentID, '| printer:', parsed.printer_config.printer.name);
|
||
assert.strictEqual(parsed.document.documentID, '770357408441408');
|
||
assert.strictEqual(parsed.printer_config.printer.name, '默认打印机');
|
||
assert.deepStrictEqual(printData.parsePrintData('not-json'), { printer_config: null, document: null, documents: [] });
|
||
});
|
||
});
|