修复库存监控提前停止并发布0.2.0
This commit is contained in:
@@ -5,6 +5,7 @@ import { useAccounts } from '@/composables/useAccounts'
|
||||
import { useActivity } from '@/composables/useActivity'
|
||||
import { useProducts } from '@/composables/useProducts'
|
||||
import { useToast } from '@/composables/useToast'
|
||||
import { GetResults } from '@wails/go/app/App'
|
||||
|
||||
const { scheduleActive, seckillRunning, startNow, stopSeckill, startFixed, startSchedule, cancelSchedule, restoreSchedule, subscribeSeckillState, unsubscribeSchedulerEvents } = useScheduler()
|
||||
const {
|
||||
@@ -36,6 +37,8 @@ const stockWatchAttempts = ref(Number(localStorage.getItem('xiaodianStockWatchAt
|
||||
const stockWatchDelayMs = ref(Number(localStorage.getItem('xiaodianStockWatchDelayMs') || 500))
|
||||
const stockWatching = ref(false)
|
||||
const stockWatchAccountProducts = ref<Record<string, string>>({})
|
||||
const stockWatchRunAccountProducts = ref<Record<string, string>>({})
|
||||
const stockWatchReportSessionID = ref('')
|
||||
const stockWatchTargetName = computed(() => {
|
||||
const assignments = stockWatchAccountProducts.value
|
||||
const accountCount = Object.keys(assignments).length
|
||||
@@ -46,6 +49,7 @@ const stockWatchTargetName = computed(() => {
|
||||
let stockWatchTimer: number | null = null
|
||||
let stockWatchStartTimer: number | null = null
|
||||
let stockRefreshing = false
|
||||
let stockWatchSettling = false
|
||||
|
||||
// 从本地缓存恢复定时设置
|
||||
const { savedTime } = restoreSchedule()
|
||||
@@ -137,6 +141,65 @@ const stopStockWatch = () => {
|
||||
}
|
||||
stockRefreshing = false
|
||||
stockWatchAccountProducts.value = {}
|
||||
stockWatchRunAccountProducts.value = {}
|
||||
stockWatchReportSessionID.value = ''
|
||||
}
|
||||
|
||||
const createStockWatchReportSessionID = () => {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||
return `stock_${crypto.randomUUID()}`
|
||||
}
|
||||
return `stock_${Date.now()}_${Math.random().toString(36).slice(2)}`
|
||||
}
|
||||
|
||||
const isStockWatchAccountTerminal = (message: string) => {
|
||||
return /Livelink.*登录态校验失败|余额不足|点券不足|已拥有该道具|已经拥有该道具|已拥有该商品|未绑定账号/.test(message)
|
||||
}
|
||||
|
||||
const isStockWatchProductTerminal = (message: string) => {
|
||||
return /商品不存在|商品已下架|不支持该商品/.test(message)
|
||||
}
|
||||
|
||||
const stockWatchResultAccountName = (key: string) => String(key || '').replace(/_第\d+包$/, '')
|
||||
|
||||
const settleStockWatchRun = async () => {
|
||||
if (stockWatchSettling || Object.keys(stockWatchRunAccountProducts.value).length === 0) return
|
||||
stockWatchSettling = true
|
||||
const runAssignments = stockWatchRunAccountProducts.value
|
||||
try {
|
||||
const data = await GetResults() as Record<string, string>
|
||||
const messagesByAccount: Record<string, string[]> = {}
|
||||
Object.entries(data || {}).forEach(([key, value]) => {
|
||||
const accountName = stockWatchResultAccountName(key)
|
||||
if (!runAssignments[accountName]) return
|
||||
if (!messagesByAccount[accountName]) messagesByAccount[accountName] = []
|
||||
messagesByAccount[accountName].push(String(value || ''))
|
||||
})
|
||||
|
||||
Object.entries(runAssignments).forEach(([accountName, productID]) => {
|
||||
const messages = messagesByAccount[accountName] || []
|
||||
const succeeded = messages.some(message => message.startsWith('成功|'))
|
||||
const terminalAccount = messages.some(isStockWatchAccountTerminal)
|
||||
const terminalProduct = messages.some(isStockWatchProductTerminal)
|
||||
if (succeeded || terminalAccount || terminalProduct) {
|
||||
delete stockWatchAccountProducts.value[accountName]
|
||||
}
|
||||
if (terminalProduct) {
|
||||
Object.entries(stockWatchAccountProducts.value).forEach(([name, assignedProductID]) => {
|
||||
if (assignedProductID === productID) delete stockWatchAccountProducts.value[name]
|
||||
})
|
||||
}
|
||||
})
|
||||
} catch {
|
||||
// 结果读取失败时保留全部账号,下一轮库存刷新后仍可继续抢购。
|
||||
} finally {
|
||||
stockWatchRunAccountProducts.value = {}
|
||||
stockWatchSettling = false
|
||||
}
|
||||
|
||||
if (stockWatching.value && Object.keys(stockWatchAccountProducts.value).length === 0) {
|
||||
stopStockWatch()
|
||||
}
|
||||
}
|
||||
|
||||
const beginKeywordWatch = async () => {
|
||||
@@ -199,7 +262,7 @@ const toggleKeywordWatch = () => {
|
||||
const tryStartStockBuy = async () => {
|
||||
const assignments = stockWatchAccountProducts.value
|
||||
if (Object.keys(assignments).length === 0) return false
|
||||
if (scheduleActive.value || seckillRunning.value) return false
|
||||
if (scheduleActive.value || seckillRunning.value || stockWatchSettling || Object.keys(stockWatchRunAccountProducts.value).length > 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]>()
|
||||
@@ -216,15 +279,14 @@ const tryStartStockBuy = async () => {
|
||||
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()
|
||||
// startFixed 返回只代表任务已提交;账号是否应停止监控要等本轮真实结果。
|
||||
stockWatchRunAccountProducts.value = { ...readyAccountProducts }
|
||||
const started = await startFixed(stockWatchAttempts.value, stockWatchDelayMs.value, readyAccountProducts, stockWatchReportSessionID.value)
|
||||
if (!started) {
|
||||
stockWatchRunAccountProducts.value = {}
|
||||
return false
|
||||
}
|
||||
|
||||
showToast('检测到库存', `${productText},已启动 ${Object.keys(readyAccountProducts).length} 个账号抢购`, 'info', 3000)
|
||||
return true
|
||||
}
|
||||
@@ -238,7 +300,7 @@ const beginStockWatch = async () => {
|
||||
|
||||
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
|
||||
if (!stockWatching.value || scheduleActive.value || seckillRunning.value || stockRefreshing || stockWatchSettling) return
|
||||
stockRefreshing = true
|
||||
try {
|
||||
await loadProductsForAccounts(accountNames(), false)
|
||||
@@ -269,6 +331,7 @@ const toggleStockWatch = () => {
|
||||
saveStockWatchConfig()
|
||||
stopKeywordWatch()
|
||||
stockWatchAccountProducts.value = { ...accountProducts }
|
||||
stockWatchReportSessionID.value = createStockWatchReportSessionID()
|
||||
stockWatching.value = true
|
||||
const [hours, minutes, seconds] = stockWatchStartTime.value.split(':').map(Number)
|
||||
const target = new Date()
|
||||
@@ -292,6 +355,12 @@ watch(isXiaodian, (enabled) => {
|
||||
}
|
||||
})
|
||||
|
||||
watch(seckillRunning, (running, wasRunning) => {
|
||||
if (!running && wasRunning && stockWatching.value) {
|
||||
void settleStockWatchRun()
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
stopKeywordWatch()
|
||||
stopStockWatch()
|
||||
|
||||
Reference in New Issue
Block a user