routerHelper.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. const childPath = path.startsWith('/') || !path ? path : `/${path}`
  110. return `${parentPath}${childPath}`
  111. }
  112. // 路由降级
  113. export function flatMultiLevelRoutes(routes: AppRouteRecordRaw[]) {
  114. const modules: AppRouteRecordRaw[] = cloneDeep(routes)
  115. for (let index = 0; index < modules.length; index++) {
  116. const route = modules[index]
  117. if (!isMultipleRoute(route)) {
  118. continue
  119. }
  120. promoteRouteLevel(route)
  121. }
  122. return modules
  123. }
  124. // 层级是否大于2
  125. function isMultipleRoute(route: AppRouteRecordRaw) {
  126. if (!route || !Reflect.has(route, 'children') || !route.children?.length) {
  127. return false
  128. }
  129. const children = route.children
  130. let flag = false
  131. for (let index = 0; index < children.length; index++) {
  132. const child = children[index]
  133. if (child.children?.length) {
  134. flag = true
  135. break
  136. }
  137. }
  138. return flag
  139. }
  140. // 生成二级路由
  141. function promoteRouteLevel(route: AppRouteRecordRaw) {
  142. let router: Router | null = createRouter({
  143. routes: [route as unknown as RouteRecordNormalized],
  144. history: createWebHashHistory()
  145. })
  146. const routes = router.getRoutes()
  147. addToChildren(routes, route.children || [], route)
  148. router = null
  149. route.children = route.children?.map((item) => omit(item, 'children'))
  150. }
  151. // 添加所有子菜单
  152. function addToChildren(
  153. routes: RouteRecordNormalized[],
  154. children: AppRouteRecordRaw[],
  155. routeModule: AppRouteRecordRaw
  156. ) {
  157. for (let index = 0; index < children.length; index++) {
  158. const child = children[index]
  159. const route = routes.find((item) => item.name === child.name)
  160. if (!route) {
  161. continue
  162. }
  163. routeModule.children = routeModule.children || []
  164. if (!routeModule.children.find((item) => item.name === route.name)) {
  165. routeModule.children?.push(route as unknown as AppRouteRecordRaw)
  166. }
  167. if (child.children?.length) {
  168. addToChildren(routes, child.children, routeModule)
  169. }
  170. }
  171. }