- 返回精简:每单只给定位字段 + params(contents[].params,形如
access_token=…&app_key=…&method=logistics.getShopKey&…&sign=…)与 template_url;
不再回整包 print_data / printer_config / document
- 限流:平台该接口有限流,改为一次只带一个 printHistoryId,逐张串行并间隔 300ms;
单张失败不影响其它张,失败原因逐单返回
- params 为空时直接判失败(平台的打印数据里没有 params)
实测:{ printHistoryId: "10000078656212" } → params 为 access_token=uo7…
&method=logistics.getShopKey&…&sign=8849…,template_url 为申通 76x130 模板。
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
82 lines
3.5 KiB
JavaScript
82 lines
3.5 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 与 access_token 串', () => {
|
||
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: '', params: '' },
|
||
{
|
||
templateURL: 'https://x/t.xml',
|
||
params: 'access_token=tok&app_key=715&method=logistics.getShopKey¶m_json={}×tamp=2026-11-07 06:43:36&v=2&sign=9cb2',
|
||
},
|
||
],
|
||
}],
|
||
},
|
||
},
|
||
}),
|
||
});
|
||
const parsed = printData.parsePrintData(params);
|
||
console.log('documentID:', parsed.document.documentID, '| printer:', parsed.printer_config.printer.name);
|
||
console.log('params:', parsed.params);
|
||
assert.strictEqual(parsed.document.documentID, '770357408441408');
|
||
assert.strictEqual(parsed.printer_config.printer.name, '默认打印机');
|
||
// 跳过空的 contents,取真正带 params 的那条
|
||
assert.ok(parsed.params.startsWith('access_token=tok&app_key=715'));
|
||
assert.strictEqual(parsed.template_url, 'https://x/t.xml');
|
||
assert.deepStrictEqual(printData.parsePrintData('not-json'), {
|
||
printer_config: null, document: null, documents: [], params: '', template_url: '',
|
||
});
|
||
});
|
||
});
|