import { timerTask } from '@/utils/timer'
import { loginStore } from '@/stores'
import { tokenCheck } from '@/services/api/account'
import eventBus from '@/services/bus'

/**
 * 令牌校验
 */
export function checkToken() {
    const { loginId, token } = loginStore.$mapGetters()
    return tokenCheck({
        data: {
            LoginID: loginId.value,
            Token: token.value,
        },
        fail: () => eventBus.$emit('LogoutNotify')
    })
}

/**
 * 轮询校验令牌
 */
export function checkTokenLoop() {
    const delay = 1 * 60 * 1000 // 每1分钟校验一次令牌
    timerTask.setTimeout(() => {
        checkToken().then(() => checkTokenLoop())
    }, delay, 'checkToken')
}

/**
 * 停止令牌校验
 */
export function stopCheckToken() {
    timerTask.clearTimeout('checkToken')
}