You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
import { ref } from 'vue'
|
|
import { newPodApi } from '@/api/user'
|
|
|
|
// 模块级变量,确保所有组件实例共享同一个定时器状态
|
|
let keepAliveTimer = null
|
|
const isRunning = ref(false)
|
|
|
|
export function useKeepAlive() {
|
|
const callKeepAlive = async () => {
|
|
const session = localStorage.getItem('newPodSession')
|
|
if (!session) {
|
|
stopKeepAlive()
|
|
return
|
|
}
|
|
try {
|
|
await newPodApi({
|
|
session: Number(session),
|
|
id: 2,
|
|
call: {
|
|
service: 'rpc',
|
|
method: 'keepAlive'
|
|
},
|
|
params: {
|
|
timeout: 60
|
|
}
|
|
})
|
|
} catch (e) {
|
|
console.error('KeepAlive error:', e)
|
|
}
|
|
}
|
|
|
|
const startKeepAlive = () => {
|
|
if (keepAliveTimer) return // 已启动,避免重复
|
|
|
|
const session = localStorage.getItem('newPodSession')
|
|
if (!session) return
|
|
isRunning.value = true
|
|
callKeepAlive()
|
|
keepAliveTimer = setInterval(callKeepAlive, 60000)
|
|
}
|
|
|
|
const stopKeepAlive = () => {
|
|
if (keepAliveTimer) {
|
|
clearInterval(keepAliveTimer)
|
|
keepAliveTimer = null
|
|
}
|
|
isRunning.value = false
|
|
}
|
|
|
|
return {
|
|
isRunning,
|
|
startKeepAlive,
|
|
stopKeepAlive
|
|
}
|
|
}
|