|
| 1 | +import { |
| 2 | + createRouter, |
| 3 | + createWebHistory, |
| 4 | + type NavigationGuardNext, |
| 5 | + type RouteLocationNormalized, |
| 6 | + type RouteRecordRaw, |
| 7 | + type RouteRecordName, |
| 8 | +} from 'vue-router' |
| 9 | +import { hasPermission, set_next_route } from '@/utils/permission/index' |
| 10 | +export const getChildRouteList: ( |
| 11 | + routeList: Array<RouteRecordRaw>, |
| 12 | + path: string, |
| 13 | + name?: RouteRecordName | null | undefined, |
| 14 | +) => Array<RouteRecordRaw> = (routeList, path, name) => { |
| 15 | + for (let index = 0; index < routeList.length; index++) { |
| 16 | + const route = routeList[index] |
| 17 | + if (name === route.name && path === route.path) { |
| 18 | + return route.children || [] |
| 19 | + } |
| 20 | + if (route.children && route.children.length > 0) { |
| 21 | + const result = getChildRouteList(route.children, path, name) |
| 22 | + if (result && result?.length > 0) { |
| 23 | + return result |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + return [] |
| 28 | +} |
| 29 | +/** |
| 30 | + * 获取同级路由 |
| 31 | + * @param routeList |
| 32 | + * @param name |
| 33 | + * @returns |
| 34 | + */ |
| 35 | +export const getSameRouteList: ( |
| 36 | + routeList: Array<RouteRecordRaw>, |
| 37 | + name?: RouteRecordName | null | undefined, |
| 38 | +) => Array<RouteRecordRaw> = (routeList, name) => { |
| 39 | + for (let index = 0; index < routeList.length; index++) { |
| 40 | + const route = routeList[index] |
| 41 | + if (name === route.name) { |
| 42 | + return routeList |
| 43 | + } |
| 44 | + if (route.children && route.children.length > 0) { |
| 45 | + const result = getSameRouteList(route.children, name) |
| 46 | + if (result && result?.length > 0) { |
| 47 | + return result |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + return [] |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * 获取有权限的路由 |
| 56 | + * @param routes |
| 57 | + * @param to |
| 58 | + * @returns |
| 59 | + */ |
| 60 | +export const getPermissionRoute = (routes: Array<RouteRecordRaw>, to: RouteLocationNormalized) => { |
| 61 | + const routeName: string = to.meta |
| 62 | + ? to.meta.permissionRoute |
| 63 | + ? (to.meta.permissionRoute as string) |
| 64 | + : (to.name as string) |
| 65 | + : (to.name as string) |
| 66 | + const routeList = getSameRouteList(routes, routeName) |
| 67 | + const route = routeList.find((route: any) => { |
| 68 | + return ( |
| 69 | + (to.meta.group ? to.meta.group == route.meta.group : true) && |
| 70 | + (route.meta.permission ? hasPermission(route.meta.permission as any, 'OR') : true) |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + if (route?.name) { |
| 75 | + return { name: route?.name, params: to.params } |
| 76 | + } |
| 77 | + return { name: 'noPermission' } |
| 78 | +} |
0 commit comments