router-guards.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { isNavigationFailure, Router } from 'vue-router';
  2. import { useUserStore } from '@/store/modules/users';
  3. import { storage } from '@/utils/storage';
  4. import { PageEnum } from '@/enums/pageEnum';
  5. import { ACCESS_TOKEN, ACCESS_TOKEN_ADMIN } from '@/store/mutation-types';
  6. const LOGIN_PATH = PageEnum.BASE_LOGIN;
  7. const whitePathList = [LOGIN_PATH]; // no redirect whitelist
  8. const isChrome = () => {
  9. const isChromium = (window as any).chrome;
  10. const winNav = window.navigator;
  11. const vendorName = winNav.vendor;
  12. const isOpera = typeof (window as any).opr !== 'undefined';
  13. const isIEedge = winNav.userAgent.indexOf('Edge') > -1;
  14. const isIOSChrome = winNav.userAgent.match('CriOS');
  15. return (
  16. isIOSChrome ||
  17. (isChromium !== null &&
  18. typeof isChromium !== 'undefined' &&
  19. vendorName === 'Google Inc.' &&
  20. isOpera === false &&
  21. isIEedge === false)
  22. );
  23. };
  24. export function createRouterGuards(router: Router) {
  25. const userStore = useUserStore();
  26. router.beforeEach(async (to, from, next) => {
  27. if (to.path === '/attend-class') {
  28. let title = to.meta.title;
  29. if (to.query.type === 'preview') {
  30. title = '预览课件';
  31. }
  32. (document as any).title = title ? title : ('音乐数字课堂' as string);
  33. } else {
  34. (document as any).title = to.meta.title
  35. ? to.meta.title
  36. : ('音乐数字课堂' as string);
  37. }
  38. if ('serviceWorker' in navigator) {
  39. console.log(caches.keys(), 'caches.keys()');
  40. caches.keys().then(function (cacheNames) {
  41. cacheNames.forEach(function (cacheName) {
  42. caches.delete(cacheName);
  43. });
  44. });
  45. }
  46. if (!isChrome()) {
  47. return;
  48. }
  49. window.$loadingBar && window.$loadingBar.start();
  50. // console.log(window.$loadingBar, '232332');
  51. if (from.path === LOGIN_PATH && to.name === 'errorPage') {
  52. next(PageEnum.BASE_HOME);
  53. return;
  54. }
  55. // Whitelist can be directly entered
  56. if (whitePathList.includes(to.path as PageEnum)) {
  57. next();
  58. return;
  59. }
  60. let token = storage.get(ACCESS_TOKEN);
  61. const authSource = sessionStorage.getItem('authSource');
  62. if (authSource === 'admin' && storage.get(ACCESS_TOKEN_ADMIN)) {
  63. token = storage.get(ACCESS_TOKEN_ADMIN);
  64. }
  65. // console.log(token, 'access token');
  66. if (!token) {
  67. // You can access without permissions. You need to set the routing meta.ignoreAuth to true
  68. if (to.meta.ignoreAuth) {
  69. next();
  70. return;
  71. }
  72. // redirect login page
  73. const redirectData: { path: string; replace: boolean; query?: any } = {
  74. path: LOGIN_PATH,
  75. replace: true
  76. };
  77. if (to.path) {
  78. redirectData.query = {
  79. ...redirectData.query,
  80. redirect: to.path
  81. };
  82. }
  83. console.log(redirectData, to);
  84. next(redirectData);
  85. return;
  86. }
  87. await userStore.getInfo();
  88. // const redirectPath = (from.query.redirect || to.path) as string;
  89. // const redirect = decodeURIComponent(redirectPath);
  90. // const nextData =
  91. // to.path === redirect ? { ...to, replace: true } : { path: redirect };
  92. // next(nextData);
  93. next();
  94. // window.$loadingBar && window.$loadingBar.finish();
  95. });
  96. router.afterEach((to, _, failure) => {
  97. if (isNavigationFailure(failure)) {
  98. console.log('failed navigation', failure);
  99. }
  100. // const asyncRouteStore = useAsyncRouteStoreWidthOut();
  101. // // 在这里设置需要缓存的组件名称
  102. // const keepAliveComponents = asyncRouteStore.keepAliveComponents;
  103. // const currentComName: any = to.matched.find(
  104. // item => item.name == to.name
  105. // )?.name;
  106. // if (
  107. // currentComName &&
  108. // !keepAliveComponents.includes(currentComName) &&
  109. // to.meta?.keepAlive
  110. // ) {
  111. // // 需要缓存的组件
  112. // keepAliveComponents.push(currentComName);
  113. // } else if (!to.meta?.keepAlive || to.name == 'Redirect') {
  114. // // 不需要缓存的组件
  115. // const index = asyncRouteStore.keepAliveComponents.findIndex(
  116. // name => name == currentComName
  117. // );
  118. // if (index != -1) {
  119. // keepAliveComponents.splice(index, 1);
  120. // }
  121. // }
  122. // asyncRouteStore.setKeepAliveComponents(keepAliveComponents);
  123. window.$loadingBar && window.$loadingBar.finish();
  124. });
  125. // router.onError(error => {});
  126. }