2024-11-18 19:40:44 +08:00
|
|
|
|
import type { RouteRecordRaw } from "vue-router"
|
2024-11-21 21:03:09 +08:00
|
|
|
|
import { pinia } from "@/pinia"
|
2024-02-25 19:26:35 +08:00
|
|
|
|
import { constantRoutes, dynamicRoutes } from "@/router"
|
2024-11-26 14:45:42 +08:00
|
|
|
|
import { routerConfig } from "@/router/config"
|
2023-08-07 15:14:15 +08:00
|
|
|
|
import { flatMultiLevelRoutes } from "@/router/helper"
|
2022-04-21 18:20:39 +08:00
|
|
|
|
|
2024-11-18 19:40:44 +08:00
|
|
|
|
function hasPermission(roles: string[], route: RouteRecordRaw) {
|
2023-05-24 18:25:49 +08:00
|
|
|
|
const routeRoles = route.meta?.roles
|
2024-11-18 19:40:44 +08:00
|
|
|
|
return routeRoles ? roles.some(role => routeRoles.includes(role)) : true
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 19:40:44 +08:00
|
|
|
|
function filterDynamicRoutes(routes: RouteRecordRaw[], roles: string[]) {
|
2022-04-21 18:20:39 +08:00
|
|
|
|
const res: RouteRecordRaw[] = []
|
|
|
|
|
routes.forEach((route) => {
|
2023-05-24 18:25:49 +08:00
|
|
|
|
const tempRoute = { ...route }
|
|
|
|
|
if (hasPermission(roles, tempRoute)) {
|
|
|
|
|
if (tempRoute.children) {
|
2024-02-25 19:26:35 +08:00
|
|
|
|
tempRoute.children = filterDynamicRoutes(tempRoute.children, roles)
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
2023-05-24 18:25:49 +08:00
|
|
|
|
res.push(tempRoute)
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 16:52:01 +08:00
|
|
|
|
export const usePermissionStore = defineStore("permission", () => {
|
2024-11-25 14:21:18 +08:00
|
|
|
|
// 可访问的路由
|
2022-08-24 16:52:01 +08:00
|
|
|
|
const routes = ref<RouteRecordRaw[]>([])
|
2024-11-25 17:26:27 +08:00
|
|
|
|
|
2024-11-25 14:21:18 +08:00
|
|
|
|
// 有访问权限的动态路由
|
2024-02-25 19:26:35 +08:00
|
|
|
|
const addRoutes = ref<RouteRecordRaw[]>([])
|
2022-08-24 16:52:01 +08:00
|
|
|
|
|
2024-11-25 14:21:18 +08:00
|
|
|
|
// 根据角色生成可访问的 Routes(可访问的路由 = 常驻路由 + 有访问权限的动态路由)
|
2022-08-24 16:52:01 +08:00
|
|
|
|
const setRoutes = (roles: string[]) => {
|
2024-02-25 19:26:35 +08:00
|
|
|
|
const accessedRoutes = filterDynamicRoutes(dynamicRoutes, roles)
|
2024-11-25 19:02:36 +08:00
|
|
|
|
set(accessedRoutes)
|
2024-02-06 20:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 14:21:18 +08:00
|
|
|
|
// 所有路由 = 所有常驻路由 + 所有动态路由
|
2024-02-06 20:22:07 +08:00
|
|
|
|
const setAllRoutes = () => {
|
2024-11-25 19:02:36 +08:00
|
|
|
|
set(dynamicRoutes)
|
2024-02-06 20:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 19:02:36 +08:00
|
|
|
|
// 统一设置
|
|
|
|
|
const set = (accessedRoutes: RouteRecordRaw[]) => {
|
2022-08-24 16:52:01 +08:00
|
|
|
|
routes.value = constantRoutes.concat(accessedRoutes)
|
2024-11-26 14:45:42 +08:00
|
|
|
|
addRoutes.value = routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(accessedRoutes) : accessedRoutes
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
2022-08-24 16:52:01 +08:00
|
|
|
|
|
2024-02-25 19:26:35 +08:00
|
|
|
|
return { routes, addRoutes, setRoutes, setAllRoutes }
|
2022-04-21 18:20:39 +08:00
|
|
|
|
})
|
|
|
|
|
|
2024-11-14 17:33:12 +08:00
|
|
|
|
/**
|
2024-11-25 14:21:18 +08:00
|
|
|
|
* @description 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
|
|
|
|
* @description 在 SSR 应用中可用于在 setup 外使用 store
|
2024-11-14 17:33:12 +08:00
|
|
|
|
*/
|
2024-11-21 20:41:48 +08:00
|
|
|
|
export function usePermissionStoreOutside() {
|
2024-11-14 17:33:12 +08:00
|
|
|
|
return usePermissionStore(pinia)
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|