refactor(auth): 权限判断与会话失效跳转收敛到 user store
- src/utils/auth: hasPerm 简化为纯权限判断,移除 redirectToLogin 与路由依赖 - src/directives/permission: v-hasPerm 复用 utils/auth 的 hasPerm - src/utils/request: token 失效改走 userStore.redirectToLogin - src/views/profile: 改密后重新登录改走 userStore.redirectToLogin
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import type { Directive, DirectiveBinding } from "vue";
|
||||
|
||||
import { useUserStore } from "@/stores";
|
||||
import { ROLE_ROOT } from "@/constants";
|
||||
import { hasPerm as hasPermFn } from "@/utils/auth";
|
||||
|
||||
/**
|
||||
* 按钮权限
|
||||
* 按钮权限(复用 utils/auth 的 hasPerm 函数,保持指令与函数行为一致)
|
||||
*/
|
||||
export const hasPerm: Directive = {
|
||||
mounted(el: HTMLElement, binding: DirectiveBinding) {
|
||||
@@ -17,20 +17,8 @@ export const hasPerm: Directive = {
|
||||
);
|
||||
}
|
||||
|
||||
const { roles, perms } = useUserStore().userInfo;
|
||||
|
||||
// 超级管理员拥有所有权限,如果是"*:*:*"权限标识,则不需要进行权限校验
|
||||
if (roles.includes(ROLE_ROOT) || requiredPerms.includes("*:*:*")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
const hasAuth = Array.isArray(requiredPerms)
|
||||
? requiredPerms.some((perm) => perms.includes(perm))
|
||||
: perms.includes(requiredPerms);
|
||||
|
||||
// 如果没有权限,移除该元素
|
||||
if (!hasAuth && el.parentNode) {
|
||||
if (!hasPermFn(requiredPerms) && el.parentNode) {
|
||||
el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user