reactor:【system 系统管理】menu 进一步统一代码风格(暂时只包括 element-plus)
This commit is contained in:
@@ -52,3 +52,8 @@ export async function updateMenu(data: SystemMenuApi.Menu) {
|
|||||||
export async function deleteMenu(id: number) {
|
export async function deleteMenu(id: number) {
|
||||||
return requestClient.delete(`/system/menu/delete?id=${id}`);
|
return requestClient.delete(`/system/menu/delete?id=${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 批量删除菜单 */
|
||||||
|
export async function deleteMenuList(ids: number[]) {
|
||||||
|
return requestClient.delete(`/system/menu/delete-list?ids=${ids.join(',')}`);
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import type { Recordable } from '@vben/types';
|
import type { Recordable } from '@vben/types';
|
||||||
|
|
||||||
import type { VbenFormSchema } from '#/adapter/form';
|
import type { VbenFormSchema } from '#/adapter/form';
|
||||||
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
import type { SystemMenuApi } from '#/api/system/menu';
|
import type { SystemMenuApi } from '#/api/system/menu';
|
||||||
|
|
||||||
import { h } from 'vue';
|
import { h } from 'vue';
|
||||||
|
|
||||||
import { useAccess } from '@vben/access';
|
|
||||||
import {
|
import {
|
||||||
CommonStatusEnum,
|
CommonStatusEnum,
|
||||||
DICT_TYPE,
|
DICT_TYPE,
|
||||||
@@ -21,8 +20,6 @@ import { getMenuList } from '#/api/system/menu';
|
|||||||
import { $t } from '#/locales';
|
import { $t } from '#/locales';
|
||||||
import { componentKeys } from '#/router/routes';
|
import { componentKeys } from '#/router/routes';
|
||||||
|
|
||||||
const { hasAccessByCodes } = useAccess();
|
|
||||||
|
|
||||||
/** 新增/修改的表单 */
|
/** 新增/修改的表单 */
|
||||||
export function useFormSchema(): VbenFormSchema[] {
|
export function useFormSchema(): VbenFormSchema[] {
|
||||||
return [
|
return [
|
||||||
@@ -63,6 +60,7 @@ export function useFormSchema(): VbenFormSchema[] {
|
|||||||
},
|
},
|
||||||
showSearch: true,
|
showSearch: true,
|
||||||
treeDefaultExpandedKeys: [0],
|
treeDefaultExpandedKeys: [0],
|
||||||
|
allowClear: true,
|
||||||
},
|
},
|
||||||
rules: 'selectRequired',
|
rules: 'selectRequired',
|
||||||
renderComponentContent() {
|
renderComponentContent() {
|
||||||
@@ -171,6 +169,7 @@ export function useFormSchema(): VbenFormSchema[] {
|
|||||||
component: 'AutoComplete',
|
component: 'AutoComplete',
|
||||||
componentProps: {
|
componentProps: {
|
||||||
clearable: true,
|
clearable: true,
|
||||||
|
allowClear: true,
|
||||||
filterOption(input: string, option: { value: string }) {
|
filterOption(input: string, option: { value: string }) {
|
||||||
return option.value.toLowerCase().includes(input.toLowerCase());
|
return option.value.toLowerCase().includes(input.toLowerCase());
|
||||||
},
|
},
|
||||||
@@ -270,10 +269,9 @@ export function useFormSchema(): VbenFormSchema[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 列表的字段 */
|
/** 列表的字段 */
|
||||||
export function useGridColumns(
|
export function useGridColumns(): VxeTableGridOptions<SystemMenuApi.Menu>['columns'] {
|
||||||
onActionClick: OnActionClickFn<SystemMenuApi.Menu>,
|
|
||||||
): VxeTableGridOptions<SystemMenuApi.Menu>['columns'] {
|
|
||||||
return [
|
return [
|
||||||
|
{ type: 'checkbox', width: 40 },
|
||||||
{
|
{
|
||||||
field: 'name',
|
field: 'name',
|
||||||
title: '菜单名称',
|
title: '菜单名称',
|
||||||
@@ -309,8 +307,8 @@ export function useGridColumns(
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'componentName',
|
field: 'componentName',
|
||||||
minWidth: 200,
|
|
||||||
title: '组件名称',
|
title: '组件名称',
|
||||||
|
minWidth: 200,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'status',
|
field: 'status',
|
||||||
@@ -322,34 +320,10 @@ export function useGridColumns(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'operation',
|
|
||||||
title: '操作',
|
title: '操作',
|
||||||
minWidth: 200,
|
width: 220,
|
||||||
fixed: 'right',
|
fixed: 'right',
|
||||||
align: 'center',
|
slots: { default: 'actions' },
|
||||||
showOverflow: false,
|
|
||||||
cellRender: {
|
|
||||||
attrs: {
|
|
||||||
nameField: 'name',
|
|
||||||
onClick: onActionClick,
|
|
||||||
},
|
|
||||||
name: 'CellOperation',
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
code: 'append',
|
|
||||||
text: '新增下级',
|
|
||||||
show: hasAccessByCodes(['system:menu:create']),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: 'edit',
|
|
||||||
show: hasAccessByCodes(['system:menu:update']),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: 'delete',
|
|
||||||
show: hasAccessByCodes(['system:menu:delete']),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type {
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
OnActionClickParams,
|
|
||||||
VxeTableGridOptions,
|
|
||||||
} from '#/adapter/vxe-table';
|
|
||||||
import type { SystemMenuApi } from '#/api/system/menu';
|
import type { SystemMenuApi } from '#/api/system/menu';
|
||||||
|
|
||||||
import { ref } from 'vue';
|
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 { 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 { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
import { deleteMenu, getMenuList } from '#/api/system/menu';
|
import { deleteMenu, deleteMenuList, getMenuList } from '#/api/system/menu';
|
||||||
import { $t } from '#/locales';
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
import { useGridColumns } from './data';
|
import { useGridColumns } from './data';
|
||||||
@@ -26,67 +24,74 @@ const [FormModal, formModalApi] = useVbenModal({
|
|||||||
});
|
});
|
||||||
|
|
||||||
/** 刷新表格 */
|
/** 刷新表格 */
|
||||||
function onRefresh() {
|
function handleRefresh() {
|
||||||
gridApi.query();
|
gridApi.query();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 创建菜单 */
|
/** 创建菜单 */
|
||||||
function onCreate() {
|
function handleCreate() {
|
||||||
formModalApi.setData({}).open();
|
formModalApi.setData(null).open();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 添加下级菜单 */
|
/** 添加下级菜单 */
|
||||||
function onAppend(row: SystemMenuApi.Menu) {
|
function handleAppend(row: SystemMenuApi.Menu) {
|
||||||
formModalApi.setData({ pid: row.id }).open();
|
formModalApi.setData({ parentId: row.id }).open();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 编辑菜单 */
|
/** 编辑菜单 */
|
||||||
function onEdit(row: SystemMenuApi.Menu) {
|
function handleEdit(row: SystemMenuApi.Menu) {
|
||||||
formModalApi.setData(row).open();
|
formModalApi.setData(row).open();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 删除菜单 */
|
/** 删除菜单 */
|
||||||
async function onDelete(row: SystemMenuApi.Menu) {
|
async function handleDelete(row: SystemMenuApi.Menu) {
|
||||||
const loadingInstance = ElLoading.service({
|
const loadingInstance = ElLoading.service({
|
||||||
text: $t('ui.actionMessage.deleting', [row.name]),
|
text: $t('ui.actionMessage.deleting', [row.name]),
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
await deleteMenu(row.id as number);
|
await deleteMenu(row.id as number);
|
||||||
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
|
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
|
||||||
onRefresh();
|
handleRefresh();
|
||||||
} finally {
|
} finally {
|
||||||
loadingInstance.close();
|
loadingInstance.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 表格操作按钮的回调函数 */
|
/** 批量删除菜单 */
|
||||||
function onActionClick({ code, row }: OnActionClickParams<SystemMenuApi.Menu>) {
|
async function handleDeleteBatch() {
|
||||||
switch (code) {
|
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
|
||||||
case 'append': {
|
const loadingInstance = ElLoading.service({
|
||||||
onAppend(row);
|
text: $t('ui.actionMessage.deletingBatch'),
|
||||||
break;
|
});
|
||||||
}
|
try {
|
||||||
case 'delete': {
|
await deleteMenuList(checkedIds.value);
|
||||||
onDelete(row);
|
checkedIds.value = [];
|
||||||
break;
|
ElMessage.success($t('ui.actionMessage.deleteSuccess'));
|
||||||
}
|
handleRefresh();
|
||||||
case 'edit': {
|
} finally {
|
||||||
onEdit(row);
|
loadingInstance.close();
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const checkedIds = ref<number[]>([]);
|
||||||
|
function handleRowCheckboxChange({
|
||||||
|
records,
|
||||||
|
}: {
|
||||||
|
records: SystemMenuApi.Menu[];
|
||||||
|
}) {
|
||||||
|
checkedIds.value = records.map((item) => item.id!);
|
||||||
|
}
|
||||||
|
|
||||||
/** 切换树形展开/收缩状态 */
|
/** 切换树形展开/收缩状态 */
|
||||||
const isExpanded = ref(false);
|
const isExpanded = ref(false);
|
||||||
function toggleExpand() {
|
function handleExpand() {
|
||||||
isExpanded.value = !isExpanded.value;
|
isExpanded.value = !isExpanded.value;
|
||||||
gridApi.grid.setAllTreeExpand(isExpanded.value);
|
gridApi.grid.setAllTreeExpand(isExpanded.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [Grid, gridApi] = useVbenVxeGrid({
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
gridOptions: {
|
gridOptions: {
|
||||||
columns: useGridColumns(onActionClick),
|
columns: useGridColumns(),
|
||||||
height: 'auto',
|
height: 'auto',
|
||||||
keepSource: true,
|
keepSource: true,
|
||||||
pagerConfig: {
|
pagerConfig: {
|
||||||
@@ -101,9 +106,11 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
|||||||
},
|
},
|
||||||
rowConfig: {
|
rowConfig: {
|
||||||
keyField: 'id',
|
keyField: 'id',
|
||||||
|
isHover: true,
|
||||||
},
|
},
|
||||||
toolbarConfig: {
|
toolbarConfig: {
|
||||||
refresh: true,
|
refresh: true,
|
||||||
|
search: true,
|
||||||
},
|
},
|
||||||
treeConfig: {
|
treeConfig: {
|
||||||
parentField: 'parentId',
|
parentField: 'parentId',
|
||||||
@@ -111,7 +118,11 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
|||||||
transform: true,
|
transform: true,
|
||||||
reserve: true,
|
reserve: true,
|
||||||
},
|
},
|
||||||
} as VxeTableGridOptions,
|
} as VxeTableGridOptions<SystemMenuApi.Menu>,
|
||||||
|
gridEvents: {
|
||||||
|
checkboxAll: handleRowCheckboxChange,
|
||||||
|
checkboxChange: handleRowCheckboxChange,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -125,20 +136,33 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
|||||||
<DocAlert title="菜单路由" url="https://doc.iocoder.cn/vue3/route/" />
|
<DocAlert title="菜单路由" url="https://doc.iocoder.cn/vue3/route/" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<FormModal @success="onRefresh" />
|
<FormModal @success="handleRefresh" />
|
||||||
<Grid>
|
<Grid table-title="菜单列表">
|
||||||
<template #toolbar-tools>
|
<template #toolbar-tools>
|
||||||
<ElButton
|
<TableAction
|
||||||
type="primary"
|
:actions="[
|
||||||
@click="onCreate"
|
{
|
||||||
v-access:code="['system:menu:create']"
|
label: $t('ui.actionTitle.create', ['菜单']),
|
||||||
>
|
type: 'primary',
|
||||||
<Plus class="mr-2 size-5" />
|
icon: ACTION_ICON.ADD,
|
||||||
{{ $t('ui.actionTitle.create', ['菜单']) }}
|
auth: ['system:menu:create'],
|
||||||
</ElButton>
|
onClick: handleCreate,
|
||||||
<ElButton class="ml-2" @click="toggleExpand">
|
},
|
||||||
{{ isExpanded ? '收缩' : '展开' }}
|
{
|
||||||
</ElButton>
|
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>
|
||||||
<template #name="{ row }">
|
<template #name="{ row }">
|
||||||
<div class="flex w-full items-center gap-1">
|
<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 class="items-center justify-end"></div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</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>
|
</Grid>
|
||||||
</Page>
|
</Page>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -59,27 +59,28 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 加载数据
|
// 加载数据
|
||||||
let data = modalApi.getData<SystemMenuApi.Menu>();
|
const data = modalApi.getData<SystemMenuApi.Menu>();
|
||||||
if (!data) {
|
if (!data || !data.id) {
|
||||||
|
// 设置上级
|
||||||
|
await formApi.setValues(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data.id) {
|
modalApi.lock();
|
||||||
modalApi.lock();
|
try {
|
||||||
try {
|
formData.value = await getMenu(data.id);
|
||||||
data = await getMenu(data.id);
|
// 设置到 values
|
||||||
} finally {
|
if (formData.value) {
|
||||||
modalApi.unlock();
|
await formApi.setValues(formData.value);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
}
|
}
|
||||||
// 设置到 values
|
|
||||||
formData.value = data;
|
|
||||||
await formApi.setValues(formData.value);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Modal class="w-[40%]" :title="getTitle">
|
<Modal class="w-2/5" :title="getTitle">
|
||||||
<Form class="mx-4" />
|
<Form class="mx-4" />
|
||||||
</Modal>
|
</Modal>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user