Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
优化路由筛选函数
Browse files Browse the repository at this point in the history
  • Loading branch information
jinmao88 committed Aug 27, 2023
1 parent 531b07a commit d17c708
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
31 changes: 7 additions & 24 deletions apps/admin/src/store/auth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { defineStore } from '@vben/stores'
import { layoutRoutes, PAGE_NOT_FOUND_ROUTE } from '@vben/router'
import { filterTree } from '@vben/utils'
import {
layoutRoutes,
PAGE_NOT_FOUND_ROUTE,
filterRouterTree,
transformObjToRoute,
transformRouteToMenu,
} from '@vben/router'
import { Menu } from '@vben/types'
import { useConfigStoreWithOut } from './config'
import { useUserStore } from './user'
import { getPermCode } from '@/apis/auth'
import { toRaw, unref } from 'vue'
import { projectSetting } from '@/setting'
import { PermissionModeEnum, PageEnum } from '@vben/constants'
import {
flatMultiLevelRoutes,
transformObjToRoute,
transformRouteToMenu,
} from '@vben/router'
import { getMenuList } from '@/apis/sys'
import { useAppConfig } from '@vben/hooks'
import { asyncRoutes } from '@/router/routes'
Expand Down Expand Up @@ -205,18 +203,3 @@ export const useAuthStore = defineStore('app-auth-store', {
},
},
})
function filterRouterTree<T = any>(
tree: T[],
func: (n: T) => boolean,
flat: boolean = true,
): RouteRecordItem[] {
let t = filterTree(tree, func).filter(func) as RouteRecordItem[]
if (flat) {
t = flatMultiLevelRoutes(t)
}
return t
}
// Need to be used outside the setup
export function useAuthStoreWithout() {
return useAuthStore()
}
14 changes: 13 additions & 1 deletion packages/router/src/helper/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RouteMeta, Router, RouteRecordNormalized } from 'vue-router'
import { Exception, FrameBlank } from '../page'
import { omit, cloneDeep } from '@vben/utils'
import { omit, cloneDeep, filterTree } from '@vben/utils'
import { createRouter, createWebHashHistory } from 'vue-router'
import { LAYOUT, PARENT_LAYOUT } from '../routes'

Expand Down Expand Up @@ -182,3 +182,15 @@ function isMultipleRoute(routeModule: RouteRecordItem) {
}
return flag
}

export function filterRouterTree<T = any>(
tree: T[],
func: (n: T) => boolean,
flat: boolean = true,
): RouteRecordItem[] {
let t = filterTree(tree, func).filter(func) as RouteRecordItem[]
if (flat) {
t = flatMultiLevelRoutes(t)
}
return t
}

0 comments on commit d17c708

Please sign in to comment.