import { defineStore } from 'pinia' import { createOrder, getOrderList, getOrderDetail, cancelOrder, payOrder } from '@/api/order' import type { Order, OrderStatus } from '@/typings/mall' export const useOrderStore = defineStore('order', { state: () => ({ orderList: [] as Order[], currentOrder: null as Order | null, }), actions: { // 获取订单列表 async fetchOrderList(status?: OrderStatus) { try { const res: any = await getOrderList(status) this.orderList = res.data } catch (error) { console.error('获取订单列表失败', error) } }, // 获取订单详情 async fetchOrderDetail(id: string) { try { const res: any = await getOrderDetail(id) this.currentOrder = res.data return res.data } catch (error) { console.error('获取订单详情失败', error) return null } }, // 创建订单 async createOrder(data: any) { try { const res: any = await createOrder(data) return res.data } catch (error) { console.error('创建订单失败', error) throw error } }, // 支付订单 async payOrder(id: string) { try { await payOrder(id) // 更新列表或详情 if (this.currentOrder && this.currentOrder.id === id) { this.fetchOrderDetail(id) } this.fetchOrderList() } catch (error) { console.error('支付订单失败', error) throw error } }, // 取消订单 async cancelOrder(id: string) { try { await cancelOrder(id) if (this.currentOrder && this.currentOrder.id === id) { this.fetchOrderDetail(id) } this.fetchOrderList() } catch (error) { console.error('取消订单失败', error) throw error } }, }, })