permission.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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({
  31. path: accessRoutes.length ? accessRoutes[0].path : to.path,
  32. replace: true,
  33. });
  34. // next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
  35. });
  36. })
  37. .catch((err) => {
  38. store.dispatch("LogOut").then(() => {
  39. Message.error(err);
  40. next({ path: "/" });
  41. });
  42. });
  43. } else {
  44. next();
  45. }
  46. //获取消息数量
  47. // store.dispatch("SET_WAITTASKNUM")
  48. }
  49. } else {
  50. // 没有token
  51. if (whiteList.indexOf(to.path) !== -1) {
  52. // 在免登录白名单,直接进入
  53. next();
  54. } else {
  55. let url = window.location.href;
  56. // if (url.endsWith("admin")) {
  57. // next(`/adminLogin`);
  58. // return;
  59. // }
  60. next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
  61. NProgress.done();
  62. // let urls = url.split("?");
  63. // if (urls.length > 1) {
  64. // if (urls[1].startsWith("ticket")) {
  65. // let params = urls[1].split("=");
  66. // let args = params[1];
  67. // if (args == "out") {
  68. // //生产 10.223.1.127:8098
  69. // console.log("重定向ISC--------3");
  70. // window.location.href =
  71. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  72. // } else {
  73. // login2(args).then((res) => {
  74. // if (res == 503) {
  75. // this.$message.warning("当前账号未开通权限!");
  76. // } else {
  77. // setToken(res.token);
  78. // store.commit("SET_TOKEN", res.token);
  79. // next(`/`)
  80. // }
  81. // });
  82. // }
  83. // } else {
  84. // console.log("重定向ISC--------1");
  85. // //生产 10.223.1.127:8098
  86. // window.location.href =
  87. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  88. // }
  89. // } else {
  90. // if (!url.endsWith("admin/")) {
  91. // console.log("重定向ISC--------2");
  92. // //生产 10.223.1.127:8098
  93. // window.location.href =
  94. // "http://10.223.1.127:8098/isc_sso/login?service=http://25.212.177.102:19888";
  95. // }
  96. // }
  97. // next(`/login`); // 否则全部重定向到登录页
  98. }
  99. }
  100. });
  101. router.afterEach(() => {
  102. NProgress.done();
  103. });