permissionGuard.ts 8.6 KB

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