permission.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { constantRoutes } from "@/router";
  2. import { getRouters } from "@/api/menu";
  3. import { getInfo } from "@/api/login";
  4. import Layout from "@/layout/index";
  5. import ParentView from "@/components/ParentView";
  6. import { toCamelCase } from "@/utils";
  7. const permission = {
  8. state: {
  9. routes: [],
  10. addRoutes: [],
  11. sidebarRouters: [],
  12. },
  13. mutations: {
  14. SET_ROUTES: (state, routes) => {
  15. state.addRoutes = routes;
  16. state.routes = constantRoutes.concat(routes);
  17. },
  18. SET_DEFAULT_ROUTES: (state, routes) => {
  19. state.defaultRoutes = constantRoutes.concat(routes);
  20. },
  21. SET_TOPBAR_ROUTES: (state, routes) => {
  22. state.topbarRouters = routes;
  23. },
  24. SET_SIDEBAR_ROUTERS: (state, routes) => {
  25. state.sidebarRouters = routes;
  26. },
  27. },
  28. actions: {
  29. // 生成路由
  30. GenerateRoutes({ commit }) {
  31. return new Promise((resolve) => {
  32. // 向后端请求路由数据
  33. const params = { zone: 2 };
  34. getInfo(params).then((res) => {
  35. const sdata = JSON.parse(JSON.stringify(res.data.menus));
  36. const rdata = JSON.parse(JSON.stringify(res.data.menus));
  37. const sidebarRoutes = filterAsyncRouter(sdata);
  38. const rewriteRoutes = filterAsyncRouter(rdata, false, true);
  39. rewriteRoutes.push({ path: "*", redirect: "/404", hidden: true });
  40. commit("SET_ROUTES", rewriteRoutes);
  41. commit("SET_SIDEBAR_ROUTERS", constantRoutes.concat(sidebarRoutes));
  42. commit("SET_DEFAULT_ROUTES", sidebarRoutes);
  43. commit("SET_TOPBAR_ROUTES", sidebarRoutes);
  44. resolve(rewriteRoutes);
  45. });
  46. });
  47. },
  48. },
  49. };
  50. // 遍历后台传来的路由字符串,转换为组件对象
  51. function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  52. return asyncRouterMap.filter((route) => {
  53. // 将 ruoyi 后端原有耦合前端的逻辑,迁移到此处
  54. // 处理 meta 属性
  55. route.meta = {
  56. title: route.name,
  57. icon: route.icon,
  58. activeMenu: route.activeMenu,
  59. id: route.id,
  60. noCache: !route.keepAlive,
  61. reportId: route.reportId,
  62. reportList: route.reportList,
  63. bpmnModelId: route.bpmnModelId,
  64. componentName: route.componentName,
  65. queryManageId: route.queryManageId,
  66. };
  67. // 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive
  68. // route.name = toCamelCase(route.path, true)
  69. // 开始调用后端返回的组件名
  70. route.name = route.componentName;
  71. route.hidden = !route.visible;
  72. // 处理 component 属性
  73. if (route.children) {
  74. // 父节点
  75. route.component = route.parentId === 0 ? Layout : ParentView;
  76. // if (route.parentId === 0) {
  77. // route.component = Layout
  78. // } else {
  79. // route.component = ParentView
  80. // }
  81. } else {
  82. // 根节点
  83. route.component = loadView(route.component);
  84. }
  85. // filterChildren
  86. if (type && route.children) {
  87. route.children = filterChildren(route.children);
  88. }
  89. if (route.children != null && route.children && route.children.length) {
  90. route.children = filterAsyncRouter(route.children, route, type);
  91. } else {
  92. delete route["children"];
  93. }
  94. return true;
  95. });
  96. }
  97. function filterChildren(childrenMap, lastRouter = false) {
  98. var children = [];
  99. childrenMap.forEach((el, index) => {
  100. if (el.children && el.children.length) {
  101. if (el.component === "ParentView" && !lastRouter) {
  102. el.children.forEach((c) => {
  103. c.path = el.path + "/" + c.path;
  104. if (c.children && c.children.length) {
  105. children = children.concat(filterChildren(c.children, c));
  106. return;
  107. }
  108. children.push(c);
  109. });
  110. return;
  111. }
  112. }
  113. if (lastRouter) {
  114. el.path = lastRouter.path + "/" + el.path;
  115. }
  116. children = children.concat(el);
  117. });
  118. return children;
  119. }
  120. export const loadView = (view) => {
  121. // 路由懒加载
  122. return (resolve) => require([`@/views/${view}`], resolve);
  123. };
  124. export default permission;