feat: P1阶段完成 - 订单模块迁移与重构

## P1.3: 订单模块(orders)

### 完整迁移
- 迁移 API: orders.ts
- 迁移 Views: 5个页面(桌面3 + 移动2)
- 迁移 Composables: useOrderSnapshot.ts
- 更新所有导入路径到 shared/

### 核心重构:拆分 useOrderDetail.ts
原始文件405行,混合了订单、支付、结算、争议等多个领域

**拆分为3个独立 composables:**

1. **usePaymentPolling.ts** - 支付轮询
   - 职责:轮询查询支付状态直到完成
   - 功能:开始/停止轮询、检查支付状态、自动重载
   - 代码:~65行

2. **useSettlement.ts** - 结算流程
   - 职责:处理订单结算完整流程
   - 功能:提交/接受/反驳/确认结算、表单管理
   - 代码:~170行

3. **useOrderDetail.ts** - 核心订单(重构后)
   - 职责:订单核心流程,组合使用上述composables
   - 功能:加载、取消、支付、交接、收货、争议
   - 代码:~215行

**重构优势:**
- 职责清晰,单一职责原则
- 可复用,支付和结算逻辑可独立使用
- 易测试,每个composable独立可测
- 易维护,从405行拆分为3个文件

### 技术改进
- 建立清晰的模块边界和导出规范
- 避免循环依赖
- 提高代码可测试性和可维护性

## 里程碑

🎉 **P1 阶段完成!**

-  P0: 基础设施(shared/)- 22个文件
-  P1.1: 钱包模块 - 5个文件
-  P1.2: 聊天模块 - 8个文件
-  P1.3: 订单模块 - 11个文件

**总计:** 3个核心业务模块,46个文件完成迁移

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
yml2213
2026-06-04 08:45:31 +08:00
co-authored by Claude Opus 4.7
parent b5903a169f
commit 125d83f2f2
13 changed files with 5075 additions and 146 deletions
@@ -0,0 +1,147 @@
import type { Order } from '../api/orders'
export interface SnapshotResource {
key: string
label: string
quantity: number
unitPrice: number
chargeMode: '赠送' | '收费'
}
export function readSnapshot(order: Order | null) {
if (!order?.listing_snapshot) return null
try {
return JSON.parse(order.listing_snapshot) as Record<string, any>
} catch {
return null
}
}
export function readNumber(value: unknown): number {
const parsed = Number(value)
return Number.isFinite(parsed) ? parsed : 0
}
export function roundQuantity(value: number): number {
return Math.round(value * 10) / 10
}
export function roundMoney(value: number): number {
return Math.round(value * 100) / 100
}
export function readSnapshotResources(order: Order | null): SnapshotResource[] {
const snapshot = readSnapshot(order)
if (!snapshot?.quantities) return []
const quantities = snapshot.quantities as Record<string, any>[]
return quantities
.map((item) => ({
key: String(item.key || ''),
label: String(item.label || ''),
quantity: readNumber(item.quantity),
unitPrice: readNumber(item.price),
chargeMode: item.charge_mode === '收费' ? '收费' : '赠送',
}))
.filter((item) => item.key && item.label)
}
export function isChargedResource(resource: SnapshotResource): boolean {
return resource.chargeMode === '收费'
}
export function getSnapshotHafCoinM(order: Order | null): number {
const snapshot = readSnapshot(order)
return roundQuantity(readNumber(snapshot?.haf_coin_amount) / 1000000)
}
export function calculateResourceChargeAmount(
resources: SnapshotResource[],
resourceUsage: Record<string, number>
): number {
return roundMoney(
resources.reduce((sum, item) => {
if (!isChargedResource(item)) return sum
const used = resourceUsage[item.key] || 0
return sum + used * item.unitPrice
}, 0)
)
}
export function calculateCheckoutTotal(
resourceCharge: number,
consumableAmount: number,
coinConsumed: number,
otherAmount: number
): number {
return roundMoney(resourceCharge + consumableAmount + coinConsumed + otherAmount)
}
export function calculateCounterTotal(counterForm: {
consumable_amount: number
coin_consumed_m: number
other_amount: number
deposit_deduct_amount: number
}): number {
return roundMoney(
counterForm.consumable_amount +
counterForm.coin_consumed_m +
counterForm.other_amount +
counterForm.deposit_deduct_amount
)
}
export function hydrateResourceUsageFromOrder(
order: Order | null,
resources: SnapshotResource[]
): Record<string, number> {
const usage: Record<string, number> = {}
if (!order?.checkout_info) {
return usage
}
try {
const info = JSON.parse(order.checkout_info) as Record<string, any>
const consumedResources = info.consumed_resources as Record<string, number> | undefined
if (consumedResources) {
resources.forEach((res) => {
if (res.key in consumedResources) {
usage[res.key] = consumedResources[res.key]
}
})
}
} catch {
// ignore
}
return usage
}
export function hydrateCounterFormFromOrder(order: Order | null) {
if (!order?.counter_info) {
return null
}
try {
const info = JSON.parse(order.counter_info) as Record<string, any>
return {
consumable_amount: readNumber(info.consumable_amount),
coin_consumed_m: readNumber(info.coin_consumed_m),
other_amount: readNumber(info.other_amount),
deposit_deduct_amount: readNumber(info.deposit_deduct_amount),
reason: String(info.reason || ''),
evidenceText: String(info.evidence || ''),
}
} catch {
return null
}
}
export function readError(error: unknown, fallback: string): string {
if (error && typeof error === 'object' && 'message' in error) {
return String(error.message)
}
return fallback
}