取号响应改用 pdf_base64 返回面单 PDF
- dxm.fetchWaybillNo 改为返回 pdf_base64(纯 base64,不带 data: 前缀),不再塞进 printinfo - waybillGet 透传 pdf_base64 给 WMS(printinfo 仍留给店管家等 JSON/加密串口径的平台) - waybill_fetch_records 新增 pdf_base64 列(迁移,TEXT medium); result_data 里只留 pdf_base64_length 避免同一张 PDF 存三份,重放时从列补回 - 文档补:WMS 取号响应新增 pdf_base64 字段(含 WMS 落库/打印建议)、店小秘 6.1 存储口径 - 补 4 条单测(dxm 返回 pdf_base64、PDF 失败降级、取号透传、重放补回) - 实测真实账号:运单号 3CAB6111900043,pdf_base64 88,796 字符,解码为 %PDF-1.4 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
5ca50e9ed9
commit
5d424f2c98
@ -206,6 +206,7 @@ OMS 侧实现:`D:\oms\backend\app\lib\wms\sign.js`。
|
||||
| `cai_niao_auth_info_id` | 成功 | **菜鸟授权网点ID**(模板绑定的 `CaiNiaoAuthInfoId`,如视频号 `6890457`、淘宝 `364375/393235`);同平台不同授权网点取值不同,WMS 需要按网点归集/对账时用它,取不到为 `null` |
|
||||
| `printinfo` | 成功 | **面单打印数据**,打单平台原样返回:店管家取 `Data.PrintDataList.task.documents[].printInfo`(base64 的 URL-encoded JSON);**快手取 `documents[].contents[0].encryptedData`**(快手该单据的 `printInfo` 为空);**菜鸟系(TB/TM/TGC/1688)回拼整包 JSON** `{"encryptedData","signature","ver","templateURL"}`(模板地址取 `contents[0].templateURL`,菜鸟云打印必需)。WMS 落 `order_express.printinfo`,打印时**原样**塞进 `task.printInfo`,不要解码 |
|
||||
| `signature` | 成功 | 面单签名,取 `documents[].contents[0].signature`;视频号自定义模板平台不返回 `contents`,此时为空串 |
|
||||
| `pdf_base64` | 成功(PDF 面单平台) | **面单 PDF 内容(纯 base64,不含 `data:` 前缀)**:店小秘(dxm)的面单是 PDF 文件,OMS 取号时一并拉下来放这里,`printinfo` 为空串。WMS 落 `order_express.pdf_base64`(建议 LONGTEXT/MEDIUMTEXT),打印时把 base64 解码成 `.pdf` 交给打印服务(实测单张 66KB PDF → 88,796 字符)。拉取失败时为空串,运单号照常返回(面单可重打) |
|
||||
| `encryptprintinfo` | 成功 | 加密打印数据:其它平台取 `contents[0].encryptedData`;**快手取 `contents[0].key`**(与菠萝派口径一致,WMS 快手打印的 `key` 参数) |
|
||||
| `key` | 成功 | **快手逐单加密密钥**(base64,约 172 字符,逐单不同),取 `contents[0].key`;其余平台为空串 |
|
||||
| `ver` | 成功 | 打印数据版本,取 `contents[0].ver`(无则为空串) |
|
||||
@ -213,7 +214,7 @@ OMS 侧实现:`D:\oms\backend\app\lib\wms\sign.js`。
|
||||
| `print_history_id` | 成功 | 第一张面单的打印记录ID(`print_history_ids[0].id` 的便捷值,多包裹时以数组为准) |
|
||||
|
||||
> **取号结果不回写 OMS 订单表**(现阶段取号与订单表不做关联),运单号与打印数据只在响应里返回,由 WMS 自行落单。
|
||||
> **WMS 仍需落自己的 `order_express`**(`waybill_code` + `printinfo` + `signature` + `encryptprintinfo` + `ver` + `cai_niao_auth_info_id` + `print_history_ids`,快手另有 `key`)**/ 队列状态**,不要依赖 OMS 的库。
|
||||
> **WMS 仍需落自己的 `order_express`**(`waybill_code` + `printinfo` + `signature` + `encryptprintinfo` + `ver` + `cai_niao_auth_info_id` + `print_history_ids`,快手另有 `key`,PDF 面单平台另有 `pdf_base64`)**/ 队列状态**,不要依赖 OMS 的库。
|
||||
> 每次取号的平台原始返回都会写进 OMS 的 `waybill_fetch_records`(见 §4.7),出问题可直接查表对账,不必再让 WMS 重放报文。
|
||||
>
|
||||
> **同一包裹重复取号会拿到新号**:店管家「新单号打印」口径是每次分配一个新号(`WaybillCodePid` 第二段自增),
|
||||
|
||||
@ -723,11 +723,11 @@ class DxmService extends PrintPlatformService {
|
||||
* 拉单时已随单存进 `extend_props.platform_waybill_code`。这里不再调用面单接口,
|
||||
* 避免重复申请面单;平台上还没有运单号时(物流商未交运)明确报错。
|
||||
*
|
||||
* 取号同时把面单 PDF 拉下来(`printinfo` 存 `data:application/pdf;base64,...`),
|
||||
* 取号同时把面单 PDF 拉下来,以 `pdf_base64`(纯 base64,不含 `data:` 前缀)返回,
|
||||
* 供 WMS 落单后直接打印;PDF 拉取失败不影响取号结果(运单号已在平台存在,可稍后重打)。
|
||||
* @param {Object} order OMS 订单(extend_props.platform_waybill_code / platform_auth_id / express_code)
|
||||
* @param {Object} [options] 取号上下文(店小秘不需要模板/快递编码)
|
||||
* @return {Promise<Object>} 取号结果(waybill_code / waybill_codes / printinfo / raw)
|
||||
* @return {Promise<Object>} 取号结果(waybill_code / waybill_codes / pdf_base64 / raw)
|
||||
*/
|
||||
async fetchWaybillNo(order, options = {}) { // eslint-disable-line no-unused-vars
|
||||
if (!this.cookie) throw new Error('店小秘账号未登录:缺少 cookie,请先登录');
|
||||
@ -742,16 +742,17 @@ class DxmService extends PrintPlatformService {
|
||||
(order && (order.parent_order_no || String(order.order_no || '').replace(/^[^-]+-/, ''))) || ''
|
||||
).trim();
|
||||
const raw = { agentTrackNum: code };
|
||||
let printinfo = '';
|
||||
let pdfBase64 = '';
|
||||
try {
|
||||
const pdf = await this.fetchPrintPdf({ authId: extendProps.platform_auth_id || '', packageId });
|
||||
printinfo = `data:${pdf.mime};base64,${pdf.base64}`;
|
||||
pdfBase64 = pdf.base64;
|
||||
raw.print = {
|
||||
package_id: packageId,
|
||||
mime: pdf.mime,
|
||||
file_name: pdf.meta.fileName || '',
|
||||
print_format: pdf.meta.printFormat || '',
|
||||
print_mb_formats: pdf.meta.printMbFormats || '',
|
||||
size: pdf.base64.length,
|
||||
size: pdfBase64.length,
|
||||
};
|
||||
} catch (err) {
|
||||
// 面单可以重拉,不因为打印数据失败丢掉已经存在的运单号
|
||||
@ -761,7 +762,7 @@ class DxmService extends PrintPlatformService {
|
||||
return {
|
||||
waybill_code: code,
|
||||
waybill_codes: [ code ],
|
||||
printinfo,
|
||||
pdf_base64: pdfBase64,
|
||||
raw,
|
||||
};
|
||||
}
|
||||
|
||||
@ -429,6 +429,7 @@ async function loadAccountModel(ctx, memo, accountId, principalId) {
|
||||
* @param {Object} [extra] 平台返回的补充数据
|
||||
* @param {string} [extra.raw] 平台原始返回
|
||||
* @param {string} [extra.printinfo] 面单打印数据
|
||||
* @param {string} [extra.pdf_base64] 面单 PDF 内容(base64,PDF 面单平台)
|
||||
* @param {string} [extra.signature] 面单签名
|
||||
* @param {Array<Object>} [extra.print_history_ids] 打印记录(平台 PrintHistoryIds:id/pid/waybill_code/batch_index)
|
||||
* @param {string} [extra.dbname] 实际命中的打单平台分区
|
||||
@ -452,8 +453,9 @@ async function saveRecord(ctx, meta, result, extra = {}) {
|
||||
waybill_source: (result && result.waybill_source) || '',
|
||||
waybill_code: (result && result.waybill_code) || '',
|
||||
waybill_codes: result && result.waybill_codes ? JSON.stringify(result.waybill_codes) : '',
|
||||
result_data: result ? JSON.stringify(result) : '',
|
||||
result_data: stringifyResult(result),
|
||||
printinfo: extra.printinfo || '',
|
||||
pdf_base64: extra.pdf_base64 || '',
|
||||
signature: extra.signature || '',
|
||||
print_history_ids: Array.isArray(extra.print_history_ids) ? JSON.stringify(extra.print_history_ids) : '',
|
||||
// 店铺唯一标识 + 分区:WMS 重取打印数据(params 只有 10 分钟有效期)时按「它 + 运单号」定位
|
||||
@ -505,9 +507,25 @@ function parseSavedResult(entry) {
|
||||
if (!result || !result.success || !result.waybill_code) {
|
||||
throw new Error('保存的取号结果不完整');
|
||||
}
|
||||
// 面单 PDF 单独存在列里(result_data 只留长度),重放时补回来
|
||||
if (!result.pdf_base64 && entry.pdf_base64) result.pdf_base64 = entry.pdf_base64;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取号结果转存储文本:面单 PDF(base64)只留在 `pdf_base64` 列,`result_data` 里只记长度
|
||||
*
|
||||
* 一张 PDF ≈ 88KB base64,同时写进 result_data 和列里会翻倍占空间。
|
||||
* @param {Object} result 取号结果
|
||||
* @return {string} JSON 文本
|
||||
*/
|
||||
function stringifyResult(result) {
|
||||
if (!result) return '';
|
||||
const { pdf_base64: pdfBase64, ...rest } = result;
|
||||
if (pdfBase64) rest.pdf_base64_length = pdfBase64.length;
|
||||
return JSON.stringify(rest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容幂等表上线前已成功的默认包裹记录。
|
||||
* 历史记录没有 package_index 时,仅在单号本身没有子包裹序号且请求序号为 0 时复用。
|
||||
@ -551,7 +569,7 @@ async function findLegacyRecord(ctx, { principalId, warehouseId, code, expressCo
|
||||
*/
|
||||
function resultFromLegacyRecord(row, { code, expressCode, packageIndex }) {
|
||||
if (row.result_data) {
|
||||
return parseSavedResult({ result_data: row.result_data });
|
||||
return parseSavedResult({ result_data: row.result_data, pdf_base64: row.pdf_base64 });
|
||||
}
|
||||
const waybillCodes = parseRecordArray(row.waybill_codes, 'waybill_codes');
|
||||
const printHistoryIds = parseRecordArray(row.print_history_ids, 'print_history_ids');
|
||||
@ -569,6 +587,7 @@ function resultFromLegacyRecord(row, { code, expressCode, packageIndex }) {
|
||||
package_index: packageIndex,
|
||||
cai_niao_auth_info_id: row.cai_niao_auth_info_id == null ? null : Number(row.cai_niao_auth_info_id),
|
||||
printinfo: row.printinfo || '',
|
||||
pdf_base64: row.pdf_base64 || '',
|
||||
signature: row.signature || '',
|
||||
print_history_ids: printHistoryIds,
|
||||
print_history_id: printHistoryIds[0] ? String(printHistoryIds[0].id || '') : '',
|
||||
@ -722,7 +741,16 @@ async function processItemWithIdempotency(ctx, {
|
||||
accountId: null,
|
||||
templateId: null,
|
||||
};
|
||||
const detail = { raw: null, printinfo: '', signature: '', encryptprintinfo: '', key: '', ver: '', print_history_ids: [] };
|
||||
const detail = {
|
||||
raw: null,
|
||||
printinfo: '',
|
||||
pdf_base64: '',
|
||||
signature: '',
|
||||
encryptprintinfo: '',
|
||||
key: '',
|
||||
ver: '',
|
||||
print_history_ids: [],
|
||||
};
|
||||
const result = await callPlatform(ctx, {
|
||||
principalId,
|
||||
warehouseId,
|
||||
@ -870,6 +898,8 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode,
|
||||
detail.dbname = String((fetched && fetched.dbname) || '');
|
||||
detail.platform_host = String((fetched && fetched.host) || '');
|
||||
detail.printinfo = String((fetched && fetched.printinfo) || '');
|
||||
// 面单 PDF(base64):店小秘等以 PDF 出面的平台,原样透传给 WMS 并落取号记录
|
||||
detail.pdf_base64 = String((fetched && fetched.pdf_base64) || '');
|
||||
detail.signature = String((fetched && fetched.signature) || '');
|
||||
detail.encryptprintinfo = String((fetched && fetched.encryptprintinfo) || '');
|
||||
detail.key = String((fetched && fetched.key) || '');
|
||||
@ -887,6 +917,7 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode,
|
||||
platformCode,
|
||||
waybillCode: waybill.waybill_code,
|
||||
printinfoLength: detail.printinfo.length,
|
||||
pdfBase64Length: detail.pdf_base64.length,
|
||||
keyLength: detail.key.length,
|
||||
});
|
||||
return {
|
||||
@ -905,6 +936,8 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode,
|
||||
cai_niao_auth_info_id: caiNiaoAuthInfoId,
|
||||
// 面单打印数据:平台原样返回,WMS 落 order_express 后打印时直接使用
|
||||
printinfo: detail.printinfo,
|
||||
// 面单 PDF 内容(纯 base64,不含 data: 前缀):PDF 面单平台(店小秘)走这里,WMS 直接落文件打印
|
||||
pdf_base64: detail.pdf_base64,
|
||||
signature: detail.signature,
|
||||
// 打印记录ID(平台 PrintHistoryIds):WMS 自行落库;重取打印数据时可直接用 printHistoryIds 直传
|
||||
print_history_ids: Array.isArray(detail.print_history_ids) ? detail.print_history_ids : [],
|
||||
|
||||
@ -29,6 +29,7 @@ module.exports = app => {
|
||||
waybill_codes: { type: TEXT, comment: '全部运单号(JSON 数组)' },
|
||||
result_data: { type: TEXT('medium'), comment: '完整的逐单返回结果(JSON)' },
|
||||
printinfo: { type: TEXT('medium'), comment: '面单打印数据(平台原样返回)' },
|
||||
pdf_base64: { type: TEXT('medium'), comment: '面单 PDF 内容(base64,不含 data: 前缀;店小秘等 PDF 面单平台)' },
|
||||
signature: { type: TEXT('medium'), comment: '面单签名(平台原样返回)' },
|
||||
print_history_ids: {
|
||||
type: TEXT,
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 取号记录增加面单 PDF 内容(base64)。
|
||||
*
|
||||
* 店小秘这类平台的面单是 PDF 文件(`printExpressNote.json?id=<包裹ID>`),
|
||||
* 取号时一并拉下来,base64 原样留痕并回给 WMS 打印;
|
||||
* 与店管家等「JSON/加密串」口径的 `printinfo` 分开存放,避免两种形态混在一列。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('waybill_fetch_records', 'pdf_base64', {
|
||||
type: Sequelize.TEXT('medium'),
|
||||
comment: '面单 PDF 内容(base64,不含 data: 前缀;店小秘等 PDF 面单平台)',
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeColumn('waybill_fetch_records', 'pdf_base64');
|
||||
},
|
||||
};
|
||||
@ -463,7 +463,8 @@ describe('printPlatform/dxm fetchWaybillNo', () => {
|
||||
assert.deepStrictEqual(calls, [{ authId: '182586000431422845', packageId: '184804424059302467' }]);
|
||||
assert.strictEqual(result.waybill_code, '3CAB6112100007');
|
||||
assert.deepStrictEqual(result.waybill_codes, [ '3CAB6112100007' ]);
|
||||
assert.strictEqual(result.printinfo, 'data:application/pdf;base64,JVBERi0xLjQ=');
|
||||
// 面单 PDF 用 pdf_base64 返回(纯 base64,不带 data: 前缀)
|
||||
assert.strictEqual(result.pdf_base64, 'JVBERi0xLjQ=');
|
||||
assert.strictEqual(result.raw.print.file_name, 'singleCache/a.pdf');
|
||||
assert.strictEqual(result.raw.print_error, undefined);
|
||||
});
|
||||
@ -478,7 +479,7 @@ describe('printPlatform/dxm fetchWaybillNo', () => {
|
||||
extend_props: { platform_waybill_code: 'YT123456' },
|
||||
});
|
||||
assert.strictEqual(result.waybill_code, 'YT123456');
|
||||
assert.strictEqual(result.printinfo, '');
|
||||
assert.strictEqual(result.pdf_base64, '');
|
||||
assert.match(result.raw.print_error, /HTTP 500/);
|
||||
});
|
||||
|
||||
|
||||
@ -295,4 +295,52 @@ describe('lib/waybillGet 订单级幂等', () => {
|
||||
assert.strictEqual(state.platformRoutes.length, 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',
|
||||
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');
|
||||
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.platformRoutes.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
10
店小秘接入.md
10
店小秘接入.md
@ -280,11 +280,13 @@ listOrders / fetchWaybillNo`,`print` 及发货回传仍继承基类报「未
|
||||
|
||||
- `dxm.js#fetchPrintPdf({authId, packageId})`:先用 `checkPrint.json` 校验并拿到面单格式,
|
||||
再 `GET printExpressNote.json?id=<包裹ID>` 取 PDF 流,返回 `{ mime, base64, meta }`;
|
||||
- `fetchWaybillNo` 取号时顺带拉面单,`printinfo` 返回 `data:application/pdf;base64,...`
|
||||
(实测 66KB PDF → 88,796 字符),由取号链路原样写进 `waybill_fetch_records.printinfo`
|
||||
(`TEXT('medium')`,上限 16MB)并回给 WMS;PDF 拉取失败只记 `raw.print_error`,
|
||||
- `fetchWaybillNo` 取号时顺带拉面单,以 **`pdf_base64`(纯 base64,不含 `data:` 前缀)** 返回,
|
||||
由取号链路(`waybillGet`)透传给 WMS 并写进 `waybill_fetch_records.pdf_base64`
|
||||
(`TEXT('medium')`,上限 16MB);`printinfo` 留空。PDF 拉取失败只记 `raw.print_error`,
|
||||
不影响运单号返回(面单可以重拉);
|
||||
- `raw.print` 留痕 `package_id / file_name / print_format / print_mb_formats`,
|
||||
- 取号结果里的 `pdf_base64` 只在响应/`pdf_base64` 列各存一份:`result_data` 里只记
|
||||
`pdf_base64_length`,避免同一张 PDF 存三遍;
|
||||
- `raw.print` 留痕 `package_id / mime / file_name / print_format / print_mb_formats`,
|
||||
`fileName` 仅作诊断,不能当下载链接;
|
||||
- 不选 COS 的原因:面单含收件人隐私,公开桶有泄露风险、私有桶签名 URL 又会过期;
|
||||
需要离线归档时再落到 `files`(`/api/files/raw/<主体ID>/<年>/<月>/<日>/<hash>.pdf`,路径不可枚举)。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user