permission.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import router from "./router";
  2. import store from "./store";
  3. import { Message } from "element-ui";
  4. import NProgress from "nprogress";
  5. import "nprogress/nprogress.css";
  6. import { getToken, setToken } from "@/utils/auth";
  7. import { isRelogin } from "@/utils/request";
  8. import { login2 } from "@/api/login";
  9. NProgress.configure({ showSpinner: false });
  10. const whiteList = ["/login", "/register", "/adminLogin"];
  11. router.beforeEach((to, from, next) => {
  12. NProgress.start();
  13. if (getToken()) {
  14. to.meta.title && store.dispatch("settings/setTitle", to.meta.title);
  15. /* has token*/
  16. if (to.path === "/login") {
  17. next({ path: "/" });
  18. NProgress.done();
  19. } else {
  20. if (store.getters.roles.length === 0) {
  21. isRelogin.show = true;
  22. // 判断当前用户是否已拉取完user_info信息
  23. store
  24. .dispatch("GetInfo")
  25. .then(() => {
  26. isRelogin.show = false;
  27. store.dispatch("GenerateRoutes").then((accessRoutes) => {
  28. // 根据roles权限生成可访问的路由表
  29. router.addRoutes(accessRoutes); // 动态添加可访问路由表
  30. next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
  31. });
  32. })
  33. .catch((err) => {
  34. store.dispatch("LogOut").then(() => {
  35. Message.error(err);
  36. next({ path: "/" });
  37. });
  38. });
  39. } else {
  40. next();
  41. }
  42. //获取消息数量
  43. // store.dispatch("SET_WAITTASKNUM")
  44. }
  45. } else {
  46. // 没有token
  47. if (whiteList.indexOf(to.path) !== -1) {
  48. // 在免登录白名单,直接进入
  49. next();
  50. } else {
  51. let url = window.location.href;
  52. // if (url.endsWith("admin")) {
  53. // next(`/adminLogin`);
  54. // return;
  55. // }
  56. next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
  57. NProgress.done();
  58. // let urls = url.split("?");
  59. // if (urls.length > 1) {
  60. // if (urls[1].startsWith("ticket")) {
  61. // let params = urls[1].split("=");
  62. // let args = params[1];
  63. // if (args == "out") {
  64. // //生产 10.223.1.127:8098
  65. // console.log("重定向ISC--------3");
  66. // window.location.href =
  67. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  68. // } else {
  69. // login2(args).then((res) => {
  70. // if (res == 503) {
  71. // this.$message.warning("当前账号未开通权限!");
  72. // } else {
  73. // setToken(res.token);
  74. // store.commit("SET_TOKEN", res.token);
  75. // next(`/`)
  76. // }
  77. // });
  78. // }
  79. // } else {
  80. // console.log("重定向ISC--------1");
  81. // //生产 10.223.1.127:8098
  82. // window.location.href =
  83. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  84. // }
  85. // } else {
  86. // if (!url.endsWith("admin/")) {
  87. // console.log("重定向ISC--------2");
  88. // //生产 10.223.1.127:8098
  89. // window.location.href =
  90. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  91. // }
  92. // }
  93. // next(`/login`); // 否则全部重定向到登录页
  94. }
  95. }
  96. });
  97. router.afterEach(() => {
  98. NProgress.done();
  99. });