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 { Button, Image, Popconfirm, Switch, Tag } from 'ant-design-vue';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { useAuthStore } from '#/store';
import { useVbenForm } from './form'; import { useVbenForm } from './form';
const authStore = useAuthStore();
setupVbenVxeTable({ setupVbenVxeTable({
configVxeTable: (vxeUI) => { configVxeTable: (vxeUI) => {
vxeUI.setConfig({ vxeUI.setConfig({
@ -162,7 +165,11 @@ setupVbenVxeTable({
}); });
return optBtn; 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) { function renderBtn(opt: Recordable<any>, listen = true) {
return h( return h(

View File

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

View File

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