permission.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import router from "./router";
  2. import store from "./store";
  3. import NProgress from "nprogress";
  4. import "nprogress/nprogress.css";
  5. import { setTenantId, setToken } from "@/utils/auth";
  6. import { Message, MessageBox } from "element-ui";
  7. import { getPath } from "@/utils/ruoyi";
  8. import { getItem, removeItem } from "@/utils/cookie";
  9. import { isRelogin } from "@/utils/request";
  10. NProgress.configure({ showSpinner: false });
  11. // 增加三方登陆 update by 芋艿
  12. const whiteList = [
  13. "/login",
  14. "/social-login",
  15. "/auth-redirect",
  16. "/bind",
  17. "/register",
  18. "/oauthLogin/gitee",
  19. ];
  20. function routerAction(to, from, next) {
  21. // 解决多级路由无法缓存
  22. if (to.matched && to.matched.length > 2) {
  23. to.matched.splice(1, to.matched.length - 2);
  24. }
  25. to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
  26. /* has token*/
  27. if (to.path === "/login") {
  28. next({ path: "/" });
  29. NProgress.done();
  30. } else {
  31. if (store.getters.roles.length === 0) {
  32. isRelogin.show = true;
  33. // 获取字典数据 add by 芋艿
  34. store.dispatch("dict/loadDictDatas");
  35. // 判断当前用户是否已拉取完user_info信息
  36. store
  37. .dispatch("GetInfo")
  38. .then(() => {
  39. isRelogin.show = false;
  40. store.dispatch("GenerateRoutes").then((accessRoutes) => {
  41. // 根据roles权限生成可访问的路由表
  42. router.addRoutes(accessRoutes); // 动态添加可访问路由表
  43. next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
  44. });
  45. })
  46. .catch((err) => {
  47. store.dispatch("LogOut").then(() => {
  48. Message.error(err);
  49. next({ path: "/" });
  50. });
  51. });
  52. } else {
  53. if (!!to.meta.queryManageId) {
  54. const { queryManageId, ...metaWithoutQueryId } = to.meta;
  55. // 构建查询参数,包含id和name
  56. const queryParams = {
  57. id: queryManageId,
  58. name: to.meta.title, // 使用原路由的标题作为name参数
  59. };
  60. const targetPath = `/querymanage/queryFormNew`;
  61. // 防止死循环:如果当前路径已经是目标路径,就不再跳转
  62. if (to.path === targetPath) {
  63. next();
  64. return;
  65. }
  66. // 在重定向前结束当前的NProgress
  67. NProgress.done();
  68. next({
  69. path: targetPath,
  70. query: queryParams,
  71. meta: metaWithoutQueryId,
  72. replace: true, // 使用 replace 避免历史栈堆积
  73. });
  74. return; // 必须 return,防止继续执行后面的 next()
  75. } else {
  76. next();
  77. }
  78. }
  79. }
  80. }
  81. router.beforeEach((to, from, next) => {
  82. NProgress.start();
  83. if (getItem("accessToken") && getItem("refreshToken")) {
  84. setToken({
  85. accessToken: getItem("accessToken"),
  86. refreshToken: getItem("refreshToken"),
  87. });
  88. setTenantId(getItem("tenantId"));
  89. routerAction(to, from, next);
  90. } else {
  91. // 没有token
  92. if (whiteList.indexOf(to.path) !== -1) {
  93. // 在免登录白名单,直接进入
  94. next();
  95. } else {
  96. if (
  97. window.location.hostname === "113.105.183.190" &&
  98. process.env.VUE_APP_TITLE === "麦禾田WMS管理系统"
  99. ) {
  100. window.location.href =
  101. "http://113.105.183.190:8263/login?redirect=%2Findex";
  102. }
  103. if (
  104. window.location.hostname === "113.105.183.190" &&
  105. process.env.VUE_APP_TITLE === "东苏发WMS管理系统"
  106. ) {
  107. window.location.href =
  108. "http://113.105.183.190:8263/login?redirect=%2Findex";
  109. }
  110. if (
  111. window.location.hostname === "113.105.183.190" &&
  112. process.env.VUE_APP_TITLE === "东旭达WMS管理系统"
  113. ) {
  114. window.location.href =
  115. "http://113.105.183.190:8267/login?redirect=%2Findex";
  116. }
  117. switch (window.location.hostname) {
  118. case "192.168.1.163":
  119. window.location.href =
  120. "http://192.168.1.163:8282/login?redirect=%2Findex";
  121. break;
  122. // case '218.2.10.30':
  123. default:
  124. // location.href = "http://localhost:81/login?redirect=%2Findex";
  125. MessageBox.confirm(
  126. "登录状态已过期,您可以继续留在该页面,或者重新登录",
  127. "系统提示",
  128. {
  129. confirmButtonText: "重新登录",
  130. cancelButtonText: "取消",
  131. type: "warning",
  132. }
  133. ).then(() => {
  134. // 清除localStorage存储
  135. localStorage.clear();
  136. // 清除cookie,防止接口卡死
  137. removeItem("tenantId");
  138. removeItem("accessToken");
  139. removeItem("refreshToken");
  140. next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
  141. // store.dispatch('LogOut').then(() => {
  142. // // 源代码
  143. // location.href = getPath('/login');
  144. // })
  145. });
  146. break;
  147. }
  148. // next(`/login?redirect=${encodeURIComponent(to.fullPath)}`); // 否则全部重定向到登录页 encodeURIComponent对fullpath做转码处理,解决缺少参数
  149. // NProgress.done();
  150. }
  151. }
  152. });
  153. router.afterEach(() => {
  154. NProgress.done();
  155. });