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