permission.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import router from "./router";
  2. import store from "./store";
  3. import { Message } from "element-ui";
  4. import NProgress from "nprogress"; // progress bar
  5. import "nprogress/nprogress.css"; // progress bar style
  6. import { getToken, getMenuTree } from "@/utils/auth"; // get token from cookie
  7. import getPageTitle from "@/utils/get-page-title";
  8. import { getMenuTreeAsync } from "@/router/index";
  9. import { currentUserName } from "./utils/trace";
  10. NProgress.configure({
  11. showSpinner: false,
  12. }); // NProgress Configuration
  13. const whiteList = ["/login"]; // no redirect whitelist
  14. router.beforeEach(async (to, from, next) => {
  15. // start progress bar
  16. NProgress.start();
  17. console.log(to);
  18. // set page title
  19. document.title = getPageTitle(to.meta.title);
  20. // determine whether the user has logged in
  21. const hasToken = getToken();
  22. const hasMenu = getMenuTree();
  23. const asyncRouter = getMenuTreeAsync();
  24. let redirectRouter = "/";
  25. if (
  26. asyncRouter &&
  27. asyncRouter.length > 0 &&
  28. asyncRouter[0].children &&
  29. asyncRouter[0].children.length > 0
  30. ) {
  31. if (asyncRouter[0].path !== "/") {
  32. redirectRouter =
  33. asyncRouter[0].path + "/" + asyncRouter[0].children[0].path;
  34. } else {
  35. redirectRouter = asyncRouter[0].children[0].path;
  36. }
  37. }
  38. //auth
  39. if (to.path.indexOf("role-user-list") > -1) {
  40. sessionStorage.setItem("RoleUserListKey", to.name);
  41. }
  42. //pmc
  43. if (to.path.indexOf("pmc") > -1) {
  44. sessionStorage.setItem("RoleUserListKey", to.name);
  45. }
  46. // console.log("redirectRouter", redirectRouter)
  47. if (hasToken && hasMenu) {
  48. if (to.path === "/login") {
  49. // if is logged in, redirect to the home page
  50. next({
  51. path: redirectRouter,
  52. });
  53. NProgress.done();
  54. } else if (
  55. (from.path === "/equip-management/add-box" ||
  56. from.path === "/equip-management/edit-box" ||
  57. from.path === "/equip-management/add-box-template" ||
  58. from.path === "/equip-management/edit-box-template") &&
  59. sessionStorage.normalProcess !== "skip"
  60. ) {
  61. const answer = window.confirm("是否确定跳转?");
  62. if (answer) {
  63. next();
  64. } else {
  65. next(false);
  66. }
  67. } else {
  68. // get analytics
  69. await store.dispatch("user/setNums");
  70. setInterval(() => {
  71. store.dispatch("user/setNums");
  72. }, 30000)
  73. const hasGetUserInfo = store.getters.name;
  74. if (hasGetUserInfo) {
  75. next();
  76. } else {
  77. try {
  78. // get user info
  79. await store.dispatch("user/getInfo");
  80. next();
  81. } catch (error) {
  82. // remove token and go to login page to re-login
  83. await store.dispatch("user/resetToken");
  84. Message.error(error || "Has Error");
  85. next(`/login?redirect=${to.path}`);
  86. NProgress.done();
  87. }
  88. }
  89. }
  90. } else {
  91. /* has no token*/
  92. if (whiteList.indexOf(to.path) !== -1) {
  93. // in the free login whitelist, go directly
  94. next();
  95. } else {
  96. // other pages that do not have permission to access are redirected to the login page.
  97. next(`/login?redirect=${to.path}`);
  98. NProgress.done();
  99. }
  100. }
  101. });
  102. router.afterEach(() => {
  103. // finish progress bar
  104. NProgress.done();
  105. });