feat: 添加货币和汇率管理功能

refactor: 重构前端路由和登录逻辑

docs: 更新业务闭环、任务和架构文档

style: 调整代码格式和文件结构

chore: 更新依赖项和配置文件
This commit is contained in:
2026-03-19 19:08:15 +08:00
parent 8de9ea0aaa
commit eafa1bbe94
203 changed files with 20240 additions and 39580 deletions

View File

@@ -0,0 +1,21 @@
# API规范索引
> **模块**: 02_Backend/api - API接口规范
> **更新日期**: 2026-03-19
---
## API文档列表
| 文档 | 描述 | 状态 |
|------|------|------|
| [01_Data_API](01_Data_API.md) | 数据模块API规范 | ✅ |
| [02_Finance_API](02_Finance_API.md) | 财务模块API规范 | ✅ |
| [03_Order_API](03_Order_API.md) | 订单模块API规范 | ✅ |
| [04_Product_API](04_Product_API.md) | 产品模块API规范 | ✅ |
---
## 返回上级
- [后端模块](../00_Backend_Index.md)

View File

@@ -0,0 +1,327 @@
# 📊 Data & API Specifications (Crawlful Hub)
> **定位**Crawlful Hub 数据架构与接口规格书 - 包含数据库 Schema、核心业务流程及全量 API 定义。
> **更新日期**: 2026-03-17
---
## 1. 数据库结构 (Data Schema)
### 1.1 命名规范
- **表前缀**: `cf_` (crawlful)
- **字段命名**: snake_case
- **金額字段**: `decimal(10,2)` 或更高精度。**禁止**使用 float/double。
- **物理单位**: 长度 (cm), 重量 (kg), 体积 (m³)。
### 1.2 数据库安全约束
- **⚠️ 严禁**: 代码中执行 `DROP`, `TRUNCATE` 等破坏性操作
- **幂等性**: 所有建表语句必须使用 `db.schema.hasTable` 前置校验
- **JSON 处理**: images/skus/attributes 入库前序列化,出库解析
- **唯一约束**: `cf_product` 表必须保证 (platform, productId) 唯一
### 1.3 核心表定义
#### 1.3.1 租户与用户
**cf_tenant** (租户表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 租户ID |
| name | string | 租户名称 |
| quota | json | 配额配置 |
| status | string | 状态 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_user** (用户表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 用户ID |
| email | string | 邮箱 |
| password_hash | string | 密码哈希 |
| role | enum | ADMIN/MANAGER/OPERATOR/FINANCE/SOURCING/LOGISTICS/ANALYST |
| tenant_id | string(FK) | 租户ID |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
#### 1.3.2 店铺与商品
**cf_shop** (店铺表)
| 字段 | 类型 | 说明 |
#### 1.3.3 跨境电商相关表
**cf_platform_integration** (平台集成表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 集成ID |
| tenant_id | string(FK) | 租户ID |
| platform_name | enum | Sellbrite/Shoplazza/SaleSmartly |
| api_key | string | API密钥 |
| api_secret | string | API密钥 |
| access_token | string | 访问令牌 |
| refresh_token | string | 刷新令牌 |
| status | string | 状态 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_cross_border_product** (跨境商品表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 跨境商品ID |
| product_id | string(FK) | 商品ID |
| hs_code | string | HS编码 |
| tariff_rate | decimal(10,2) | 关税税率 |
| compliance_status | string | 合规状态 |
| country_of_origin | string | 原产国 |
| weight | decimal(10,2) | 重量(kg) |
| dimensions | json | 尺寸(cm) |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_customs_document** (清关文件表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 文档ID |
| order_id | string(FK) | 订单ID |
| document_type | string | 文档类型 |
| document_url | string | 文档URL |
| status | string | 状态 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_cross_border_order** (跨境订单表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 跨境订单ID |
| order_id | string(FK) | 订单ID |
| customs_status | string | 清关状态 |
| shipping_status | string | 物流状态 |
| tracking_number | string | 追踪号 |
| logistics_provider | string | 物流提供商 |
| estimated_delivery | datetime | 预计送达时间 |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
|------|------|------|
| id | string(PK) | 店铺ID |
| tenant_id | string(FK) | 租户ID |
| platform | enum | AMAZON/EBAY/SHOPEE/TIKTOK/TEMU/1688 |
| platform_shop_id | string | 平台店铺ID |
| name | string | 店铺名称 |
| auth_token | encrypted | 授权Token(加密存储) |
| status | string | 状态 |
| **trace_id** | string | 链路追踪ID |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_product** (商品主表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 商品ID |
| tenant_id | string(FK) | 租户ID |
| shop_id | string(FK) | 店铺ID |
| platform | enum | 平台类型 |
| platform_product_id | string | 平台商品ID |
| title | string | 商品标题 |
| description | text | 商品描述 |
| images | json | 图片列表(JSON数组) |
| category | string | 类目 |
| status | string | 状态 |
| **trace_id** | string | 链路追踪ID |
| **business_type** | enum | TOC/TOB |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
| **唯一约束**: (platform, platform_product_id) |
**cf_product_sku** (SKU变体表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | SKU ID |
| tenant_id | string(FK) | 租户ID |
| shop_id | string(FK) | 店铺ID |
| product_id | string(FK) | 商品ID |
| sku_code | string | SKU编码 |
| attributes | json | 属性JSON(颜色/尺寸等) |
| cost_price | decimal(10,2) | 成本价 |
| retail_price | decimal(10,2) | 零售价 |
| weight_kg | decimal(8,3) | 重量(kg) |
| length_cm | decimal(8,2) | 长度(cm) |
| width_cm | decimal(8,2) | 宽度(cm) |
| height_cm | decimal(8,2) | 高度(cm) |
| **trace_id** | string | 链路追踪ID |
| **business_type** | enum | TOC/TOB |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
#### 1.3.3 订单与库存
**cf_inventory** (库存表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 库存ID |
| tenant_id | string(FK) | 租户ID |
| shop_id | string(FK) | 店铺ID |
| sku_id | string(FK) | SKU ID |
| warehouse_id | string | 仓库ID |
| total_qty | int | 总数量 |
| available_qty | int | 可用数量 |
| reserved_qty | int | 预留数量 |
| **trace_id** | string | 链路追踪ID |
| **business_type** | enum | TOC/TOB |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_consumer_orders** (C端订单表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 订单ID |
| tenant_id | string(FK) | 租户ID |
| shop_id | string(FK) | 店铺ID |
| platform | enum | 平台类型 |
| platform_order_id | string | 平台订单号 |
| total_amount | decimal(10,2) | 总金额 |
| currency | string | 币种 |
| profit | decimal(10,2) | 利润 |
| profit_margin | decimal(5,2) | 利润率(%) |
| status | string | 订单状态 |
| **trace_id** | string | 链路追踪ID |
| **task_id** | string | 任务ID |
| **business_type** | enum | TOC/TOB |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_order_item** (订单项表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 订单项ID |
| order_id | string(FK) | 订单ID |
| sku_id | string(FK) | SKU ID |
| unit_price | decimal(10,2) | 单价 |
| quantity | int | 数量 |
| created_at | datetime | 创建时间 |
#### 1.3.4 供应链与财务
**cf_supplier** (供应商表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 供应商ID |
| tenant_id | string(FK) | 租户ID |
| name | string | 供应商名称 |
| contact | json | 联系方式 |
| rating | decimal(3,2) | 评分 |
| status | string | 状态 |
| **trace_id** | string | 链路追踪ID |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_purchase_order** (采购订单表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 采购单号 |
| tenant_id | string(FK) | 租户ID |
| supplier_id | string(FK) | 供应商ID |
| total_amount | decimal(10,2) | 总金额 |
| status | string | 状态 |
| **trace_id** | string | 链路追踪ID |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
**cf_finance_reconciliation** (财务对账表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 对账ID |
| tenant_id | string(FK) | 租户ID |
| shop_id | string(FK) | 店铺ID |
| period | string | 对账周期 |
| total_sales | decimal(12,2) | 总销售额 |
| total_profit | decimal(12,2) | 总利润 |
| variance_status | string | 差异状态 |
| **trace_id** | string | 链路追踪ID |
| created_at | datetime | 创建时间 |
| updated_at | datetime | 更新时间 |
#### 1.3.5 审计日志
**cf_audit_log** (审计日志表)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string(PK) | 日志ID |
| tenant_id | string | 租户ID |
| shop_id | string | 店铺ID |
| task_id | string | 任务ID |
| trace_id | string | 链路追踪ID |
| business_type | enum | TOC/TOB |
| action | string | 操作类型 |
| entity_type | string | 实体类型 |
| entity_id | string | 实体ID |
| old_values | json | 旧值 |
| new_values | json | 新值 |
| user_id | string | 操作用户ID |
| ip_address | string | IP地址 |
| created_at | datetime | 创建时间 |
### 1.4 五元组追踪字段说明
所有业务表必须包含以下追踪字段:
| 字段 | 类型 | 说明 | 必填 |
|------|------|------|------|
| tenant_id | string | 租户ID - 业务隔离 | ✅ |
| shop_id | string | 店铺ID - 平台/店铺隔离 | ✅ (店铺相关表) |
| task_id | string | 任务ID - 任务追踪 | ✅ (异步任务) |
| trace_id | string | 链路追踪ID - 全链路唯一 | ✅ |
| business_type | enum | TOC/TOB | ✅ |
---
## 2. 核心业务流程 (Business Processes)
### 2.1 商品采集与刊登 (Collection & Listing)
1. **采集**: 用户输入 URL → Extension 解析 DOM → 发送至 Hub 草稿箱。
2. **刊登**: 选择草稿 → 平台适配器转换字段 → 调用平台 API 发布 → 记录刊登历史。
### 2.2 订单履约与财务 (Fulfillment & Finance)
1. **订单**: 平台 Webhook 推送 → 解析并入库 → 利润审计 (红线校验) → 状态流转。
2. **库存**: 平台同步 → 更新本地库存 → 低于阈值自动触发补货提醒。
3. **对账**: 拉取平台账单 → 自动差异匹配 → 生成报告 → 人工审核异常项。
---
## 3. 全量 API 端点映射 (API Map)
### 3.1 基础管理
- `POST /api/auth/login`: 用户登录。
- `GET /api/users`: 获取用户列表。
- `GET /api/shops`: 获取授权店铺。
### 3.2 商品与库存 (Product & Inventory)
- `GET /api/products`: 获取商品列表。
- `POST /api/products/publish`: 发布商品到平台。
- `GET /api/inventory/aging`: 库存老化分析。
- `GET /api/inventory/forecast`: 库存预测。
### 3.3 订单与支付 (Order & Payment)
- `GET /api/orders`: 获取订单列表。
- `PUT /api/orders/:id/status`: 更新订单状态。
- `POST /api/payments`: 创建支付订单。
- `POST /api/payments/callback`: 处理支付回调。
### 3.4 财务管理 (Finance)
- `GET /api/finance/reconciliation`: 财务对账。
---
## 4. 通用响应与状态码
### 响应格式
```typescript
{
"success": true,
"data": { ... },
"error": "optional error message"
}
```
### 常用状态码
- `200`: 成功 | `401`: 未授权 | `403`: 禁止访问 | `404`: 资源不存在 | `500`: 服务器错误。

View File

@@ -0,0 +1,403 @@
# Finance API Specification
> **定位**:财务管理 API 规范 - 包含财务结算、税务、汇率对冲、利润对账等接口。
> **更新日期**: 2026-03-18
> **版本**: v1.0
> **最高优先级参考**: [Business_ClosedLoops.md](../../00_Business/Business_ClosedLoops.md)
---
## 1. 接口概览
| 方法 | 路径 | 功能 | 权限 |
|------|------|------|------|
| POST | `/api/v1/finance/orders/:id/record` | 记录订单财务 | finance:create |
| POST | `/api/v1/finance/transactions` | 记录交易流水 | finance:create |
| GET | `/api/v1/finance/reconciliation` | 执行利润对账 | finance:reconcile |
| GET | `/api/v1/finance/bills/:orderId` | 账单回放 | finance:read |
| GET | `/api/v1/finance/stats` | 财务统计 | finance:read |
| POST | `/api/v1/finance/hedge` | 汇率对冲 | finance:hedge |
| GET | `/api/v1/finance/tax/:orderId` | 税务信息 | finance:read |
---
## 2. 接口详情
### 2.1 记录订单财务 [BIZ_FIN_01]
**请求**
```http
POST /api/v1/finance/orders/:id/record
```
**请求体**
```json
{
"platform": "AMAZON",
"productId": "product-uuid",
"sellingPrice": 59.99,
"purchasePrice": 25.00,
"countryCode": "US",
"logisticsCost": 8.00,
"adBudget": 5.00,
"status": "COMPLETED"
}
```
**业务逻辑**
1. 插入订单主表 (`cf_orders`)
2. 实时税务计提 (`BIZ_FIN_02`)
3. 汇率自动对冲 (`BIZ_FIN_03`)
**响应**
```json
{
"success": true,
"data": {
"orderId": "order-uuid",
"taxAccrued": 5.40,
"hedgeApplied": true
}
}
```
---
### 2.2 记录交易流水 [BIZ_FIN_04]
**请求**
```http
POST /api/v1/finance/transactions
```
**请求体**
```json
{
"amount": 59.99,
"currency": "USD",
"type": "ORDER_REVENUE",
"category": "SALES",
"orderId": "order-uuid",
"metadata": {
"platform": "AMAZON",
"productId": "product-uuid"
}
}
```
**交易类型**
| 类型 | 说明 |
|------|------|
| ORDER_REVENUE | 订单收入 |
| LOGISTICS_COST | 物流成本 |
| PLATFORM_FEE | 平台费用 |
| REFUND | 退款 |
| COMMISSION | 佣金 |
| INCOME | 其他收入 |
**响应**
```json
{
"success": true,
"data": {
"transactionId": 12345,
"recordedAt": "2026-03-18T10:00:00Z"
}
}
```
---
### 2.3 执行利润对账 [BIZ_FIN_06]
**请求**
```http
GET /api/v1/finance/reconciliation?shopId=shop-uuid
```
**查询参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| shopId | string | 是 | 店铺ID |
**业务逻辑**
1. 获取所有已完成且未对账的订单
2. 聚合各项成本(采购、物流、税务)
3. 计算净利润
4. 更新对账状态
**响应**
```json
{
"success": true,
"data": {
"totalReconciled": 50,
"details": [
{
"orderId": "order-1",
"sellingPrice": 59.99,
"purchaseCost": 25.00,
"logisticsCost": 8.00,
"taxAmount": 5.40,
"netProfit": 21.59,
"profitMargin": 36.0
}
]
}
}
```
---
### 2.4 账单回放
**请求**
```http
GET /api/v1/finance/bills/:orderId
```
**响应**
```json
{
"success": true,
"data": {
"orderId": "order-uuid",
"timeline": [
{
"event": "ORDER_CREATED",
"time": "2026-03-18T10:00:00Z",
"amount": 59.99,
"description": "订单创建"
},
{
"event": "PURCHASE_COMPLETED",
"time": "2026-03-18T11:00:00Z",
"amount": -25.00,
"description": "采购完成"
},
{
"event": "LOGISTICS_DEDUCTED",
"time": "2026-03-18T12:00:00Z",
"amount": -8.00,
"description": "物流扣款"
},
{
"event": "TAX_ACCRUED",
"time": "2026-03-18T12:00:00Z",
"amount": -5.40,
"description": "税务计提"
}
],
"finalProfit": 21.59,
"profitMargin": 36.0
}
}
```
---
### 2.5 财务统计
**请求**
```http
GET /api/v1/finance/stats?startDate=2026-03-01&endDate=2026-03-31
```
**查询参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| startDate | string | 否 | 开始日期 |
| endDate | string | 否 | 结束日期 |
| shopId | string | 否 | 店铺筛选 |
**响应**
```json
{
"success": true,
"data": {
"period": "2026-03-01 ~ 2026-03-31",
"totalRevenue": 59980.00,
"totalCost": 35988.00,
"totalProfit": 23992.00,
"averageMargin": 40.0,
"byType": {
"ORDER_REVENUE": 59980.00,
"LOGISTICS_COST": -8000.00,
"PLATFORM_FEE": -3998.00,
"TAX": -5400.00,
"PURCHASE": -25000.00
},
"byPlatform": {
"AMAZON": { "revenue": 29990.00, "profit": 11996.00 },
"SHOPIFY": { "revenue": 19993.00, "profit": 7997.00 },
"TIKTOK": { "revenue": 9997.00, "profit": 3999.00 }
}
}
}
```
---
### 2.6 汇率对冲 [BIZ_FIN_03]
**请求**
```http
POST /api/v1/finance/hedge
```
**请求体**
```json
{
"pair": "USD/CNY",
"amount": 59980.00,
"strategy": "AUTO"
}
```
**响应**
```json
{
"success": true,
"data": {
"hedgeId": "hedge-uuid",
"pair": "USD/CNY",
"amount": 59980.00,
"rate": 7.25,
"hedgedAmount": 434855.00,
"status": "EXECUTED"
}
}
```
---
### 2.7 税务信息 [BIZ_FIN_02]
**请求**
```http
GET /api/v1/finance/tax/:orderId
```
**响应**
```json
{
"success": true,
"data": {
"orderId": "order-uuid",
"countryCode": "US",
"baseAmount": 59.99,
"taxRate": 9.0,
"totalTax": 5.40,
"isIOSS": false,
"taxType": "STANDARD_VAT",
"status": "ACCRUED",
"accruedAt": "2026-03-18T10:00:00Z"
}
}
```
---
## 3. 数据模型
### 3.1 OrderRecord (订单财务记录)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string | 主键 |
| tenantId | string | 租户ID |
| shopId | string | 店铺ID |
| taskId | string | 任务ID |
| traceId | string | 追踪ID |
| platform | string | 平台 |
| productId | string | 商品ID |
| sellingPrice | number | 售价 |
| purchasePrice | number | 采购价 |
| countryCode | string | 国家代码 |
| logisticsCost | number | 物流成本 |
| adBudget | number | 广告预算 |
| status | string | 状态 |
| netProfit | number | 净利润 |
| reconciledAt | string | 对账时间 |
### 3.2 Transaction (交易流水)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | number | 自增ID |
| tenantId | string | 租户ID |
| orderId | string | 订单ID |
| amount | decimal(10,2) | 金额 |
| currency | string | 币种 |
| transactionType | string | 交易类型 |
| traceId | string | 追踪ID |
| metadata | object | 元数据 |
| createdAt | string | 创建时间 |
### 3.3 TaxAccrual (税务计提)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | number | 自增ID |
| orderId | string | 订单ID |
| tenantId | string | 租户ID |
| amount | decimal(10,2) | 税额 |
| currency | string | 币种 |
| taxType | string | 税务类型 |
| status | string | 状态 |
| traceId | string | 追踪ID |
| createdAt | string | 创建时间 |
| updatedAt | string | 更新时间 |
---
## 4. 利润计算公式
### 4.1 净利润
```
净利润 = 售价 - 采购成本 - 物流成本 - 税费 - 平台费用 - 广告费用
```
### 4.2 利润率
```
利润率 = (净利润 / 售价) × 100%
```
### 4.3 利润红线
| 模式 | 红线 | 处理 |
|------|------|------|
| B2B | < 15% | 禁止报价 |
| B2C | < 20% | 触发风控预警 |
---
## 5. 错误码
| 错误码 | 说明 | HTTP状态 |
|--------|------|----------|
| ORDER_NOT_FOUND | 订单不存在 | 404 |
| INSUFFICIENT_FUNDS | 资金不足 | 400 |
| INVALID_CURRENCY | 无效币种 | 400 |
| HEDGE_FAILED | 对冲失败 | 500 |
| RECONCILIATION_FAILED | 对账失败 | 500 |
| UNAUTHORIZED | 未授权 | 401 |
| FORBIDDEN | 无权限 | 403 |
---
## 6. 相关文档
- [Backend Design](../Backend_Design.md)
- [Product API](./Product_API.md)
- [Order API](./Order_API.md)
- [Business ClosedLoops](../../00_Business/Business_ClosedLoops.md) - 财务结算闭环
---
*本文档基于代码自动生成,最后更新: 2026-03-18*

View File

@@ -0,0 +1,521 @@
# Order API Specification
> **定位**:订单管理 API 规范 - 包含订单 CRUD、Webhook、批量操作、状态流转等接口。
> **更新日期**: 2026-03-18
> **版本**: v1.0
> **最高优先级参考**: [Business_ClosedLoops.md](../../00_Business/Business_ClosedLoops.md)
---
## 1. 接口概览
| 方法 | 路径 | 功能 | 权限 |
|------|------|------|------|
| POST | `/api/v1/orders/webhook/:platform` | 平台 Webhook 接收 | public |
| GET | `/api/v1/orders` | 获取订单列表 | order:read |
| GET | `/api/v1/orders/:id` | 获取订单详情 | order:read |
| POST | `/api/v1/orders` | 创建订单 | order:create |
| PUT | `/api/v1/orders/:id` | 更新订单 | order:update |
| DELETE | `/api/v1/orders/:id` | 删除订单 | order:delete |
| POST | `/api/v1/orders/sync` | 手动触发同步 | order:sync |
| GET | `/api/v1/orders/stats` | 订单统计 | order:read |
| PUT | `/api/v1/orders/batch` | 批量更新 | order:update |
| POST | `/api/v1/orders/:id/status` | 状态流转 | order:update |
| POST | `/api/v1/orders/batch/audit` | 批量审核 | order:audit |
---
## 2. 接口详情
### 2.1 平台 Webhook 接收 [BIZ_OPS_01]
**请求**
```http
POST /api/v1/orders/webhook/:platform
```
**路径参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| platform | string | 是 | 平台 (SHOPIFY, AMAZON, TIKTOK, ALIEXPRESS) |
**请求头**
```
X-Tenant-Id: {tenantId}
X-Shop-Id: {shopId}
```
**请求体 (Shopify 示例)**
```json
{
"id": "1234567890",
"name": "#1001",
"customer": {
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com"
},
"line_items": [
{
"sku": "SKU001",
"title": "Product Name",
"price": "29.99",
"quantity": 2
}
],
"total_price": "59.98",
"currency": "USD",
"financial_status": "paid",
"fulfillment_status": null
}
```
**响应**
```json
{
"success": true,
"orderId": "internal-order-id"
}
```
**支持平台**
- Shopify
- Amazon
- TikTok Shop
- AliExpress
---
### 2.2 获取订单列表
**请求**
```http
GET /api/v1/orders?status=PAID&platform=AMAZON&page=1&pageSize=20
```
**查询参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| status | string | 否 | 状态筛选 |
| platform | string | 否 | 平台筛选 |
| startDate | string | 否 | 开始日期 (ISO 8601) |
| endDate | string | 否 | 结束日期 (ISO 8601) |
| page | number | 否 | 页码,默认 1 |
| pageSize | number | 否 | 每页数量,默认 20 |
**响应**
```json
{
"success": true,
"data": {
"items": [
{
"id": "order-uuid",
"platform": "AMAZON",
"platformOrderId": "123-4567890-1234567",
"customerName": "John Doe",
"customerEmail": "john@example.com",
"status": "PAID",
"paymentStatus": "COMPLETED",
"fulfillmentStatus": "PENDING",
"totalAmount": 59.98,
"currency": "USD",
"items": [
{
"skuId": "SKU001",
"title": "Product Name",
"price": 29.99,
"quantity": 2
}
],
"createdAt": "2026-03-18T10:00:00Z"
}
],
"pagination": {
"page": 1,
"pageSize": 20,
"total": 100
}
}
}
```
---
### 2.3 获取订单详情
**请求**
```http
GET /api/v1/orders/:id
```
**响应**
```json
{
"success": true,
"data": {
"id": "order-uuid",
"platform": "AMAZON",
"platformOrderId": "123-4567890-1234567",
"tenantId": "tenant-uuid",
"shopId": "shop-uuid",
"customerName": "John Doe",
"customerEmail": "john@example.com",
"shippingAddress": {
"name": "John Doe",
"address1": "123 Main St",
"city": "New York",
"province": "NY",
"country": "US",
"zip": "10001"
},
"items": [
{
"skuId": "SKU001",
"title": "Product Name",
"price": 29.99,
"quantity": 2,
"costPrice": 15.00
}
],
"totalAmount": 59.98,
"currency": "USD",
"status": "PAID",
"paymentStatus": "COMPLETED",
"fulfillmentStatus": "PENDING",
"profit": 29.98,
"profitMargin": 50.0,
"traceId": "trace-uuid",
"createdAt": "2026-03-18T10:00:00Z",
"updatedAt": "2026-03-18T10:00:00Z"
}
}
```
---
### 2.4 创建订单
**请求**
```http
POST /api/v1/orders
```
**请求体**
```json
{
"platform": "MANUAL",
"customerName": "John Doe",
"customerEmail": "john@example.com",
"shippingAddress": {
"name": "John Doe",
"address1": "123 Main St",
"city": "New York",
"province": "NY",
"country": "US",
"zip": "10001"
},
"items": [
{
"skuId": "SKU001",
"title": "Product Name",
"price": 29.99,
"quantity": 2
}
],
"totalAmount": 59.98,
"currency": "USD"
}
```
**响应**
```json
{
"success": true,
"orderId": "order-uuid"
}
```
---
### 2.5 更新订单
**请求**
```http
PUT /api/v1/orders/:id
```
**请求体**
```json
{
"status": "SHIPPED",
"fulfillmentStatus": "SHIPPED",
"trackingNumber": "1Z999AA1234567890"
}
```
**响应**
```json
{
"success": true,
"message": "Order updated successfully"
}
```
---
### 2.6 删除订单
**请求**
```http
DELETE /api/v1/orders/:id
```
**响应**
```json
{
"success": true,
"message": "Order deleted successfully"
}
```
---
### 2.7 手动触发同步
**请求**
```http
POST /api/v1/orders/sync
```
**请求体**
```json
{
"platform": "AMAZON",
"shopId": "shop-uuid"
}
```
**响应**
```json
{
"success": true,
"message": "Manual sync triggered for AMAZON"
}
```
---
### 2.8 订单统计
**请求**
```http
GET /api/v1/orders/stats
```
**响应**
```json
{
"success": true,
"data": {
"totalOrders": 1000,
"totalRevenue": 59980.00,
"totalProfit": 29990.00,
"averageOrderValue": 59.98,
"byStatus": {
"PAID": 500,
"SHIPPED": 300,
"DELIVERED": 200
},
"byPlatform": {
"AMAZON": 400,
"SHOPIFY": 300,
"TIKTOK": 300
}
}
}
```
---
### 2.9 批量更新
**请求**
```http
PUT /api/v1/orders/batch
```
**请求体**
```json
{
"orderIds": ["order-1", "order-2", "order-3"],
"updates": {
"status": "SHIPPED",
"fulfillmentStatus": "SHIPPED"
}
}
```
**响应**
```json
{
"success": true,
"data": {
"updated": 3,
"failed": 0
}
}
```
---
### 2.10 状态流转
**请求**
```http
POST /api/v1/orders/:id/status
```
**请求体**
```json
{
"status": "SHIPPED",
"reason": "Order shipped via UPS"
}
```
**状态机**
```
PULLED → PENDING_REVIEW → CONFIRMED → ALLOCATED → READY_TO_SHIP → SHIPPED → DELIVERED → CLOSED
```
**响应**
```json
{
"success": true,
"message": "Order status updated to SHIPPED"
}
```
---
### 2.11 批量审核
**请求**
```http
POST /api/v1/orders/batch/audit
```
**请求体**
```json
{
"orderIds": ["order-1", "order-2", "order-3"]
}
```
**响应**
```json
{
"success": true,
"data": {
"approved": 3,
"rejected": 0
}
}
```
---
## 3. 数据模型
### 3.1 Order (订单)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string | 主键 (UUID) |
| tenantId | string | 租户ID |
| shopId | string | 店铺ID |
| platform | string | 平台 |
| platformOrderId | string | 平台订单ID |
| customerName | string | 客户姓名 |
| customerEmail | string | 客户邮箱 |
| shippingAddress | object | 配送地址 |
| items | OrderItem[] | 订单项 |
| totalAmount | decimal(10,2) | 订单金额 |
| currency | string | 币种 |
| status | string | 订单状态 |
| paymentStatus | string | 支付状态 |
| fulfillmentStatus | string | 履约状态 |
| profit | number | 利润 |
| profitMargin | number | 利润率 |
| traceId | string | 追踪ID |
| createdAt | string | 创建时间 |
| updatedAt | string | 更新时间 |
### 3.2 OrderItem (订单项)
| 字段 | 类型 | 说明 |
|------|------|------|
| skuId | string | SKU ID |
| title | string | 商品标题 |
| price | number | 单价 |
| quantity | number | 数量 |
| costPrice | number | 成本价 |
### 3.3 状态枚举
```typescript
enum OrderStatus {
PULLED = 'PULLED', // 已拉取
PENDING_REVIEW = 'PENDING_REVIEW', // 待审核
CONFIRMED = 'CONFIRMED', // 已确认
ALLOCATED = 'ALLOCATED', // 已分配
READY_TO_SHIP = 'READY_TO_SHIP', // 待发货
SHIPPED = 'SHIPPED', // 已发货
DELIVERED = 'DELIVERED', // 已送达
CLOSED = 'CLOSED' // 已关闭
}
enum PaymentStatus {
PENDING = 'PENDING',
COMPLETED = 'COMPLETED',
FAILED = 'FAILED',
REFUNDED = 'REFUNDED'
}
enum FulfillmentStatus {
PENDING = 'PENDING',
PROCESSING = 'PROCESSING',
SHIPPED = 'SHIPPED',
DELIVERED = 'DELIVERED'
}
```
---
## 4. 错误码
| 错误码 | 说明 | HTTP状态 |
|--------|------|----------|
| ORDER_NOT_FOUND | 订单不存在 | 404 |
| INVALID_STATUS_TRANSITION | 非法状态流转 | 400 |
| PLATFORM_NOT_SUPPORTED | 不支持的平台 | 400 |
| UNAUTHORIZED | 未授权 | 401 |
| FORBIDDEN | 无权限 | 403 |
| VALIDATION_ERROR | 参数校验失败 | 400 |
| INTERNAL_ERROR | 内部错误 | 500 |
---
## 5. 相关文档
- [Backend Design](../Backend_Design.md)
- [Product API](./Product_API.md)
- [Finance API](./Finance_API.md)
- [Business ClosedLoops](../../00_Business/Business_ClosedLoops.md) - 订单履约闭环
---
*本文档基于代码自动生成,最后更新: 2026-03-18*

View File

@@ -0,0 +1,399 @@
# Product API Specification
> **定位**:商品管理 API 规范 - 包含商品 CRUD、采集、定价、审核等接口。
> **更新日期**: 2026-03-18
> **版本**: v1.0
---
## 1. 接口概览
| 方法 | 路径 | 功能 | 权限 |
|------|------|------|------|
| GET | `/api/v1/products` | 获取商品列表 | product:read |
| GET | `/api/v1/products/:id` | 获取商品详情 | product:read |
| POST | `/api/v1/products` | 创建商品 | product:create |
| PUT | `/api/v1/products/:id` | 更新商品 | product:update |
| DELETE | `/api/v1/products/:id` | 删除商品 | product:delete |
| POST | `/api/v1/products/:id/approve` | 审核商品 | product:review |
| POST | `/api/v1/products/collect` | 采集商品 | product:create |
| POST | `/api/v1/products/:id/pricing` | 动态调价 | product:update |
| POST | `/api/v1/products/autonomous-listing` | 自治上架 | product:create |
---
## 2. 接口详情
### 2.1 获取商品列表
**请求**
```http
GET /api/v1/products?platform=AMAZON&status=ACTIVE&page=1&limit=20
```
**请求头**
```
Authorization: Bearer {token}
X-Tenant-Id: {tenantId}
X-Trace-Id: {traceId}
```
**查询参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| platform | string | 否 | 平台筛选 (AMAZON, EBAY, SHOPIFY, etc.) |
| status | string | 否 | 状态筛选 (DRAFTED, PENDING_REVIEW, APPROVED, etc.) |
| page | number | 否 | 页码,默认 1 |
| limit | number | 否 | 每页数量,默认 20 |
**响应**
```json
{
"success": true,
"data": {
"items": [
{
"id": 1,
"title": "Product Name",
"platform": "AMAZON",
"platformProductId": "B08N5WRWNW",
"status": "APPROVED",
"sellingPrice": 29.99,
"purchasePrice": 15.00,
"stock": 100,
"createdAt": "2026-03-18T10:00:00Z",
"updatedAt": "2026-03-18T10:00:00Z"
}
],
"pagination": {
"page": 1,
"limit": 20,
"total": 100,
"totalPages": 5
}
}
}
```
---
### 2.2 获取商品详情
**请求**
```http
GET /api/v1/products/:id
```
**响应**
```json
{
"success": true,
"data": {
"id": 1,
"title": "Product Name",
"description": "Product description...",
"platform": "AMAZON",
"platformProductId": "B08N5WRWNW",
"status": "APPROVED",
"sellingPrice": 29.99,
"purchasePrice": 15.00,
"currency": "USD",
"stock": 100,
"images": ["url1", "url2"],
"attributes": {
"color": "black",
"size": "M"
},
"profitMargin": 45.5,
"roi": 99.9,
"createdAt": "2026-03-18T10:00:00Z",
"updatedAt": "2026-03-18T10:00:00Z"
}
}
```
---
### 2.3 创建商品
**请求**
```http
POST /api/v1/products
```
**请求体**
```json
{
"title": "Product Name",
"description": "Product description",
"platform": "AMAZON",
"platformProductId": "B08N5WRWNW",
"sellingPrice": 29.99,
"purchasePrice": 15.00,
"currency": "USD",
"stock": 100,
"images": ["url1", "url2"],
"attributes": {
"color": "black",
"size": "M"
},
"category": "Electronics",
"tags": ["new", "hot"]
}
```
**响应**
```json
{
"success": true,
"data": {
"id": 1,
"message": "Product created successfully"
}
}
```
---
### 2.4 更新商品
**请求**
```http
PUT /api/v1/products/:id
```
**请求体**
```json
{
"title": "Updated Product Name",
"sellingPrice": 34.99,
"stock": 150,
"status": "PENDING_REVIEW"
}
```
**状态机校验**
- 商品状态流转必须符合预定义状态机
- 非法状态流转返回 400 错误
**响应**
```json
{
"success": true,
"data": {
"id": 1,
"title": "Updated Product Name",
"status": "PENDING_REVIEW",
"updatedAt": "2026-03-18T11:00:00Z"
}
}
```
---
### 2.5 删除商品
**请求**
```http
DELETE /api/v1/products/:id
```
**响应**
```json
{
"success": true,
"message": "Product deleted successfully"
}
```
---
### 2.6 审核商品 [UX_REVIEW_01]
**请求**
```http
POST /api/v1/products/:id/approve
```
**请求体**
```json
{
"decision": "APPROVE",
"reason": "符合上架标准"
}
```
**参数说明**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| decision | string | 是 | APPROVE 或 REJECT |
| reason | string | 否 | 审核意见 |
**响应**
```json
{
"success": true,
"message": "Product approved successfully"
}
```
---
### 2.7 采集商品 [CORE_WORK_01]
**请求**
```http
POST /api/v1/products/collect
```
**请求体**
```json
{
"url": "https://www.1688.com/product/123456.html",
"sandbox": false
}
```
**参数说明**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| url | string | 是 | 商品链接 |
| sandbox | boolean | 否 | 是否使用沙箱模式 |
**响应**
```json
{
"success": true,
"data": {
"jobId": "job-uuid",
"status": "PENDING",
"message": "Crawl job submitted successfully"
}
}
```
---
### 2.8 动态调价 [BIZ_MKT_30]
**请求**
```http
POST /api/v1/products/:id/pricing
```
**响应**
```json
{
"success": true,
"data": {
"productId": 1,
"oldPrice": 29.99,
"newPrice": 32.99,
"reason": "Competitor price increased",
"profitMargin": 48.5,
"updatedAt": "2026-03-18T12:00:00Z"
}
}
```
---
### 2.9 自治上架 [CORE_AUTO_01]
**请求**
```http
POST /api/v1/products/autonomous-listing
```
**请求体**
```json
{
"url": "https://www.1688.com/product/123456.html",
"targetPlatforms": ["AMAZON", "EBAY"]
}
```
**响应**
```json
{
"success": true,
"data": {
"instanceId": "pipeline-uuid",
"status": "RUNNING",
"steps": [
{ "id": "crawl", "status": "PENDING" },
{ "id": "optimize", "status": "PENDING" },
{ "id": "publish", "status": "PENDING" }
]
}
}
```
---
## 3. 数据模型
### 3.1 Product (商品)
| 字段 | 类型 | 说明 |
|------|------|------|
| id | number | 主键 |
| tenantId | string | 租户ID |
| title | string | 商品标题 |
| description | string | 商品描述 |
| platform | string | 平台 (AMAZON, EBAY, etc.) |
| platformProductId | string | 平台商品ID |
| status | string | 状态 |
| sellingPrice | number | 售价 |
| purchasePrice | number | 采购价 |
| currency | string | 币种 |
| stock | number | 库存 |
| images | string[] | 图片URL列表 |
| attributes | object | 属性 |
| category | string | 类目 |
| tags | string[] | 标签 |
| profitMargin | number | 利润率 |
| roi | number | ROI |
| createdAt | string | 创建时间 |
| updatedAt | string | 更新时间 |
### 3.2 状态枚举
```typescript
enum ProductStatus {
DRAFTED = 'DRAFTED', // 草稿
PENDING_REVIEW = 'PENDING_REVIEW', // 待审核
APPROVED = 'APPROVED', // 已通过
REJECTED = 'REJECTED', // 已拒绝
LISTED = 'LISTED', // 已上架
DELISTED = 'DELISTED' // 已下架
}
```
---
## 4. 错误码
| 错误码 | 说明 | HTTP状态 |
|--------|------|----------|
| PRODUCT_NOT_FOUND | 商品不存在 | 404 |
| INVALID_STATUS_TRANSITION | 非法状态流转 | 400 |
| UNAUTHORIZED | 未授权 | 401 |
| FORBIDDEN | 无权限 | 403 |
| VALIDATION_ERROR | 参数校验失败 | 400 |
| INTERNAL_ERROR | 内部错误 | 500 |
---
## 5. 相关服务
- [Backend Design](../Backend_Design.md)
- [Order API](./Order_API.md)
- [Finance API](./Finance_API.md)
---
*本文档基于代码自动生成,最后更新: 2026-03-18*