问题:取号拿到面单后,店小秘侧一直显示「未打面单」。原因是 fetchWaybillNo 只取 PDF,
没调 setPrint;而店小秘不会因为外部取号自动改打印状态。
- dxm.fetchWaybillNo:拿到面单 PDF 后自动调用 markPrinted(失败只记 raw.print_error,
不影响运单号/面单返回);支持 { skipMarkPrinted: true } 只取面单不改状态
- waybillGet:重放(订单级/历史记录)时也会补写一次 setPrint(仅结果里 print_marked 不为 true
且平台实现了 markPrinted),成功后把 print_marked 回填到幂等结果,
避免「面单已发给 WMS、平台还是未打单」
- 取号响应新增 print_marked 字段(WMS 可据此判断平台状态是否回写成功)
- 单测:取号带标记、skipMarkPrinted、标记失败降级、重放补写;71 passing
- 文档:店小秘接入 5.2/6.2(含 signUnPrintMd 反向接口与 batchSignMd 对「有货」不生效的坑)、
取号对接 4.2 字段表
实测(包裹 184804424061188407 / 运单 3CAB6112200001):
- 重置为未打印后走 /openapi 取号:success=true, pdf_base64=150,136, print_marked=true
- 平台复查 isPrintMd=true、mdSignTime=1795533941000
- 同 request_id 回放、换 request_id(订单级重放)两种路径均 print_marked=true
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
418 lines
15 KiB
JavaScript
418 lines
15 KiB
JavaScript
'use strict';
|
||
|
||
const { app, assert } = require('egg-mock/bootstrap');
|
||
const mm = require('egg-mock');
|
||
const orderCallback = require('../../../app/lib/orderCallback');
|
||
const waybillGet = require('../../../app/lib/waybillGet');
|
||
|
||
const PRINCIPAL_ID = 5;
|
||
const WAREHOUSE_ID = 8;
|
||
const DELIVERY_ORDER_CODE = 'dgj-550598429043014280';
|
||
|
||
function makeEntry(data, list) {
|
||
const entry = {
|
||
...data,
|
||
update: async values => Object.assign(entry, values),
|
||
destroy: async () => {
|
||
const index = Array.isArray(list) ? list.indexOf(entry) : -1;
|
||
if (index >= 0) list.splice(index, 1);
|
||
},
|
||
};
|
||
return entry;
|
||
}
|
||
|
||
function setupContext({ entries = [], legacyRecord = null, waybillCode = 'WB-NEW', fetchError = null, hasOwnWaybill = 0, templateMissing = false } = {}) {
|
||
const ctx = app.mockContext();
|
||
const fetchedKeys = [];
|
||
const platformRoutes = [];
|
||
let platformCalls = 0;
|
||
|
||
ctx.model.WaybillFetchIdempotency.findOne = async ({ where }) =>
|
||
entries.find(entry => Object.keys(where).every(key => entry[key] === where[key])) || null;
|
||
ctx.model.WaybillFetchIdempotency.create = async data => {
|
||
const duplicate = entries.find(entry =>
|
||
[ 'principal_id', 'warehouse_id', 'delivery_order_code', 'express_code', 'package_index' ]
|
||
.every(key => entry[key] === data[key]));
|
||
if (duplicate) throw new Error('unique key conflict');
|
||
const entry = makeEntry(data, entries);
|
||
entries.push(entry);
|
||
return entry;
|
||
};
|
||
ctx.model.WaybillFetchRecord.findOne = async options => {
|
||
fetchedKeys.push(options.where);
|
||
return legacyRecord;
|
||
};
|
||
ctx.model.WaybillFetchRecord.create = async () => {};
|
||
ctx.service.cache.getShopBasic = async () => ({
|
||
id: 10,
|
||
principal_id: PRINCIPAL_ID,
|
||
type: 2,
|
||
external_print_account_id: 12,
|
||
});
|
||
ctx.model.Shop.findByPk = async () => ({
|
||
toJSON: () => ({
|
||
id: 10,
|
||
principal_id: PRINCIPAL_ID,
|
||
type: 2,
|
||
external_print_account_id: 12,
|
||
}),
|
||
});
|
||
ctx.model.ExternalPrintAccount.findOne = async () => ({
|
||
id: 12,
|
||
platform: { code: 'TEST_PLATFORM', name: 'Test platform', fetch_waybill_no: 1, has_own_waybill: hasOwnWaybill },
|
||
get: () => ({ id: 12, platform: { code: 'TEST_PLATFORM', name: 'Test platform' } }),
|
||
});
|
||
ctx.service.externalPrintExpressInterface.resolveByCode = async () => (templateMissing ? null : {
|
||
template_id: 'template-1',
|
||
get: () => ({ template_id: 'template-1' }),
|
||
});
|
||
ctx.service.externalPrintOrder.createPlatformService = account => {
|
||
platformRoutes.push(account.platform.code);
|
||
return {
|
||
needFetchWaybillNo: true,
|
||
fetchWaybillNo: async () => {
|
||
platformCalls += 1;
|
||
if (fetchError) throw fetchError;
|
||
return { waybill_code: waybillCode, printinfo: 'print-data' };
|
||
},
|
||
};
|
||
};
|
||
mm(orderCallback, 'resolveDeliveryBill', async () => ({
|
||
isNewModel: false,
|
||
order: {
|
||
warehouse_id: WAREHOUSE_ID,
|
||
status: 1,
|
||
source_platform_code: 'KS',
|
||
shop_id: 10,
|
||
get: () => ({
|
||
warehouse_id: WAREHOUSE_ID,
|
||
status: 1,
|
||
source_platform_code: 'KS',
|
||
shop_id: 10,
|
||
}),
|
||
},
|
||
}));
|
||
return { ctx, entries, fetchedKeys, platformRoutes, get platformCalls() { return platformCalls; } };
|
||
}
|
||
|
||
function requestItem(overrides = {}) {
|
||
return {
|
||
deliveryOrderCode: DELIVERY_ORDER_CODE,
|
||
expressCode: 'STO',
|
||
packageIndex: 0,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
function requestBody(item) {
|
||
return { request: { orders: [ item ], request_id: `request-${Date.now()}` } };
|
||
}
|
||
|
||
describe('lib/waybillGet 订单级幂等', () => {
|
||
afterEach(() => mm.restore());
|
||
|
||
it('同订单、快递和包裹序号直接重放数据库中的完整结果', async () => {
|
||
const savedResult = {
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
success: true,
|
||
message: 'ok',
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
waybill_code: 'WB-SAVED',
|
||
waybill_codes: [ 'WB-SAVED' ],
|
||
printinfo: 'saved-print-data',
|
||
key: 'saved-key',
|
||
};
|
||
const entry = makeEntry({
|
||
principal_id: PRINCIPAL_ID,
|
||
warehouse_id: WAREHOUSE_ID,
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
status: 1,
|
||
result_data: JSON.stringify(savedResult),
|
||
});
|
||
const state = setupContext({ entries: [ entry ] });
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.deepStrictEqual(result.data.results[0], savedResult);
|
||
// 重放不再向平台取号;但会尝试补写「已打印面单」(测试桩没有 markPrinted,故无平台调用)
|
||
assert.strictEqual(state.platformCalls, 0);
|
||
assert.strictEqual(state.fetchedKeys.length, 0);
|
||
});
|
||
|
||
it('同一批里的重复订单项只调用一次平台并复用结果', async () => {
|
||
const state = setupContext();
|
||
const item = requestItem();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: { request: { orders: [ item, { ...item }] } },
|
||
});
|
||
|
||
assert.strictEqual(result.data.success, 2);
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(result.data.results[1].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(state.platformRoutes.length, 1);
|
||
});
|
||
|
||
it('相同订单但快递编码不同会按订单当前平台路由重新取号', async () => {
|
||
const state = setupContext();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem({ expressCode: 'ZTO' })),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-NEW');
|
||
assert.strictEqual(state.platformRoutes[0], 'TEST_PLATFORM');
|
||
assert.strictEqual(state.entries[0].express_code, 'ZTO');
|
||
});
|
||
|
||
it('相同订单和快递但包裹序号不同会独立取号', async () => {
|
||
const state = setupContext();
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem({ packageIndex: 1 })),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].package_index, 1);
|
||
assert.strictEqual(state.entries[0].package_index, 1);
|
||
assert.strictEqual(state.platformRoutes[0], 'TEST_PLATFORM');
|
||
});
|
||
|
||
it('另一请求已有相同订单键正在处理时不重复调用平台', async () => {
|
||
const state = setupContext({
|
||
entries: [ makeEntry({
|
||
principal_id: PRINCIPAL_ID,
|
||
warehouse_id: WAREHOUSE_ID,
|
||
delivery_order_code: DELIVERY_ORDER_CODE,
|
||
express_code: 'STO',
|
||
package_index: 0,
|
||
status: 0,
|
||
}) ],
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].success, false);
|
||
assert.match(result.data.results[0].message, /正在处理中/);
|
||
assert.strictEqual(state.platformRoutes.length, 0);
|
||
});
|
||
|
||
it('并发的不同 request_id 由数据库唯一键仲裁,只执行一次平台取号', async () => {
|
||
const state = setupContext();
|
||
const makePayload = requestId => ({
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: { request: { orders: [ requestItem() ], request_id: requestId } },
|
||
});
|
||
|
||
await Promise.all([
|
||
waybillGet.getWaybills(state.ctx, makePayload('request-a')),
|
||
waybillGet.getWaybills(state.ctx, makePayload('request-b')),
|
||
]);
|
||
|
||
assert.strictEqual(state.platformCalls, 1);
|
||
assert.strictEqual(state.entries.length, 1);
|
||
assert.strictEqual(state.entries[0].status, 1);
|
||
});
|
||
|
||
it('平台请求超时后标记结果待核实,后续重试不再次消耗面单', async () => {
|
||
const state = setupContext({ fetchError: new Error('platform request timeout') });
|
||
const payload = {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
};
|
||
|
||
const first = await waybillGet.getWaybills(state.ctx, payload);
|
||
const second = await waybillGet.getWaybills(state.ctx, payload);
|
||
|
||
assert.strictEqual(first.data.results[0].success, false);
|
||
assert.match(first.data.results[0].message, /结果暂不确定/);
|
||
assert.strictEqual(second.data.results[0].success, false);
|
||
assert.match(second.data.results[0].message, /待核实/);
|
||
assert.strictEqual(state.platformCalls, 1);
|
||
assert.strictEqual(state.entries[0].status, 2);
|
||
});
|
||
|
||
it('未消耗面单的明确失败(如店管家查不到订单)允许重试,不写待核实', async () => {
|
||
const fetchError = new Error('店管家未找到订单:系统编号 40000834686950(域名 fxdd.dgjapp.com,分区 -)');
|
||
fetchError.waybillNotConsumed = true;
|
||
const state = setupContext({ fetchError });
|
||
const payload = {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
};
|
||
|
||
const first = await waybillGet.getWaybills(state.ctx, payload);
|
||
const second = await waybillGet.getWaybills(state.ctx, payload);
|
||
|
||
assert.strictEqual(first.data.results[0].success, false);
|
||
assert.match(first.data.results[0].message, /未找到订单/);
|
||
assert.doesNotMatch(first.data.results[0].message, /待核实|暂不确定/);
|
||
assert.match(second.data.results[0].message, /未找到订单/);
|
||
assert.strictEqual(state.platformCalls, 2);
|
||
assert.strictEqual(state.entries.length, 0);
|
||
});
|
||
|
||
it('首次命中旧取号记录时回填幂等结果,不再调用平台', async () => {
|
||
const state = setupContext({
|
||
legacyRecord: {
|
||
waybill_code: 'WB-LEGACY',
|
||
waybill_codes: '["WB-LEGACY"]',
|
||
waybill_source: 'express_print',
|
||
printinfo: 'legacy-print-data',
|
||
signature: 'legacy-signature',
|
||
print_history_ids: '[]',
|
||
cai_niao_auth_info_id: 123,
|
||
platform_code: 'KS',
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].waybill_code, 'WB-LEGACY');
|
||
assert.strictEqual(result.data.results[0].printinfo, 'legacy-print-data');
|
||
assert.strictEqual(state.platformCalls, 0);
|
||
assert.strictEqual(state.entries[0].status, 1);
|
||
});
|
||
|
||
it('平台返回面单 PDF 时以 pdf_base64 透传(不进 printinfo)', async () => {
|
||
const state = setupContext();
|
||
const platformService = state.ctx.service.externalPrintOrder.createPlatformService({ platform: { code: 'dxm' } });
|
||
state.ctx.service.externalPrintOrder.createPlatformService = () => ({
|
||
...platformService,
|
||
fetchWaybillNo: async () => ({
|
||
waybill_code: '3CAB6112100007',
|
||
waybill_codes: [ '3CAB6112100007' ],
|
||
pdf_base64: 'JVBERi0xLjQK',
|
||
print_marked: true,
|
||
raw: { agentTrackNum: '3CAB6112100007' },
|
||
}),
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
const item = result.data.results[0];
|
||
assert.strictEqual(item.success, true);
|
||
assert.strictEqual(item.waybill_code, '3CAB6112100007');
|
||
assert.strictEqual(item.pdf_base64, 'JVBERi0xLjQK');
|
||
// 平台侧「已打印面单」回写结果随结果返回,便于 WMS 排查
|
||
assert.strictEqual(item.print_marked, true);
|
||
assert.strictEqual(item.printinfo, '');
|
||
});
|
||
|
||
it('重放已保存结果时补回 pdf_base64(result_data 里只存长度)', async () => {
|
||
const state = setupContext({
|
||
legacyRecord: {
|
||
waybill_code: 'WB-LEGACY',
|
||
waybill_codes: '["WB-LEGACY"]',
|
||
waybill_source: 'express_print',
|
||
result_data: JSON.stringify({ success: true, waybill_code: 'WB-LEGACY', pdf_base64_length: 12 }),
|
||
pdf_base64: 'JVBERi0xLjQK',
|
||
print_history_ids: '[]',
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].pdf_base64, 'JVBERi0xLjQK');
|
||
assert.strictEqual(state.platformCalls, 0);
|
||
});
|
||
|
||
it('重放历史结果时补写「已打印面单」(自带面单平台)', async () => {
|
||
const state = setupContext({
|
||
legacyRecord: {
|
||
waybill_code: 'WB-LEGACY',
|
||
waybill_codes: '["WB-LEGACY"]',
|
||
waybill_source: 'express_print',
|
||
result_data: JSON.stringify({ success: true, waybill_code: 'WB-LEGACY', pdf_base64: 'JVBERi0xLjQK' }),
|
||
print_history_ids: '[]',
|
||
},
|
||
});
|
||
const marked = [];
|
||
const baseCreate = state.ctx.service.externalPrintOrder.createPlatformService;
|
||
state.ctx.service.externalPrintOrder.createPlatformService = account => ({
|
||
...baseCreate(account),
|
||
markPrinted: async id => {
|
||
marked.push(id);
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
// 面单早已下发,重放时只补平台打印状态,不再取号
|
||
assert.deepStrictEqual(marked, [ '550598429043014280' ]);
|
||
assert.strictEqual(result.data.results[0].print_marked, true);
|
||
assert.strictEqual(state.platformCalls, 0);
|
||
assert.strictEqual(JSON.parse(state.entries[0].result_data).print_marked, true);
|
||
});
|
||
|
||
it('自带运单的平台没有快递接口模板也能取号(模板可缺省)', async () => {
|
||
const state = setupContext({ hasOwnWaybill: 1, templateMissing: true, waybillCode: '3CAB6112100007' });
|
||
const templates = [];
|
||
const baseCreate = state.ctx.service.externalPrintOrder.createPlatformService;
|
||
state.ctx.service.externalPrintOrder.createPlatformService = account => ({
|
||
...baseCreate(account),
|
||
fetchWaybillNo: async (order, options) => {
|
||
templates.push(options.template);
|
||
return { waybill_code: '3CAB6112100007', pdf_base64: 'JVBERi0xLjQK' };
|
||
},
|
||
});
|
||
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
|
||
assert.strictEqual(result.data.results[0].success, true);
|
||
assert.strictEqual(result.data.results[0].waybill_code, '3CAB6112100007');
|
||
assert.strictEqual(templates.length, 1);
|
||
assert.strictEqual(templates[0], null);
|
||
});
|
||
|
||
it('非自带运单的平台缺模板仍然报未绑定模板', async () => {
|
||
const state = setupContext({ hasOwnWaybill: 0, templateMissing: true });
|
||
const result = await waybillGet.getWaybills(state.ctx, {
|
||
principalId: PRINCIPAL_ID,
|
||
warehouseId: WAREHOUSE_ID,
|
||
data: requestBody(requestItem()),
|
||
});
|
||
assert.strictEqual(result.data.results[0].success, false);
|
||
assert.match(result.data.results[0].message, /暂无面单模板/);
|
||
});
|
||
});
|