feat: improve xiaodian seckill flow
This commit is contained in:
@@ -25,6 +25,7 @@ const accounts = ref<Account[]>([])
|
||||
const selectedIndices = ref<Set<number>>(new Set())
|
||||
const statusMap = ref<Record<string, AccountStatus | boolean>>({})
|
||||
const scoreMap = ref<Record<string, string>>({})
|
||||
const accountProductMap = ref<Record<string, string>>({})
|
||||
export interface XiaodianAssets {
|
||||
success: boolean
|
||||
coupons: number
|
||||
@@ -93,6 +94,12 @@ export function useAccounts() {
|
||||
statusMap.value = JSON.parse(savedStatus)
|
||||
} catch {}
|
||||
}
|
||||
const savedProducts = localStorage.getItem('accountProducts')
|
||||
if (savedProducts) {
|
||||
try {
|
||||
accountProductMap.value = JSON.parse(savedProducts)
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
const queryAllScore = async () => {
|
||||
@@ -170,26 +177,43 @@ export function useAccounts() {
|
||||
markSelectedStatus({ pending: true, message })
|
||||
}
|
||||
|
||||
const buildAccountProducts = (productId: string): Record<string, string> | null => {
|
||||
const setAccountProduct = (accountName: string, productId: string) => {
|
||||
if (productId) {
|
||||
accountProductMap.value[accountName] = productId
|
||||
} else {
|
||||
delete accountProductMap.value[accountName]
|
||||
}
|
||||
localStorage.setItem('accountProducts', JSON.stringify(accountProductMap.value))
|
||||
}
|
||||
|
||||
const getAccountProduct = (accountName: string): string => {
|
||||
return accountProductMap.value[accountName] || ''
|
||||
}
|
||||
|
||||
const buildAccountProducts = (productId: string, respectOverrides = true): Record<string, string> | null => {
|
||||
if (selectedIndices.value.size === 0) {
|
||||
showToast('提示', '请勾选要抢购的账号', 'warn')
|
||||
return null
|
||||
}
|
||||
if (!productId) {
|
||||
showToast('提示', '请先选择商品', 'warn')
|
||||
return null
|
||||
}
|
||||
|
||||
const ap: Record<string, string> = {}
|
||||
selectedIndices.value.forEach(i => {
|
||||
const account = accounts.value[i]
|
||||
if (account) {
|
||||
ap[account.name] = productId
|
||||
const selectedProduct = respectOverrides ? getAccountProduct(account.name) : ''
|
||||
const assignedProduct = selectedProduct || productId
|
||||
if (assignedProduct) {
|
||||
ap[account.name] = assignedProduct
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (Object.keys(ap).length === 0) {
|
||||
showToast('提示', '没有有效账号可提交', 'warn')
|
||||
showToast('提示', '请先选择商品,或为每个账号分配商品', 'warn')
|
||||
return null
|
||||
}
|
||||
if (Object.keys(ap).length !== selectedIndices.value.size) {
|
||||
showToast('提示', '还有已选账号没有商品', 'warn')
|
||||
return null
|
||||
}
|
||||
return ap
|
||||
@@ -201,6 +225,7 @@ export function useAccounts() {
|
||||
statusMap,
|
||||
scoreMap,
|
||||
xiaodianAssetsMap,
|
||||
accountProductMap,
|
||||
loadAccounts,
|
||||
reloadAccounts,
|
||||
toggleAccount,
|
||||
@@ -217,6 +242,8 @@ export function useAccounts() {
|
||||
setStatus,
|
||||
markSelectedStatus,
|
||||
markSelectedPending,
|
||||
setAccountProduct,
|
||||
getAccountProduct,
|
||||
selectedAccountNames,
|
||||
buildAccountProducts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user