routerHelper.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import type { Router, RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'
  3. import { isUrl } from '@/utils/is'
  4. import { useCache } from '@/hooks/web/useCache'
  5. import { useAppStoreWithOut } from '@/store/modules/app'
  6. import { omit, cloneDeep } from 'lodash-es'
  7. const appStore = useAppStoreWithOut()
  8. const { wsCache } = useCache()
  9. const modules = import.meta.glob('../../views/**/*.{vue,tsx}')
  10. /* Layout */
  11. export const Layout = () => import('@/layout/Layout.vue')
  12. export const getParentLayout = () => {
  13. return () =>
  14. new Promise((resolve) => {
  15. resolve({
  16. name: 'ParentLayout'
  17. })
  18. })
  19. }
  20. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  21. if (!route) return route
  22. const { matched, ...opt } = route
  23. return {
  24. ...opt,
  25. matched: (matched
  26. ? matched.map((item) => ({
  27. meta: item.meta,
  28. name: item.name,
  29. path: item.path
  30. }))
  31. : undefined) as RouteRecordNormalized[]
  32. }
  33. }
  34. // 前端控制路由生成
  35. export function generateRoutesFn1(
  36. routes: AppRouteRecordRaw[],
  37. basePath = '/'
  38. ): AppRouteRecordRaw[] {
  39. const res: AppRouteRecordRaw[] = []
  40. for (const route of routes) {
  41. // skip some route
  42. if (route.meta && route.meta.hidden && !route.meta.showMainRoute) {
  43. continue
  44. }
  45. let data: Nullable<AppRouteRecordRaw> = null
  46. let onlyOneChild: Nullable<string> = null
  47. if (route.children && route.children.length === 1 && !route.meta.alwaysShow) {
  48. onlyOneChild = (
  49. isUrl(route.children[0].path)
  50. ? route.children[0].path
  51. : pathResolve(pathResolve(basePath, route.path), route.children[0].path)
  52. ) as string
  53. }
  54. // 权限过滤,通过获取登录信息里面的角色权限,动态的渲染菜单。
  55. const list = wsCache.get(appStore.getUserInfo).checkedNodes
  56. // 开发者可以根据实际情况进行扩展
  57. for (const item of list) {
  58. // 通过路径去匹配
  59. if (isUrl(item.path) && (onlyOneChild === item.path || route.path === item.path)) {
  60. data = Object.assign({}, route)
  61. } else {
  62. const routePath = pathResolve(basePath, onlyOneChild || route.path)
  63. if (routePath === item.path || (route.meta && route.meta.followRoute === item.path)) {
  64. data = Object.assign({}, route)
  65. }
  66. }
  67. }
  68. // recursive child routes
  69. if (route.children && data) {
  70. data.children = generateRoutesFn1(route.children, pathResolve(basePath, data.path))
  71. }
  72. if (data) {
  73. res.push(data as AppRouteRecordRaw)
  74. }
  75. }
  76. return res
  77. }
  78. // 后端控制路由生成
  79. export function generateRoutesFn2(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
  80. const res: AppRouteRecordRaw[] = []
  81. for (const route of routes) {
  82. const data: AppRouteRecordRaw = {
  83. path: route.path,
  84. name: route.name,
  85. redirect: route.redirect,
  86. meta: route.meta
  87. }
  88. if (route.component) {
  89. const comModule =
  90. modules[`../../${route.component}.vue`] || modules[`../../${route.component}.tsx`]
  91. if (comModule) {
  92. // 动态加载路由文件,可根据实际情况进行自定义逻辑
  93. const component = route.component as string
  94. data.component =
  95. component === '#' ? Layout : component.includes('##') ? getParentLayout() : comModule
  96. } else {
  97. console.error(`未找到${route.component}.vue文件或${route.component}.tsx文件,请创建`)
  98. }
  99. }
  100. // recursive child routes
  101. if (route.children) {
  102. data.children = generateRoutesFn2(route.children)
  103. }
  104. res.push(data as AppRouteRecordRaw)
  105. }
  106. return res
  107. }
  108. export function pathResolve(parentPath: string, path: string) {
  109. return `${parentPath}/${path}`
  110. }
  111. // 路由降级
  112. export function flatMultiLevelRoutes(routes: AppRouteRecordRaw[]) {
  113. const modules: AppRouteRecordRaw[] = cloneDeep(routes)
  114. for (let index = 0; index < modules.length; index++) {
  115. const route = modules[index]
  116. if (!isMultipleRoute(route)) {
  117. continue
  118. }
  119. promoteRouteLevel(route)
  120. }
  121. return modules
  122. }
  123. // 层级是否大于2
  124. function isMultipleRoute(route: AppRouteRecordRaw) {
  125. if (!route || !Reflect.has(route, 'children') || !route.children?.length) {
  126. return false
  127. }
  128. const children = route.children
  129. let flag = false
  130. for (let index = 0; index < children.length; index++) {
  131. const child = children[index]
  132. if (child.children?.length) {
  133. flag = true
  134. break
  135. }
  136. }
  137. return flag
  138. }
  139. // 生成二级路由
  140. function promoteRouteLevel(route: AppRouteRecordRaw) {
  141. let router: Router | null = createRouter({
  142. routes: [route as unknown as RouteRecordNormalized],
  143. history: createWebHashHistory()
  144. })
  145. const routes = router.getRoutes()
  146. addToChildren(routes, route.children || [], route)
  147. router = null
  148. route.children = route.children?.map((item) => omit(item, 'children'))
  149. }
  150. // 添加所有子菜单
  151. function addToChildren(
  152. routes: RouteRecordNormalized[],
  153. children: AppRouteRecordRaw[],
  154. routeModule: AppRouteRecordRaw
  155. ) {
  156. for (let index = 0; index < children.length; index++) {
  157. const child = children[index]
  158. const route = routes.find((item) => item.name === child.name)
  159. if (!route) {
  160. continue
  161. }
  162. routeModule.children = routeModule.children || []
  163. if (!routeModule.children.find((item) => item.name === route.name)) {
  164. routeModule.children?.push(route as unknown as AppRouteRecordRaw)
  165. }
  166. if (child.children?.length) {
  167. addToChildren(routes, child.children, routeModule)
  168. }
  169. }
  170. }