permission.js 5.6 KB

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