index.ts 901 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { timerTask } from '@/utils/timer'
  2. import { tokenCheck } from '@/services/api/account'
  3. import eventBus from '@/services/bus'
  4. /**
  5. * 令牌校验
  6. */
  7. export async function checkToken() {
  8. try {
  9. return await tokenCheck()
  10. } catch (err) {
  11. // 临时解决方案,有可能出现网络问题导致超时后账号被登出
  12. if (err !== '业务超时') {
  13. eventBus.$emit('LogoutNotify', '登录失效,请重新登录')
  14. }
  15. return Promise.reject(err)
  16. }
  17. }
  18. /**
  19. * 轮询校验令牌
  20. */
  21. export function checkTokenLoop() {
  22. const delay = 1 * 60 * 1000 // 每1分钟校验一次令牌
  23. timerTask.setTimeout(() => {
  24. checkToken().then(() => checkTokenLoop())
  25. }, delay, 'checkToken')
  26. }
  27. /**
  28. * 停止令牌校验
  29. */
  30. export function stopCheckToken() {
  31. timerTask.clearTimeout('checkToken')
  32. }