mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-12 06:57:13 +08:00
1.后端 API 注解鉴权使用方式:@SaCheckPermission("system:user:add") 2.前端全局指令函数使用方式:v-permission="['system:user:add']" 3.前端权限判断函数使用方式:checkPermission(['system:user:add'])
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { DirectiveBinding } from 'vue';
|
|
import { useLoginStore } from '@/store';
|
|
|
|
function checkPermission(el: HTMLElement, binding: DirectiveBinding) {
|
|
const { value } = binding;
|
|
const loginStore = useLoginStore();
|
|
const { permissions, roles } = loginStore;
|
|
const superAdmin = 'admin';
|
|
const allPermission = '*';
|
|
|
|
if (Array.isArray(value) && value.length > 0) {
|
|
const permissionValues = value;
|
|
// 校验权限码
|
|
const hasPermission = permissions.some((permission: string) => {
|
|
return (
|
|
allPermission === permission || permissionValues.includes(permission)
|
|
);
|
|
});
|
|
// 检验角色编码
|
|
const hasRole = roles.some((role: string) => {
|
|
return superAdmin === role || permissionValues.includes(role);
|
|
});
|
|
// 如果没有权限,移除元素
|
|
if (!hasPermission && !hasRole && el.parentNode) {
|
|
el.parentNode.removeChild(el);
|
|
}
|
|
} else {
|
|
throw new Error(
|
|
`need roles! Like v-permission="['admin','system:user:add']"`
|
|
);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
mounted(el: HTMLElement, binding: DirectiveBinding) {
|
|
checkPermission(el, binding);
|
|
},
|
|
updated(el: HTMLElement, binding: DirectiveBinding) {
|
|
checkPermission(el, binding);
|
|
},
|
|
};
|