permission.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // import Vue from 'vue'
  2. import router from "./router";
  3. import store from "./store";
  4. import { Message } from "element-ui";
  5. import NProgress from "nprogress"; // progress bar
  6. import "nprogress/nprogress.css"; // progress bar style
  7. import { getToken } from "@/utils/auth"; // get token from cookie
  8. import getPageTitle from "@/utils/get-page-title";
  9. NProgress.configure({ showSpinner: false }); // NProgress Configuration
  10. const whiteList = ["/login", "/noPermission"]; // no redirect whitelist
  11. let isOpen = false;
  12. router.onError(error => {
  13. if (error instanceof Error) {
  14. const isChunkLoadFailed = error.name.indexOf("chunk");
  15. const targetPath = router.history.pending.fullPath;
  16. if (isChunkLoadFailed && !isOpen) {
  17. isOpen = true;
  18. // (router.app.$confirm)
  19. // router.push({ path: "/403", query: { path: targetPath } })
  20. router.app
  21. .$confirm("网站有更新请点击确定刷新页面?", "更新提示", {
  22. confirmButtonText: "确定",
  23. cancelButtonText: "取消",
  24. type: "warning"
  25. })
  26. .then(() => {
  27. // router.replace(targetPath);
  28. location.hash = targetPath;
  29. window.location.reload();
  30. })
  31. .catch(() => {
  32. return;
  33. });
  34. }
  35. }
  36. });
  37. function getFirstMenu(routes) {
  38. // console.log(routes)
  39. // let firstMenu = null
  40. // routes.forEach(item => {
  41. // if(!firstMenu && item.children?.length > 0) {
  42. // firstMenu = pathErgodic(item)
  43. // }
  44. // })
  45. // console.log('firstMenu',firstMenu)
  46. return routes[0].redirect;
  47. }
  48. function pathErgodic(item) {
  49. let firstMenu = null;
  50. item.children.forEach(i => {
  51. if (!firstMenu && i.children?.length > 0) {
  52. firstMenu = pathErgodic(i);
  53. } else {
  54. if (!firstMenu && checkPathUrl(i.path)) {
  55. firstMenu = i.path;
  56. } else {
  57. if (!firstMenu && !i.hidden) {
  58. firstMenu = item.path + "/" + i.path;
  59. }
  60. }
  61. }
  62. });
  63. return firstMenu;
  64. }
  65. // 判断path有没有带/,并且是第一个位置
  66. function checkPathUrl(path) {
  67. return path.indexOf("/") === 0 ? true : false;
  68. }
  69. router.beforeEach(async (to, from, next) => {
  70. // from.query = to.query
  71. // start progress bar
  72. NProgress.start();
  73. // set page title
  74. // document.title = getPageTitle(to.meta.title)
  75. document.title = getPageTitle();
  76. // determine whether the user has logged in
  77. const hasToken = getToken();
  78. if (hasToken) {
  79. if (to.path === "/login") {
  80. // 如果有tonken直接跳转到首页
  81. next({ path: "/" });
  82. NProgress.done();
  83. } else {
  84. const hasGetUserInfo = store.getters.phone;
  85. // 有名字 说明有用户信息 跳走
  86. if (hasGetUserInfo) {
  87. // const accessRoutes = await store.dispatch('permission/generateRoutes')
  88. // 动态添加可访问的路由
  89. // router.addRoutes(accessRoutes)
  90. next();
  91. } else {
  92. try {
  93. // 异步获取用户信息
  94. await store.dispatch("user/getInfo");
  95. // 请求接口 生成可访问路由
  96. const accessRoutes = await store.dispatch(
  97. "permission/generateRoutes"
  98. );
  99. // console.log(accessRoutes, 'accessRoutes')
  100. const isMenu = accessRoutes && accessRoutes.length > 0 ? true : false;
  101. accessRoutes.push({ path: "*", redirect: "/404", hidden: true });
  102. console.log(accessRoutes, "accessRoutes");
  103. // 动态添加可访问的路由
  104. router.addRoutes(accessRoutes);
  105. // 确保addroutes完整的hack方法
  106. localStorage.removeItem("firstMenuUrl");
  107. // 设置replace:true,这样导航就不会留下历史记录。
  108. let firstMenu = getFirstMenu(accessRoutes);
  109. localStorage.setItem("firstMenuUrl", firstMenu);
  110. // console.log(firstMenu, 'firstMenu', isMenu)
  111. // 判断是否有菜单
  112. if (isMenu) {
  113. if (to.path == "/workbench") {
  114. next({ path: firstMenu, replace: true });
  115. } else {
  116. next({ ...to, replace: true });
  117. }
  118. } else {
  119. next({ path: "/noPermission", replace: true });
  120. }
  121. } catch (error) {
  122. // remove token and go to login page to re-login
  123. await store.dispatch("user/resetToken");
  124. if (error.msg) {
  125. Message.error(error.msg);
  126. }
  127. next(`/login`);
  128. NProgress.done();
  129. }
  130. }
  131. }
  132. } else {
  133. /* has no token*/
  134. console.log(to.path, "to.path");
  135. if (whiteList.indexOf(to.path) !== -1) {
  136. // in the free login whitelist, go directly
  137. next();
  138. } else {
  139. // other pages that do not have permission to access are redirected to the login page.
  140. next(`/login`);
  141. NProgress.done();
  142. }
  143. }
  144. });
  145. router.afterEach(() => {
  146. // finish progress bar
  147. NProgress.done();
  148. });