oms/店小秘接入.md
liufumingren 78f07651f1 店小秘接入基础:图形验证码登录
- 新增 dxm 平台实现:取图形验证码、AES 加密账号密码登录、登录态检查
- 账号接口返回 requires_login_captcha;新增 GET /external-print-accounts/:id/login-captcha
- 验证码绑定的会话存 Redis,登录成功/失败后都清理,支持多实例
- 前端登录按钮按平台能力弹出验证码输入框
- 补 dxm 平台字典迁移、接入文档与单元测试

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 21:32:25 +08:00

66 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 店小秘(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
└─ 已登录:200(登录成功后用它确认会话真正可用,再落库)
```
### 账号密码加密格式
与前端 `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. 待接入
`dxm.js` 目前只实现 `getLoginCaptcha / login / checkSession / listShopSources`,
其余方法继承基类会明确报「未实现」。后续按以下顺序补:
1. 店铺列表(授权店铺)→ `listShops`
2. 店铺商品快照 → `listProducts`
3. 订单列表(含明細)→ `listOrders`
4. 取号 / 打单 / 发货回传 → `fetchWaybillNo` / `print`
每补一个接口,先抓真实报文确认字段口径,再补 `backend/test/app/lib/printPlatformDxm.test.js` 用例。