perf: 代码优化 store/modules/permission
This commit is contained in:
parent
5c09716fa5
commit
d090c294ae
@ -6,28 +6,19 @@ import { constantRoutes, asyncRoutes } from "@/router"
|
|||||||
import asyncRouteSettings from "@/config/async-route"
|
import asyncRouteSettings from "@/config/async-route"
|
||||||
|
|
||||||
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
|
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
|
||||||
if (route.meta && route.meta.roles) {
|
const routeRoles = route.meta?.roles
|
||||||
return roles.some((role) => {
|
return routeRoles ? roles.some((role) => routeRoles.includes(role)) : true
|
||||||
if (route.meta?.roles !== undefined) {
|
|
||||||
return route.meta.roles.includes(role)
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) => {
|
const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) => {
|
||||||
const res: RouteRecordRaw[] = []
|
const res: RouteRecordRaw[] = []
|
||||||
routes.forEach((route) => {
|
routes.forEach((route) => {
|
||||||
const r = { ...route }
|
const tempRoute = { ...route }
|
||||||
if (hasPermission(roles, r)) {
|
if (hasPermission(roles, tempRoute)) {
|
||||||
if (r.children) {
|
if (tempRoute.children) {
|
||||||
r.children = filterAsyncRoutes(r.children, roles)
|
tempRoute.children = filterAsyncRoutes(tempRoute.children, roles)
|
||||||
}
|
}
|
||||||
res.push(r)
|
res.push(tempRoute)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return res
|
return res
|
||||||
@ -38,12 +29,7 @@ export const usePermissionStore = defineStore("permission", () => {
|
|||||||
const dynamicRoutes = ref<RouteRecordRaw[]>([])
|
const dynamicRoutes = ref<RouteRecordRaw[]>([])
|
||||||
|
|
||||||
const setRoutes = (roles: string[]) => {
|
const setRoutes = (roles: string[]) => {
|
||||||
let accessedRoutes
|
const accessedRoutes = asyncRouteSettings.open ? filterAsyncRoutes(asyncRoutes, roles) : asyncRoutes
|
||||||
if (!asyncRouteSettings.open) {
|
|
||||||
accessedRoutes = asyncRoutes
|
|
||||||
} else {
|
|
||||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
|
||||||
}
|
|
||||||
routes.value = constantRoutes.concat(accessedRoutes)
|
routes.value = constantRoutes.concat(accessedRoutes)
|
||||||
dynamicRoutes.value = accessedRoutes
|
dynamicRoutes.value = accessedRoutes
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user