新增:新增功能权限适配及校验

1.后端 API 注解鉴权使用方式:@SaCheckPermission("system:user:add")
2.前端全局指令函数使用方式:v-permission="['system:user:add']"
3.前端权限判断函数使用方式:checkPermission(['system:user:add'])
This commit is contained in:
2023-03-02 23:39:22 +08:00
parent 843cac4e54
commit 94be1f9553
51 changed files with 548 additions and 149 deletions

View File

@@ -0,0 +1,32 @@
import { useLoginStore } from '@/store';
/**
* 权限判断
*
* @param value 权限码列表
* @return true 有权限false 没有权限
*/
export default function checkPermission(value: Array<string>) {
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);
});
return hasPermission || hasRole;
}
throw new Error(
`need roles! Like v-permission="['admin','system:user:add']"`
);
}