|
|
@@ -1,5 +1,5 @@
|
|
|
import type { RouteRecordRaw } from 'vue-router'
|
|
|
-import { constantRouter, asyncRouter } from '@/router'
|
|
|
+import { asyncRouter } from '@/router'
|
|
|
|
|
|
function hasPermission(route: RouteRecordRaw, permission: any[]) {
|
|
|
if (route.meta && route.meta.permission) {
|
|
|
@@ -29,6 +29,7 @@ function hasRole(route: RouteRecordRaw, role: any[]) {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+// 根据角色过滤路由
|
|
|
function filterAsyncRouter(routerMap: RouteRecordRaw[], role: any[]) {
|
|
|
const accessedRouters = routerMap.filter(route => {
|
|
|
if (hasRole(route, role)) {
|
|
|
@@ -42,16 +43,34 @@ function filterAsyncRouter(routerMap: RouteRecordRaw[], role: any[]) {
|
|
|
return accessedRouters
|
|
|
}
|
|
|
|
|
|
+// 过滤掉hidden为true的路由
|
|
|
+function filterMenuRouter(routerMap: RouteRecordRaw[]) {
|
|
|
+ const accessedRouters = routerMap.filter(route => {
|
|
|
+ console.log(route)
|
|
|
+ if (!route.hidden) {
|
|
|
+ if (route.children && route.children.length) {
|
|
|
+ route.children = filterMenuRouter(route.children)
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ return accessedRouters
|
|
|
+}
|
|
|
+
|
|
|
export const useRouterStore = defineStore({
|
|
|
id: 'router',
|
|
|
state: () => ({
|
|
|
- menuRouter: <any>[]
|
|
|
+ menuRouter: <any>[],
|
|
|
+ asyncRouter: <any>[]
|
|
|
}),
|
|
|
actions: {
|
|
|
generatorRouter(role?: any[]): Promise<RouteRecordRaw[]> {
|
|
|
return new Promise(resolve => {
|
|
|
- this.menuRouter = role ? filterAsyncRouter(asyncRouter, role) : asyncRouter
|
|
|
- resolve(this.menuRouter)
|
|
|
+ this.asyncRouter = role ? filterAsyncRouter(asyncRouter, role) : asyncRouter
|
|
|
+ this.menuRouter = filterMenuRouter(this.asyncRouter)
|
|
|
+ console.log(this.menuRouter)
|
|
|
+ resolve(this.asyncRouter)
|
|
|
})
|
|
|
}
|
|
|
}
|