Skip to content

Commit

Permalink
feat: ✨ 页签title支持动态设置
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxianrui committed Jun 8, 2024
1 parent 4f80c68 commit 23876aa
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/plugins/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@ 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"];
Expand All @@ -43,6 +25,13 @@ export function setupPermission() {
if (to.matched.length === 0) {
from.name ? next({ name: from.name }) : next("/404");
} else {
// 如果路由参数中有 title,覆盖路由元信息中的 title
const title =
(to.params.title as string) || (to.query.title as string);
if (title) {
to.meta.title = title;
}

next();
}
} else {
Expand Down Expand Up @@ -77,3 +66,21 @@ export function setupPermission() {
NProgress.done();
});
}

// 是否有权限
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);
});
}

0 comments on commit 23876aa

Please sign in to comment.