店小秘自带运单:回源取号直接读 agentTrackNum

- 拉单归一化补 waybill_code(agentTrackNum,回退 trackingNumber)与 platform_auth_id(authId)
- 待审核订单写入 extend_props.platform_waybill_code / platform_auth_id,
  重新拉单会刷新(店小秘换号后跟着更新)
- dxm.fetchWaybillNo 不再申请新面单,直接返回平台运单号;没有运单号时提示先交运出号再拉单
- 文档补 5.2 运单号口径,并记录 checkPrint.json / printExpressNote.json 取 PDF 的实测结论
- 补 2 条单测

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-24 02:05:43 +08:00
parent 338d8fca14
commit f214c24e30
4 changed files with 103 additions and 4 deletions

View File

@ -639,6 +639,10 @@ class DxmService extends PrintPlatformService {
logistics_code: logistics ? logistics.code : null,
logistics_name: logistics ? logistics.name : null,
logistics_provider_name: providerName || null,
// 平台运单号(物流商已交运):店小秘自带运单,回源取号直接读它,不再申请新面单
waybill_code: String(data.agentTrackNum || data.trackingNumber || '').trim() || null,
// 店铺授权ID(取打印数据 checkPrint 的入参 authId)
platform_auth_id: String(data.authId || '').trim() || null,
order_no: externalOrderId,
total_amount: Number((goodsAmount + shipAmount).toFixed(2)),
pay_amount: Number((goodsAmount + shipAmount).toFixed(2)),
@ -666,6 +670,29 @@ class DxmService extends PrintPlatformService {
};
}
/**
* 回源取号:店小秘自带运单,直接读平台的 `agentTrackNum`
*
* 店小秘的运单号由「系统对接物流商」在交运后写入订单报文(`agentTrackNum` / `trackingNumber`),
* 拉单时已随单存进 `extend_props.platform_waybill_code`。这里不再调用面单接口,
* 避免重复申请面单;平台上还没有运单号时(物流商未交运)明确报错。
* @param {Object} order OMS 订单(extend_props.platform_waybill_code / express_code)
* @param {Object} [options] 取号上下文(店小秘不需要模板/快递编码)
* @return {Promise<Object>} 取号结果(waybill_code / waybill_codes / raw)
*/
async fetchWaybillNo(order, options = {}) { // eslint-disable-line no-unused-vars
const extendProps = order && order.extend_props && typeof order.extend_props === 'object' ? order.extend_props : {};
const code = String(extendProps.platform_waybill_code || (order && order.express_code) || '').trim();
if (!code) {
throw new Error('店小秘订单还没有平台运单号(agentTrackNum):请先在店小秘为物流商交运出号,再重新拉单');
}
return {
waybill_code: code,
waybill_codes: [ code ],
raw: { agentTrackNum: code },
};
}
/**
* 店小秘订单明细 → 统一明细结构
*

View File

@ -743,6 +743,10 @@ class ExternalPrintOrderService extends Service {
platform_shop_id: item.external_shop_id || null,
platform_shop_name: item.external_shop_name || null,
// 平台单号(店管家「订单编号」)与来源订单号(「系统编号」)分开保存,便于对账
// 平台运单号(物流商交运后由平台写入 agentTrackNum):店小秘自带运单,取号时直接复用
platform_waybill_code: item.waybill_code || null,
// 店铺授权ID(重新取打印数据 checkPrint 的 authId 入参)
platform_auth_id: item.platform_auth_id || null,
platform_order_no: item.platform_order_no || null,
// 店小秘物流商渠道名(如「跨源速递-元驿MetaHub-普货」),快递公司取物流商一级
logistics_provider_name: item.logistics_provider_name || null,

View File

@ -290,6 +290,8 @@ describe('printPlatform/dxm listOrders', () => {
orderAmount: 54.9,
shipAmount: 6.9,
agentProviderName: '跨源速递-元驿MetaHub-普货',
agentTrackNum: '3CAB6112100007',
authId: '182586000431422845',
orderCreateTime: Date.UTC(2026, 10, 4, 0, 0, 0),
updateTime: Date.UTC(2026, 10, 5, 0, 0, 0),
dxmPackageAddr: {
@ -365,6 +367,9 @@ describe('printPlatform/dxm listOrders', () => {
assert.strictEqual(order.logistics_code, 'KJKYSD');
assert.strictEqual(order.logistics_name, '跨源速递');
assert.strictEqual(order.logistics_provider_name, '跨源速递-元驿MetaHub-普货');
// 平台运单号与店铺授权ID(取打印数据用)
assert.strictEqual(order.waybill_code, '3CAB6112100007');
assert.strictEqual(order.platform_auth_id, '182586000431422845');
assert.strictEqual(order.total_amount, 61.8);
assert.strictEqual(order.pay_amount, 61.8);
assert.deepStrictEqual(order.receiver, {
@ -441,6 +446,26 @@ describe('printPlatform/dxm listOrders', () => {
});
});
describe('printPlatform/dxm fetchWaybillNo', () => {
it('直接读取拉单存下的平台运单号(agentTrackNum),不再申请面单', async () => {
const { service } = createService({ account: { cookie: 'JSESSIONID=ok' } });
const result = await service.fetchWaybillNo({
order_no: 'dxm-184804424059302467',
extend_props: { platform_waybill_code: '3CAB6112100007' },
});
assert.strictEqual(result.waybill_code, '3CAB6112100007');
assert.deepStrictEqual(result.waybill_codes, [ '3CAB6112100007' ]);
});
it('订单上没有平台运单号时提示先出号再拉单', async () => {
const { service } = createService({ account: { cookie: 'JSESSIONID=ok' } });
await assert.rejects(
service.fetchWaybillNo({ order_no: 'dxm-1', extend_props: {} }),
/还没有平台运单号/
);
});
});
describe('printPlatform/dxm listShops', () => {
const shopifyShop = (id, name, extra = { }) => ({
id: String(id),

View File

@ -227,13 +227,56 @@ OMS 实现(`dxm.js#listOrders`):
- 该值只作为**待审核订单的默认快递**(`order_status_pending.logistics_code/logistics_name`),
人工改过快递(「修改快递」)的订单重新拉单不覆盖;没有 `agentProviderName` 的订单不写快递公司。
### 5.2 运单号(agentTrackNum)与回源取号
店小秘的运单号由「系统对接物流商」在交运后写进订单报文:
| 字段 | 含义 |
|---|---|
| `agentTrackNum` | 运单号(`trackingNumber` 同值,优先取前者) |
| `authId` | 店铺授权ID(取打印数据 `checkPrint.json` 的入参) |
| `applyTrackNumStatus` | 取号状态,实测 `success` |
| `agentProviderName` | 物流商 + 渠道(见 5.1) |
- 拉单时把运单号存进 `order_status_pending.extend_props.platform_waybill_code`,
店铺授权ID 存 `extend_props.platform_auth_id`;
- `dxm.js#fetchWaybillNo` **不再向平台申请面单**,直接读 `platform_waybill_code` 回给调用方
(fallback:订单 `express_code`);没有运单号说明物流商尚未交运,报
「还没有平台运单号(agentTrackNum):请先在店小秘为物流商交运出号,再重新拉单」;
- 重新拉单会刷新运单号快照(店小秘换号后 OMS 跟着更新)。
---
## 6. 待接入
## 6. 打印数据(PDF)与待接入
`dxm.js` 目前实现 `getLoginCaptcha / login / checkSession / listShopSources / listShops / listProducts / listOrders`,
其余方法继承基类会明确报「未实现」。后续按以下顺序补:
面单打印数据(实测 2026-11-24,用真实账号跑通):
1. 取号 / 打单 / 发货回传 → `fetchWaybillNo` / `print`
```
POST /api/checkPrint.json (x-www-form-urlencoded,需登录 cookie)
authId=<店铺授权ID>&packageIds=<包裹ID>
→ { code: 0, data: { success: 1, printMbFormats: "物流商系统面单",
printFormat: "标签纸(10*10厘米)+标签(PDF)",
fileName: "singleCache/<uuid>/2026-11-21/<包裹ID>_<运单号>_20103_0.pdf" } }
GET /api/pddkjInventory/printExpressNote.json?id=<包裹ID>
→ application/pdf(实测 200,约 66KB,内容以 %PDF-1.4 开头)
```
`fileName` 是店小秘服务器上的相对路径,**不能直接拼域名访问**(`/singleCache/...` 等均 404);
真正取文件走 `printExpressNote.json?id=<包裹ID>`(前端打印页 `printPage` 也是这么做的,
拿到 blob 后塞进 iframe 打印)。
`dxm.js` 目前实现 `getLoginCaptcha / login / checkSession / listShopSources / listShops / listProducts /
listOrders / fetchWaybillNo`,`print` 及发货回传仍继承基类报「未实现」。后续按以下顺序补:
1. `print(order)`:checkPrint 校验 → 下载 PDF → 返回给打印端(存储方式待定,见下)
2. 发货回传(`/api/package/batchShipGoods.json` 一类的交运接口)
每补一个接口,先抓真实报文确认字段口径,再补 `backend/test/app/lib/printPlatformDxm.test.js` 用例。
### 6.1 PDF 的存储口径
- 面单 PDF 含收件人隐私,**不建议**上传到公开可读的 COS 桶并长期保存链接;
- 现有 OMS 只有本地文件存储(`files` 表 + `app.config.upload.dir` + `/api/files/raw/...`),没有 COS/OSS 适配;
- 建议:取号/打印时**实时拉取** PDF 流,按需回给 WMS 或前端打印,不落盘;
需要归档重打时再落到 `files`(hash 去重、路径不可枚举),把链接存进取号记录。