Files
tkAiPage/src/composables/useSchedule.js
2025-09-08 20:52:31 +08:00

46 lines
1.4 KiB
JavaScript

import { ref } from "vue"
import { ElMessage } from "element-plus"
import { tryActivate } from "./useTaskControl"
export function useSchedule(runTaskFn) {
const scheduleEnabled = ref(true)
let schedulePlan = [
{ key: "follow", duration: 40 * 60 * 1000 },
{ key: "like", duration: 20 * 60 * 1000 }
]
let scheduleState = { index: 0, startTime: Date.now() }
let scheduleTimer = null
function runTask(key) {
if (!scheduleEnabled.value) return
runTaskFn(key) // 交给外部实现 follow/like/brushLive 等
}
function startScheduleLoop() {
runTask(schedulePlan[scheduleState.index].key)
if (scheduleTimer) clearInterval(scheduleTimer)
scheduleTimer = setInterval(() => {
const now = Date.now()
const cur = schedulePlan[scheduleState.index]
if (now - scheduleState.startTime >= cur.duration) {
scheduleState.index = (scheduleState.index + 1) % schedulePlan.length
scheduleState.startTime = now
runTask(schedulePlan[scheduleState.index].key)
}
}, 30 * 1000)
}
function stopSchedule() {
scheduleEnabled.value = false
if (scheduleTimer) clearInterval(scheduleTimer)
}
return {
scheduleEnabled,
schedulePlan,
scheduleState,
startScheduleLoop,
stopSchedule
}
}