permissionGuard.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 } from '/@/router/routes/basic';
  6. import { RootRoute } from '/@/router/routes';
  7. import {isOAuth2AppEnv, isOAuth2DingAppEnv} from '/@/views/sys/login/useLogin';
  8. import { OAUTH2_THIRD_LOGIN_TENANT_ID } from "/@/enums/cacheEnum";
  9. import { setAuthCache } from "/@/utils/auth";
  10. import { PAGE_NOT_FOUND_NAME_404 } from '/@/router/constant';
  11. const LOGIN_PATH = PageEnum.BASE_LOGIN;
  12. //auth2登录路由
  13. const OAUTH2_LOGIN_PAGE_PATH = PageEnum.OAUTH2_LOGIN_PAGE_PATH;
  14. //分享免登录路由
  15. const SYS_FILES_PATH = PageEnum.SYS_FILES_PATH;
  16. // 邮件中的跳转地址,对应此路由,携带token免登录直接去办理页面
  17. const TOKEN_LOGIN = PageEnum.TOKEN_LOGIN;
  18. const ROOT_PATH = RootRoute.path;
  19. // 代码逻辑说明: [VUEN-2472]分享免登录------------
  20. const whitePathList: PageEnum[] = [LOGIN_PATH, OAUTH2_LOGIN_PAGE_PATH,SYS_FILES_PATH, TOKEN_LOGIN ];
  21. export function createPermissionGuard(router: Router) {
  22. const userStore = useUserStoreWithOut();
  23. const permissionStore = usePermissionStoreWithOut();
  24. // 自定义首页跳转次数
  25. let homePathJumpCount = 0;
  26. router.beforeEach(async (to, from, next) => {
  27. if (
  28. // 【#6861】跳转到自定义首页的逻辑,只跳转一次即可
  29. homePathJumpCount < 1 &&
  30. from.path === ROOT_PATH &&
  31. to.path === PageEnum.BASE_HOME &&
  32. userStore.getUserInfo.homePath &&
  33. userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
  34. ) {
  35. homePathJumpCount++;
  36. next(userStore.getUserInfo.homePath);
  37. return;
  38. }
  39. const token = userStore.getToken;
  40. // Whitelist can be directly entered
  41. if (whitePathList.includes(to.path as PageEnum)) {
  42. if (to.path === LOGIN_PATH && token) {
  43. const isSessionTimeout = userStore.getSessionTimeout;
  44. //TODO vben默认写法,暂时不知目的,有问题暂时先注释掉
  45. //await userStore.afterLoginAction();
  46. try {
  47. if (!isSessionTimeout) {
  48. next((to.query?.redirect as string) || '/');
  49. return;
  50. }
  51. } catch {}
  52. // 代码逻辑说明: [issues/I5BG1I]vue3不支持auth2登录------------
  53. } else if (to.path === LOGIN_PATH && isOAuth2AppEnv() && !token) {
  54. //退出登录进入此逻辑
  55. //如果进入的页面是login页面并且当前是OAuth2app环境,并且token为空,就进入OAuth2登录页面
  56. // 代码逻辑说明: [QQYUN-3440]新建企业微信和钉钉配置表,通过租户模式隔离------------
  57. if(to.query.tenantId){
  58. setAuthCache(OAUTH2_THIRD_LOGIN_TENANT_ID,to.query.tenantId)
  59. }
  60. next({ path: OAUTH2_LOGIN_PAGE_PATH });
  61. return;
  62. }
  63. next();
  64. return;
  65. }
  66. // token does not exist
  67. if (!token) {
  68. // You can access without permission. You need to set the routing meta.ignoreAuth to true
  69. if (to.meta.ignoreAuth) {
  70. next();
  71. return;
  72. }
  73. // 代码逻辑说明: [issues/I5BG1I]vue3 Auth2未实现------------
  74. let path = LOGIN_PATH;
  75. if (whitePathList.includes(to.path as PageEnum)) {
  76. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  77. if (to.path === LOGIN_PATH && isOAuth2AppEnv()) {
  78. next({ path: OAUTH2_LOGIN_PAGE_PATH });
  79. } else {
  80. //在免登录白名单,直接进入
  81. next();
  82. }
  83. } else {
  84. //----------【首次登陆并且是企业微信或者钉钉的情况下才会调用】-----------------------------------------------
  85. //只有首次登陆并且是企业微信或者钉钉的情况下才会调用
  86. const href = window.location.href;
  87. //判断当前是auth2页面,并且是钉钉/企业微信,并且包含tenantId参数
  88. if(isOAuth2AppEnv() && href.indexOf("/tenantId/")!= -1){
  89. const params = to.params;
  90. if(params && params.path && params.path.length>0){
  91. //直接获取参数最后一位
  92. setAuthCache(OAUTH2_THIRD_LOGIN_TENANT_ID,params.path[params.path.length-1])
  93. }
  94. }
  95. //---------【首次登陆并且是企业微信或者钉钉的情况下才会调用】------------------------------------------------
  96. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面,否则跳转到登录页面
  97. path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : LOGIN_PATH;
  98. }
  99. // redirect login page
  100. const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
  101. // 代码逻辑说明: [issues/I5BG1I]vue3 Auth2未实现------------
  102. path: path,
  103. replace: true,
  104. };
  105. // 代码逻辑说明: 【QQYUN-4713】登录代码调整逻辑有问题,改造待观察--
  106. if (to.fullPath) {
  107. console.log("to.fullPath 1",to.fullPath)
  108. console.log("to.path 2",to.path)
  109. const getFullPath = to.fullPath;
  110. if(getFullPath=='/' || getFullPath=='/500' || getFullPath=='/400' || getFullPath=='/login?redirect=/' || getFullPath=='/login?redirect=/login?redirect=/'){
  111. return;
  112. }
  113. redirectData.query = {
  114. ...redirectData.query,
  115. // 代码逻辑说明: 修复登录成功后,没有正确重定向的问题
  116. redirect: to.fullPath,
  117. };
  118. }
  119. next(redirectData);
  120. return;
  121. }
  122. //==============================【首次登录并且是企业微信或者钉钉的情况下才会调用】==================
  123. //判断是免登录页面,如果页面包含/tenantId/,那么就直接前往主页
  124. if(isOAuth2AppEnv() && to.path.indexOf("/tenantId/") != -1){
  125. // 代码逻辑说明: 【TV360X-2958】钉钉登录后打开了敲敲云,换其他账号登录后,再打开敲敲云显示的是原来账号的应用---
  126. if (isOAuth2DingAppEnv()) {
  127. next(OAUTH2_LOGIN_PAGE_PATH);
  128. } else {
  129. next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  130. }
  131. return;
  132. }
  133. //==============================【首次登录并且是企业微信或者钉钉的情况下才会调用】==================
  134. // Jump to the 404 page after processing the login
  135. if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_NAME_404 && to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)) {
  136. next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  137. return;
  138. }
  139. // // get userinfo while last fetch time is empty
  140. // if (userStore.getLastUpdateTime === 0) {
  141. // try {
  142. // console.log("--LastUpdateTime---getUserInfoAction-----")
  143. // await userStore.getUserInfoAction();
  144. // } catch (err) {
  145. // console.info(err);
  146. // next();
  147. // }
  148. // }
  149. // 代码逻辑说明: 【QQYUN-8572】表格行选择卡顿问题(customRender中字典引起的)
  150. if (userStore.getLastUpdateTime === 0) {
  151. userStore.setAllDictItemsByLocal();
  152. }
  153. if (permissionStore.getIsDynamicAddedRoute) {
  154. next();
  155. return;
  156. }
  157. // 构建后台菜单路由
  158. const routes = await permissionStore.buildRoutesAction();
  159. routes.forEach((route) => {
  160. router.addRoute(route as unknown as RouteRecordRaw);
  161. });
  162. router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
  163. permissionStore.setDynamicAddedRoute(true);
  164. // 代码逻辑说明: 【issues/7500】vue-router4.5.0版本路由name:PageNotFound同名导致登录进不去
  165. if (to.name === PAGE_NOT_FOUND_NAME_404) {
  166. // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
  167. next({ path: to.fullPath, replace: true, query: to.query });
  168. } else {
  169. const redirectPath = (from.query.redirect || to.path) as string;
  170. const redirect = decodeURIComponent(redirectPath);
  171. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
  172. next(nextData);
  173. }
  174. });
  175. }