mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 19:09:19 +08:00
feat: 按钮级权限复用 /auth/codes,新增 PermissionGate/v-permission 与操作列无权限整列隐藏
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { MenuDTO, MenuVO } from '#/api/sys/menu/types';
|
||||
|
||||
import { ref } from 'vue';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
|
||||
@@ -15,10 +15,17 @@ import {
|
||||
deleteMenuApi,
|
||||
getAllMenusApi,
|
||||
} from '#/api/sys/menu';
|
||||
import type { VxeRowContextMenuItem } from '#/components/vxe-row-context-menu/types';
|
||||
import {
|
||||
useVxeRowContextMenu,
|
||||
VxeRowContextMenu,
|
||||
} from '#/components/vxe-row-context-menu';
|
||||
import { PermissionGate } from '#/components/permission';
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
import {
|
||||
syncVxeGridColumns,
|
||||
useActionColumnVisible,
|
||||
} from '#/composables/use-vxe-action-column';
|
||||
|
||||
import MenuFormModal from './components/MenuFormModal.vue';
|
||||
|
||||
@@ -31,6 +38,15 @@ const PlusCircleIcon = createIconifyIcon('lucide:plus-circle');
|
||||
const ExpandIcon = createIconifyIcon('lucide:chevrons-down');
|
||||
const CollapseIcon = createIconifyIcon('lucide:chevrons-up');
|
||||
|
||||
const { hasPermission } = usePermission();
|
||||
|
||||
const menuActionCodes = [
|
||||
'sys:menu:create',
|
||||
'sys:menu:update',
|
||||
'sys:menu:delete',
|
||||
];
|
||||
const showActionColumn = useActionColumnVisible(menuActionCodes);
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const menuFormModalRef = ref();
|
||||
@@ -50,82 +66,90 @@ function sameMenuParent(a: unknown, b: unknown) {
|
||||
return normalizeMenuParentId(a) === normalizeMenuParentId(b);
|
||||
}
|
||||
|
||||
const menuTableBaseColumns: VxeGridProps<any>['columns'] = [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '菜单名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'routeName',
|
||||
title: '路由名称',
|
||||
minWidth: 140,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'title',
|
||||
title: '菜单标题',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'icon',
|
||||
title: '图标',
|
||||
width: 72,
|
||||
align: 'center' as const,
|
||||
slots: { default: 'icon' },
|
||||
},
|
||||
{
|
||||
field: 'path',
|
||||
title: '路由路径',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'component',
|
||||
title: '页面组件',
|
||||
minWidth: 180,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '权限列表',
|
||||
minWidth: 200,
|
||||
slots: { default: 'permissions' },
|
||||
},
|
||||
{
|
||||
title: '类型',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuType' },
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuStatus' },
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
];
|
||||
|
||||
const menuActionColumn: NonNullable<VxeGridProps<any>['columns']>[number] = {
|
||||
title: '操作',
|
||||
minWidth: 280,
|
||||
fixed: 'right' as const,
|
||||
showOverflow: false,
|
||||
slots: { default: 'action' },
|
||||
};
|
||||
|
||||
const menuTableColumns = computed(() =>
|
||||
showActionColumn.value
|
||||
? [...(menuTableBaseColumns ?? []), menuActionColumn]
|
||||
: (menuTableBaseColumns ?? []),
|
||||
);
|
||||
|
||||
// VbenVxeGrid 表格配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '菜单名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true, // 启用树节点显示
|
||||
dragSort: true, // 启用拖拽排序
|
||||
// 移除自定义插槽,让拖拽控件正常显示
|
||||
},
|
||||
{
|
||||
field: 'routeName',
|
||||
title: '路由名称',
|
||||
minWidth: 140,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'title',
|
||||
title: '菜单标题',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'icon',
|
||||
title: '图标',
|
||||
width: 72,
|
||||
align: 'center' as const,
|
||||
slots: { default: 'icon' },
|
||||
},
|
||||
{
|
||||
field: 'path',
|
||||
title: '路由路径',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'component',
|
||||
title: '页面组件',
|
||||
minWidth: 180,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '权限列表',
|
||||
minWidth: 200,
|
||||
slots: { default: 'permissions' },
|
||||
},
|
||||
{
|
||||
title: '类型',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuType' },
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuStatus' },
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 280,
|
||||
fixed: 'right' as const,
|
||||
showOverflow: false,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
columns: menuTableColumns.value,
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
@@ -274,24 +298,32 @@ const {
|
||||
row: ctxMenuRow,
|
||||
items: ctxMenuItems,
|
||||
onCellMenu,
|
||||
} = useVxeRowContextMenu<MenuVO.MenuInfo>((_row) => [
|
||||
{
|
||||
key: 'addChild',
|
||||
label: '新增',
|
||||
icon: PlusCircleIcon,
|
||||
},
|
||||
{
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
},
|
||||
]);
|
||||
} = useVxeRowContextMenu<MenuVO.MenuInfo>(() => {
|
||||
const items: VxeRowContextMenuItem[] = [];
|
||||
if (hasPermission('sys:menu:create')) {
|
||||
items.push({
|
||||
key: 'addChild',
|
||||
label: '新增',
|
||||
icon: PlusCircleIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:menu:update')) {
|
||||
items.push({
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:menu:delete')) {
|
||||
items.push({
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
|
||||
function handleCtxMenuSelect(key: string) {
|
||||
const row = ctxMenuRow.value;
|
||||
@@ -429,6 +461,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
cellMenu: onCellMenu,
|
||||
} as any,
|
||||
});
|
||||
|
||||
syncVxeGridColumns(gridApi, menuTableColumns);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -438,10 +472,12 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
<!-- 工具栏工具 -->
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加菜单
|
||||
</Button>
|
||||
<PermissionGate code="sys:menu:create">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加菜单
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<Button @click="expandAllTree">
|
||||
<ExpandIcon />
|
||||
全部展开
|
||||
@@ -506,25 +542,31 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
</template>
|
||||
|
||||
<!-- 操作列 -->
|
||||
<template #action="{ row }: { row: any }">
|
||||
<template v-if="showActionColumn" #action="{ row }: { row: any }">
|
||||
<Space size="small" :wrap="false">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定要删除这个菜单吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
<PermissionGate code="sys:menu:create">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:menu:update">
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:menu:delete">
|
||||
<Popconfirm
|
||||
title="确定要删除这个菜单吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { PermissionVO } from '#/api/sys/permission';
|
||||
|
||||
import { nextTick, ref } from 'vue';
|
||||
import { computed, nextTick, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
@@ -15,10 +15,17 @@ import {
|
||||
deletePermissionApi,
|
||||
getSystemPermissionTreeApi,
|
||||
} from '#/api/sys/permission';
|
||||
import type { VxeRowContextMenuItem } from '#/components/vxe-row-context-menu/types';
|
||||
import {
|
||||
useVxeRowContextMenu,
|
||||
VxeRowContextMenu,
|
||||
} from '#/components/vxe-row-context-menu';
|
||||
import { PermissionGate } from '#/components/permission';
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
import {
|
||||
syncVxeGridColumns,
|
||||
useActionColumnVisible,
|
||||
} from '#/composables/use-vxe-action-column';
|
||||
|
||||
import PermissionFormModal from './components/PermissionFormModal.vue';
|
||||
|
||||
@@ -31,6 +38,15 @@ const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
const UnfoldIcon = createIconifyIcon('lucide:unfold-vertical');
|
||||
const FoldIcon = createIconifyIcon('lucide:fold-vertical');
|
||||
|
||||
const { hasPermission } = usePermission();
|
||||
|
||||
const permissionActionCodes = [
|
||||
'sys:permission:create',
|
||||
'sys:permission:update',
|
||||
'sys:permission:delete',
|
||||
];
|
||||
const showActionColumn = useActionColumnVisible(permissionActionCodes);
|
||||
|
||||
const loading = ref(false);
|
||||
const permissionFormModalRef = ref();
|
||||
|
||||
@@ -47,62 +63,72 @@ function flattenTree(
|
||||
return result;
|
||||
}
|
||||
|
||||
const permissionTableBaseColumns: VxeGridProps<any>['columns'] = [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'code',
|
||||
title: '权限码',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'type',
|
||||
title: '权限类型',
|
||||
minWidth: 120,
|
||||
slots: { default: 'type' },
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 80,
|
||||
slots: { default: 'status' },
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '备注',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
];
|
||||
|
||||
const permissionActionColumn: NonNullable<VxeGridProps<any>['columns']>[number] =
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 260,
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
};
|
||||
|
||||
const permissionTableColumns = computed(() =>
|
||||
showActionColumn.value
|
||||
? [...(permissionTableBaseColumns ?? []), permissionActionColumn]
|
||||
: (permissionTableBaseColumns ?? []),
|
||||
);
|
||||
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'code',
|
||||
title: '权限码',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'type',
|
||||
title: '权限类型',
|
||||
minWidth: 120,
|
||||
slots: { default: 'type' },
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 80,
|
||||
slots: { default: 'status' },
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '备注',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 260,
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
columns: permissionTableColumns.value,
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
@@ -308,24 +334,32 @@ const {
|
||||
row: ctxMenuRow,
|
||||
items: ctxMenuItems,
|
||||
onCellMenu,
|
||||
} = useVxeRowContextMenu<PermissionVO.PermissionInfoVO>(() => [
|
||||
{
|
||||
key: 'addChild',
|
||||
label: '增加下级',
|
||||
icon: PlusCircleIcon,
|
||||
},
|
||||
{
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
},
|
||||
]);
|
||||
} = useVxeRowContextMenu<PermissionVO.PermissionInfoVO>(() => {
|
||||
const items: VxeRowContextMenuItem[] = [];
|
||||
if (hasPermission('sys:permission:create')) {
|
||||
items.push({
|
||||
key: 'addChild',
|
||||
label: '增加下级',
|
||||
icon: PlusCircleIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:permission:update')) {
|
||||
items.push({
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:permission:delete')) {
|
||||
items.push({
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
|
||||
function handleCtxMenuSelect(key: string) {
|
||||
const row = ctxMenuRow.value;
|
||||
@@ -355,6 +389,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
} as any,
|
||||
});
|
||||
|
||||
syncVxeGridColumns(gridApi, permissionTableColumns);
|
||||
|
||||
async function handleModalSuccess(payload?: { parentId?: null | number }) {
|
||||
const extra =
|
||||
payload?.parentId != null ? [payload.parentId] : ([] as number[]);
|
||||
@@ -387,10 +423,12 @@ const getTypeColor = (type: string) => {
|
||||
<Grid class="permission-grid">
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加权限
|
||||
</Button>
|
||||
<PermissionGate code="sys:permission:create">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加权限
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<Button @click="expandAllTree">
|
||||
<UnfoldIcon class="h-4 w-4" />
|
||||
全部展开
|
||||
@@ -414,25 +452,31 @@ const getTypeColor = (type: string) => {
|
||||
</Tag>
|
||||
</template>
|
||||
|
||||
<template #action="{ row }">
|
||||
<template v-if="showActionColumn" #action="{ row }">
|
||||
<Space size="small" :wrap="false">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
<PermissionGate code="sys:permission:create">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:permission:update">
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:permission:delete">
|
||||
<Popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
Reference in New Issue
Block a user