feat: 移除mock并接入真实权限控制
This commit is contained in:
+7
-1
@@ -173,7 +173,13 @@ router.beforeEach(async (to: ToRouteType, _from, next) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasRoutePermission(to.meta, useUserStoreHook().permissions ?? [])) {
|
||||
if (
|
||||
!hasRoutePermission(
|
||||
to.meta,
|
||||
useUserStoreHook().permissions ?? [],
|
||||
useUserStoreHook().permissionMenus ?? []
|
||||
)
|
||||
) {
|
||||
next({ path: "/access-denied" });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ const Layout = () => import("@/layout/index.vue");
|
||||
/**
|
||||
* 权限管理业务菜单。
|
||||
*
|
||||
* 三个子页面都是静态路由,菜单展示顺序由这里的 children 决定;
|
||||
* 子页面都是静态路由,菜单展示顺序由这里的 children 决定;
|
||||
* 默认访问该模块时进入员工管理,保证和登录/登出后的主工作流一致。
|
||||
*/
|
||||
export default {
|
||||
@@ -49,6 +49,17 @@ export default {
|
||||
permission: ["employee:view:all", "employee:view:store"],
|
||||
keepAlive: true
|
||||
}
|
||||
},
|
||||
{
|
||||
path: "/permissions",
|
||||
name: "PermissionPolicies",
|
||||
component: () => import("@/views/permissions/index.vue"),
|
||||
meta: {
|
||||
title: "权限策略",
|
||||
menuKey: "permissions",
|
||||
permission: "permission:view",
|
||||
keepAlive: true
|
||||
}
|
||||
}
|
||||
]
|
||||
} satisfies RouteConfigsTable;
|
||||
|
||||
+33
-141
@@ -9,25 +9,16 @@ import { router } from "./index";
|
||||
import { isProxy, toRaw } from "vue";
|
||||
import { useTimeoutFn } from "@vueuse/core";
|
||||
import {
|
||||
isString,
|
||||
cloneDeep,
|
||||
isAllEmpty,
|
||||
intersection,
|
||||
storageLocal,
|
||||
isIncludeAllChildren
|
||||
storageLocal
|
||||
} from "@pureadmin/utils";
|
||||
import { getConfig } from "@/config";
|
||||
import { buildHierarchyTree } from "@/utils/tree";
|
||||
import { userKey, type DataInfo } from "@/utils/auth";
|
||||
import { type menuType, routerArrays } from "@/layout/types";
|
||||
import { userKey, type DataInfo, type PermissionMenuInfo } from "@/utils/auth";
|
||||
import type { menuType } from "@/layout/types";
|
||||
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
|
||||
import { usePermissionStoreHook } from "@/store/modules/permission";
|
||||
const IFrame = () => import("@/layout/frame.vue");
|
||||
// https://cn.vitejs.dev/guide/features.html#glob-import
|
||||
const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}");
|
||||
|
||||
// 动态路由
|
||||
import { getAsyncRoutes } from "@/api/routes";
|
||||
|
||||
function handRank(routeInfo: any) {
|
||||
const { name, path, parentId, meta } = routeInfo;
|
||||
@@ -81,26 +72,49 @@ function isOneOfArray(a: Array<string>, b: Array<string>) {
|
||||
: true;
|
||||
}
|
||||
|
||||
function hasRoutePermission(meta: CustomizeRouteMeta, permissions: string[]) {
|
||||
const required = meta?.permission;
|
||||
function hasWildcardPermission(permissions: string[]) {
|
||||
return permissions.includes("*") || permissions.includes("*:*:*");
|
||||
}
|
||||
|
||||
if (!required) return true;
|
||||
if (permissions.includes("*") || permissions.includes("*:*:*")) return true;
|
||||
function hasMenuAccess(
|
||||
meta: CustomizeRouteMeta,
|
||||
permissions: string[],
|
||||
permissionMenus: PermissionMenuInfo[] = []
|
||||
) {
|
||||
if (!meta?.menuKey) return true;
|
||||
if (hasWildcardPermission(permissions)) return true;
|
||||
|
||||
return permissionMenus.some(menu => menu.key === meta.menuKey);
|
||||
}
|
||||
|
||||
function hasRoutePermission(
|
||||
meta: CustomizeRouteMeta,
|
||||
permissions: string[],
|
||||
permissionMenus: PermissionMenuInfo[] = []
|
||||
) {
|
||||
const required = meta?.permission;
|
||||
const hasMenu = hasMenuAccess(meta, permissions, permissionMenus);
|
||||
|
||||
if (!required) return hasMenu;
|
||||
if (hasWildcardPermission(permissions)) return true;
|
||||
|
||||
const requiredList = Array.isArray(required) ? required : [required];
|
||||
|
||||
return requiredList.some(permission => permissions.includes(permission));
|
||||
return (
|
||||
hasMenu && requiredList.some(permission => permissions.includes(permission))
|
||||
);
|
||||
}
|
||||
|
||||
/** 从localStorage里取出当前登录用户权限码,过滤无权限菜单 */
|
||||
/** 从 localStorage 取出后端菜单和权限码,过滤无权限菜单 */
|
||||
function filterNoPermissionTree(data: RouteComponent[]) {
|
||||
const userInfo = storageLocal().getItem<DataInfo<number>>(userKey);
|
||||
const currentRoles = userInfo?.roles ?? [];
|
||||
const currentPermissions = userInfo?.permissions ?? [];
|
||||
const currentMenus = userInfo?.permissionMenus ?? [];
|
||||
const newTree = cloneDeep(data).filter((v: any) => {
|
||||
return (
|
||||
isOneOfArray(v.meta?.roles, currentRoles) &&
|
||||
hasRoutePermission(v.meta, currentPermissions)
|
||||
hasRoutePermission(v.meta, currentPermissions, currentMenus)
|
||||
);
|
||||
});
|
||||
newTree.forEach(
|
||||
@@ -169,78 +183,6 @@ function addPathMatch() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 处理动态路由(后端返回的路由) */
|
||||
function handleAsyncRoutes(routeList) {
|
||||
if (routeList.length === 0) {
|
||||
usePermissionStoreHook().handleWholeMenus(routeList);
|
||||
} else {
|
||||
formatFlatteningRoutes(addAsyncRoutes(routeList)).map(
|
||||
(v: RouteRecordRaw) => {
|
||||
// 防止重复添加路由
|
||||
if (
|
||||
router.options.routes[0].children.findIndex(
|
||||
value => value.path === v.path
|
||||
) !== -1
|
||||
) {
|
||||
return;
|
||||
} else {
|
||||
// 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
|
||||
router.options.routes[0].children.push(v);
|
||||
// 最终路由进行升序
|
||||
ascending(router.options.routes[0].children);
|
||||
if (!router.hasRoute(v?.name)) router.addRoute(v);
|
||||
const flattenRouters: any = router
|
||||
.getRoutes()
|
||||
.find(n => n.path === "/");
|
||||
// 保持router.options.routes[0].children与path为"/"的children一致,防止数据不一致导致异常
|
||||
flattenRouters.children = router.options.routes[0].children;
|
||||
router.addRoute(flattenRouters);
|
||||
}
|
||||
}
|
||||
);
|
||||
usePermissionStoreHook().handleWholeMenus(routeList);
|
||||
}
|
||||
if (!useMultiTagsStoreHook().getMultiTagsCache) {
|
||||
useMultiTagsStoreHook().handleTags("equal", [
|
||||
...routerArrays,
|
||||
...usePermissionStoreHook().flatteningRoutes.filter(
|
||||
v => v?.meta?.fixedTag
|
||||
)
|
||||
]);
|
||||
}
|
||||
addPathMatch();
|
||||
}
|
||||
|
||||
/** 初始化路由(`new Promise` 写法防止在异步请求中造成无限循环)*/
|
||||
function initRouter() {
|
||||
if (getConfig()?.CachingAsyncRoutes) {
|
||||
// 开启动态路由缓存本地localStorage
|
||||
const key = "async-routes";
|
||||
const asyncRouteList = storageLocal().getItem(key) as any;
|
||||
if (asyncRouteList && asyncRouteList?.length > 0) {
|
||||
return new Promise(resolve => {
|
||||
handleAsyncRoutes(asyncRouteList);
|
||||
resolve(router);
|
||||
});
|
||||
} else {
|
||||
return new Promise(resolve => {
|
||||
getAsyncRoutes().then(({ data }) => {
|
||||
handleAsyncRoutes(cloneDeep(data));
|
||||
storageLocal().setItem(key, data);
|
||||
resolve(router);
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return new Promise(resolve => {
|
||||
getAsyncRoutes().then(({ data }) => {
|
||||
handleAsyncRoutes(cloneDeep(data));
|
||||
resolve(router);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将多级嵌套路由处理成一维数组
|
||||
* @param routesList 传入路由
|
||||
@@ -320,35 +262,6 @@ function handleAliveRoute({ name }: ToRouteType, mode?: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 过滤后端传来的动态路由 重新生成规范路由 */
|
||||
function addAsyncRoutes(arrRoutes: Array<RouteRecordRaw>) {
|
||||
if (!arrRoutes || !arrRoutes.length) return;
|
||||
const modulesRoutesKeys = Object.keys(modulesRoutes);
|
||||
arrRoutes.forEach((v: RouteRecordRaw) => {
|
||||
// 将backstage属性加入meta,标识此路由为后端返回路由
|
||||
v.meta.backstage = true;
|
||||
// 父级的redirect属性取值:如果子级存在且父级的redirect属性不存在,默认取第一个子级的path;如果子级存在且父级的redirect属性存在,取存在的redirect属性,会覆盖默认值
|
||||
if (v?.children && v.children.length && !v.redirect)
|
||||
v.redirect = v.children[0].path;
|
||||
// 父级的name属性取值:如果子级存在且父级的name属性不存在,默认取第一个子级的name;如果子级存在且父级的name属性存在,取存在的name属性,会覆盖默认值(注意:测试中发现父级的name不能和子级name重复,如果重复会造成重定向无效(跳转404),所以这里给父级的name起名的时候后面会自动加上`Parent`,避免重复)
|
||||
if (v?.children && v.children.length && !v.name)
|
||||
v.name = (v.children[0].name as string) + "Parent";
|
||||
if (v.meta?.frameSrc) {
|
||||
v.component = IFrame;
|
||||
} else {
|
||||
// 对后端传component组件路径和不传做兼容(如果后端传component组件路径,那么path可以随便写,如果不传,component组件路径会跟path保持一致)
|
||||
const index = v?.component
|
||||
? modulesRoutesKeys.findIndex(ev => ev.includes(v.component as any))
|
||||
: modulesRoutesKeys.findIndex(ev => ev.includes(v.path));
|
||||
v.component = modulesRoutes[modulesRoutesKeys[index]];
|
||||
}
|
||||
if (v?.children && v.children.length) {
|
||||
addAsyncRoutes(v.children);
|
||||
}
|
||||
});
|
||||
return arrRoutes;
|
||||
}
|
||||
|
||||
/** 获取路由历史模式 https://next.router.vuejs.org/zh/guide/essentials/history-mode.html */
|
||||
function getHistoryMode(routerHistory): RouterHistory {
|
||||
// len为1 代表只有历史模式 为2 代表历史模式中存在base参数 https://next.router.vuejs.org/zh/api/#%E5%8F%82%E6%95%B0-1
|
||||
@@ -372,23 +285,6 @@ function getHistoryMode(routerHistory): RouterHistory {
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取当前页面按钮级别的权限 */
|
||||
function getAuths(): Array<string> {
|
||||
return router.currentRoute.value.meta.auths as Array<string>;
|
||||
}
|
||||
|
||||
/** 是否有按钮级别的权限(根据路由`meta`中的`auths`字段进行判断)*/
|
||||
function hasAuth(value: string | Array<string>): boolean {
|
||||
if (!value) return false;
|
||||
/** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
|
||||
const metaAuths = getAuths();
|
||||
if (!metaAuths) return false;
|
||||
const isAuths = isString(value)
|
||||
? metaAuths.includes(value)
|
||||
: isIncludeAllChildren(value, metaAuths);
|
||||
return isAuths ? true : false;
|
||||
}
|
||||
|
||||
function handleTopMenu(route) {
|
||||
if (route?.children && route.children.length > 1) {
|
||||
if (route.redirect) {
|
||||
@@ -411,17 +307,13 @@ function getTopMenu(tag = false): menuType {
|
||||
}
|
||||
|
||||
export {
|
||||
hasAuth,
|
||||
getAuths,
|
||||
ascending,
|
||||
filterTree,
|
||||
initRouter,
|
||||
getTopMenu,
|
||||
addPathMatch,
|
||||
isOneOfArray,
|
||||
hasRoutePermission,
|
||||
getHistoryMode,
|
||||
addAsyncRoutes,
|
||||
getParentPaths,
|
||||
findRouteByPath,
|
||||
handleAliveRoute,
|
||||
|
||||
Reference in New Issue
Block a user