diff --git a/backend/app/lib/printPlatform/dgj.js b/backend/app/lib/printPlatform/dgj.js index 16c02be..b9fe80a 100644 --- a/backend/app/lib/printPlatform/dgj.js +++ b/backend/app/lib/printPlatform/dgj.js @@ -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} options.printHistoryIds 打印记录ID数组 + * @return {Promise>} 打印数据 + */ + 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` 同口径) * diff --git a/backend/app/lib/waybillGet.js b/backend/app/lib/waybillGet.js index 2a72ff1..3c62dd0 100644 --- a/backend/app/lib/waybillGet.js +++ b/backend/app/lib/waybillGet.js @@ -419,6 +419,7 @@ async function loadAccountModel(ctx, memo, accountId, principalId) { * @param {string} [extra.printinfo] 面单打印数据 * @param {string} [extra.signature] 面单签名 * @param {Array} [extra.print_history_ids] 打印记录(平台 PrintHistoryIds:id/pid/waybill_code/batch_index) + * @param {string} [extra.dbname] 实际命中的打单平台分区 * @return {Promise} 无返回值 */ 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) || ''); diff --git a/backend/app/model/waybillFetchRecord.js b/backend/app/model/waybillFetchRecord.js index e85b404..3cc5235 100644 --- a/backend/app/model/waybillFetchRecord.js +++ b/backend/app/model/waybillFetchRecord.js @@ -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: '创建时间' }, diff --git a/backend/database/migrations/20261107040000-add-shop-and-dbname-to-waybill-fetch-records.js b/backend/database/migrations/20261107040000-add-shop-and-dbname-to-waybill-fetch-records.js new file mode 100644 index 0000000..13f175f --- /dev/null +++ b/backend/database/migrations/20261107040000-add-shop-and-dbname-to-waybill-fetch-records.js @@ -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'); + }, +}; diff --git a/店管家取号-旧系统提取.md b/店管家取号-旧系统提取.md index 33d5f45..087b878 100644 --- a/店管家取号-旧系统提取.md +++ b/店管家取号-旧系统提取.md @@ -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[]=&IsCustomerOrder=false&FxPageType=1`; + 响应 `Data[0].PrintDataJson` 就是新的 params(实测 7888 字符,`id/sendContent/printDataJson`)。 + 因此取号记录里同时落了 `dbname` 与 `cai_niao_auth_info_id`(= 店铺唯一标识), + 后续按「店铺标识 + 运单号」即可定位到该运单号对应的打印记录,再回平台换新数据。 - 因此 OMS「按 `LastWaybillCode` 复用」是错的判断;是否重复取号由 **OMS 幂等表(request_id)** 决定, 而**同一个包裹重复取号拿回同号**是平台行为,不是 bug。