优化和平小店关键词监控配置
This commit is contained in:
@@ -12,11 +12,15 @@ const { currentActivity } = useActivity()
|
|||||||
const isXiaodian = computed(() => currentActivity.value?.id === 'xiaodian')
|
const isXiaodian = computed(() => currentActivity.value?.id === 'xiaodian')
|
||||||
const { products, selectedProductId, loadProducts, selectProduct } = useProducts()
|
const { products, selectedProductId, loadProducts, selectProduct } = useProducts()
|
||||||
const { showToast } = useToast()
|
const { showToast } = useToast()
|
||||||
const fixedKeyword = ref(localStorage.getItem('xiaodianFixedKeyword') || '')
|
const fixedKeyword = ref(localStorage.getItem('xiaodianFixedKeyword') || localStorage.getItem('xiaodianWatchKeyword') || '')
|
||||||
const fixedAttempts = ref(Number(localStorage.getItem('xiaodianFixedAttempts') || 3))
|
const fixedAttempts = ref(Number(localStorage.getItem('xiaodianFixedAttempts') || 3))
|
||||||
const fixedDelayMs = ref(Number(localStorage.getItem('xiaodianFixedDelayMs') || 500))
|
const fixedDelayMs = ref(Number(localStorage.getItem('xiaodianFixedDelayMs') || 500))
|
||||||
|
const watchStartTime = ref(localStorage.getItem('xiaodianWatchStartTime') || '00:00:00')
|
||||||
|
const watchIntervalSec = ref(Number(localStorage.getItem('xiaodianWatchIntervalSec') || 3))
|
||||||
const keywordWatching = ref(false)
|
const keywordWatching = ref(false)
|
||||||
let keywordTimer: number | null = null
|
let keywordTimer: number | null = null
|
||||||
|
let keywordStartTimer: number | null = null
|
||||||
|
let keywordRefreshing = false
|
||||||
|
|
||||||
// 从本地缓存恢复定时设置
|
// 从本地缓存恢复定时设置
|
||||||
const { savedTime } = restoreSchedule()
|
const { savedTime } = restoreSchedule()
|
||||||
@@ -36,55 +40,99 @@ const queryAssets = () => {
|
|||||||
|
|
||||||
const saveFixedConfig = () => {
|
const saveFixedConfig = () => {
|
||||||
localStorage.setItem('xiaodianFixedKeyword', fixedKeyword.value.trim())
|
localStorage.setItem('xiaodianFixedKeyword', fixedKeyword.value.trim())
|
||||||
|
localStorage.setItem('xiaodianWatchKeyword', fixedKeyword.value.trim())
|
||||||
localStorage.setItem('xiaodianFixedAttempts', String(fixedAttempts.value))
|
localStorage.setItem('xiaodianFixedAttempts', String(fixedAttempts.value))
|
||||||
localStorage.setItem('xiaodianFixedDelayMs', String(fixedDelayMs.value))
|
localStorage.setItem('xiaodianFixedDelayMs', String(fixedDelayMs.value))
|
||||||
|
localStorage.setItem('xiaodianWatchStartTime', watchStartTime.value)
|
||||||
|
localStorage.setItem('xiaodianWatchIntervalSec', String(watchIntervalSec.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
const startFixedBuy = () => {
|
const startFixedBuy = () => {
|
||||||
saveFixedConfig()
|
saveFixedConfig()
|
||||||
|
if (keywordWatching.value) stopKeywordWatch()
|
||||||
startFixed(fixedAttempts.value, fixedDelayMs.value)
|
startFixed(fixedAttempts.value, fixedDelayMs.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stopKeywordWatch = () => {
|
||||||
|
keywordWatching.value = false
|
||||||
|
if (keywordTimer) {
|
||||||
|
clearInterval(keywordTimer)
|
||||||
|
keywordTimer = null
|
||||||
|
}
|
||||||
|
if (keywordStartTimer) {
|
||||||
|
clearTimeout(keywordStartTimer)
|
||||||
|
keywordStartTimer = null
|
||||||
|
}
|
||||||
|
keywordRefreshing = false
|
||||||
|
}
|
||||||
|
|
||||||
|
const beginKeywordWatch = async () => {
|
||||||
|
if (!keywordWatching.value || !isXiaodian.value) return
|
||||||
|
await loadProducts(true)
|
||||||
|
const keyword = fixedKeyword.value.trim().toLowerCase()
|
||||||
|
const hit = products.value.find(p => p.commodityName.toLowerCase().includes(keyword))
|
||||||
|
if (hit) {
|
||||||
|
stopKeywordWatch()
|
||||||
|
selectProduct(hit.commodityId, hit.commodityName)
|
||||||
|
await startFixed(fixedAttempts.value, fixedDelayMs.value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const interval = Math.max(1, Math.min(600, Number(watchIntervalSec.value) || 3)) * 1000
|
||||||
|
keywordTimer = window.setInterval(async () => {
|
||||||
|
if (!keywordWatching.value || scheduleActive.value || keywordRefreshing) return
|
||||||
|
keywordRefreshing = true
|
||||||
|
try {
|
||||||
|
await loadProducts(false)
|
||||||
|
const currentKeyword = fixedKeyword.value.trim().toLowerCase()
|
||||||
|
const currentHit = products.value.find(p => p.commodityName.toLowerCase().includes(currentKeyword))
|
||||||
|
if (currentHit) {
|
||||||
|
stopKeywordWatch()
|
||||||
|
selectProduct(currentHit.commodityId, currentHit.commodityName)
|
||||||
|
await startFixed(fixedAttempts.value, fixedDelayMs.value)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
keywordRefreshing = false
|
||||||
|
}
|
||||||
|
}, interval)
|
||||||
|
}
|
||||||
|
|
||||||
const toggleKeywordWatch = () => {
|
const toggleKeywordWatch = () => {
|
||||||
|
if (keywordWatching.value) {
|
||||||
|
stopKeywordWatch()
|
||||||
|
return
|
||||||
|
}
|
||||||
keywordWatching.value = !keywordWatching.value
|
keywordWatching.value = !keywordWatching.value
|
||||||
if (keywordWatching.value) {
|
if (keywordWatching.value) {
|
||||||
saveFixedConfig()
|
saveFixedConfig()
|
||||||
if (!fixedKeyword.value.trim()) {
|
if (!fixedKeyword.value.trim()) {
|
||||||
keywordWatching.value = false
|
stopKeywordWatch()
|
||||||
showToast('请输入关键词', '设置商品关键词后再开始监控', 'warn')
|
showToast('请输入关键词', '设置商品关键词后再开始监控', 'warn')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
void loadProducts(true)
|
const [hours, minutes, seconds] = watchStartTime.value.split(':').map(Number)
|
||||||
keywordTimer = window.setInterval(async () => {
|
const target = new Date()
|
||||||
if (!keywordWatching.value || scheduleActive.value) return
|
target.setHours(hours || 0, minutes || 0, seconds || 0, 0)
|
||||||
await loadProducts(false)
|
const waitMs = Math.max(0, target.getTime() - Date.now())
|
||||||
const keyword = fixedKeyword.value.trim().toLowerCase()
|
if (waitMs > 0) {
|
||||||
const hit = products.value.find(p => p.commodityName.toLowerCase().includes(keyword))
|
showToast('监控已设置', `${watchStartTime.value} 开始监控`, 'info', 3000)
|
||||||
if (hit) {
|
keywordStartTimer = window.setTimeout(() => {
|
||||||
keywordWatching.value = false
|
keywordStartTimer = null
|
||||||
if (keywordTimer) clearInterval(keywordTimer)
|
void beginKeywordWatch()
|
||||||
selectProduct(hit.commodityId, hit.commodityName)
|
}, waitMs)
|
||||||
await startFixed(fixedAttempts.value, fixedDelayMs.value)
|
} else {
|
||||||
}
|
void beginKeywordWatch()
|
||||||
}, 3000)
|
}
|
||||||
} else if (keywordTimer) {
|
|
||||||
clearInterval(keywordTimer)
|
|
||||||
keywordTimer = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(isXiaodian, (enabled) => {
|
watch(isXiaodian, (enabled) => {
|
||||||
if (!enabled && keywordWatching.value) {
|
if (!enabled && keywordWatching.value) {
|
||||||
keywordWatching.value = false
|
stopKeywordWatch()
|
||||||
if (keywordTimer) {
|
|
||||||
clearInterval(keywordTimer)
|
|
||||||
keywordTimer = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
if (keywordTimer) clearInterval(keywordTimer)
|
stopKeywordWatch()
|
||||||
})
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -112,25 +160,38 @@ onUnmounted(() => {
|
|||||||
<template v-if="isXiaodian">
|
<template v-if="isXiaodian">
|
||||||
<button class="btn btn-fixed" @click="startFixedBuy">固定抢购</button>
|
<button class="btn btn-fixed" @click="startFixedBuy">固定抢购</button>
|
||||||
<button class="btn btn-watch" :class="{ watching: keywordWatching }" @click="toggleKeywordWatch">
|
<button class="btn btn-watch" :class="{ watching: keywordWatching }" @click="toggleKeywordWatch">
|
||||||
{{ keywordWatching ? '停止监控' : '关键词监控' }}
|
{{ keywordWatching ? '停止监控' : '启动关键词监控' }}
|
||||||
</button>
|
</button>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="schedule-bar">
|
<div class="schedule-bar">
|
||||||
<template v-if="isXiaodian">
|
<template v-if="isXiaodian">
|
||||||
<div class="schedule-item fixed-keyword">
|
<div class="xiaodian-settings">
|
||||||
<label>上架关键词</label>
|
<span class="settings-title">监控设置</span>
|
||||||
<input v-model="fixedKeyword" type="text" placeholder="例如:礼包" @change="saveFixedConfig">
|
<div class="schedule-item">
|
||||||
</div>
|
<label>启动</label>
|
||||||
<div class="schedule-item">
|
<input v-model="watchStartTime" type="time" step="1" @change="saveFixedConfig">
|
||||||
<label>次数</label>
|
</div>
|
||||||
<input v-model.number="fixedAttempts" type="number" min="1" max="100" step="1" @change="saveFixedConfig">
|
<div class="schedule-item">
|
||||||
</div>
|
<label>刷新</label>
|
||||||
<div class="schedule-item">
|
<input v-model.number="watchIntervalSec" type="number" min="1" max="600" step="1" @change="saveFixedConfig">
|
||||||
<label>间隔</label>
|
<span class="input-unit">秒</span>
|
||||||
<input v-model.number="fixedDelayMs" type="number" min="0" max="60000" step="50" @change="saveFixedConfig">
|
</div>
|
||||||
<span class="input-unit">ms</span>
|
<span class="settings-title buy-title">商品抢购设置</span>
|
||||||
|
<div class="schedule-item fixed-keyword">
|
||||||
|
<label>关键词</label>
|
||||||
|
<input v-model="fixedKeyword" type="text" placeholder="例如:礼包" @change="saveFixedConfig">
|
||||||
|
</div>
|
||||||
|
<div class="schedule-item">
|
||||||
|
<label>次数</label>
|
||||||
|
<input v-model.number="fixedAttempts" type="number" min="1" max="100" step="1" @change="saveFixedConfig">
|
||||||
|
</div>
|
||||||
|
<div class="schedule-item">
|
||||||
|
<label>间隔</label>
|
||||||
|
<input v-model.number="fixedDelayMs" type="number" min="0" max="60000" step="50" @change="saveFixedConfig">
|
||||||
|
<span class="input-unit">ms</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<div class="schedule-item">
|
<div class="schedule-item">
|
||||||
@@ -212,6 +273,20 @@ onUnmounted(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.fixed-keyword input { width: 132px; }
|
.fixed-keyword input { width: 132px; }
|
||||||
|
.xiaodian-settings {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
justify-content: flex-end;
|
||||||
|
}
|
||||||
|
.settings-title {
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin-right: 2px;
|
||||||
|
}
|
||||||
|
.buy-title { margin-left: 8px; }
|
||||||
.btn-fixed { border-color: var(--warning); color: var(--warning); }
|
.btn-fixed { border-color: var(--warning); color: var(--warning); }
|
||||||
.btn-watch { border-color: var(--accent-border); color: var(--accent); }
|
.btn-watch { border-color: var(--accent-border); color: var(--accent); }
|
||||||
.btn-watch.watching { background: var(--accent-soft); }
|
.btn-watch.watching { background: var(--accent-soft); }
|
||||||
|
|||||||
Reference in New Issue
Block a user