permission.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 } from "@/utils/auth";
  7. import { isRelogin } from "@/utils/request";
  8. import { getBaseUrl } from "@/utils/baseUrl.js";
  9. NProgress.configure({ showSpinner: false });
  10. const whiteList = ["/login", "/register", "/postcheck"];
  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: "/login" });
  18. NProgress.done();
  19. } else if (whiteList.indexOf(to.path) !== -1) {
  20. next();
  21. } else {
  22. if (store.getters.roles.length === 0) {
  23. isRelogin.show = true;
  24. // 判断当前用户是否已拉取完user_info信息
  25. store
  26. .dispatch("GetInfo")
  27. .then(() => {
  28. isRelogin.show = false;
  29. store.dispatch("GenerateRoutes").then((accessRoutes) => {
  30. // 根据roles权限生成可访问的路由表
  31. router.addRoutes(accessRoutes); // 动态添加可访问路由表
  32. next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
  33. });
  34. })
  35. .catch((err) => {
  36. store.dispatch("LogOut").then(() => {
  37. Message.error(err);
  38. const type = window.localStorage.getItem("isPathType");
  39. if (type == 2) {
  40. next({ path: "/login" });
  41. } else {
  42. location.href = `http://10.152.70.21:8080/cas/logout?service=${getBaseUrl()}/redirectpostcheck`;
  43. }
  44. });
  45. });
  46. } else {
  47. next();
  48. }
  49. }
  50. } else {
  51. if (whiteList.indexOf(to.path) === -1) {
  52. // 存没有登录地址
  53. window.localStorage.setItem("redirect", to.path);
  54. }
  55. // 没有token
  56. if (whiteList.indexOf(to.path) !== -1) {
  57. // 在免登录白名单,直接进入
  58. next();
  59. } else {
  60. // 否则全部重定向到登录页
  61. if (to.path === "/login") {
  62. // 普通登录
  63. next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
  64. NProgress.done();
  65. return;
  66. }
  67. // cas认证登录
  68. next("/postcheck");
  69. NProgress.done();
  70. }
  71. }
  72. });
  73. router.afterEach(() => {
  74. NProgress.done();
  75. });