index.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { timerTask } from '@/utils/timer'
  2. import { commonStore, accountStore, futuresStore, sessionData } from '@/stores'
  3. import { tokenCheck } from '@/services/api/account'
  4. import eventBus from '@/services/bus'
  5. import socket from '@/services/socket'
  6. /**
  7. * 退出登录
  8. */
  9. export function logout(callback?: () => void) {
  10. socket.closeAll()
  11. timerTask.clearAll()
  12. sessionData.reset()
  13. accountStore.reset()
  14. callback && callback()
  15. }
  16. /**
  17. * 初始化业务数据
  18. * @param callback
  19. */
  20. export async function initBaseData(callback?: () => void) {
  21. if (sessionData.getLoginInfo('Token')) {
  22. checkTokenLoop()
  23. const asyncTask = [
  24. commonStore.getLoginData(),
  25. futuresStore.getGoodsList(),
  26. sessionData.getUserMenuList(),
  27. sessionData.getAllEnumList(),
  28. //sessionData.getTableColumnList(),
  29. ]
  30. await Promise.all(asyncTask).then(() => {
  31. accountStore.getAccountList()
  32. callback && callback()
  33. }).catch(() => {
  34. return Promise.reject('初始化失败')
  35. })
  36. } else {
  37. callback && callback()
  38. }
  39. }
  40. /**
  41. * 令牌效验
  42. */
  43. export function checkToken() {
  44. const { LoginID, Token } = sessionData.getValue('loginInfo')
  45. return tokenCheck({
  46. data: {
  47. LoginID,
  48. Token,
  49. },
  50. fail: () => eventBus.$emit('LogoutNotify')
  51. })
  52. }
  53. /**
  54. * 轮询效验令牌
  55. */
  56. export function checkTokenLoop() {
  57. const delay = 1 * 60 * 1000 // 每1分钟效验一次令牌
  58. timerTask.setTimeout(() => {
  59. checkToken().then(() => checkTokenLoop())
  60. }, delay, 'checkToken')
  61. }
  62. /**
  63. * 停止令牌效验
  64. */
  65. export function stopCheckToken() {
  66. timerTask.clearTimeout('checkToken')
  67. }