Files
SupplierSystem/docs/API.md
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

163 lines
8.3 KiB
Markdown
Raw Permalink 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.
# API 接口概览
Base URL:开发 `http://localhost:3001`,生产通常为同源 `/api`。
健康检查:`GET /api/health` → `{ status, arkConfigured, speechConfigured }`
## 认证 `/api/auth`
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/token` | 登录(用户名/邮箱 + 密码)→ JWT |
| POST | `/refresh` | 刷新令牌 |
| POST | `/change-password` | 修改密码 |
| GET | `/me` | 当前用户信息与权限 |
登录限速:10 分钟内 30 次(`/token`、`/mobile/login`)。
## 通用数据 `/api/data`
兼容 PostgREST 风格的表级 CRUD,由 `queryExecutor` + `writeValidators` 执行,自动附加分公司数据范围。
典型用法(前端 Local API Client):
- `GET /api/data/orders?...`
- `POST /api/data/orders`
- `PATCH /api/data/order_items?id=eq.xxx`
具体表名与过滤语法见 `server/src/routes/data.ts`。
## 业务流程 `/api/workflow`
聚合复杂业务操作,由 `server/src/modules/workflow/` 按模块挂载(非完整列表,以各 `*Routes.ts` 为准):
| 域 | 示例端点 | 说明 |
|----|----------|------|
| 订单 | `GET /orders/:orderCode/inbound-photos` | 订单详情入库照签名 URL(只读,已对接) |
| 验收合规 | `POST /inspection/compliance` | 验收提交、损耗确权;采购成本按 SKU 入账 |
| 分拣 | `POST /picking/*` | 拣货任务、扫码、出库 |
| 采购审核 | `POST /purchase/submit-audit`、`/purchase/approve` | 自采审核流 |
| 独立自采入库 | `POST /purchase/self-purchase-stock-inbound/:id/complete` | 待验退回后再完成:复用批次号重建待验或直收 |
| 供应商 Web 备货 | `GET /preparation/supplier-board` | 供应商账号本供应商备货看板(已对接) |
| 报价 | `POST /quote-versions` | 报价版本创建 |
| 客户结算 | `GET/POST /finance/customer-statements/*` | 预览 / 生成客户对账单 |
| 供应商结算 | `GET/POST /finance/supplier-statements/*` | 预览 / 生成供应商对账单 |
| 结算确认 | `POST /finance/statement/confirm` · `confirm-batch` · `dispute` | 确认 / 批量确认 / 异议 |
| 账龄与风控 | `POST /finance/aging/scan` · `GET /finance/customer/:code/can-order` | 账龄扫描;can-order 仅客户不存在才 403,信用额度/冻结不再拦单 |
| 催收策略 | `GET /finance/collection-strategies` | 催收推荐 |
| 成本入账 | `POST /finance/cost/post-orders` | 按订单补记采购成本 |
| 成本分摊 | `POST /cost/allocate` · `GET /cost/allocation-summary/:cycle` | 成本池分摊 |
| 毛利 / SKU | `GET /cost/margin-summary/:cycle` · `/cost/sku-costs/:cycle` | 周期毛利与 SKU 直接成本 |
| 根因 | `GET /cost/root-cause/:cycleCode` | 负毛利根因 |
| 其他成本 | `GET/POST /cost/free-cost*` | 录入、汇总、AI 归类 |
| 成本/销售预警 | `GET /finance/cost/budget-alerts*` · `PATCH /finance/cost/budget-alerts/:code/budget` | 列表/下钻(扩展销售占用字段,无新路径);写成本预算(同步 `budget_amount`)与可选销售预算 |
| 换品 | `GET /replace/candidates` | 备货缺货换品候选 |
| 市场 | `GET /market/blocked-skus` | 红灯暂停 SKU(关阻断时为空) |
| 市场 | `GET/PUT /market/red-block-config` | 红灯阻断分配开关 |
| 季节推荐 | `GET /seasonal/recommendations` | 当季性价比 Top5 |
| 配送 | `POST /delivery/plan-route` | 路线规划 |
| 异常 | `GET /exceptions/predict`、`/degradation` | 异常预测与降级 |
| 收货人 | `GET/POST /customers/:code/recipients` | 多收货人 CRUD |
财务/成本能力与缺口详见 [财务结算模块实现对照.md](./财务结算模块实现对照.md)。
## AI 函数 `/api/functions/:name`
需 JWT。限速:60 次/分钟。
| name | 说明 |
|------|------|
| `parse-order-three-layer` | 三层订单解析 |
| `parse-image-order` | 图片识单 |
| `parse-voice-order` | 语音识单 |
| `transcribe-audio` | 音频转文字 |
| `learn-alias` | 别名学习回写 |
| `sync-customer-mappings` | 同步客户映射 |
| `generate-sku-image` | SKU AI 生图 |
| `ai-quality-analysis` | 验收质量 AI 评级 |
| `invoice-ocr` | 发票 OCR + 验真 |
## 移动端 `/api/mobile`
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/login` | 移动端登录 |
| GET | `/me` | 当前用户 |
| GET/POST | `/delivery/*` | 配送任务、定位上报、轨迹 |
| GET/POST | `/supplier/*` | 备货、换品 |
| GET/POST | `/inspection/*` | 验收任务、拍照 |
| GET/POST | `/picking/*` | 分拣确认、出库 |
| GET/POST | `/purchase/*` | 采购单 |
| GET/POST | `/processing/*` | 加工作业与验收 |
| GET/POST | `/order-entry/*` | 录单员下单 |
| POST | `/customer-order/parse` 等 | 录单员解析/分类/prefill/预算等(路径名历史遗留,非客户自助) |
| GET/POST | `/customer-order/me`、历史单、售后 | 客户自助已下线,410 |
## 录单助手 `/api/mcp/order-entry`
已登录即可调用。工具来自能力目录(权限 × `client`:`web` / `miniprogram` / `api`)。缺参 `need_input`;写操作弹出确认单后走 `POST /confirm`(不调模型),模型不可自填 `confirm`。
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/tools` | 当前账号可见模块摘要 + 能力 schema。`?client=web` 不含 `submit_order` |
| POST | `/chat` | `{ text \| messages, client, visible_pages, snapshot, hint_module, locale }`。控当前页用 `set_page_fields` / `run_page_action` |
| POST | `/confirm` | `{ session_id, confirmed_action_id }` 用会话 pending args 执行写;`cancel: true` 只清 pending。不调大模型 |
## 其他路由
| 前缀 | 说明 |
|------|------|
| `/api/dashboard` | 数据看板聚合指标 |
| `/api/catalog` | 商品目录同步 |
| `/api/modules` | 业务模块 manifest |
| `/api/storage` | 文件上传/访问 |
| `/api/sku-image` | SKU 图片管理 |
| `/api/menu-group-icon` | 导航组图标库 |
| `/api/purchasers` | 采购员账号 |
| `/api/supplier` | **供应商开放 API**(登录 + 备货/改价/质检/换品)。**外部接入文档**:[供应商开放API接入](./供应商开放API接入.md);设计见 [supplier-independent-api](./design/20260801-supplier-independent-api.md) |
| `/api/suppliers` | 管理端供应商账号开通/扩展(**复数**,勿与上一行混淆) |
| `/api/roles` | 角色定义 CRUD |
| `/api/mobile` | 小程序多角色(含兼容路径 `/api/mobile/supplier/*`) |
### 供应商开放 API(`/api/supplier`)一期
| 方法 | 路径 | 说明 |
|------|------|------|
| POST | `/api/supplier/login` | 账号密码登录,强制 `roleType=supplier`,JWT `scope=supplier` |
| POST | `/api/supplier/login/phone` | 微信手机号授权登录(同上) |
| POST | `/api/supplier/oauth/token` | **进销存对接**:`client_id`+`client_secret`+`supplier_code` 换 Token(`scope=supplier_integration`,与登录密码无关) |
| GET | `/api/supplier/me` | 当前供应商资料 |
| GET | `/api/supplier/units` | 单位字典 |
| GET | `/api/supplier/preparations` | 备货列表(对齐 `/api/mobile/supplier/preparations`) |
| POST | `/api/supplier/preparations/status` | 更新备货状态 |
| POST | `/api/supplier/preparations/amend-price` | 追加改价申请 |
| GET/POST | `/api/supplier/quality-reports` | 质检报告列表/上传 |
| GET | `/api/supplier/substitution/candidates` | 换品候选 |
| POST | `/api/supplier/substitution` | 提交换品 |
鉴权:`Authorization: Bearer <token>`。账号登录按 `supplier_users` 隔离;集成 Token 按 oauth 时选定的 `supplier_code` 隔离。兼容旧小程序 token(`scope=mobile` + `roleType=supplier`)。进销存密钥说明见 [供应商开放API接入](./供应商开放API接入.md) §3.4;分配 Webhook 见 §3.5;设计 [supplier-integration-client](./design/20260804-supplier-integration-client.md)、[allocation-prep-webhook](./design/20260804-allocation-prep-webhook.md)。
管理端(复数 `/api/suppliers`):`/integration-clients*` 创建/绑定/轮换对接密钥;`PATCH .../webhook` 配置分配回调 URL。
## 认证方式
请求头:
```
Authorization: Bearer <access_token>
```
移动端与 Web 共用 JWT;`tenantScope` 从 `profiles.branch_code` 解析分公司上下文。
## 错误格式
```json
{ "error": "error_code", "error_description": "..." }
```
或 workflow/functions 风格:
```json
{ "data": null, "error": { "message": "..." } }
```