permission.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import router from "./router";
  2. import { getCookie } from "@/utils/auth"; // getToken from cookie
  3. const whiteList = ["/login", "/logout", '/check']; // 不重定向白名单
  4. import store from "@/store";
  5. // router.beforeEach((to, from, next) => {
  6. // if (getCookie("authToken")) {
  7. // if (to.path == "/login") {
  8. // next({ path: "/" });
  9. // } else {
  10. // if (store.getters.roles.length === 0) {
  11. // // 判断当前用户是否已拉取完user_info信息
  12. // store
  13. // .dispatch("user/GetInfo")
  14. // .then(async () => {
  15. // let permissions = store.getters.permissions;
  16. // let accessRoutes = [];
  17. // accessRoutes = await store.dispatch(
  18. // "routes/setRoutes",
  19. // permissions
  20. // );
  21. // accessRoutes?.forEach((item) => {
  22. // router.addRoute(item);
  23. // });
  24. // next({ ...to, replace: true });
  25. // })
  26. // .catch((err) => {});
  27. // } else {
  28. // next();
  29. // }
  30. // }
  31. // } else if (whiteList.indexOf(to.path) !== -1) {
  32. // next();
  33. // } else {
  34. // next(`/login?redirect=${to.fullPath}`);
  35. // }
  36. // });
  37. router.beforeEach((to, from, next) => {
  38. document.title = `${to.meta.title} | 综合报警系统`;
  39. const token = getCookie("accessToken");
  40. // const role = sessionStorage.getItem("ms_chinesename");
  41. if (token) {
  42. if (to.path == "/login") {
  43. next("/");
  44. } else {
  45. if (store.getters.roles.length == 0) {
  46. store.dispatch("user/GetInfo").then(async () => {
  47. next();
  48. });
  49. } else {
  50. next();
  51. }
  52. }
  53. } else if (whiteList.indexOf(to.path) !== -1) {
  54. next();
  55. } else {
  56. next(`/login?redirect=${to.fullPath}`);
  57. }
  58. });
  59. router.afterEach((to, from, next) => {});