routeConvert.js 703 B

123456789101112131415161718192021222324252627282930
  1. import cloneDeep from 'lodash.clonedeep'
  2. export function convertRoutes (nodes) {
  3. if (!nodes) return null
  4. nodes = cloneDeep(nodes)
  5. let queue = Array.isArray(nodes) ? nodes.concat() : [nodes]
  6. while (queue.length) {
  7. const levelSize = queue.length
  8. for (let i = 0; i < levelSize; i++) {
  9. const node = queue.shift()
  10. if (!node.children || !node.children.length) continue
  11. node.children.forEach(child => {
  12. // 转化相对路径
  13. if (child.path[0] !== '/' && !child.path.startsWith('http')) {
  14. child.path = node.path.replace(/(\w*)[/]*$/, `$1/${child.path}`)
  15. }
  16. })
  17. queue = queue.concat(node.children)
  18. }
  19. }
  20. return nodes
  21. }