permission.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { constantRoutes } from "@/router";
  2. import { getRouters } from "@/api/menu";
  3. import Layout from "@/layout/index";
  4. import ParentView from "@/components/ParentView";
  5. import InnerLink from "@/layout/components/InnerLink";
  6. import store from "@/store";
  7. // 匹配views里面所有的.vue文件
  8. const modules = import.meta.glob("./../../views/**/*.vue");
  9. const permission = {
  10. state: {
  11. routes: [],
  12. addRoutes: [],
  13. defaultRoutes: [],
  14. topbarRouters: [],
  15. sidebarRouters: [],
  16. routerRules: [],
  17. },
  18. mutations: {
  19. SET_ROUTES: (state, routes) => {
  20. state.addRoutes = routes;
  21. state.routes = constantRoutes.concat(routes);
  22. },
  23. SET_DEFAULT_ROUTES: (state, routes) => {
  24. state.defaultRoutes = constantRoutes.concat(routes);
  25. },
  26. SET_TOPBAR_ROUTES: (state, routes) => {
  27. // 顶部导航菜单默认添加统计报表栏指向首页
  28. // const index = [{
  29. // path: 'index',
  30. // meta: { title: '统计报表', icon: 'dashboard' }
  31. // }]
  32. // state.topbarRouters = routes.concat(index);
  33. state.topbarRouters = routes;
  34. },
  35. SET_SIDEBAR_ROUTERS: (state, routes) => {
  36. state.sidebarRouters = routes;
  37. },
  38. SET_ROUTER_RULES: (state, rules) => {
  39. state.routerRules = rules;
  40. },
  41. },
  42. actions: {
  43. // 生成路由
  44. GenerateRoutes({ commit }) {
  45. return new Promise((resolve) => {
  46. // 向后端请求路由数据
  47. getRouters({
  48. // 后台0 前台1
  49. modulesource: 1,
  50. }).then((res) => {
  51. const sdata = reset(JSON.parse(JSON.stringify(res.data)));
  52. const rdata = reset(JSON.parse(JSON.stringify(res.data)));
  53. const defaultData = reset(JSON.parse(JSON.stringify(res.data)));
  54. const sidebarRoutes = filterAsyncRouter(sdata);
  55. const rewriteRoutes = filterAsyncRouter(rdata, false, true);
  56. const defaultRoutes = filterAsyncRouter(defaultData);
  57. commit("SET_ROUTES", rewriteRoutes);
  58. commit("SET_SIDEBAR_ROUTERS", constantRoutes.concat(sidebarRoutes));
  59. commit("SET_DEFAULT_ROUTES", sidebarRoutes);
  60. commit("SET_TOPBAR_ROUTES", defaultRoutes);
  61. commit("SET_ROUTER_RULES", sdata);
  62. resolve(rewriteRoutes);
  63. });
  64. });
  65. },
  66. },
  67. };
  68. // 遍历后台传来的路由字符串,转换为组件对象
  69. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  70. return asyncRouterMap.filter((route) => {
  71. if (type && route.children) {
  72. route.children = filterChildren(route.children);
  73. }
  74. if (route.component) {
  75. // Layout ParentView 组件特殊处理
  76. if (route.component === "Layout") {
  77. route.component = Layout;
  78. } else if (route.component === "ParentView") {
  79. route.component = ParentView;
  80. } else if (route.component === "InnerLink") {
  81. route.component = InnerLink;
  82. } else {
  83. route.component = loadView(route.component);
  84. }
  85. }
  86. if (route.children != null && route.children && route.children.length) {
  87. route.children = filterAsyncRouter(route.children, route, type);
  88. } else {
  89. delete route["children"];
  90. delete route["redirect"];
  91. }
  92. return true;
  93. });
  94. }
  95. function filterChildren(childrenMap, lastRouter = false) {
  96. var children = [];
  97. childrenMap.forEach((el, index) => {
  98. if (el.children && el.children.length) {
  99. if (el.component === "ParentView" && !lastRouter) {
  100. el.children.forEach((c) => {
  101. c.path = el.path + "/" + c.path;
  102. if (c.children && c.children.length) {
  103. children = children.concat(filterChildren(c.children, c));
  104. return;
  105. }
  106. children.push(c);
  107. });
  108. return;
  109. }
  110. }
  111. if (lastRouter) {
  112. el.path = lastRouter.path + "/" + el.path;
  113. }
  114. children = children.concat(el);
  115. });
  116. return children;
  117. }
  118. function reset(list) {
  119. let newList = [];
  120. let tk = store.getters.superToken
  121. list.map((item) => {
  122. let newObj = item;
  123. //#region 把单点登录携带的token添加到路由参数中,便于跳转路由时验证token是否过期
  124. if (tk) {
  125. if (!newObj.query) {
  126. let params = { token: tk }
  127. newObj.query = JSON.stringify(params)
  128. } else {
  129. let params = JSON.parse(newObj.query)
  130. newObj.query = Object.assign(params, { token: tk })
  131. }
  132. }
  133. //#endregion
  134. if (item.name === "Zrzy") {
  135. newObj.meta.bsm = "101001";
  136. } else if (item.name === "Sthj") {
  137. newObj.meta.bsm = "101002";
  138. } else if (item.name === "Kjkfly") {
  139. newObj.meta.bsm = "101003";
  140. } else if (item.name === "Shjj") {
  141. newObj.meta.bsm = "101004";
  142. } else if (item.name === "Jsyj") {
  143. newObj.meta.bsm = "101005";
  144. } else if (item.name === "Csydjc") {
  145. newObj.meta.bsm = "101006";
  146. }
  147. if (item.children) {
  148. newObj.children = reset(item.children);
  149. }
  150. newList.push(newObj);
  151. });
  152. return newList;
  153. }
  154. export const loadView = (view) => {
  155. let res;
  156. for (const path in modules) {
  157. const dir = ((path || "").split("views/")[1] || "").split(".vue")[0];
  158. if (dir === view) {
  159. res = () => modules[path]();
  160. }
  161. }
  162. return res;
  163. };
  164. export default permission;