158 lines
3.7 KiB
TypeScript
158 lines
3.7 KiB
TypeScript
/**
|
|
* 订单 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<Order[]> {
|
|
this.loading = true
|
|
try {
|
|
const list = getOrderList(params)
|
|
this.orderList = list
|
|
return list
|
|
} finally {
|
|
this.loading = false
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取订单详情
|
|
*/
|
|
async fetchOrderDetail(id: number): Promise<Order | undefined> {
|
|
const order = getOrderDetail(id)
|
|
if (order) {
|
|
this.currentOrder = order
|
|
}
|
|
return order
|
|
},
|
|
|
|
/**
|
|
* 创建订单
|
|
*/
|
|
async createOrder(data: Partial<Order>): Promise<Order> {
|
|
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<boolean> {
|
|
const response = await mockApiResponse(true)
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* 取消订单
|
|
*/
|
|
async cancelOrder(orderId: number, reason: string): Promise<boolean> {
|
|
const response = await mockApiResponse(true)
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* 确认完成
|
|
*/
|
|
async confirmOrder(orderId: number): Promise<boolean> {
|
|
const response = await mockApiResponse(true)
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* 获取顾客订单列表
|
|
*/
|
|
async getCustomerOrders(): Promise<Order[]> {
|
|
const userStore = useUserStore()
|
|
const customerId = userStore.userInfo?.customerId || userStore.userId
|
|
return this.fetchOrderList({ customerId })
|
|
},
|
|
|
|
/**
|
|
* 获取代练订单列表
|
|
*/
|
|
async getPlayerOrders(): Promise<Order[]> {
|
|
const userStore = useUserStore()
|
|
const playerId = userStore.userInfo?.playerId || userStore.userId
|
|
return this.fetchOrderList({ playerId })
|
|
},
|
|
|
|
/**
|
|
* 获取商家订单列表
|
|
*/
|
|
async getMerchantOrders(): Promise<Order[]> {
|
|
const userStore = useUserStore()
|
|
const tenantId = userStore.tenantId
|
|
return this.fetchOrderList({ tenantId })
|
|
},
|
|
|
|
/**
|
|
* 获取订单详情(别名)
|
|
*/
|
|
async getOrderDetail(id: number): Promise<Order | undefined> {
|
|
return this.fetchOrderDetail(id)
|
|
}
|
|
}
|
|
})
|