优化前端
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { useToast } from './useToast'
|
||||
|
||||
export interface Product {
|
||||
commodityId: string
|
||||
commodityName: string
|
||||
score: number
|
||||
count: number
|
||||
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 App = window.go.main.App
|
||||
|
||||
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 function useProducts() {
|
||||
const { showToast } = useToast()
|
||||
|
||||
const filteredProducts = computed(() => {
|
||||
const keyword = searchQuery.value.toLowerCase().trim()
|
||||
if (!keyword) return products.value
|
||||
return products.value.filter(p =>
|
||||
[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 loadProducts = async (showToastMsg = true) => {
|
||||
loading.value = true
|
||||
try {
|
||||
const data = await App.LoadProducts(true)
|
||||
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')
|
||||
}
|
||||
} 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 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,
|
||||
loadProducts,
|
||||
selectProduct,
|
||||
clearSelection,
|
||||
setSearch,
|
||||
clearSearch,
|
||||
restoreSelection
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user