支持按账号商品监控库存抢购
This commit is contained in:
@@ -14,11 +14,12 @@ const {
|
||||
queryAllScore,
|
||||
queryAllXiaodianAssets,
|
||||
queryAllXiaodianRoles,
|
||||
getXiaodianBindQRCode
|
||||
getXiaodianBindQRCode,
|
||||
buildAccountProducts
|
||||
} = useAccounts()
|
||||
const { currentActivity } = useActivity()
|
||||
const isXiaodian = computed(() => currentActivity.value?.id === 'xiaodian')
|
||||
const { products, selectedProductId, selectedProductName, loadProducts, selectProduct } = useProducts()
|
||||
const { products, selectedProductId, selectedProductName, loadProducts, loadProductsForAccounts, selectProduct } = useProducts()
|
||||
const { showToast } = useToast()
|
||||
const fixedKeyword = ref(localStorage.getItem('xiaodianFixedKeyword') || localStorage.getItem('xiaodianWatchKeyword') || '')
|
||||
const fixedAttempts = ref(Number(localStorage.getItem('xiaodianFixedAttempts') || 3))
|
||||
@@ -34,8 +35,14 @@ const stockWatchIntervalSec = ref(Number(localStorage.getItem('xiaodianStockWatc
|
||||
const stockWatchAttempts = ref(Number(localStorage.getItem('xiaodianStockWatchAttempts') || 3))
|
||||
const stockWatchDelayMs = ref(Number(localStorage.getItem('xiaodianStockWatchDelayMs') || 500))
|
||||
const stockWatching = ref(false)
|
||||
const stockWatchTarget = ref<{ id: string; name: string } | null>(null)
|
||||
const stockWatchTargetName = computed(() => stockWatchTarget.value?.name || selectedProductName.value || '请先在左侧选择商品')
|
||||
const stockWatchAccountProducts = ref<Record<string, string>>({})
|
||||
const stockWatchTargetName = computed(() => {
|
||||
const assignments = stockWatchAccountProducts.value
|
||||
const accountCount = Object.keys(assignments).length
|
||||
if (accountCount === 0) return selectedProductName.value || '请先为账号选择商品'
|
||||
const names = [...new Set(Object.values(assignments).map(id => products.value.find(item => item.commodityId === id)?.commodityName || id))]
|
||||
return `${names.join('、')} · ${accountCount} 个账号`
|
||||
})
|
||||
let stockWatchTimer: number | null = null
|
||||
let stockWatchStartTimer: number | null = null
|
||||
let stockRefreshing = false
|
||||
@@ -129,6 +136,7 @@ const stopStockWatch = () => {
|
||||
stockWatchStartTimer = null
|
||||
}
|
||||
stockRefreshing = false
|
||||
stockWatchAccountProducts.value = {}
|
||||
}
|
||||
|
||||
const beginKeywordWatch = async () => {
|
||||
@@ -189,32 +197,51 @@ const toggleKeywordWatch = () => {
|
||||
}
|
||||
|
||||
const tryStartStockBuy = async () => {
|
||||
const target = stockWatchTarget.value
|
||||
if (!target) return false
|
||||
const assignments = stockWatchAccountProducts.value
|
||||
if (Object.keys(assignments).length === 0) return false
|
||||
if (scheduleActive.value || seckillRunning.value) return false
|
||||
const product = products.value.find(item => item.commodityId === target.id)
|
||||
// count 为 -1 时代表库存未公开,不能把未知状态误判为可抢。
|
||||
if (!product || product.count <= 0) return false
|
||||
const productsByID = new Map(products.value.map(product => [product.commodityId, product]))
|
||||
const readyAccountProducts: Record<string, string> = {}
|
||||
const readyProducts = new Map<string, typeof products.value[number]>()
|
||||
Object.entries(assignments).forEach(([accountName, productID]) => {
|
||||
const product = productsByID.get(productID)
|
||||
// count 为 -1 时代表库存未公开,不能把未知状态误判为可抢。
|
||||
if (product && product.count > 0) {
|
||||
readyAccountProducts[accountName] = productID
|
||||
readyProducts.set(productID, product)
|
||||
}
|
||||
})
|
||||
if (Object.keys(readyAccountProducts).length === 0) return false
|
||||
|
||||
stopStockWatch()
|
||||
selectProduct(product.commodityId, product.commodityName)
|
||||
const stockText = product.stockLabel || `库存 ${product.count}`
|
||||
showToast('检测到库存', `${product.commodityName} ${stockText},开始固定抢购`, 'info', 3000)
|
||||
await startFixed(stockWatchAttempts.value, stockWatchDelayMs.value)
|
||||
const productText = [...readyProducts.values()]
|
||||
.map(product => `${product.commodityName} ${product.stockLabel || `库存 ${product.count}`}`)
|
||||
.join(';')
|
||||
const started = await startFixed(stockWatchAttempts.value, stockWatchDelayMs.value, readyAccountProducts)
|
||||
if (!started) return false
|
||||
|
||||
Object.keys(readyAccountProducts).forEach(accountName => {
|
||||
delete stockWatchAccountProducts.value[accountName]
|
||||
})
|
||||
if (Object.keys(stockWatchAccountProducts.value).length === 0) {
|
||||
stopStockWatch()
|
||||
}
|
||||
showToast('检测到库存', `${productText},已启动 ${Object.keys(readyAccountProducts).length} 个账号抢购`, 'info', 3000)
|
||||
return true
|
||||
}
|
||||
|
||||
const beginStockWatch = async () => {
|
||||
if (!stockWatching.value || !isXiaodian.value || !stockWatchTarget.value) return
|
||||
await loadProducts(true)
|
||||
if (await tryStartStockBuy()) return
|
||||
if (!stockWatching.value || !isXiaodian.value || Object.keys(stockWatchAccountProducts.value).length === 0) return
|
||||
const accountNames = () => Object.keys(stockWatchAccountProducts.value)
|
||||
await loadProductsForAccounts(accountNames(), true)
|
||||
await tryStartStockBuy()
|
||||
if (!stockWatching.value) return
|
||||
|
||||
const interval = Math.max(1, Math.min(600, Number(stockWatchIntervalSec.value) || 3)) * 1000
|
||||
stockWatchTimer = window.setInterval(async () => {
|
||||
if (!stockWatching.value || scheduleActive.value || seckillRunning.value || stockRefreshing) return
|
||||
stockRefreshing = true
|
||||
try {
|
||||
await loadProducts(false)
|
||||
await loadProductsForAccounts(accountNames(), false)
|
||||
await tryStartStockBuy()
|
||||
} finally {
|
||||
stockRefreshing = false
|
||||
@@ -227,22 +254,28 @@ const toggleStockWatch = () => {
|
||||
stopStockWatch()
|
||||
return
|
||||
}
|
||||
const product = products.value.find(item => item.commodityId === selectedProductId.value)
|
||||
if (!product) {
|
||||
showToast('请选择商品', '请先在左侧选择要监控库存的商品', 'warn')
|
||||
const accountProducts = buildAccountProducts(selectedProductId.value || '')
|
||||
if (!accountProducts) {
|
||||
return
|
||||
}
|
||||
|
||||
const missingProductIDs = [...new Set(Object.values(accountProducts))]
|
||||
.filter(productID => !products.value.some(product => product.commodityId === productID))
|
||||
if (missingProductIDs.length > 0) {
|
||||
showToast('商品未加载', '请刷新商品后再启动库存监控', 'warn')
|
||||
return
|
||||
}
|
||||
|
||||
saveStockWatchConfig()
|
||||
stopKeywordWatch()
|
||||
stockWatchTarget.value = { id: product.commodityId, name: product.commodityName }
|
||||
stockWatchAccountProducts.value = { ...accountProducts }
|
||||
stockWatching.value = true
|
||||
const [hours, minutes, seconds] = stockWatchStartTime.value.split(':').map(Number)
|
||||
const target = new Date()
|
||||
target.setHours(hours || 0, minutes || 0, seconds || 0, 0)
|
||||
const waitMs = Math.max(0, target.getTime() - Date.now())
|
||||
if (waitMs > 0) {
|
||||
showToast('库存监控已设置', `${stockWatchTarget.value.name} 将在 ${stockWatchStartTime.value} 开始监控`, 'info', 3000)
|
||||
showToast('库存监控已设置', `${stockWatchTargetName.value} 将在 ${stockWatchStartTime.value} 开始监控`, 'info', 3000)
|
||||
stockWatchStartTimer = window.setTimeout(() => {
|
||||
stockWatchStartTimer = null
|
||||
void beginStockWatch()
|
||||
|
||||
Reference in New Issue
Block a user