52 lines
2.2 KiB
Markdown
52 lines
2.2 KiB
Markdown
# order-detail.uvue — 订单详情
|
||
|
||
## 概要
|
||
展示单笔订单全部信息并提供状态流转(接单、确认取货、确认送达、拒单等)。页面会联合查询 `ml_orders`、`ml_order_items`、`ml_shops` 与 `ml_delivery_tasks`。
|
||
|
||
## 数据结构(常用字段)
|
||
- `Order`
|
||
- `id: string | number`
|
||
- `order_no: string`
|
||
- `cid?: number` (兼容旧字段)
|
||
- `shipping_address | pickup_address`: object/string
|
||
- `shipping_fee`, `total_amount`, `status`
|
||
|
||
- `OrderItem`
|
||
- `id`, `order_id`, `product_name`, `qty`, `price`
|
||
|
||
- `DeliveryTask`
|
||
- `id`, `order_id`, `driver_id`, `status`, `accepted_at`, `picked_at`, `delivered_at`
|
||
|
||
## 关键方法
|
||
- `onLoad(options)`
|
||
- 解析 `options.id` 与 `options.status`,调用 `loadOrderDetail(id)`。
|
||
|
||
- `loadOrderDetail(id)`
|
||
- 判断 ID 类型(UUID / 数字 / 非数字)以决定查询字段(`id`、`cid`、`order_no`)。
|
||
- 并行查询 `ml_orders`, `ml_order_items`, `ml_shops`, `ml_delivery_tasks` 并合并到页面状态。
|
||
|
||
- `acceptOrder()` / `rejectOrder(reason)`
|
||
- accept: 尝试对 `ml_delivery_tasks` 执行 `update driver_id` 操作并设置 `status=2`(处理中),需要后端并发保护。
|
||
- reject: 增加拒单原因到 `ml_delivery_tasks` 或 `order_notes` 并回滚本地 UI 状态。
|
||
|
||
- `confirmPickup()` / `confirmDelivery()`
|
||
- 根据 `task.id` 更新相应时间戳字段(`picked_at`/`delivered_at`)并设置状态(例如 `status=3/4`)。
|
||
|
||
## 示例:按 id 类型查询(伪代码)
|
||
```
|
||
let q = supa.from('ml_orders').select('*')
|
||
if (isUUID(id)) q = q.eq('id', id)
|
||
else if (isNumeric(id)) q = q.eq('cid', id)
|
||
else q = q.eq('order_no', id)
|
||
const { data: order } = await q.limit(1).execute()
|
||
```
|
||
|
||
## 事务与并发注意
|
||
- 接单场景应使用后端原子性检查(数据库事务或行级乐观锁)以避免多司机同时接单。
|
||
- 前端接单流程:先尝试 update(带 where driver_id IS NULL),若返回 0 row affected 则提示已被接单。
|
||
|
||
## 错误处理与回退
|
||
- 捕获所有 supa 调用错误并将友好错误展示给用户(例如:'网络错误,请稍后重试')。
|
||
- 对可能缺失的字段(地址为字符串或对象)使用 `_transformAddress()` 做兼容处理。
|
||
|