permissionGuard.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type { Router, RouteRecordRaw } from 'vue-router';
  2. import { usePermissionStoreWithOut } from '/@/store/modules/permission';
  3. import { PageEnum } from '/@/enums/pageEnum';
  4. import { useUserStoreWithOut } from '/@/store/modules/user';
  5. import { PAGE_NOT_FOUND_ROUTE, QIANKUN_ROUTE } from '/@/router/routes/basic';
  6. import { RootRoute } from '/@/router/routes';
  7. import { isOAuth2AppEnv } from '/@/views/sys/login/useLogin';
  8. import _ from 'lodash';
  9. const LOGIN_PATH = PageEnum.BASE_LOGIN;
  10. //auth2登录路由
  11. const OAUTH2_LOGIN_PAGE_PATH = PageEnum.OAUTH2_LOGIN_PAGE_PATH;
  12. const ROOT_PATH = RootRoute.path;
  13. //update-begin---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
  14. const whitePathList: PageEnum[] = [LOGIN_PATH, OAUTH2_LOGIN_PAGE_PATH];
  15. //update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
  16. export function createPermissionGuard(router: Router) {
  17. const userStore = useUserStoreWithOut();
  18. const permissionStore = usePermissionStoreWithOut();
  19. router.beforeEach(async (to, from, next) => {
  20. if (_.isEmpty(history.state.current)) {
  21. _.assign(history.state, { current: from.fullPath });
  22. }
  23. if (
  24. from.path === ROOT_PATH &&
  25. to.path === PageEnum.BASE_HOME &&
  26. userStore.getUserInfo.homePath &&
  27. userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
  28. ) {
  29. next(userStore.getUserInfo.homePath);
  30. return;
  31. }
  32. const token = userStore.getToken;
  33. // Whitelist can be directly entered
  34. if (whitePathList.includes(to.path as PageEnum)) {
  35. if (to.path === LOGIN_PATH && token) {
  36. const isSessionTimeout = userStore.getSessionTimeout;
  37. try {
  38. await userStore.afterLoginAction();
  39. if (!isSessionTimeout) {
  40. next((to.query?.redirect as string) || '/');
  41. return;
  42. }
  43. } catch {}
  44. //update-begin---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
  45. } else if (to.path === LOGIN_PATH && isOAuth2AppEnv() && !token) {
  46. //退出登录进入此逻辑
  47. //如果进入的页面是login页面并且当前是OAuth2app环境,并且token为空,就进入OAuth2登录页面
  48. next({ path: OAUTH2_LOGIN_PAGE_PATH });
  49. return;
  50. //update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
  51. }
  52. next();
  53. return;
  54. }
  55. // token does not exist
  56. if (!token) {
  57. // You can access without permission. You need to set the routing meta.ignoreAuth to true
  58. if (to.meta.ignoreAuth) {
  59. next();
  60. return;
  61. }
  62. //update-begin---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3 Auth2未实现------------
  63. let path = LOGIN_PATH;
  64. if (whitePathList.includes(to.path as PageEnum)) {
  65. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  66. if (to.path === LOGIN_PATH && isOAuth2AppEnv()) {
  67. next({ path: OAUTH2_LOGIN_PAGE_PATH });
  68. } else {
  69. //在免登录白名单,直接进入
  70. next();
  71. }
  72. } else {
  73. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面,否则跳转到登录页面
  74. path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : LOGIN_PATH;
  75. }
  76. //update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3 Auth2未实现------------
  77. // redirect login page
  78. const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
  79. //update-begin---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3 Auth2未实现------------
  80. path: path,
  81. //update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3 Auth2未实现------------
  82. replace: true,
  83. };
  84. if (to.path) {
  85. redirectData.query = {
  86. ...redirectData.query,
  87. redirect: to.path,
  88. };
  89. }
  90. next(redirectData);
  91. return;
  92. }
  93. // Jump to the 404 page after processing the login
  94. if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name && to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)) {
  95. next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  96. return;
  97. }
  98. // get userinfo while last fetch time is empty
  99. if (userStore.getLastUpdateTime === 0) {
  100. try {
  101. await userStore.getUserInfoAction();
  102. } catch (err) {
  103. console.info(err);
  104. next();
  105. }
  106. }
  107. if (permissionStore.getIsDynamicAddedRoute) {
  108. next();
  109. return;
  110. }
  111. const routes = await permissionStore.buildRoutesAction();
  112. routes.forEach((route) => {
  113. router.addRoute(route as unknown as RouteRecordRaw);
  114. });
  115. router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw); //
  116. router.addRoute(QIANKUN_ROUTE as unknown as RouteRecordRaw);
  117. permissionStore.setDynamicAddedRoute(true);
  118. if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
  119. // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
  120. next({ path: to.fullPath, replace: true, query: to.query });
  121. } else {
  122. const redirectPath = (from.query.redirect || to.path) as string;
  123. const redirect = decodeURIComponent(redirectPath);
  124. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
  125. next(nextData);
  126. }
  127. });
  128. }