- 拉单归一化补 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>
283 lines
13 KiB
Markdown
283 lines
13 KiB
Markdown
# 店小秘(dxm)接入
|
||
|
||
> 平台实现:`backend/app/lib/printPlatform/dxm.js`
|
||
> 平台字典:`external_print_platforms.code = 'dxm'`(`url` 留空时默认 `https://www.dianxiaomi.com`)
|
||
> 当前范围:**接入基础** = 图形验证码 + 账号密码登录 + 登录态检查;店铺/商品/订单/取号待按真实报文补齐。
|
||
|
||
---
|
||
|
||
## 1. 登录协议(2026-11-23 实测)
|
||
|
||
```
|
||
① GET /verify/code.htm?t=<时间戳>
|
||
└─ 返回 4 位图形验证码(image/jpeg),同时下发 JSESSIONID 与 dxm_vc(验证码绑定会话)
|
||
|
||
② POST /user/userLoginNew2.json (x-www-form-urlencoded)
|
||
├─ account / password:AES 加密串(见下)
|
||
├─ dxmVerify:验证码
|
||
├─ loginVerifyCode / remeber / loginRedUrl / url:固定空串
|
||
└─ 返回 { code, error, url }:code=0 成功;code=-1 等为失败(error 可直接展示)
|
||
必须带 ① 的 cookie(尤其 dxm_vc),否则验证码必然校验失败
|
||
|
||
③ GET /home.htm
|
||
├─ 未登录:302 → /index.htm(官网登录页)
|
||
└─ 已登录:**302 → /web/home**(ERP 后台,200)
|
||
登录响应本身还会下发 dxm_i / dxm_w / dxm_t / dxm_c / dxm_s / rememberMe 等会话 cookie,
|
||
这些 cookie 是在 **302 响应头**里下发的,用 `fetch` 的 `redirect: 'follow'` 会丢失,
|
||
必须手动逐跳跟随并合并 set-cookie(`dxm.js#followGet`)。
|
||
```
|
||
|
||
判断登录态只能看「最终落到哪个页面」,不能按状态码判断:
|
||
`checkSession` / 登录后校验都走 `followGet`,最终地址命中 `/index.htm`(或页面含 `id="loginForm"`)才算未登录。
|
||
|
||
### 账号密码加密格式
|
||
|
||
与前端 `dxmEncrypt.js` 一致:
|
||
|
||
```
|
||
"0" + 4 位随机 hex + 16 字节 IV(hex) + 16 字节 Key(hex) + AES-128-CBC 密文(hex)
|
||
```
|
||
|
||
`dxm.js#encryptLoginField()` 负责实现;账号、密码各自独立加密。
|
||
|
||
---
|
||
|
||
## 2. 验证码会话的存放
|
||
|
||
浏览器端「取验证码 → 输入 → 登录」是两次接口请求,可能落到不同 worker,
|
||
因此验证码绑定的 cookie 不能放内存:
|
||
|
||
- 后端:`GET /api/external-print-accounts/{id}/login-captcha` 取图并把会话写入 Redis
|
||
(`service/cache.js#saveExternalPrintCaptcha`,TTL 300 秒);
|
||
- 前端:登录按钮根据账号响应里的 `requires_login_captcha` 决定是否弹验证码框;
|
||
- 提交登录:`POST /api/external-print-accounts/{id}/login`,body `{ captcha }`;
|
||
后端取出会话、交给 `dxm.login({ captcha, session })`,**无论成功失败都清理验证码会话**。
|
||
|
||
`requires_login_captcha` 由平台实现声明(`static get loginRequiresCaptcha`),
|
||
前端不写死平台 code。
|
||
|
||
---
|
||
|
||
## 3. 店铺列表
|
||
|
||
一个平台一个接口,请求与响应结构一致(实测 2026-11-23):
|
||
|
||
```
|
||
GET /api/shop/list/all.json # 仅用来拿「已授权平台」清单
|
||
GET /api/shop/list/<platform>.json # 如 shopify / shopee / amazon
|
||
```
|
||
|
||
```jsonc
|
||
{
|
||
"code": 0, "msg": "Successful",
|
||
"data": {
|
||
"platform": "shopify",
|
||
"platformZhName": "Shopify",
|
||
"shopCount": 10, // 平台侧统计值,与 list 长度不等,不要当分页总数用
|
||
"authOfPlatform": { "authorizePlatform": ["shopify"], "unauthorizePlatform": [ ... ] },
|
||
"list": [{
|
||
"id": "9139394", // 店铺ID(字符串)
|
||
"name": "c1eqfe-0a", // 店铺名
|
||
"sellerLoginId": "c1eqfe-0a.myshopify.com",
|
||
"platform": "shopify",
|
||
"authTime": 1787039321000, // 授权时间(毫秒)
|
||
"updateTime": 1795433768000,
|
||
"isDel": 0, "isExpire": 0, "isNewExpire": 0,
|
||
"accessToken": "shpat_...", // 平台侧凭据,仅原样留痕,不要外传
|
||
"uniqueKey": "...", "marketplaceIds": "..."
|
||
}]
|
||
}
|
||
}
|
||
```
|
||
|
||
OMS 实现(`dxm.js#listShops`):
|
||
|
||
1. `GET /api/shop/list/all.json` 取 `authOfPlatform.authorizePlatform`,拿到账号真正授权的平台;
|
||
2. 逐平台 `GET /api/shop/list/<platform>.json` 合并店铺;
|
||
3. 归一化为统一店铺结构(`external_id` = 店铺ID、`name`、`platform` 原始标识、
|
||
`platform_code` 走 `platformCode.resolvePlatform`、`platform_name` 用平台给的中文名、
|
||
`auth_time` 取 `authTime`、`status` 由 `isDel/isExpire` 决定);
|
||
4. 关键词 / 状态 / 平台过滤与分页都在本地做(单平台接口不分页,店铺量不大)。
|
||
|
||
> 注意:`shopCount` 不是该平台店铺数(实测 3 家店铺时它返回 10),总数只用 `list.length`。
|
||
|
||
---
|
||
|
||
## 4. 店铺商品
|
||
|
||
按平台分接口,参数与页面「商品列表」一致(实测 shopify,2026-11-23):
|
||
|
||
```
|
||
GET /api/<platform>Product/pageList.json
|
||
?sortName=2&pageNo=1&pageSize=100&total=&sortValue=0&searchType=1&searchValue=
|
||
&productSearchType=0&sellType=0&listingStatus=Active&shopId=<店铺ID>
|
||
&dxmState=online&dxmOfflineState=&fullCid=
|
||
```
|
||
|
||
```jsonc
|
||
{
|
||
"code": 0, "msg": "Successful",
|
||
"data": {
|
||
"page": {
|
||
"list": [{
|
||
"id": "184807708987825769", // 店小秘内部商品ID
|
||
"shopifyProductId": "9116758048958", // 平台商品ID(OMS 的 outer_item_id)
|
||
"shopId": "9139394",
|
||
"title": "…", "mainImage": "https://cdn.shopify.com/…",
|
||
"dxmState": "online", // 上下架(online / offline)
|
||
"variants": [{
|
||
"id": "184807708701793891",
|
||
"shopifyVariantId": "49533728391358", // 平台规格ID(OMS 的 outer_sku_id)
|
||
"sku": null, // 规格编码(OMS 的 outer_sku_code)
|
||
"option1": "Buy 1 Get 1 FREE", "option2": "", "option3": "",
|
||
"price": 49.9, "compareAtPrice": 129.9,
|
||
"inventoryQuantity": 9999,
|
||
"barcode": null, "imageUrl": null
|
||
}]
|
||
}],
|
||
"pageNo": 1, "pageSize": 100, "totalPage": 1, "totalSize": 78
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
OMS 实现(`dxm.js#listProducts`):
|
||
|
||
- 按 `totalPage` 逐页拉(每页 100),一个**规格**展开成一行(78 个商品 → 179 行);
|
||
- 字段口径:`outer_item_id` = `shopifyProductId`、`outer_sku_id` = `shopifyVariantId`、
|
||
`outer_sku_code` = 规格 `sku`、`outer_code` = `barcode` || `sku`、`spec` = `option1/2/3` 拼接、
|
||
`price` = 规格 `price`、`stock` = `inventoryQuantity`、`status` 由 `dxmState` 决定;
|
||
- `verified` 用商品数与平台 `totalSize` 比较(本地行数是 SKU 数,不能直接比)。
|
||
|
||
> 注意:接口路径里的平台名是 `<platform>Product`(如 `shopifyProduct`、`shopeeProduct`),
|
||
> 其它平台是否同名未验证,接入新平台时先抓包确认。
|
||
|
||
---
|
||
|
||
## 5. 订单(「有货」待发货队列)
|
||
|
||
OMS 只拉「有货」(`allocated_has`) 列表,其它状态忽略(实测 2026-11-24):
|
||
|
||
```
|
||
POST /api/package/list.json (x-www-form-urlencoded)
|
||
pageNo=1&pageSize=100&shopId=<平台店铺ID>&state=allocated_has
|
||
&platform=&isSearch=0&searchType=orderId&authId=-1&startTime=&endTime=&country=
|
||
&orderField=order_pay_time&isVoided=0&isRemoved=0&ruleId=-1&sysRule=&applyType=&applyStatus=
|
||
&printJh=-1&printMd=-1&commitPlatform=&productStatus=&jhComment=-1&storageId=0
|
||
&isOversea=-1&isFree=0&isBatch=0&history=&custom=-1&timeOut=0&refundStatus=0
|
||
&buyerAccount=&forbiddenStatus=-1&forbiddenReason=0&behindTrack=-1&orderId=&axios_cancelToken=true
|
||
```
|
||
|
||
```jsonc
|
||
{
|
||
"code": 0, "msg": "Successful",
|
||
"data": {
|
||
"page": {
|
||
"pageNo": 1, "pageSize": 100, "totalPage": 1, "totalSize": 60,
|
||
"list": [{
|
||
"id": "184804423947346433", // 包裹ID(OMS external_order_id,唯一)
|
||
"shopId": "9139394",
|
||
"orderId": "9139394-1350", // 平台店铺单号(仅留痕)
|
||
"platform": "shopify",
|
||
"orderState": "allocated_has",
|
||
"orderStatePlatform": "paid_fulfilled",
|
||
"orderAmount": 54.9, "shipAmount": 6.9, "orderUnit": "USD",
|
||
"orderCreateTime": 1793765481143, "updateTime": 1795435680000,
|
||
"dxmPackageAddr": { // 收件人
|
||
"contactPerson": "Dave Fohne", "phoneNumber": "+16183430668",
|
||
"countryCh": "美国", "province": "Illinois", "city": "Collinsville", "district": "",
|
||
"detailAddress": "620 E Church St", "hasAsterisk": false
|
||
},
|
||
"productList": [{
|
||
"id": "184807702548539675", // 明细ID(OMS external_item_id)
|
||
"productId": "49366194192574", // **平台规格ID**(= 店铺商品的 outer_sku_id)
|
||
"productSubSku": "49366194192574",
|
||
"productName": "…", "oriProductImg": "https://cdn.shopify.com/…",
|
||
"attrList": [{ "pName": "Variants", "pValue": "Buy 1 Get 1 Free" }],
|
||
"quantity": 1, "price": 54.9, "cancelFlag": false
|
||
}]
|
||
}]
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
OMS 实现(`dxm.js#listOrders`):
|
||
|
||
- 按 `totalPage` 逐页拉(每页 100),**一个包裹 = 一张 OMS 订单**,`external_order_id` = 包裹ID;
|
||
- `total_amount` / `pay_amount` = `orderAmount + shipAmount`;
|
||
- 收件人取 `dxmPackageAddr`:`contactPerson`→姓名、`phoneNumber`/`mobile`→电话、`province`→省(州)、
|
||
`city`→市、`district`→区县、`detailAddress`+`address2`→详细地址、`country`→`country_code`、
|
||
`zip`→`zip_code`、`email`→`email`;
|
||
**跨境单没有区县/街道,`area`/`town` 用 `-` 占位**(下游必填项、面单打印都需要有值),不做中英文转换;
|
||
- 明细跳过 `cancelFlag=true` 的行;
|
||
- **订单明细只给平台规格ID**(`productId`/`vid`/`stockProductId` 都是规格ID),不给平台商品ID,
|
||
因此明细的 `outer_item_id` 留空,由 `lib/shopProductMatch` 按「规格ID 在店铺内唯一命中」匹配店铺商品,
|
||
落库时再回填 `outer_item_id`(取匹配到的店铺商品)。
|
||
|
||
### 5.1 快递公司(物流商)口径
|
||
|
||
订单报文的 `agentProviderName` 是店小秘「系统对接物流商」页面上的**物流商名 + 渠道名**,
|
||
形如 `跨源速递-元驿MetaHub-普货`、`跨源速递-ZF-元驿MetaHub 欧洲特货`。
|
||
|
||
- 识别到 `agentProviderName` 时,订单快递公司按**物流商一级**落 OMS 快递公司字典
|
||
(`express_companies`):`跨源速递` → `KJKYSD`(`PROVIDER_LOGISTICS_MAP`);
|
||
未在映射表内的物流商先按默认 `KJKYSD` 兜底(当前只授权了跨源速递);
|
||
- 渠道原值写 `orders.external_data.agentProviderName` 与本单号 `extend_props.logistics_provider_name` 留痕;
|
||
- 该值只作为**待审核订单的默认快递**(`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. 打印数据(PDF)与待接入
|
||
|
||
面单打印数据(实测 2026-11-24,用真实账号跑通):
|
||
|
||
```
|
||
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 去重、路径不可枚举),把链接存进取号记录。
|