reactor:【system 系统管理】menu 进一步统一代码风格(暂时只包括 element-plus)

This commit is contained in:
YunaiV
2025-09-08 13:12:57 +08:00
parent 96dadf9971
commit 3564c123a1
4 changed files with 131 additions and 94 deletions

View File

@@ -1,20 +1,18 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemMenuApi } from '#/api/system/menu';
import { ref } from 'vue';
import { DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { SystemMenuTypeEnum } from '@vben/constants';
import { IconifyIcon, Plus } from '@vben/icons';
import { IconifyIcon } from '@vben/icons';
import { isEmpty } from '@vben/utils';
import { ElButton, ElLoading, ElMessage } from 'element-plus';
import { ElLoading, ElMessage } from 'element-plus';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteMenu, getMenuList } from '#/api/system/menu';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteMenu, deleteMenuList, getMenuList } from '#/api/system/menu';
import { $t } from '#/locales';
import { useGridColumns } from './data';
@@ -26,67 +24,74 @@ const [FormModal, formModalApi] = useVbenModal({
});
/** 刷新表格 */
function onRefresh() {
function handleRefresh() {
gridApi.query();
}
/** 创建菜单 */
function onCreate() {
formModalApi.setData({}).open();
function handleCreate() {
formModalApi.setData(null).open();
}
/** 添加下级菜单 */
function onAppend(row: SystemMenuApi.Menu) {
formModalApi.setData({ pid: row.id }).open();
function handleAppend(row: SystemMenuApi.Menu) {
formModalApi.setData({ parentId: row.id }).open();
}
/** 编辑菜单 */
function onEdit(row: SystemMenuApi.Menu) {
function handleEdit(row: SystemMenuApi.Menu) {
formModalApi.setData(row).open();
}
/** 删除菜单 */
async function onDelete(row: SystemMenuApi.Menu) {
async function handleDelete(row: SystemMenuApi.Menu) {
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.name]),
});
try {
await deleteMenu(row.id as number);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
onRefresh();
handleRefresh();
} finally {
loadingInstance.close();
}
}
/** 表格操作按钮的回调函数 */
function onActionClick({ code, row }: OnActionClickParams<SystemMenuApi.Menu>) {
switch (code) {
case 'append': {
onAppend(row);
break;
}
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
/** 批量删除菜单 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deletingBatch'),
});
try {
await deleteMenuList(checkedIds.value);
checkedIds.value = [];
ElMessage.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
loadingInstance.close();
}
}
const checkedIds = ref<number[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemMenuApi.Menu[];
}) {
checkedIds.value = records.map((item) => item.id!);
}
/** 切换树形展开/收缩状态 */
const isExpanded = ref(false);
function toggleExpand() {
function handleExpand() {
isExpanded.value = !isExpanded.value;
gridApi.grid.setAllTreeExpand(isExpanded.value);
}
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions: {
columns: useGridColumns(onActionClick),
columns: useGridColumns(),
height: 'auto',
keepSource: true,
pagerConfig: {
@@ -101,9 +106,11 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
treeConfig: {
parentField: 'parentId',
@@ -111,7 +118,11 @@ const [Grid, gridApi] = useVbenVxeGrid({
transform: true,
reserve: true,
},
} as VxeTableGridOptions,
} as VxeTableGridOptions<SystemMenuApi.Menu>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
});
</script>
@@ -125,20 +136,33 @@ const [Grid, gridApi] = useVbenVxeGrid({
<DocAlert title="菜单路由" url="https://doc.iocoder.cn/vue3/route/" />
</template>
<FormModal @success="onRefresh" />
<Grid>
<FormModal @success="handleRefresh" />
<Grid table-title="菜单列表">
<template #toolbar-tools>
<ElButton
type="primary"
@click="onCreate"
v-access:code="['system:menu:create']"
>
<Plus class="mr-2 size-5" />
{{ $t('ui.actionTitle.create', ['菜单']) }}
</ElButton>
<ElButton class="ml-2" @click="toggleExpand">
{{ isExpanded ? '收缩' : '展开' }}
</ElButton>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['菜单']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['system:menu:create'],
onClick: handleCreate,
},
{
label: isExpanded ? '收缩' : '展开',
type: 'primary',
onClick: handleExpand,
},
{
label: $t('ui.actionTitle.deleteBatch'),
type: 'danger',
icon: ACTION_ICON.DELETE,
auth: ['system:menu:delete'],
disabled: isEmpty(checkedIds),
onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #name="{ row }">
<div class="flex w-full items-center gap-1">
@@ -158,6 +182,39 @@ const [Grid, gridApi] = useVbenVxeGrid({
<div class="items-center justify-end"></div>
</div>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: '新增下级',
type: 'primary',
link: true,
icon: ACTION_ICON.ADD,
auth: ['system:menu:create'],
onClick: handleAppend.bind(null, row),
},
{
label: $t('common.edit'),
type: 'primary',
link: true,
icon: ACTION_ICON.EDIT,
auth: ['system:menu:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'danger',
link: true,
icon: ACTION_ICON.DELETE,
auth: ['system:menu:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>