permission.js 6.6 KB

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