router-guards.ts 3.5 KB

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