feat(@vben/common-ui): 添加权限控制功能

This commit is contained in:
Kven 2025-05-30 10:02:42 +08:00
parent d3c25dd899
commit 250b319c89
3 changed files with 25 additions and 11 deletions

View File

@ -11,9 +11,12 @@ import { objectOmit } from '@vueuse/core';
import { Button, Image, Popconfirm, Switch, Tag } from 'ant-design-vue';
import { $t } from '#/locales';
import { useAuthStore } from '#/store';
import { useVbenForm } from './form';
const authStore = useAuthStore();
setupVbenVxeTable({
configVxeTable: (vxeUI) => {
vxeUI.setConfig({
@ -162,7 +165,11 @@ setupVbenVxeTable({
});
return optBtn;
})
.filter((opt) => opt.show !== false);
.filter(
(opt) =>
opt.show !== false &&
(!opt.permission || authStore.hasPerm(opt.permission)),
);
function renderBtn(opt: Recordable<any>, listen = true) {
return h(

View File

@ -134,6 +134,12 @@ export const useAuthStore = defineStore('auth', () => {
loginLoading.value = false;
}
// 👇 添加 hasPerm 方法
function hasPerm(permission: string): boolean {
const userInfo = userStore.userInfo;
return Boolean(userInfo?.permissions?.includes(permission));
}
return {
$reset,
authLogin,
@ -141,5 +147,6 @@ export const useAuthStore = defineStore('auth', () => {
loginLoading,
logout,
csrfToken,
hasPerm,
};
});

View File

@ -186,17 +186,17 @@ export function useColumns<T = UserApi.User>(
nameField: 'name',
nameTitle: '用户',
onClick: onActionClick,
options: [
{
code: 'edit',
text: '修改',
},
{
code: 'delete',
text: '删除',
},
],
},
options: [
{
code: 'edit',
text: '修改',
},
{
code: 'delete',
text: '删除',
},
],
name: 'CellOperation',
},
field: 'operation',