permission.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-01-13 17:32:55
  4. * @LastEditTime: 2021-12-02 17:02:24
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \vue3-element-admin\src\plugins\permission.js
  8. */
  9. import { SET_MENU_LIST } from "@/store/modules/app/type";
  10. import globalRoutes from "@/router/globalRoutes";
  11. import mainRoutes from "@/router/mainRoutes";
  12. import NProgress from "nprogress";
  13. /**
  14. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  15. * @param {*} route 当前路由
  16. */
  17. function fnCurrentRouteType(route, globalRoutes = []) {
  18. let temp = [];
  19. for (let i = 0; i < globalRoutes.length; i++) {
  20. if (route.name === globalRoutes[i].name) {
  21. return "global";
  22. } else if (
  23. globalRoutes[i].children &&
  24. globalRoutes[i].children.length >= 1
  25. ) {
  26. temp = temp.concat(globalRoutes[i].children);
  27. }
  28. }
  29. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : "main";
  30. }
  31. export default {
  32. install: (app, { router, store }) => {
  33. // let router = opt;
  34. router.beforeEach(async (to, from, next) => {
  35. if (
  36. fnCurrentRouteType(to, globalRoutes) === "global"
  37. ) {
  38. //* 1. 已经添加 or 全局路由, 直接访问
  39. if (to.meta.title) {
  40. document.title = to.meta.title;
  41. }
  42. NProgress.start();
  43. next();
  44. } else {
  45. let token = sessionStorage.getItem("token");
  46. if (!token || !/\S/.test(token)) {
  47. next({ name: "Login" });
  48. } else {
  49. NProgress.start();
  50. next();
  51. }
  52. }
  53. });
  54. router.afterEach(() => {
  55. NProgress.done();
  56. });
  57. /**
  58. * 添加动态(菜单)路由
  59. * @param {*} menuList 菜单列表
  60. * @param {*} routes 递归创建的动态(菜单)路由
  61. */
  62. // eslint-disable-next-line no-unused-vars
  63. // const fnAddDynamicMenuRoutes = async (menuList = [], routes = []) => {
  64. // let temp = [];
  65. // for (let i = 0; i < menuList.length; i++) {
  66. // if (
  67. // menuList[i].type == 0 &&
  68. // menuList[i].children &&
  69. // menuList[i].children.length >= 1
  70. // ) {
  71. // temp = temp.concat(menuList[i].children);
  72. // } else if (menuList[i].type == 1) {
  73. // // } else if (menuList[i].type==1 && /\S/.test(menuList[i].url)) {
  74. // // const url = menuList[i].url.replace(/\//g, "_");
  75. // let route = {
  76. // path:
  77. // menuList[i].url.replace(/\//g, "-") +
  78. // `-${menuList[i].id}`,
  79. // component: null,
  80. // name: menuList[i].url
  81. // // menuList[i].url.replace(/\//g, "-") +
  82. // // `-${menuList[i].id}`,
  83. // // meta: {
  84. // // }
  85. // };
  86. // // url以http[s]://开头, 通过iframe展示
  87. // if (menuList[i].iframe == 1) {
  88. // route["path"] = `i-${menuList[i].id}`;
  89. // route["name"] = `i-${menuList[i].id}`;
  90. // route["props"] = { url: menuList[i].url };
  91. // route["component"] = () => import("@/views/IFrame.vue");
  92. // } else {
  93. // const l = "views/layoutpages/" + menuList[i].url;
  94. // let component = () => import("@/" + l + ".vue");
  95. // route["component"] = component;
  96. // }
  97. // routes.push(route);
  98. // }
  99. // }
  100. // if (temp.length >= 1) {
  101. // fnAddDynamicMenuRoutes(temp, routes);
  102. // } else {
  103. // let homeRoute = mainRoutes[0].children[0];
  104. //
  105. // homeRoute.children = homeRoute.children.concat(routes);
  106. // // mainRoutes.children = routes;
  107. // console.log(
  108. // "控制台打印--> ~ file: permission.js ~ line 127 ~ fnAddDynamicMenuRoutes ~ mainRoutes.children",
  109. // homeRoute.children
  110. // );
  111. // mainRoutes[0].children[0] = homeRoute;
  112. // await router.addRoute(mainRoutes);
  113. // await router.addRoute({
  114. // path: "/:w+",
  115. // redirect: { name: "404" },
  116. // });
  117. // }
  118. // };
  119. },
  120. };