mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-10-02 10:57:10 +08:00
新增:新增功能权限适配及校验
1.后端 API 注解鉴权使用方式:@SaCheckPermission("system:user:add") 2.前端全局指令函数使用方式:v-permission="['system:user:add']" 3.前端权限判断函数使用方式:checkPermission(['system:user:add'])
This commit is contained in:
@@ -4,19 +4,30 @@ import { useLoginStore } from '@/store';
|
||||
function checkPermission(el: HTMLElement, binding: DirectiveBinding) {
|
||||
const { value } = binding;
|
||||
const loginStore = useLoginStore();
|
||||
const { role } = loginStore;
|
||||
const { permissions, roles } = loginStore;
|
||||
const superAdmin = 'admin';
|
||||
const allPermission = '*';
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length > 0) {
|
||||
const permissionValues = value;
|
||||
|
||||
const hasPermission = permissionValues.includes(role);
|
||||
if (!hasPermission && el.parentNode) {
|
||||
el.parentNode.removeChild(el);
|
||||
}
|
||||
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','user']"`);
|
||||
throw new Error(
|
||||
`need roles! Like v-permission="['admin','system:user:add']"`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user