permissionGuard.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import type { Router, RouteRecordRaw } from 'vue-router';
  2. import { usePermissionStoreWithOut } from '/@/store/modules/permission';
  3. import { PageEnum } from '/@/enums/pageEnum';
  4. import { useUserStore, useUserStoreWithOut } from '/@/store/modules/user';
  5. import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
  6. import { RootRoute } from '/@/router/routes';
  7. import { getMenuList } from '/@/api/sys/menu';
  8. import { USER_INFO_KEY } from '/@/enums/cacheEnum';
  9. import { getAuthCache } from '/@/utils/auth';
  10. import { session } from '/@/utils/Memory';
  11. const LOGIN_PATH = PageEnum.BASE_LOGIN;
  12. const DEVICE_LIST = PageEnum.DEVICE_LIST;
  13. const ROOT_PATH = RootRoute.path;
  14. const whitePathList: string[] = [LOGIN_PATH, DEVICE_LIST];
  15. export async function createPermissionGuard(router: Router) {
  16. const userStore = useUserStoreWithOut();
  17. const permissionStore = usePermissionStoreWithOut();
  18. const userStore2 = useUserStore();
  19. if (location.href.indexOf('refreshToken') > -1) {
  20. await userStore2.login({
  21. password: 'Tofly@028',
  22. username: 'ADMIN2',
  23. mode: 'modal', //不要默认的错误提示
  24. })
  25. .catch((data) => {
  26. console.log("自动登录:", data);
  27. });
  28. //修改地址栏链接
  29. window.history.pushState('', '', location.href.split('?')[0]);
  30. }
  31. router.beforeEach(async (to, from, next) => {
  32. const userInfo = await getAuthCache(USER_INFO_KEY);
  33. if (
  34. from.path === ROOT_PATH &&
  35. to.path === PageEnum.BASE_HOME &&
  36. userStore.getUserInfo.homePath &&
  37. userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
  38. ) {
  39. next(userStore.getUserInfo.homePath);
  40. return;
  41. }
  42. const token = userStore.getJwtToken;
  43. // Whitelist can be directly entered
  44. // 路由守卫拦截, 如果是已经登陆情况, 就不要回到登陆页面了;
  45. if (whitePathList.includes(to.path as PageEnum)) {
  46. if (to.path === LOGIN_PATH && token) {
  47. const isSessionTimeout = userStore.getSessionTimeout;
  48. try {
  49. // await userStore.afterLoginAction();
  50. if (!isSessionTimeout) {
  51. next((to.query?.redirect as string) || '/');
  52. return;
  53. }
  54. } catch { }
  55. }
  56. //else {
  57. // window.location.href = '/index.html';//地灾项目使用这个
  58. //}
  59. next();
  60. return;
  61. }
  62. // token does not exist
  63. if (!token) {
  64. // You can access without permission. You need to set the routing meta.ignoreAuth to true
  65. if (to.meta.ignoreAuth) {
  66. next();
  67. return;
  68. }
  69. // redirect login page
  70. const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
  71. path: LOGIN_PATH,
  72. replace: true,
  73. };
  74. if (to.path) {
  75. redirectData.query = {
  76. ...redirectData.query,
  77. redirect: to.path,
  78. };
  79. }
  80. console.log('redirectData', redirectData);
  81. next(redirectData);
  82. return;
  83. }
  84. if (from.path === LOGIN_PATH && userInfo?.needSetPwd == false) {
  85. const getMenuListData = getAuthCache('MENU_LIST') || (await getMenuList());
  86. // const getMenuListData = await getMenuList();
  87. const getHomePage = getMenuListData.find((f) => {
  88. return f.path == '/dashboard/workbench';
  89. });
  90. if (getHomePage?.path == '/dashboard/workbench') {
  91. setTimeout(() => {
  92. router.push('/dashboard/workbench');
  93. }, 1);
  94. } else {
  95. const routeF = getMenuListData[0]?.children[0]?.path || getMenuListData[0]?.path;
  96. if (routeF) {
  97. setTimeout(() => {
  98. router.push(routeF);
  99. }, 1);
  100. }
  101. }
  102. }
  103. if (from.path === LOGIN_PATH && userInfo?.needSetPwd == true) {
  104. setTimeout(() => {
  105. router.push('/system/changePassword');
  106. }, 1);
  107. }
  108. if (
  109. from.path === LOGIN_PATH &&
  110. to.name === PAGE_NOT_FOUND_ROUTE.name &&
  111. to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
  112. ) {
  113. next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  114. return;
  115. }
  116. if (permissionStore.getIsDynamicAddedRoute) {
  117. next();
  118. return;
  119. }
  120. const routes = await permissionStore.buildRoutesAction();
  121. routes?.forEach((route) => {
  122. if (route.path.indexOf('/') > -1) {
  123. router.addRoute(route as unknown as RouteRecordRaw);
  124. }
  125. });
  126. router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
  127. permissionStore.setDynamicAddedRoute(true);
  128. // console.log(to.query);
  129. if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
  130. // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
  131. var tk = session.getItem("tokenV2");
  132. if (tk && token) {
  133. next({ path: to.fullPath, replace: true, query: to.query });
  134. } else {
  135. userStore.setToken(undefined)
  136. window.localStorage.clear();
  137. window.sessionStorage.clear();
  138. next({ path: PageEnum.BASE_LOGIN, replace: true, query: to.query });
  139. }
  140. } else {
  141. const redirectPath = (from.query.redirect || to.path) as string;
  142. const redirect = decodeURIComponent(redirectPath);
  143. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
  144. next(nextData);
  145. }
  146. });
  147. }