permission.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { staticRoutes } from "@/router/constantRoutes"
  2. import { dynamicRoutes } from "@/router/dynamicRoutes"
  3. const Micro = () => import("@/micro/index.vue")
  4. import { useUserStore } from "@/store/user.js"
  5. import Layout from "@/layout/index.vue"
  6. import ParentView from "@/components/ParentView/index.vue"
  7. const modules = import.meta.glob("./../views/**/*.vue")
  8. export const usePermissionStore = defineStore(
  9. "permission",
  10. {
  11. state: () => ({
  12. routes: [],
  13. addRoutes: [],
  14. sidebarRouters: [],
  15. defaultRoutes: []
  16. }),
  17. actions: {
  18. setRoutes(routes) {
  19. this.addRoutes = routes
  20. this.routes = staticRoutes.concat(routes)
  21. },
  22. setSidebarRouters(routes) {
  23. this.sidebarRouters = routes
  24. },
  25. setDefaultRoutes(routes) {
  26. this.defaultRoutes = staticRoutes.concat(routes)
  27. },
  28. generateRoutes() {
  29. return new Promise(resolve => {
  30. const sidebarRoutes = filterAsyncRouter(useUserStore().menuList[0].children[0].children)
  31. this.setSidebarRouters(staticRoutes.concat(sidebarRoutes))
  32. const rewriteRoutes = filterAsyncRouter(useUserStore().menuList[0].children[0].children, false)
  33. this.setRoutes(rewriteRoutes)
  34. resolve(rewriteRoutes)
  35. })
  36. }
  37. }
  38. }
  39. )
  40. //
  41. function filterAsyncRouter(asyncRouterMap, httpSave = true) {
  42. const blackList = [
  43. // "/workflow", "/system/dataList", "/system/template",
  44. // "/publicDomain", "/manage/policyStrive", "/policyStrive",
  45. // "/caseShare"
  46. ]
  47. let arr = []
  48. asyncRouterMap.filter(route => {
  49. if (!blackList.includes(route.path)) {
  50. if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId == "1762659463383281665") {
  51. let info = {
  52. alwaysShow: true,
  53. children: [],
  54. component: Layout,
  55. hidden: false,
  56. meta: {
  57. icon: route.icon,
  58. link: "",
  59. cache: false,
  60. title: route.name
  61. },
  62. name: route.name,
  63. path: route.path,
  64. redirect: false
  65. }
  66. info.children = filterAsyncRouter(route.children)
  67. if (info.children.length > 0) {
  68. info.redirect = info.children[0].path
  69. }
  70. arr.push(info)
  71. } else if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId != "1762659463383281665") {
  72. let info = {
  73. alwaysShow: true,
  74. children: [],
  75. component: ParentView,
  76. hidden: false,
  77. meta: {
  78. icon: route.icon,
  79. link: "",
  80. cache: false,
  81. title: route.name
  82. },
  83. name: route.name,
  84. path: route.path,
  85. redirect: false
  86. }
  87. info.children = filterAsyncRouter(route.children)
  88. if (info.children.length > 0) {
  89. info.redirect = info.children[0].path
  90. }
  91. arr.push(info)
  92. } else if (route.children.length == 0 && route.parentId == "1762659463383281665") {
  93. let info = {
  94. path: "",
  95. component: Layout,
  96. redirect: route.path,
  97. alwaysShow: false,
  98. hidden: false,
  99. meta: {
  100. icon: route.icon,
  101. title: route.name
  102. },
  103. children: [
  104. {
  105. // alwaysShow: false, //菜单不支持设置
  106. children: [],
  107. component: loadView(route.path),
  108. hidden: false,
  109. meta: {
  110. icon: route.icon,
  111. link: "",
  112. cache: false,
  113. title: route.name
  114. },
  115. name: route.name,
  116. path: route.path,
  117. redirect: false
  118. }
  119. ]
  120. }
  121. if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
  122. if (httpSave) {
  123. // const prefix = "/tjmchild"
  124. // if (route.path.startsWith(prefix)) {
  125. // info.redirect = route.path.slice(prefix.length)
  126. // info.children[0].path = route.path.slice(prefix.length)
  127. // }
  128. arr.push(info)
  129. }
  130. } else {
  131. arr.push(info)
  132. }
  133. } else if (route.type == 1) {
  134. let info = {
  135. // alwaysShow: false, //菜单不支持设置
  136. children: [],
  137. component: loadView(route.path),
  138. hidden: false,
  139. meta: {
  140. icon: route.icon,
  141. link: "",
  142. cache: false,
  143. title: route.name
  144. },
  145. name: route.name,
  146. path: route.path,
  147. redirect: false
  148. }
  149. if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
  150. if (httpSave) {
  151. // const prefix = "/tjmchild"
  152. // if (route.path.startsWith(prefix)) {
  153. // info.redirect = route.path.slice(prefix.length)
  154. // info.path = route.path.slice(prefix.length)
  155. // }
  156. arr.push(info)
  157. }
  158. } else {
  159. arr.push(info)
  160. }
  161. }
  162. }
  163. })
  164. return arr
  165. }
  166. function loadView(view) {
  167. for (const path in modules) {
  168. const dir = "/" + path.split("views/")[1].split(".vue")[0];
  169. if (dir === view || dir == view + "/index") {
  170. return modules[path]
  171. }
  172. }
  173. }