router-guards.ts 3.6 KB

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