router-guards.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 } 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. // console.log(to, '修改标题');
  28. // console.log('access token');
  29. if (to.path === '/attend-class') {
  30. let title = to.meta.title;
  31. if (to.query.type === 'preview') {
  32. title = '预览课件';
  33. }
  34. document.title = title ? title : ('音乐数字课堂' as string);
  35. } else {
  36. document.title = to.meta.title
  37. ? to.meta.title
  38. : ('音乐数字课堂' as string);
  39. }
  40. if ('serviceWorker' in navigator) {
  41. console.log(caches.keys(), 'caches.keys()');
  42. caches.keys().then(function (cacheNames) {
  43. cacheNames.forEach(function (cacheName) {
  44. caches.delete(cacheName);
  45. });
  46. });
  47. }
  48. if (!isChrome()) {
  49. return;
  50. }
  51. window.$loadingBar && window.$loadingBar.start();
  52. // console.log(window.$loadingBar, '232332');
  53. if (from.path === LOGIN_PATH && to.name === 'errorPage') {
  54. next(PageEnum.BASE_HOME);
  55. return;
  56. }
  57. // Whitelist can be directly entered
  58. if (whitePathList.includes(to.path as PageEnum)) {
  59. next();
  60. return;
  61. }
  62. const token = storage.get(ACCESS_TOKEN);
  63. // console.log(token, 'access token');
  64. if (!token) {
  65. // You can access without permissions. You need to set the routing meta.ignoreAuth to true
  66. if (to.meta.ignoreAuth) {
  67. next();
  68. return;
  69. }
  70. // redirect login page
  71. const redirectData: { path: string; replace: boolean; query?: any } = {
  72. path: LOGIN_PATH,
  73. replace: true
  74. };
  75. if (to.path) {
  76. redirectData.query = {
  77. ...redirectData.query,
  78. redirect: to.path
  79. };
  80. }
  81. console.log(redirectData, to);
  82. next(redirectData);
  83. return;
  84. }
  85. await userStore.getInfo();
  86. // const redirectPath = (from.query.redirect || to.path) as string;
  87. // const redirect = decodeURIComponent(redirectPath);
  88. // const nextData =
  89. // to.path === redirect ? { ...to, replace: true } : { path: redirect };
  90. // next(nextData);
  91. next();
  92. // window.$loadingBar && window.$loadingBar.finish();
  93. });
  94. router.afterEach((to, _, failure) => {
  95. if (isNavigationFailure(failure)) {
  96. console.log('failed navigation', failure);
  97. }
  98. // const asyncRouteStore = useAsyncRouteStoreWidthOut();
  99. // // 在这里设置需要缓存的组件名称
  100. // const keepAliveComponents = asyncRouteStore.keepAliveComponents;
  101. // const currentComName: any = to.matched.find(
  102. // item => item.name == to.name
  103. // )?.name;
  104. // if (
  105. // currentComName &&
  106. // !keepAliveComponents.includes(currentComName) &&
  107. // to.meta?.keepAlive
  108. // ) {
  109. // // 需要缓存的组件
  110. // keepAliveComponents.push(currentComName);
  111. // } else if (!to.meta?.keepAlive || to.name == 'Redirect') {
  112. // // 不需要缓存的组件
  113. // const index = asyncRouteStore.keepAliveComponents.findIndex(
  114. // name => name == currentComName
  115. // );
  116. // if (index != -1) {
  117. // keepAliveComponents.splice(index, 1);
  118. // }
  119. // }
  120. // asyncRouteStore.setKeepAliveComponents(keepAliveComponents);
  121. window.$loadingBar && window.$loadingBar.finish();
  122. });
  123. // router.onError(error => {});
  124. }