feat(waybill): 取号记录补店铺标识与分区,新增回捞打印数据能力

抖店面单打印数据(params)只有 10 分钟有效期,WMS 真正打印前必须回平台换一次新的,
因此取号时要留下「定位用」的信息。

- waybill_fetch_records 新增 cai_niao_auth_info_id(=店铺唯一标识)与 dbname(取号命中的分区)
- 迁移 20261107040000 加列 + 运单号索引(principal_id + cai_niao_auth_info_id + waybill_code)
- dgj.fetchWaybillNo 回传实际命中的 dbname;waybillGet 落库并把 CaiNiaoAuthInfoId 写入留痕上下文
- dgj.loadPrintData:POST /PrintHistory/LoadPrintData(printHistoryIds[] + dbname),
  解析 Data[].ID/ExpressWaybillCode/TemplateId/PrintDataJson,只读不消耗面单

实测(只读):打印记录 10000078656212 → 运单号 770357408441408,模板 10776786,
print_data 长度 7888。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-07 06:33:15 +08:00
parent 7f8cff808b
commit 9252763aba
5 changed files with 111 additions and 0 deletions

View File

@ -49,6 +49,14 @@ const EXPRESS_PRINT_PATH = '/NewOrder/ExpressPrint';
*/
const ONLINE_SEND_PATH = '/NewOrder/OnlineSendBatch';
/**
* 按打印记录重新拉取面单打印数据(**只读,不消耗面单**)。
*
* 取号返回的打印数据(params)只有 10 分钟有效期,WMS 真正打印前要再取一次:
* `POST /PrintHistory/LoadPrintData?dbname=...`,表单 `printHistoryIds[]=...&IsCustomerOrder=false&FxPageType=1`。
*/
const PRINT_HISTORY_DATA_PATH = '/PrintHistory/LoadPrintData';
/**
* 是否允许调用真实取号(默认开启)
*
@ -2367,6 +2375,8 @@ class DgjService extends PrintPlatformService {
waybill_codes: waybillCodes,
// 打印记录ID(平台 PrintHistoryIds):重打/作废/查打印记录时平台按它回捞,仅本地留痕
print_history_ids: extractPrintHistoryIds(printed),
// 实际命中的分区库名:重取打印数据(PrintHistory/LoadPrintData)要带上同一个 dbname
dbname,
// 面单打印数据:原样透传(WMS 打印端直接塞进 task.printInfo)
printinfo: firstPrintInfo(items, printed, waybillCodes[0]),
signature: contents.signature,
@ -2647,6 +2657,49 @@ class DgjService extends PrintPlatformService {
return res.json;
}
/**
* 按打印记录重新拉取面单打印数据(只读,不消耗面单)
*
* 取号返回的打印数据(params)**只有 10 分钟有效期**,WMS 真正打印前要再用一次本接口换新的;
* 平台入参为打印记录ID(`PrintHistoryIds[].Id`)数组 + 分区 dbname。
* @param {string} host 平台域名
* @param {Object} options 参数
* @param {string} options.traceId 登录上下文 traceId
* @param {string} options.dbname 分区库名(取号时命中的那个)
* @param {Array<string|number>} options.printHistoryIds 打印记录ID数组
* @return {Promise<Array<{print_history_id: string, waybill_code: string, template_id: string, print_data: string, raw: Object}>>} 打印数据
*/
async loadPrintData(host, { traceId = '', dbname = '', printHistoryIds = [] }) {
const ids = (printHistoryIds || [])
.map(value => String(value == null ? '' : value).trim())
.filter(Boolean);
if (!ids.length) throw new Error('重新获取打印数据需要打印记录ID(printHistoryIds)');
if (!this.token || !this.cookie) {
throw new Error('店管家账号未登录:缺少 token 或 cookie,请先登录');
}
const form = buildPhpForm({
printHistoryIds: ids,
IsCustomerOrder: 'false',
FxPageType: '1',
});
const res = await this.postPlatformForm(host, PRINT_HISTORY_DATA_PATH, form, traceId, dbname);
if (!res.json || res.json.Success !== true) {
const reason = (res.json && (res.json.Message || res.json.ErrorCode)) || `HTTP ${res.status}`;
throw buildPlatformError(`店管家读取打印记录失败:${reason}`, res);
}
const rows = Array.isArray(res.json.Data) ? res.json.Data : [];
return rows.map(row => ({
print_history_id: String(row && row.ID == null ? '' : row.ID),
waybill_code: String((row && row.ExpressWaybillCode) || ''),
template_id: row && row.TemplateId == null ? '' : String(row.TemplateId),
// 平台原样返回的打印数据(params):WMS 打印端直接使用
print_data: String((row && row.PrintDataJson) || ''),
raw: row,
}));
}
/**
* 通知平台发货(旧系统 `Dianguanjia.php#OnlineSendBatch` 同口径)
*

View File

@ -419,6 +419,7 @@ async function loadAccountModel(ctx, memo, accountId, principalId) {
* @param {string} [extra.printinfo] 面单打印数据
* @param {string} [extra.signature] 面单签名
* @param {Array<Object>} [extra.print_history_ids] 打印记录(平台 PrintHistoryIds:id/pid/waybill_code/batch_index)
* @param {string} [extra.dbname] 实际命中的打单平台分区
* @return {Promise<void>} 无返回值
*/
async function saveRecord(ctx, meta, result, extra = {}) {
@ -441,6 +442,9 @@ async function saveRecord(ctx, meta, result, extra = {}) {
printinfo: extra.printinfo || '',
signature: extra.signature || '',
print_history_ids: Array.isArray(extra.print_history_ids) ? JSON.stringify(extra.print_history_ids) : '',
// 店铺唯一标识 + 分区:WMS 重取打印数据(params 只有 10 分钟有效期)时按「它 + 运单号」定位
cai_niao_auth_info_id: meta.caiNiaoAuthInfoId == null ? null : Number(meta.caiNiaoAuthInfoId),
dbname: extra.dbname || '',
raw_response: raw ? JSON.stringify(raw) : '',
error_message: result && result.success ? '' : (result && result.message) || '',
});
@ -553,6 +557,8 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode,
meta.templateId = String(template.template_id || (template.raw && template.raw.Id) || '');
// 菜鸟授权网点ID:随取号结果回给 WMS(同平台不同网点取号用的授权不同)
const caiNiaoAuthInfoId = template.cai_niao_auth_info_id == null ? null : Number(template.cai_niao_auth_info_id);
// 留痕用:店铺唯一标识(WMS 重取打印数据时按它 + 运单号定位)
meta.caiNiaoAuthInfoId = caiNiaoAuthInfoId;
const orderPlain = order.get ? order.get({ plain: true }) : { ...order };
// 1688 分销单被换算成抖音小店/淘宝后,取号域名仍要按原始平台(否则回读订单行查错域名)
@ -574,6 +580,8 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode,
detail.raw = fetched && fetched.raw ? fetched.raw : null;
// 打印记录ID(平台 PrintHistoryIds):仅本地留痕,不进 WMS 响应契约
detail.print_history_ids = Array.isArray(fetched && fetched.print_history_ids) ? fetched.print_history_ids : [];
// 实际命中的打单平台分区(重取打印数据时要用同一个 dbname)
detail.dbname = String((fetched && fetched.dbname) || '');
detail.printinfo = String((fetched && fetched.printinfo) || '');
detail.signature = String((fetched && fetched.signature) || '');
detail.encryptprintinfo = String((fetched && fetched.encryptprintinfo) || '');

