164 lines
4.3 KiB
TypeScript
164 lines
4.3 KiB
TypeScript
import { ref, computed } from 'vue'
|
|
import { useToast } from './useToast'
|
|
import { useActivity } from './useActivity'
|
|
import { LoadProducts, LoadProductsForAccounts } from '@wails/go/app/App'
|
|
|
|
export interface Product {
|
|
commodityId: string
|
|
commodityName: string
|
|
actionId?: string
|
|
category: string
|
|
score: number
|
|
payType?: number
|
|
payLabel?: string
|
|
count: number
|
|
stockLabel?: string
|
|
pic: string
|
|
status: number
|
|
openStatus: number
|
|
storeStatus: number
|
|
timeLimited: number
|
|
countLimited: number
|
|
eliteLimited: number
|
|
}
|
|
|
|
export interface ProductPayload {
|
|
success: boolean
|
|
source: string
|
|
products: Product[]
|
|
count: number
|
|
fetchedAt: string
|
|
ageSecond: number
|
|
stale: boolean
|
|
}
|
|
|
|
const products = ref<Product[]>([])
|
|
const productInfo = ref<ProductPayload | null>(null)
|
|
const selectedProductId = ref<string | null>(null)
|
|
const selectedProductName = ref('')
|
|
const searchQuery = ref('')
|
|
const loading = ref(false)
|
|
export type XiaodianTab = 'all' | 'discount' | 'hot'
|
|
const xiaodianTab = ref<XiaodianTab>('all')
|
|
|
|
export function useProducts() {
|
|
const { showToast } = useToast()
|
|
const { currentActivity } = useActivity()
|
|
|
|
const filteredProducts = computed(() => {
|
|
const keyword = searchQuery.value.toLowerCase().trim()
|
|
return products.value.filter(p => {
|
|
const matchesTab = currentActivity.value?.id !== 'xiaodian' || xiaodianTab.value === 'all'
|
|
|| (xiaodianTab.value === 'discount' && p.category === '45')
|
|
|| (xiaodianTab.value === 'hot' && p.category === '76')
|
|
if (!matchesTab) return false
|
|
if (!keyword) return true
|
|
return [p.commodityName, p.commodityId, p.score, p.count].some(
|
|
v => String(v ?? '').toLowerCase().includes(keyword)
|
|
)
|
|
})
|
|
})
|
|
|
|
const selectedProduct = computed(() =>
|
|
products.value.find(p => p.commodityId === selectedProductId.value)
|
|
)
|
|
|
|
const applyProductPayload = (data: ProductPayload, showToastMsg: boolean) => {
|
|
products.value = Array.isArray(data.products) ? data.products : []
|
|
productInfo.value = data
|
|
|
|
if (selectedProductId.value && !products.value.some(p => p.commodityId === selectedProductId.value)) {
|
|
clearSelection()
|
|
}
|
|
|
|
if (showToastMsg) {
|
|
showToast('商品已刷新', `共 ${products.value.length} 个`, 'success')
|
|
}
|
|
}
|
|
|
|
const loadProducts = async (showToastMsg = true) => {
|
|
loading.value = true
|
|
try {
|
|
const data = await LoadProducts(true)
|
|
applyProductPayload(data, showToastMsg)
|
|
} catch (e: any) {
|
|
products.value = []
|
|
productInfo.value = null
|
|
clearSelection()
|
|
showToast('商品刷新失败', e.message, 'error')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const loadProductsForAccounts = async (accountNames: string[], showToastMsg = true) => {
|
|
loading.value = true
|
|
try {
|
|
const data = await LoadProductsForAccounts(accountNames)
|
|
applyProductPayload(data, showToastMsg)
|
|
} catch (e: any) {
|
|
products.value = []
|
|
productInfo.value = null
|
|
clearSelection()
|
|
showToast('商品刷新失败', e.message, 'error')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const selectProduct = (id: string, name: string) => {
|
|
selectedProductId.value = id
|
|
selectedProductName.value = name
|
|
localStorage.setItem('selProd', id)
|
|
localStorage.setItem('selProdName', name)
|
|
}
|
|
|
|
const clearSelection = () => {
|
|
selectedProductId.value = null
|
|
selectedProductName.value = ''
|
|
localStorage.removeItem('selProd')
|
|
localStorage.removeItem('selProdName')
|
|
}
|
|
|
|
const setSearch = (query: string) => {
|
|
searchQuery.value = query.trim()
|
|
}
|
|
|
|
const clearSearch = () => {
|
|
searchQuery.value = ''
|
|
}
|
|
|
|
const setXiaodianTab = (tab: XiaodianTab) => {
|
|
xiaodianTab.value = tab
|
|
}
|
|
|
|
const restoreSelection = () => {
|
|
const savedId = localStorage.getItem('selProd')
|
|
const savedName = localStorage.getItem('selProdName')
|
|
if (savedId) {
|
|
selectedProductId.value = savedId
|
|
selectedProductName.value = savedName || ''
|
|
}
|
|
}
|
|
|
|
return {
|
|
products,
|
|
productInfo,
|
|
selectedProductId,
|
|
selectedProductName,
|
|
selectedProduct,
|
|
searchQuery,
|
|
filteredProducts,
|
|
loading,
|
|
xiaodianTab,
|
|
loadProducts,
|
|
loadProductsForAccounts,
|
|
selectProduct,
|
|
clearSelection,
|
|
setSearch,
|
|
clearSearch,
|
|
setXiaodianTab,
|
|
restoreSelection
|
|
}
|
|
}
|