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