refactor: ♻️ 抽离权限判断代码

This commit is contained in:
cshaptx4869
2024-05-17 10:57:20 +08:00
parent 5dd0185ac3
commit 02b2cfc653
2 changed files with 27 additions and 26 deletions

View File

@@ -4,6 +4,24 @@ import NProgress from "@/utils/nprogress";
import { RouteRecordRaw } from "vue-router";
import { TOKEN_KEY } from "@/enums/CacheEnum";
// 是否有权限
export function hasAuth(
value: string | string[],
type: "button" | "role" = "button"
) {
const { roles, perms } = useUserStore().user;
//「超级管理员」拥有所有的按钮权限
if (type === "button" && roles.includes("ROOT")) {
return true;
}
const auths = type === "button" ? perms : roles;
return typeof value === "string"
? auths.includes(value)
: auths.some((perm) => {
return value.includes(perm);
});
}
export function setupPermission() {
// 白名单路由
const whiteList = ["/login"];