优化前端
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import { ref } from 'vue'
|
||||
import { useToast } from './useToast'
|
||||
|
||||
export interface Account {
|
||||
index: number
|
||||
name: string
|
||||
cookie: string
|
||||
}
|
||||
|
||||
export interface AccountStatus {
|
||||
ok?: boolean
|
||||
message?: string
|
||||
}
|
||||
|
||||
const App = window.go.main.App
|
||||
|
||||
const accounts = ref<Account[]>([])
|
||||
const selectedIndices = ref<Set<number>>(new Set())
|
||||
const statusMap = ref<Record<string, AccountStatus | boolean>>({})
|
||||
const scoreMap = ref<Record<string, string>>({})
|
||||
|
||||
export function useAccounts() {
|
||||
const { showToast } = useToast()
|
||||
|
||||
const loadAccounts = async () => {
|
||||
const data = await App.GetAccounts()
|
||||
accounts.value = data || []
|
||||
|
||||
// 清理无效选择
|
||||
selectedIndices.value = new Set(
|
||||
[...selectedIndices.value].filter(i => i >= 0 && i < accounts.value.length)
|
||||
)
|
||||
saveSelection()
|
||||
}
|
||||
|
||||
const reloadAccounts = async () => {
|
||||
const data = await App.ReloadAccounts()
|
||||
await loadAccounts()
|
||||
showToast('账号已刷新', `共 ${data.count} 个账号`, 'success')
|
||||
}
|
||||
|
||||
const toggleAccount = (index: number) => {
|
||||
if (selectedIndices.value.has(index)) {
|
||||
selectedIndices.value.delete(index)
|
||||
} else {
|
||||
selectedIndices.value.add(index)
|
||||
}
|
||||
saveSelection()
|
||||
}
|
||||
|
||||
const toggleAll = () => {
|
||||
if (selectedIndices.value.size === accounts.value.length) {
|
||||
selectedIndices.value.clear()
|
||||
} else {
|
||||
accounts.value.forEach((_, i) => selectedIndices.value.add(i))
|
||||
}
|
||||
saveSelection()
|
||||
}
|
||||
|
||||
const saveSelection = () => {
|
||||
localStorage.setItem('selAccs', JSON.stringify([...selectedIndices.value]))
|
||||
}
|
||||
|
||||
const restoreSelection = () => {
|
||||
const saved = localStorage.getItem('selAccs')
|
||||
if (saved) {
|
||||
try {
|
||||
selectedIndices.value = new Set(JSON.parse(saved))
|
||||
} catch {}
|
||||
}
|
||||
const savedStatus = localStorage.getItem('statusMap')
|
||||
if (savedStatus) {
|
||||
try {
|
||||
statusMap.value = JSON.parse(savedStatus)
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
const queryAllScore = async () => {
|
||||
showToast('查询积分', '正在并发查询...', 'info')
|
||||
const tasks = accounts.value.map(async (a) => {
|
||||
try {
|
||||
const d = await App.QueryScore(a.name)
|
||||
const score = d.score ?? 'N/A'
|
||||
scoreMap.value[a.name] = String(score)
|
||||
localStorage.setItem('accScore_' + a.name, String(score))
|
||||
} catch {}
|
||||
})
|
||||
await Promise.all(tasks)
|
||||
showToast('积分查询完成', '', 'success')
|
||||
}
|
||||
|
||||
const getScore = (name: string): string => {
|
||||
return scoreMap.value[name] || localStorage.getItem('accScore_' + name) || '-'
|
||||
}
|
||||
|
||||
const resetStatus = () => {
|
||||
statusMap.value = {}
|
||||
localStorage.removeItem('statusMap')
|
||||
}
|
||||
|
||||
const setStatus = (name: string, status: AccountStatus | boolean) => {
|
||||
statusMap.value[name] = status
|
||||
localStorage.setItem('statusMap', JSON.stringify(statusMap.value))
|
||||
}
|
||||
|
||||
const selectedAccountNames = (): string[] => {
|
||||
return [...selectedIndices.value]
|
||||
.map(i => accounts.value[i]?.name)
|
||||
.filter(Boolean) as string[]
|
||||
}
|
||||
|
||||
const buildAccountProducts = (productId: string): 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
|
||||
}
|
||||
})
|
||||
|
||||
if (Object.keys(ap).length === 0) {
|
||||
showToast('提示', '没有有效账号可提交', 'warn')
|
||||
return null
|
||||
}
|
||||
return ap
|
||||
}
|
||||
|
||||
return {
|
||||
accounts,
|
||||
selectedIndices,
|
||||
statusMap,
|
||||
scoreMap,
|
||||
loadAccounts,
|
||||
reloadAccounts,
|
||||
toggleAccount,
|
||||
toggleAll,
|
||||
restoreSelection,
|
||||
queryAllScore,
|
||||
getScore,
|
||||
resetStatus,
|
||||
setStatus,
|
||||
selectedAccountNames,
|
||||
buildAccountProducts
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user