permission.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. let arr = []
  43. asyncRouterMap.filter(route => {
  44. if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId == "1762659463383281665") {
  45. let info = {
  46. alwaysShow: true,
  47. children: [],
  48. component: Layout,
  49. hidden: false,
  50. meta: {
  51. icon: route.icon,
  52. link: "",
  53. cache: false,
  54. title: route.name
  55. },
  56. name: route.name,
  57. path: route.path,
  58. redirect: false
  59. }
  60. info.children = filterAsyncRouter(route.children)
  61. if (info.children.length > 0) {
  62. info.redirect = info.children[0].path
  63. }
  64. arr.push(info)
  65. } else if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId != "1762659463383281665") {
  66. let info = {
  67. alwaysShow: true,
  68. children: [],
  69. component: ParentView,
  70. hidden: false,
  71. meta: {
  72. icon: route.icon,
  73. link: "",
  74. cache: false,
  75. title: route.name
  76. },
  77. name: route.name,
  78. path: route.path,
  79. redirect: false
  80. }
  81. info.children = filterAsyncRouter(route.children)
  82. if (info.children.length > 0) {
  83. info.redirect = info.children[0].path
  84. }
  85. arr.push(info)
  86. } else if (route.children.length == 0 && route.parentId == "1762659463383281665") {
  87. let info = {
  88. path: "",
  89. component: Layout,
  90. redirect: route.path,
  91. alwaysShow: false,
  92. hidden: false,
  93. meta: {
  94. icon: route.icon,
  95. title: route.name
  96. },
  97. children: [
  98. {
  99. // alwaysShow: false, //菜单不支持设置
  100. children: [],
  101. component: loadView(route.path),
  102. hidden: false,
  103. meta: {
  104. icon: route.icon,
  105. link: "",
  106. cache: false,
  107. title: route.name
  108. },
  109. name: route.name,
  110. path: route.path,
  111. redirect: false
  112. }
  113. ]
  114. }
  115. if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
  116. if (httpSave) {
  117. // const prefix = "/tjmchild"
  118. // if (route.path.startsWith(prefix)) {
  119. // info.redirect = route.path.slice(prefix.length)
  120. // info.children[0].path = route.path.slice(prefix.length)
  121. // }
  122. arr.push(info)
  123. }
  124. } else {
  125. arr.push(info)
  126. }
  127. } else if (route.type == 1) {
  128. let info = {
  129. // alwaysShow: false, //菜单不支持设置
  130. children: [],
  131. component: loadView(route.path),
  132. hidden: false,
  133. meta: {
  134. icon: route.icon,
  135. link: "",
  136. cache: false,
  137. title: route.name
  138. },
  139. name: route.name,
  140. path: route.path,
  141. redirect: false
  142. }
  143. if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
  144. if (httpSave) {
  145. // const prefix = "/tjmchild"
  146. // if (route.path.startsWith(prefix)) {
  147. // info.redirect = route.path.slice(prefix.length)
  148. // info.path = route.path.slice(prefix.length)
  149. // }
  150. arr.push(info)
  151. }
  152. } else {
  153. arr.push(info)
  154. }
  155. }
  156. })
  157. return arr
  158. }
  159. function loadView(view) {
  160. for (const path in modules) {
  161. const dir = "/" + path.split("views/")[1].split(".vue")[0];
  162. if (dir === view || dir == view + "/index") {
  163. return modules[path]
  164. }
  165. }
  166. }