route.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { isNull } from './index'
  2. import { privateRoutes } from '@/router/private-routes'
  3. /**
  4. * 所有子集路由
  5. * @param {*}
  6. * @returns
  7. */
  8. export const getChildrenRoutes = (routes) => {
  9. const result = []
  10. routes.forEach((item) => {
  11. if (item.children && item.children.length > 0) {
  12. result.push(...item.children)
  13. }
  14. })
  15. return result
  16. }
  17. /**
  18. * 处理脱离层级的路由
  19. * @param {*}
  20. * @returns
  21. */
  22. export const filterRoutes = (routes) => {
  23. // 所有子集路由
  24. const childrenRoutes = getChildrenRoutes(routes)
  25. // 根据子集路由查重
  26. return routes.filter((route) => {
  27. // 根据route 在 childrenRoutes 中进行查重 把所有路由表剔除
  28. return !childrenRoutes.find((childrenRoute) => {
  29. return childrenRoute.path === route.path
  30. })
  31. })
  32. }
  33. /**
  34. * 根据routes 数据 返回对应的 menu 规则的数据
  35. * @param {*}
  36. * @returns 返回对应的 menu 规则的数据
  37. */
  38. export const generteMenus = (routes, basePath = '') => {
  39. const result = []
  40. // 不满足 meta && meta.title 的数据去除
  41. routes.forEach((item) => {
  42. // item 不存在children && 不存在meta 直接 return
  43. if (isNull(item.children) && isNull(item.meta)) return
  44. // 判断是否是子路由 子路由直接 return 下面处理
  45. if (item.path.split('/').length > 2) return
  46. // 添加第一级路由
  47. let route = {
  48. ...item,
  49. children: []
  50. }
  51. result.push(route)
  52. })
  53. // 给子路由匹配上 一级路由
  54. result.forEach((key) => {
  55. routes.forEach((item) => {
  56. if (key.path === item.path) {
  57. return
  58. } else if (key.path === `/${item.path.split('/')[1]}`) {
  59. key.children.push(item)
  60. }
  61. })
  62. })
  63. return result
  64. }
  65. export const changeSortMenus = (tree) => {
  66. if (!tree || !Array.isArray(tree)) return []
  67. function customSort(n1, n2) {
  68. const { sort: sort1 = Number.MAX_SAFE_INTEGER } = n1.sort || {}
  69. const { sort: sort2 = Number.MAX_SAFE_INTEGER } = n2.sort || {}
  70. return sort1 - sort2
  71. }
  72. tree.sort(customSort)
  73. tree.forEach((n) => {
  74. if (n.children && Array.isArray(n.children) && n.children.length > 0) {
  75. n.children = changeSortMenus(n.children)
  76. }
  77. })
  78. return tree
  79. }
  80. export const getMenusData = () => {
  81. const result = []
  82. let uniqueArr = {}
  83. privateRoutes.forEach((item) => {
  84. if (!uniqueArr[item.meta.title]) {
  85. uniqueArr[item.meta.title] = true
  86. let obj = {
  87. label: item.meta.title,
  88. id: item.name,
  89. path: item.path,
  90. sort: item.sort,
  91. children: []
  92. }
  93. result.push(obj)
  94. }
  95. })
  96. privateRoutes.forEach((n) => {
  97. result.forEach((i) => {
  98. if (i.path == n.path) {
  99. let obj = { parentSymbol: i.id, label: n.children[0].meta.title, id: n.name, sort: n.children[0].sort }
  100. i.children.push(obj)
  101. }
  102. })
  103. })
  104. return result
  105. }
  106. export const flatMenus = (router) => {
  107. const arr = []
  108. router.sort((a, b) => a.sort - b.sort)
  109. const f = (router) => {
  110. router.sort((a, b) => a.sort - b.sort)
  111. router.forEach((r) => {
  112. if (r.children && r.children.length > 0) {
  113. f(r.children)
  114. } else {
  115. arr.push(r.id)
  116. }
  117. })
  118. }
  119. f(router)
  120. return arr
  121. }