refactor(permission): ♻️ 优化路由权限模块,简化命名和职责分离

This commit is contained in:
Ray.Hao
2025-09-25 16:35:53 +08:00
parent 3ddf428922
commit 0e3550b5f8
4 changed files with 63 additions and 99 deletions

View File

@@ -1,5 +1,5 @@
import { Storage } from "./storage";
import { AUTH_KEYS } from "@/constants";
import { AUTH_KEYS, ROLE_ROOT } from "@/constants";
import { useUserStoreHook } from "@/store/modules/user-store";
import router from "@/router";
@@ -44,6 +44,23 @@ export const AuthStorage = {
},
};
/**
* 权限判断
*/
export function hasPerm(value: string | string[], type: "button" | "role" = "button"): boolean {
const { roles, perms } = useUserStoreHook().userInfo;
// 超级管理员拥有所有权限
if (type === "button" && roles.includes(ROLE_ROOT)) {
return true;
}
const auths = type === "button" ? perms : roles;
return typeof value === "string"
? auths.includes(value)
: value.some((perm) => auths.includes(perm));
}
/**
* 重定向到登录页面
*/