router-guards.ts 3.6 KB

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