permission.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 api from "@/api/api";
  6. import InnerLink from "@/layout/components/InnerLink";
  7. const permission = {
  8. state: {
  9. routes: [],
  10. addRoutes: [],
  11. defaultRoutes: [],
  12. topbarRouters: [],
  13. sidebarRouters: [],
  14. dictManages: {}, //字典数据
  15. },
  16. getters: {
  17. dictManages: (state) => state.dictManages,
  18. },
  19. mutations: {
  20. SET_ROUTES: (state, routes) => {
  21. state.addRoutes = routes;
  22. state.routes = constantRoutes.concat(routes);
  23. },
  24. GET_DICT_INFO(state) {
  25. // api.obtaindictdata().then((res) => {
  26. // let newList = {};
  27. // let list = res.rows || [];
  28. // for (let i = 0; i < list.length; i++) {
  29. // let item = list[i];
  30. // if (newList.hasOwnProperty(item.dictType)) {
  31. // newList[item.dictType].push(item.dictLabel);
  32. // } else {
  33. // newList[item.dictType] = [item.dictLabel];
  34. // }
  35. // }
  36. // state.dictManages = newList;
  37. // });
  38. },
  39. SET_DEFAULT_ROUTES: (state, routes) => {
  40. state.defaultRoutes = constantRoutes.concat(routes);
  41. },
  42. SET_TOPBAR_ROUTES: (state, routes) => {
  43. // 顶部导航菜单默认添加统计报表栏指向首页
  44. const index = [
  45. {
  46. path: "index",
  47. meta: { title: "工作台", icon: "dashboard" },
  48. },
  49. ];
  50. let ary = [];
  51. state.topbarRouters = ary.concat(index, routes);
  52. },
  53. SET_SIDEBAR_ROUTERS: (state, routes) => {
  54. state.sidebarRouters = routes;
  55. },
  56. },
  57. actions: {
  58. // 生成路由
  59. GenerateRoutes({ commit }) {
  60. return new Promise((resolve) => {
  61. // 向后端请求路由数据
  62. getRouters().then((res) => {
  63. res.data.forEach((item, i) => {
  64. if (!item.children) {
  65. res.data[i] = {
  66. meta: "",
  67. path: "",
  68. redirect: "/redirect",
  69. component: "Layout",
  70. alwaysShow: false,
  71. children: [
  72. {
  73. name: item.name,
  74. path: item.path.substr(0),
  75. component: item.component,
  76. hidden: false,
  77. noChildren: true,
  78. meta: item.meta,
  79. },
  80. ],
  81. };
  82. }
  83. });
  84. const sdata = JSON.parse(JSON.stringify(res.data));
  85. const rdata = JSON.parse(JSON.stringify(res.data));
  86. const sidebarRoutes = filterAsyncRouter(sdata);
  87. const rewriteRoutes = filterAsyncRouter(rdata, false, true);
  88. commit("SET_ROUTES", rewriteRoutes);
  89. commit("SET_SIDEBAR_ROUTERS", constantRoutes.concat(sidebarRoutes));
  90. commit("SET_DEFAULT_ROUTES", sidebarRoutes);
  91. commit("SET_TOPBAR_ROUTES", sidebarRoutes);
  92. commit("GET_DICT_INFO");
  93. resolve(rewriteRoutes);
  94. });
  95. });
  96. },
  97. },
  98. };
  99. // 遍历后台传来的路由字符串,转换为组件对象
  100. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  101. return asyncRouterMap.filter((route) => {
  102. if (type && route.children) {
  103. route.children = filterChildren(route.children);
  104. }
  105. if (route.component) {
  106. // Layout ParentView 组件特殊处理
  107. if (route.component === "Layout") {
  108. route.component = Layout;
  109. } else if (route.component === "ParentView") {
  110. route.component = ParentView;
  111. } else if (route.component === "InnerLink") {
  112. route.component = InnerLink;
  113. } else {
  114. route.component = loadView(route.component);
  115. }
  116. }
  117. if (route.children != null && route.children && route.children.length) {
  118. route.children = filterAsyncRouter(route.children, route, type);
  119. } else {
  120. delete route["children"];
  121. delete route["redirect"];
  122. }
  123. return true;
  124. });
  125. }
  126. function filterChildren(childrenMap, lastRouter = false) {
  127. var children = [];
  128. childrenMap.forEach((el, index) => {
  129. if (el.children && el.children.length) {
  130. if (el.component === "ParentView" && !lastRouter) {
  131. el.children.forEach((c) => {
  132. c.path = el.path + "/" + c.path;
  133. if (c.children && c.children.length) {
  134. children = children.concat(filterChildren(c.children, c));
  135. return;
  136. }
  137. children.push(c);
  138. });
  139. return;
  140. }
  141. }
  142. if (lastRouter) {
  143. el.path = lastRouter.path + "/" + el.path;
  144. }
  145. children = children.concat(el);
  146. });
  147. return children;
  148. }
  149. export const loadView = (view) => {
  150. if (process.env.NODE_ENV === "development") {
  151. return (resolve) => require([`@/views/${view}`], resolve);
  152. } else {
  153. // 使用 import 实现生产环境的路由懒加载
  154. return (resolve) => require([`@/views/${view}`], resolve);
  155. }
  156. };
  157. export default permission;