|
17 | 17 |
|
18 | 18 | import { asyncRouterMap, constantRouterMap } from '@/config/router' |
19 | 19 |
|
20 | | -function hasApi (apis, route) { |
21 | | - if (route.meta && route.meta.permission) { |
22 | | - for (const permission of route.meta.permission) { |
23 | | - if (!apis.includes(permission)) { |
24 | | - return false |
25 | | - } |
26 | | - } |
| 20 | +function hasAccessToRoute (apis, route) { |
| 21 | + if (!route.meta || !route.meta.permission) { |
27 | 22 | return true |
28 | 23 | } |
| 24 | + for (const permission of route.meta.permission) { |
| 25 | + if (!apis.includes(permission)) { |
| 26 | + return false |
| 27 | + } |
| 28 | + } |
| 29 | + return true |
| 30 | +} |
| 31 | + |
| 32 | +function hasAccessToSection (route) { |
| 33 | + const visibleChildren = route.children.filter(child => !child.hidden) |
| 34 | + if (visibleChildren.length === 0) { |
| 35 | + return false |
| 36 | + } |
| 37 | + const redirect = '/' + visibleChildren[0].meta.name |
| 38 | + if (redirect !== route.path) { |
| 39 | + route.redirect = redirect |
| 40 | + } |
29 | 41 | return true |
30 | 42 | } |
31 | 43 |
|
32 | 44 | function filterAsyncRouter (routerMap, apis) { |
33 | 45 | const accessedRouters = routerMap.filter(route => { |
34 | | - if (hasApi(apis, route)) { |
| 46 | + if (hasAccessToRoute(apis, route)) { |
35 | 47 | if (route.children && route.children.length > 0) { |
36 | 48 | route.children = filterAsyncRouter(route.children, apis) |
37 | 49 | } |
| 50 | + if (route.meta && route.meta.section) { |
| 51 | + return hasAccessToSection(route) |
| 52 | + } |
38 | 53 | return true |
39 | 54 | } |
40 | 55 | return false |
|
0 commit comments