View File

@ -32,6 +32,11 @@ module.exports = app => {
type: TEXT,
comment: '打印记录(平台 PrintHistoryIds,JSON 数组:id/pid/waybill_code/batch_index),重打/作废用',
},
cai_niao_auth_info_id: {
type: BIGINT.UNSIGNED,
comment: '菜鸟授权网点ID(=店铺唯一标识,重取打印数据时按它+运单号定位)',
},
dbname: { type: STRING(100), comment: '实际命中的打单平台分区(重取打印数据要带同一个 dbname)' },
raw_response: { type: TEXT('medium'), comment: '平台原始返回(JSON)' },
error_message: { type: TEXT, comment: '失败原因' },
created_at: { type: DATE, comment: '创建时间' },

View File

@ -0,0 +1,40 @@
'use strict';
/**
* 取号记录补「店铺唯一标识 + 分区」:`cai_niao_auth_info_id` / `dbname`。
*
* 店管家的面单打印数据(params)只有 **10 分钟**有效期,WMS 真正打印时需要重新拉一次
* (`POST /PrintHistory/LoadPrintData`,入参 printHistoryIds[] + dbname)。
* 因此取号时把「店铺唯一标识(= 模板绑定的 CaiNiaoAuthInfoId)」与「实际命中的分区 dbname」
* 一起落库,后续按「CaiNiaoAuthInfoId + 运单号」即可定位到取号记录、再回平台换新的打印数据。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('waybill_fetch_records', 'cai_niao_auth_info_id', {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '菜鸟授权网点ID(=店铺唯一标识,重取打印数据时按它+运单号定位)',
after: 'print_history_ids',
});
await queryInterface.addColumn('waybill_fetch_records', 'dbname', {
type: Sequelize.STRING(100),
allowNull: true,
comment: '实际命中的打单平台分区(重取打印数据要带同一个 dbname)',
after: 'cai_niao_auth_info_id',
});
// 按运单号回查取号记录(WMS 只带运单号 + 店铺标识)
await queryInterface.addIndex('waybill_fetch_records', [ 'waybill_code' ], {
name: 'idx_waybill_fetch_records_waybill_code',
});
await queryInterface.addIndex('waybill_fetch_records', [ 'principal_id', 'cai_niao_auth_info_id', 'waybill_code' ], {
name: 'idx_waybill_fetch_records_shop_waybill',
});
},
async down(queryInterface) {
await queryInterface.removeIndex('waybill_fetch_records', 'idx_waybill_fetch_records_shop_waybill');
await queryInterface.removeIndex('waybill_fetch_records', 'idx_waybill_fetch_records_waybill_code');
await queryInterface.removeColumn('waybill_fetch_records', 'dbname');
await queryInterface.removeColumn('waybill_fetch_records', 'cai_niao_auth_info_id');
},
};

View File

@ -268,6 +268,11 @@ WaybillCodeCreateDate='2026-11-04 08:20:37'
**`Id` 即 printHistoryId**,每打一张新增一条;`Pid` 与 `WaybillCode[].WaybillCodePid`、`WaybillCodeId` 是不同概念,别混。
OMS 把解析结果(`id/pid/waybill_code/batch_index` 数组)落到 `waybill_fetch_records.print_history_ids`,
供后续重打/作废(`PrintHistory/LoadPrintData` 按它回捞)使用,暂不回传 WMS。
- **打印数据(params)只有 10 分钟有效期**:取号返回的 `printinfo` 不能长期保存着用,WMS 真正打印前必须重新拉一次
`POST /PrintHistory/LoadPrintData?token=&dbname=<取号命中的分区>`,表单 `printHistoryIds[]=<Id>&IsCustomerOrder=false&FxPageType=1`;
响应 `Data[0].PrintDataJson` 就是新的 params(实测 7888 字符,`id/sendContent/printDataJson`)。
因此取号记录里同时落了 `dbname` 与 `cai_niao_auth_info_id`(= 店铺唯一标识),
后续按「店铺标识 + 运单号」即可定位到该运单号对应的打印记录,再回平台换新数据。
- 因此 OMS「按 `LastWaybillCode` 复用」是错的判断;是否重复取号由 **OMS 幂等表(request_id)** 决定,
而**同一个包裹重复取号拿回同号**是平台行为,不是 bug。