/** * 订单 Store */ import { defineStore } from 'pinia' import type { Order, OrderStatus } from '@/types' import { getOrderList, getOrderDetail, mockApiResponse } from '@/mock' import { useUserStore } from './user' interface OrderState { orderList: Order[] currentOrder: Order | null loading: boolean } export const useOrderStore = defineStore('order', { state: (): OrderState => ({ orderList: [], currentOrder: null, loading: false }), getters: { // 待支付订单数 waitPayCount: (state) => state.orderList.filter(o => o.status === 0).length, // 进行中订单数 inProgressCount: (state) => state.orderList.filter(o => [1, 2, 3, 4, 5].includes(o.status)).length, // 已完成订单数 completedCount: (state) => state.orderList.filter(o => [6, 7].includes(o.status)).length, // 顾客订单列表 customerOrders: (state) => { // TODO: 根据当前用户的 customerId 过滤 return state.orderList }, // 代练订单列表 playerOrders: (state) => { // TODO: 根据当前用户的 playerId 过滤 return state.orderList }, // 商家订单列表 merchantOrders: (state) => { // TODO: 根据当前用户的 tenantId 过滤 return state.orderList } }, actions: { /** * 获取订单列表 */ async fetchOrderList(params?: { status?: OrderStatus customerId?: number playerId?: number tenantId?: number }): Promise { this.loading = true try { const list = getOrderList(params) this.orderList = list return list } finally { this.loading = false } }, /** * 获取订单详情 */ async fetchOrderDetail(id: number): Promise { const order = getOrderDetail(id) if (order) { this.currentOrder = order } return order }, /** * 创建订单 */ async createOrder(data: Partial): Promise { const response = await mockApiResponse({ id: Date.now(), orderNo: 'ORDER' + Date.now(), ...data, status: 0, createTime: new Date().toISOString(), updateTime: new Date().toISOString() } as Order) return response.data }, /** * 支付订单 */ async payOrder(orderId: number): Promise { const response = await mockApiResponse(true) return response.data }, /** * 取消订单 */ async cancelOrder(orderId: number, reason: string): Promise { const response = await mockApiResponse(true) return response.data }, /** * 确认完成 */ async confirmOrder(orderId: number): Promise { const response = await mockApiResponse(true) return response.data }, /** * 获取顾客订单列表 */ async getCustomerOrders(): Promise { const userStore = useUserStore() const customerId = userStore.userInfo?.customerId || userStore.userId return this.fetchOrderList({ customerId }) }, /** * 获取代练订单列表 */ async getPlayerOrders(): Promise { const userStore = useUserStore() const playerId = userStore.userInfo?.playerId || userStore.userId return this.fetchOrderList({ playerId }) }, /** * 获取商家订单列表 */ async getMerchantOrders(): Promise { const userStore = useUserStore() const tenantId = userStore.tenantId return this.fetchOrderList({ tenantId }) }, /** * 获取订单详情(别名) */ async getOrderDetail(id: number): Promise { return this.fetchOrderDetail(id) } } })