Files
shop-toy/src/store/merchant.ts
2025-12-19 12:04:22 +08:00

165 lines
4.2 KiB
TypeScript

/**
* 商户端状态管理
*/
import { defineStore } from 'pinia'
import type {
MerchantStats,
MerchantOrder,
MerchantGoods,
FinanceOverview,
ShopInfo,
} from '@/typings/merchant'
import { OrderStatus, GoodsStatus } from '@/typings/merchant'
import * as api from '@/pagesMerchant/api'
export const useMerchantStore = defineStore('merchant', {
state: () => ({
// 统计数据
stats: null as MerchantStats | null,
// 订单相关
orders: [] as MerchantOrder[],
orderTotal: 0,
currentOrder: null as MerchantOrder | null,
// 商品相关
goods: [] as MerchantGoods[],
goodsTotal: 0,
currentGoods: null as MerchantGoods | null,
// 财务相关
financeOverview: null as FinanceOverview | null,
// 店铺相关
shopInfo: null as ShopInfo | null,
// 加载状态
loading: false,
}),
getters: {
// 待处理订单数
pendingOrderCount(state): number {
return state.orders.filter(o =>
o.status === OrderStatus.PENDING || o.status === OrderStatus.SHIPPING
).length
},
// 上架商品数
onlineGoodsCount(state): number {
return state.goods.filter(g => g.status === GoodsStatus.ON).length
},
// 库存预警商品数
lowStockGoodsCount(state): number {
return state.goods.filter(g => g.stock <= 10).length
},
},
actions: {
// ==================== 统计 ====================
async fetchStats() {
this.loading = true
try {
this.stats = await api.getMerchantStats()
} finally {
this.loading = false
}
},
// ==================== 订单 ====================
async fetchOrders(params: { status?: OrderStatus | 'all'; keyword?: string } = {}) {
this.loading = true
try {
const res = await api.getMerchantOrders(params)
this.orders = res.list
this.orderTotal = res.total
} finally {
this.loading = false
}
},
async fetchOrderDetail(id: string) {
this.loading = true
try {
this.currentOrder = await api.getMerchantOrderDetail(id)
} finally {
this.loading = false
}
},
async confirmOrder(id: string) {
await api.confirmOrder(id)
// 更新本地状态
const order = this.orders.find(o => o.id === id)
if (order) order.status = OrderStatus.SHIPPING
if (this.currentOrder?.id === id) {
this.currentOrder.status = OrderStatus.SHIPPING
}
},
async shipOrder(id: string, data: { company: string; trackingNo: string }) {
await api.shipOrder(id, data)
const order = this.orders.find(o => o.id === id)
if (order) order.status = OrderStatus.SHIPPED
if (this.currentOrder?.id === id) {
this.currentOrder.status = OrderStatus.SHIPPED
}
},
// ==================== 商品 ====================
async fetchGoods(params: { status?: GoodsStatus | 'all' | 'lowStock'; keyword?: string } = {}) {
this.loading = true
try {
const res = await api.getMerchantGoodsList(params)
this.goods = res.list
this.goodsTotal = res.total
} finally {
this.loading = false
}
},
async fetchGoodsDetail(id: string) {
this.loading = true
try {
this.currentGoods = await api.getMerchantGoodsDetail(id)
} finally {
this.loading = false
}
},
async updateGoodsStatus(id: string, status: GoodsStatus) {
await api.updateGoodsStatus(id, status)
const goods = this.goods.find(g => g.id === id)
if (goods) goods.status = status
},
// ==================== 财务 ====================
async fetchFinanceOverview() {
this.loading = true
try {
this.financeOverview = await api.getFinanceOverview()
} finally {
this.loading = false
}
},
// ==================== 店铺 ====================
async fetchShopInfo() {
this.loading = true
try {
this.shopInfo = await api.getShopInfo()
} finally {
this.loading = false
}
},
async updateShopInfo(data: Partial<ShopInfo>) {
await api.updateShopInfo(data)
if (this.shopInfo) {
Object.assign(this.shopInfo, data)
}
},
},
})