router-guards.ts 4.5 KB

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