外部打单平台增加「是否自带运单」

- external_print_platforms 新增 has_own_waybill(默认 0),同步接口契约与前端类型
- 自带运单的平台取号时复用平台已确认运单号(WaybillCodes),只重新拉取打印数据,不分配新面单
- 平台属性查询补齐 has_own_waybill,更新店管家取号文档并补测试

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-23 21:13:48 +08:00
parent ccaea5928f
commit 2b8fcc2286
17 changed files with 200 additions and 40 deletions

View File

@ -1387,6 +1387,7 @@ module.exports = {
name: { type: 'string', description: '平台名称' }, name: { type: 'string', description: '平台名称' },
url: { type: 'string', description: '平台链接地址' }, url: { type: 'string', description: '平台链接地址' },
fetch_waybill_no: { type: 'integer', description: '是否回打单平台取号:1是 0否' }, fetch_waybill_no: { type: 'integer', description: '是否回打单平台取号:1是 0否' },
has_own_waybill: { type: 'integer', description: '是否自带运单:1是(取号时复用平台已有运单号) 0否' },
sort: { type: 'integer', description: '排序,越小越靠前' }, sort: { type: 'integer', description: '排序,越小越靠前' },
status: { type: 'integer', description: '状态:1启用 0禁用' }, status: { type: 'integer', description: '状态:1启用 0禁用' },
created_at: { type: 'string', description: '创建时间' }, created_at: { type: 'string', description: '创建时间' },

View File

@ -150,7 +150,7 @@ class ExternalPrintAccountController extends Controller {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });
@ -1076,7 +1076,7 @@ class ExternalPrintAccountController extends Controller {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
]; ];
} }

View File

@ -24,7 +24,7 @@ class ExternalPrintExpressInterfaceController extends Controller {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -720,7 +720,7 @@ class ShopController extends Controller {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -33,7 +33,7 @@ async function getShopBasic(ctx, shopId, principalId) {
} }
/** /**
* 读取外部打单账号及其平台配置(含 fetch_waybill_no) * 读取外部打单账号及其平台配置(含 fetch_waybill_no / has_own_waybill)
* @param {Object} ctx egg context * @param {Object} ctx egg context
* @param {Number} accountId 外部打单账号ID * @param {Number} accountId 外部打单账号ID
* @param {Number} [principalId] 主体ID,传入时校验归属 * @param {Number} [principalId] 主体ID,传入时校验归属
@ -53,7 +53,7 @@ async function getAccountWithPlatform(ctx, accountId, principalId) {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -57,6 +57,11 @@ class PrintPlatformService {
return Number(this.platform.fetch_waybill_no) === 1; return Number(this.platform.fetch_waybill_no) === 1;
} }
/** 是否自带运单:1 是(取号时复用平台已有运单号,不分配新面单) */
get hasOwnWaybill() {
return Number(this.platform.has_own_waybill) === 1;
}
/** /**
* 读取打单账号密码,未配置时抛错,避免各实现重复判空 * 读取打单账号密码,未配置时抛错,避免各实现重复判空
* @return {{account: string, password: string}} 平台登录凭据 * @return {{account: string, password: string}} 平台登录凭据

View File

@ -307,6 +307,24 @@ function deriveCurrentWaybillCode(row) {
return String(data.LastWaybillCode || (list[list.length - 1] || {}).WaybillCode || '').trim(); return String(data.LastWaybillCode || (list[list.length - 1] || {}).WaybillCode || '').trim();
} }
/**
* 平台已确认的出号列表(`WaybillCodes`),自带运单的平台据此复用
*
* `LastWaybillCode` 只是「最近一次打印用到的号」,不代表已出号(见文档 §4.2.4),
* 所以复用只认 `WaybillCodes`,`LastWaybillCode` 只作为补充。
* @param {Object} row 平台订单行
* @return {string[]} 运单号清单(去重),未出号时为空数组
*/
function existingWaybillCodes(row) {
const data = row || {};
const list = (Array.isArray(data.WaybillCodes) ? data.WaybillCodes : [])
.map(item => String((item && item.WaybillCode) || '').trim())
.filter(Boolean);
const last = String(data.LastWaybillCode || '').trim();
if (last && !list.includes(last)) list.push(last);
return [ ...new Set(list) ];
}
/** /**
* 构造取号(`ExpressPrint`)的 `Orders[i]` 报文 * 构造取号(`ExpressPrint`)的 `Orders[i]` 报文
* *
@ -3175,9 +3193,11 @@ class DgjService extends PrintPlatformService {
* 流程:回读平台订单行 → 已发货/已终结拦截 → * 流程:回读平台订单行 → 已发货/已终结拦截 →
* CheckTemplateAvaliable(模板/余额)→ ExpressPrint(取号 + 打印数据)。 * CheckTemplateAvaliable(模板/余额)→ ExpressPrint(取号 + 打印数据)。
* *
* **不做「平台已有运单号就复用」的判断**:是否算重复调用只由幂等表 * 默认**不做「平台已有运单号就复用」的判断**:是否算重复调用只由幂等表
* (`openapi_request_idempotency`,按 request_id)决定;平台订单行上的 `LastWaybillCode` * (`openapi_request_idempotency`,按 request_id)决定;平台订单行上的 `LastWaybillCode`
* 只用于诊断(判断是否真换了号),不参与取号决策。 * 只用于诊断(判断是否真换了号),不参与取号决策。
* 平台配置 `has_own_waybill=1` 时例外:复用平台已确认的出号(`WaybillCodes`),
* 只重新拉取打印数据,不分配新面单。
* ⚠️ ExpressPrint 会真实消耗面单:默认开启,需要「只读联调」时用 DGJ_WAYBILL_ENABLE=0 关闭。 * ⚠️ ExpressPrint 会真实消耗面单:默认开启,需要「只读联调」时用 DGJ_WAYBILL_ENABLE=0 关闭。
* @param {Object} order OMS 订单(order_no / parent_order_no / oaid_order_source_code / place_order_time) * @param {Object} order OMS 订单(order_no / parent_order_no / oaid_order_source_code / place_order_time)
* @param {Object} options 取号上下文 * @param {Object} options 取号上下文
@ -3259,17 +3279,14 @@ class DgjService extends PrintPlatformService {
)); ));
} }
// 平台已取号但未发货:不再复用。重复调用是否要拦,由幂等表(request_id)负责; // 平台已取号但未发货:是否重复调用由幂等表(request_id)负责;
// 这里只在结果里带上原来的号码,便于判断这次到底有没有换号。 // 这里把原来的号码带进结果,便于判断这次到底有没有换号。
const previousCode = String(row.LastWaybillCode || '').trim(); const previousCode = String(row.LastWaybillCode || '').trim();
// 多包裹 / 子单:每次调用都走「新单号打印」,平台会分配一个新号, // 多包裹 / 子单:新号取号每次调用都走「新单号打印」,平台会分配一个新号,
// 所以 WMS 按包裹各调一次即可(packageIndex 无需下发给平台)。 // 所以 WMS 按包裹各调一次即可(packageIndex 无需下发给平台)。
if (!isWaybillEnabled()) { // 自带运单的平台不分配新号:复用平台已确认的出号(WaybillCodes),只重新拉取打印数据。
throw markWaybillNotConsumed(new Error('店管家真实取号已关闭(DGJ_WAYBILL_ENABLE=0):当前只做只读校验,不会调用平台取号;需要真实取号请去掉该环境变量或置为 1')); const runPrint = async ({ reuseWaybill = false } = {}) => {
}
const runPrint = async () => {
let check; let check;
try { try {
check = await this.checkTemplateAvailable(host, { traceId, dbname, templateId, rows: [ row ] }); check = await this.checkTemplateAvailable(host, { traceId, dbname, templateId, rows: [ row ] });
@ -3277,9 +3294,10 @@ class DgjService extends PrintPlatformService {
// 模板/余额校验是只读接口,失败时 ExpressPrint 还没发出去,可安全重试 // 模板/余额校验是只读接口,失败时 ExpressPrint 还没发出去,可安全重试
throw markWaybillNotConsumed(err); throw markWaybillNotConsumed(err);
} }
// 复用已有运单号不消耗面单,不需要看余额
const balance = Number(check && check.Data && check.Data.Balance); const balance = Number(check && check.Data && check.Data.Balance);
const needCount = Number(check && check.Data && check.Data.PrintCount); const needCount = Number(check && check.Data && check.Data.PrintCount);
if (Number.isFinite(balance) && Number.isFinite(needCount) && balance < needCount) { if (!reuseWaybill && Number.isFinite(balance) && Number.isFinite(needCount) && balance < needCount) {
throw markWaybillNotConsumed(new Error('店管家面单余额不足,请充值后再取号')); throw markWaybillNotConsumed(new Error('店管家面单余额不足,请充值后再取号'));
} }
return await this.expressPrint(host, { return await this.expressPrint(host, {
@ -3288,28 +3306,51 @@ class DgjService extends PrintPlatformService {
templateId, templateId,
rows: [ row ], rows: [ row ],
checkData: check && check.Data, checkData: check && check.Data,
packageCount: Number(options.packageCount || 1), packageCount: reuseWaybill ? 1 : Number(options.packageCount || 1),
expressCode: String(options.expressCode || '').trim(), expressCode: String(options.expressCode || '').trim(),
reuseWaybill,
}); });
}; };
const runPrintWithRelogin = async runOptions => {
try {
return await runPrint(runOptions);
} catch (err) {
// 账号在别处重新登录会把这里的 token 顶掉(平台返回 401):重新登录后重试一次
if (!/登录已失效|登录信息过期/.test(String(err && err.message))) throw err;
await this.loginByPassword();
return await runPrint(runOptions);
}
};
const existingCodes = this.hasOwnWaybill ? existingWaybillCodes(row) : [];
let printed; let printed;
try { if (this.hasOwnWaybill) {
printed = await runPrint(); if (!existingCodes.length) {
} catch (err) { throw markWaybillNotConsumed(new Error('该平台配置为自带运单,但订单在店管家还没有运单号:请先在平台取号/打印面单后再推送'));
// 账号在别处重新登录会把这里的 token 顶掉(平台返回 401):重新登录后重试一次 }
if (!/登录已失效|登录信息过期/.test(String(err && err.message))) throw err; printed = await runPrintWithRelogin({ reuseWaybill: true });
await this.loginByPassword(); } else {
printed = await runPrint(); if (!isWaybillEnabled()) {
throw markWaybillNotConsumed(new Error('店管家真实取号已关闭(DGJ_WAYBILL_ENABLE=0):当前只做只读校验,不会调用平台取号;需要真实取号请去掉该环境变量或置为 1'));
}
printed = await runPrintWithRelogin({ reuseWaybill: false });
} }
const items = Array.isArray(printed && printed.Data && printed.Data.WaybillCode) ? printed.Data.WaybillCode : []; const items = Array.isArray(printed && printed.Data && printed.Data.WaybillCode) ? printed.Data.WaybillCode : [];
const errorItem = items.find(item => item && (item.IsError === true || Number(item.IsError) === 1)); const errorItem = items.find(item => item && (item.IsError === true || Number(item.IsError) === 1));
if (errorItem) { if (errorItem) {
throw buildPlatformError(`店管家取号失败:${describeWaybillError(errorItem)}`, { status: 200, json: printed, text: '' }); const error = buildPlatformError(
`店管家${this.hasOwnWaybill ? '复用运单号' : '取号'}失败:${describeWaybillError(errorItem)}`,
{ status: 200, json: printed, text: '' }
);
// 复用路径不消耗面单,失败可直接重试
throw this.hasOwnWaybill ? markWaybillNotConsumed(error) : error;
} }
const waybillCodes = extractWaybillCodes(items); let waybillCodes = extractWaybillCodes(items);
if (!waybillCodes.length && existingCodes.length) waybillCodes = existingCodes;
if (!waybillCodes.length) { if (!waybillCodes.length) {
const firstMessage = items.map(item => describeWaybillError(item)).find(text => text && text !== '平台返回取号失败'); const firstMessage = items.map(item => describeWaybillError(item)).find(text => text && text !== '平台返回取号失败');
throw buildPlatformError( throw buildPlatformError(
@ -3318,11 +3359,28 @@ class DgjService extends PrintPlatformService {
); );
} }
return this.buildWaybillResult(printed, { waybillCodes, previousCode, dbname, host, template, options });
}
/**
* 归一化 ExpressPrint 响应为取号结果(新号 / 复用已有号共用)
* @param {Object} printed ExpressPrint 响应
* @param {Object} params 参数
* @param {string[]} params.waybillCodes 本次运单号(首个为主号码,返回给 WMS)
* @param {string} params.previousCode 取号前的平台运单号(仅用于诊断)
* @param {string} params.dbname 实际命中的分区
* @param {string} params.host 实际命中的订单域名
* @param {Object} params.template 命中的快递接口
* @param {Object} params.options 调用上下文
* @return {Object} 取号结果
*/
buildWaybillResult(printed, { waybillCodes, previousCode, dbname, host, template, options }) {
const document = firstPrintDocument(printed, waybillCodes[0]); const document = firstPrintDocument(printed, waybillCodes[0]);
const contents = extractDocumentContents(document); const contents = extractDocumentContents(document);
// 模板上落的是 OMS 预设编码,优先用它判断平台;缺失时再按打单平台原始类型/调用方编码换算 // 模板上落的是 OMS 预设编码,优先用它判断平台;缺失时再按打单平台原始类型/调用方编码换算
const platformCode = String((template && template.platform_code) || '').trim().toUpperCase() const platformCode = String((template && template.platform_code) || '').trim().toUpperCase()
|| resolveOrderPlatform((template && template.platform_type) || options.platformCode).code; || resolveOrderPlatform((template && template.platform_type) || (options && options.platformCode)).code;
const items = Array.isArray(printed && printed.Data && printed.Data.WaybillCode) ? printed.Data.WaybillCode : [];
const printFields = buildWaybillPrintFields({ const printFields = buildWaybillPrintFields({
platformCode, platformCode,
contents, contents,

View File

@ -407,7 +407,7 @@ async function loadAccountModel(ctx, memo, accountId, principalId) {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}).catch(err => { }).catch(err => {

View File

@ -16,6 +16,12 @@ module.exports = app => {
name: { type: STRING(100), allowNull: false, comment: '平台名称' }, name: { type: STRING(100), allowNull: false, comment: '平台名称' },
url: { type: STRING(255), comment: '平台链接地址' }, url: { type: STRING(255), comment: '平台链接地址' },
fetch_waybill_no: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否回打单平台取号:1是 0否' }, fetch_waybill_no: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否回打单平台取号:1是 0否' },
has_own_waybill: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否自带运单:1是(取号时复用平台已有运单号,不分配新面单) 0否',
},
sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序,越小越靠前' }, sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序,越小越靠前' },
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' }, status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
created_at: { type: DATE, comment: '创建时间' }, created_at: { type: DATE, comment: '创建时间' },

View File

@ -62,7 +62,7 @@ module.exports = {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -70,7 +70,7 @@ module.exports = {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -239,7 +239,7 @@ class CacheService extends Service {
} }
/** /**
* 外部打单账号及其平台配置(含 fetch_waybill_no,决定是否回 OMS 取号) * 外部打单账号及其平台配置(含 fetch_waybill_no 是否回 OMS 取号、has_own_waybill 是否复用平台已有运单号)
* @param {Number} accountId 外部打单账号ID * @param {Number} accountId 外部打单账号ID
* @return {Promise<Object>} 账号与平台配置,不存在为 null * @return {Promise<Object>} 账号与平台配置,不存在为 null
*/ */
@ -252,7 +252,7 @@ class CacheService extends Service {
{ {
model: this.ctx.model.ExternalPrintPlatform, model: this.ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -445,7 +445,7 @@ class OrderSyncTaskService extends Service {
{ {
model: ctx.model.ExternalPrintPlatform, model: ctx.model.ExternalPrintPlatform,
as: 'platform', as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ], attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no', 'has_own_waybill' ],
}, },
], ],
}); });

View File

@ -0,0 +1,24 @@
'use strict';
/**
* 外部打单平台增加「是否自带运单」。
*
* 自带运单的平台:订单在平台侧已经出号,OMS 取号时直接复用平台已有运单号(只重新拉取打印数据),
* 不再分配新面单;未带运单的平台保持原有「每次真实取号」的口径。
* 与 fetch_waybill_no 的关系:自带运单的平台同样需要回平台取号(把已有运单号回给 WMS),
* 因此 has_own_waybill=1 时 fetch_waybill_no 也应为 1。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('external_print_platforms', 'has_own_waybill', {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否自带运单:1是(取号时复用平台已有运单号,不分配新面单) 0否',
});
},
async down(queryInterface) {
await queryInterface.removeColumn('external_print_platforms', 'has_own_waybill');
},
};

View File

@ -236,6 +236,64 @@ describe('printPlatform/dgj fetchWaybillNo 失败标记', () => {
}); });
}); });
describe('printPlatform/dgj fetchWaybillNo 自带运单', () => {
const account = { id: 1, account: 'acc', password: 'pwd', token: 'tk', cookie: 'ck' };
const order = { order_no: 'dgj-40000834686950', place_order_time: '2026-11-20 10:00:00' };
const template = { template_id: 'tpl-1', platform_type: 'taobao' };
function createService() {
const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com', fetch_waybill_no: 1, has_own_waybill: 1 };
const service = new DgjService({ platform, account: { ...account } });
service.resolveTraceId = async () => 'trace-1';
service.checkTemplateAvailable = async () => ({ Data: { Balance: 1, PrintCount: 1 } });
return service;
}
it('订单已有出号时复用平台运单号,不分配新面单', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({
ErpState: 'waitsellersend',
LastWaybillCode: '770000000001',
WaybillCodes: [{ WaybillCode: '770000000002' }],
});
let printedWith = null;
service.expressPrint = async (host, options) => {
printedWith = options;
return {
Data: {
WaybillCode: [{ WaybillCode: '770000000002', IsError: false }],
PrintHistoryIds: [],
PrintDataList: { task: { documents: [{ documentID: '770000000002', printInfo: 'PLAIN_PRINT_DATA' }] } },
},
};
};
const result = await service.fetchWaybillNo(order, { template });
assert.strictEqual(printedWith.reuseWaybill, true);
assert.strictEqual(printedWith.packageCount, 1);
assert.strictEqual(result.waybill_code, '770000000002');
assert.deepStrictEqual(result.waybill_codes, [ '770000000002' ]);
});
it('订单还没有出号时直接报错,且标记「未消耗面单」', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({ ErpState: 'waitsellersend', WaybillCodes: [] });
service.expressPrint = async () => {
throw new Error('不应该调用 ExpressPrint');
};
await assert.rejects(
service.fetchWaybillNo(order, { template }),
err => {
assert.match(err.message, /还没有运单号/);
assert.strictEqual(err.waybillNotConsumed, true);
return true;
}
);
});
});
describe('printPlatform/dgj resolveTraceId', () => { describe('printPlatform/dgj resolveTraceId', () => {
const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com' }; const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com' };
const account = { id: 90123, token: 'tk-cache-test', cookie: 'ck' }; const account = { id: 90123, token: 'tk-cache-test', cookie: 'ck' };

View File

@ -8,6 +8,8 @@ export interface ExternalPrintPlatformItem {
url: string | null url: string | null
/** 是否回打单平台取号:1 是 0 否 */ /** 是否回打单平台取号:1 是 0 否 */
fetch_waybill_no: number fetch_waybill_no: number
/** 是否自带运单:1 是(取号时复用平台已有运单号) 0 否 */
has_own_waybill: number
sort: number sort: number
status: number status: number
created_at?: string created_at?: string

View File

@ -12,7 +12,8 @@
- 取号 = `POST /TemplateSet/CheckTemplateAvaliable`(校验/余额,只读)+ `POST /NewOrder/ExpressPrint`(取号 + 打印数据)。 - 取号 = `POST /TemplateSet/CheckTemplateAvaliable`(校验/余额,只读)+ `POST /NewOrder/ExpressPrint`(取号 + 打印数据)。
- 前提是先从 `POST /NewOrder/List` 拿到**平台订单行**(Id / LogicOrderId / ShopId / SubOrders…)。 - 前提是先从 `POST /NewOrder/List` 拿到**平台订单行**(Id / LogicOrderId / ShopId / SubOrders…)。
- **订单接口与模板接口不在同一域名**(踩坑点,见 §2)。 - **订单接口与模板接口不在同一域名**(踩坑点,见 §2)。
- 旧系统在平台已有运单号(`LastWaybillCode`)时会直接复用;**OMS 不采用该判断**,每次都真实取号,去重只靠幂等表 `request_id`。 - 旧系统在平台已有运单号(`LastWaybillCode`)时会直接复用;**OMS 默认不采用该判断**,每次都真实取号,去重只靠幂等表 `request_id`。
平台配置 `has_own_waybill=1`(自带运单)时例外:复用平台已确认的出号(`WaybillCodes`),只重新拉取打印数据、不分配新面单。
- OMS 已实现(`backend/app/lib/printPlatform/dgj.js`),**默认开启真实取号**(会调用 ExpressPrint 消耗面单);需要只读联调时用 `DGJ_WAYBILL_ENABLE=0` 关闭,并提供手工验证脚本。 - OMS 已实现(`backend/app/lib/printPlatform/dgj.js`),**默认开启真实取号**(会调用 ExpressPrint 消耗面单);需要只读联调时用 `DGJ_WAYBILL_ENABLE=0` 关闭,并提供手工验证脚本。
--- ---
@ -236,8 +237,10 @@ NOT_MATCH_SEGMENT_CHANNEL#…此运单号非抖音的运单号…`。
└─ 命中后拿到 Id / LogicOrderId / ShopId / FxUserId / SubOrders / LastWaybillCode └─ 命中后拿到 Id / LogicOrderId / ShopId / FxUserId / SubOrders / LastWaybillCode
② 已发货/已终结(isOrderFinished) → 直接失败:该单应作废/取消,不能再取号 ② 已发货/已终结(isOrderFinished) → 直接失败:该单应作废/取消,不能再取号
└─ 旧系统会「平台已有运单号就直接复用」;**OMS 不采用**: └─ 旧系统会「平台已有运单号就直接复用」;**OMS 默认不采用**:
重复调用与否只由幂等表(request_id)决定,每次都真实取号,LastWaybillCode 仅用于事后对比是否换号 重复调用与否只由幂等表(request_id)决定,每次都真实取号,LastWaybillCode 仅用于事后对比是否换号
└─ 例外:平台 has_own_waybill=1(自带运单)时复用 WaybillCodes 里已确认的出号,
走 ExpressPrint 的「复用已有运单号」参数(不分配新面单,只重新下发打印数据)
③ POST /TemplateSet/CheckTemplateAvaliable (只读) ③ POST /TemplateSet/CheckTemplateAvaliable (只读)
└─ 返回 Data.Balance / PrintCount / DateNow / DayPrintBatchNumber / Branches └─ 返回 Data.Balance / PrintCount / DateNow / DayPrintBatchNumber / Branches
@ -450,7 +453,7 @@ _reqModel[ShopId]=0&_reqModel[ShopIds]=&_reqModel[Status]=0&_reqModel[PlatformTy
| `PlatformOrderId` | 平台单号(OMS `extend_props.platform_order_no`) | | `PlatformOrderId` | 平台单号(OMS `extend_props.platform_order_no`) |
| `ShopId` / `FxUserId` / `PathFlowCode` | 取号报文必填 | | `ShopId` / `FxUserId` / `PathFlowCode` | 取号报文必填 |
| `SubOrders[].OrderItemId / OrderItemCode` | 取号明细(数量固定写 1) | | `SubOrders[].OrderItemId / OrderItemCode` | 取号明细(数量固定写 1) |
| `LastWaybillCode` | 平台已有运单号(旧系统据此复用;**OMS 只用于事后对比是否换号**)。注意:**它非空不代表已经出号给业务方**,见 §4.2.4 | | `LastWaybillCode` | 平台已有运单号(旧系统据此复用;**OMS 默认只用于事后对比是否换号**,仅 `has_own_waybill=1` 时作为 `WaybillCodes` 的补充复用)。注意:**它非空不代表已经出号给业务方**,见 §4.2.4 |
| `PrintState` / `ExpressPrintTimes` / `ExpressPrintTime` | `PrintState=1` 已打印;`ExpressPrintTimes` 是该单被打印/取号的次数,`ExpressPrintTime` 是最近一次时间 | | `PrintState` / `ExpressPrintTimes` / `ExpressPrintTime` | `PrintState=1` 已打印;`ExpressPrintTimes` 是该单被打印/取号的次数,`ExpressPrintTime` 是最近一次时间 |
| `WaybillCodes` / `IsMultiPack` / `SumPackageCount` | 平台侧**已出号列表** / 是否多包裹 / 包裹数;单包裹单号场景下 `WaybillCodes` 常为空数组 | | `WaybillCodes` / `IsMultiPack` / `SumPackageCount` | 平台侧**已出号列表** / 是否多包裹 / 包裹数;单包裹单号场景下 `WaybillCodes` 常为空数组 |
| `CaiNiaoAuthInfoId` | **菜鸟授权网点ID**(模板绑定的授权网点;同平台不同网点取值不同)。OMS 已独立成列 `external_print_express_interfaces.cai_niao_auth_info_id`,并在取号结果里回传给 WMS | | `CaiNiaoAuthInfoId` | **菜鸟授权网点ID**(模板绑定的授权网点;同平台不同网点取值不同)。OMS 已独立成列 `external_print_express_interfaces.cai_niao_auth_info_id`,并在取号结果里回传给 WMS |
@ -532,8 +535,9 @@ WaybillCodeCreateDate='2026-11-04 08:20:37'
- **params 是逐张面单的**:`printDataJson.PrintDataList.task.documents[].documentID` = 运单号, - **params 是逐张面单的**:`printDataJson.PrintDataList.task.documents[].documentID` = 运单号,
`contents[].params` 是该张面单的内容;同店铺可共用的只有 `PrinterConfig` 与 `templateURL`, `contents[].params` 是该张面单的内容;同店铺可共用的只有 `PrinterConfig` 与 `templateURL`,
**不能拿 A 单的 document 去打 B 单**(会把收件人/面单号搞错)。 **不能拿 A 单的 document 去打 B 单**(会把收件人/面单号搞错)。
- 因此 OMS「按 `LastWaybillCode` 复用」是错的判断;是否重复取号由 **OMS 幂等表(request_id)** 决定, - 因此 OMS 默认不按 `LastWaybillCode` 复用;是否重复取号由 **OMS 幂等表(request_id)** 决定,
而**同一个包裹重复取号拿回同号**是平台行为,不是 bug。 而**同一个包裹重复取号拿回同号**是平台行为,不是 bug。
- 自带运单的平台(`has_own_waybill=1`)复用 `WaybillCodes`(已确认的出号),`LastWaybillCode` 仅在 `WaybillCodes` 为空时兜底。
> **多包裹 / 子单(已实测跑通,2026-11-04 09:28)** > **多包裹 / 子单(已实测跑通,2026-11-04 09:28)**
> >
@ -851,7 +855,8 @@ $documentID = $PrintDataList["task"]["documents"][0]["documentID"]; // = 运单
DGJ_WAYBILL_ENABLE=0 # 0/false/off/no 视为关闭;不设置或其他值均为开启 DGJ_WAYBILL_ENABLE=0 # 0/false/off/no 视为关闭;不设置或其他值均为开启
``` ```
- 关闭后取号直接返回「店管家真实取号已关闭(DGJ_WAYBILL_ENABLE=0):…」(不再有「复用平台运单号」这种只读成功路径); - 关闭后新号取号直接返回「店管家真实取号已关闭(DGJ_WAYBILL_ENABLE=0):…」;
自带运单平台(`has_own_waybill=1`)的复用路径不消耗面单,不受该开关影响;
- **是否重复取号只看幂等表**:同一 `request_id` 的失败/部分失败可重试,成功则回放;清表或换 `request_id` 就会再取一次号。 - **是否重复取号只看幂等表**:同一 `request_id` 的失败/部分失败可重试,成功则回放;清表或换 `request_id` 就会再取一次号。
### 6.3 自动作废的备注口径(区分「已发货」与「查不到」) ### 6.3 自动作废的备注口径(区分「已发货」与「查不到」)
@ -886,7 +891,8 @@ DGJ_WAYBILL_ENABLE=0 # 0/false/off/no 视为关闭;不设置或其他值均
`fetchWaybillNo` 回读到平台订单行后: `fetchWaybillNo` 回读到平台订单行后:
- `isOrderFinished(row)` 为真 → 直接抛错「订单在店管家已发货或已终结(ErpState=sended,平台运单号 …),取号失败:请作废或取消该单」; - `isOrderFinished(row)` 为真 → 直接抛错「订单在店管家已发货或已终结(ErpState=sended,平台运单号 …),取号失败:请作废或取消该单」;
- 否则**继续调 ExpressPrint**(不再判断 `LastWaybillCode`,也不复用)。若平台返回的号码与订单行原号码相同,响应 `message` 会注明「本次调用平台取号,但平台仍返回原运单号 X」。 - 否则**继续调 ExpressPrint**(默认不判断 `LastWaybillCode`、不复用)。若平台返回的号码与订单行原号码相同,响应 `message` 会注明「本次调用平台取号,但平台仍返回原运单号 X」;
- 平台 `has_own_waybill=1` 时不分配新号:订单还没有出号(`WaybillCodes` 与 `LastWaybillCode` 都为空)会直接报错「该平台配置为自带运单,但订单在店管家还没有运单号」。
### 6.6 手工验证脚本 ### 6.6 手工验证脚本
@ -927,6 +933,6 @@ LastWaybillCode: '772085250163045' SubOrders: 1
## 8. 安全提示 ## 8. 安全提示
- `ExpressPrint` 会**真实消耗面单**并可能产生费用;`IsIgnoreWaybillRepeat=true` 表示每次都取新号,重复调用会重复取。 - `ExpressPrint` 会**真实消耗面单**并可能产生费用;`IsIgnoreWaybillRepeat=true` 表示每次都取新号,重复调用会重复取。
- 因此:**每次请求都会真实取号**(不再复用平台已有运单号),去重只剩幂等表 `request_id` 这一道; - 因此:默认**每次请求都会真实取号**(不含 `has_own_waybill=1` 的复用路径),去重只剩幂等表 `request_id` 这一道;
联调/回归测试只用只读路径(订单行查询 + 报文字段核对),不要把真实订单反复取号。 联调/回归测试只用只读路径(订单行查询 + 报文字段核对),不要把真实订单反复取号。
- 生产环境如需启用,应由业务指定测试单、确认余额后再开开关。 - 生产环境如需启用,应由业务指定测试单、确认余额后再开开关。