mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-12 20:26:36 +08:00
feat(project): 添加vben5前端
This commit is contained in:
434
Yi.Vben5.Vue3/apps/web-antd/src/views/system/menu/data.tsx
Normal file
434
Yi.Vben5.Vue3/apps/web-antd/src/views/system/menu/data.tsx
Normal file
@@ -0,0 +1,434 @@
|
||||
import type { FormSchemaGetter } from '#/adapter/form';
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
|
||||
import { h } from 'vue';
|
||||
|
||||
import { DictEnum } from '@vben/constants';
|
||||
import { FolderIcon, MenuIcon, OkButtonIcon, VbenIcon } from '@vben/icons';
|
||||
import { $t } from '@vben/locales';
|
||||
import { getPopupContainer } from '@vben/utils';
|
||||
|
||||
import { z } from '#/adapter/form';
|
||||
import { renderDict } from '#/utils/render';
|
||||
|
||||
export const querySchema: FormSchemaGetter = () => [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'menuName',
|
||||
label: '菜单名称 ',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
getPopupContainer,
|
||||
options: [
|
||||
{ label: '启用', value: true },
|
||||
{ label: '禁用', value: false },
|
||||
],
|
||||
},
|
||||
fieldName: 'state',
|
||||
label: '菜单状态 ',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
getPopupContainer,
|
||||
options: [
|
||||
{ label: '显示', value: true },
|
||||
{ label: '隐藏', value: false },
|
||||
],
|
||||
},
|
||||
fieldName: 'isShow',
|
||||
label: '显示状态',
|
||||
},
|
||||
];
|
||||
|
||||
// 菜单类型
|
||||
export const menuTypeOptions = [
|
||||
{ label: '目录', value: 'Catalogue' },
|
||||
{ label: '菜单', value: 'Menu' },
|
||||
{ label: '按钮', value: 'Component' },
|
||||
];
|
||||
|
||||
export const yesNoOptions = [
|
||||
{ label: '是', value: '0' },
|
||||
{ label: '否', value: '1' },
|
||||
];
|
||||
|
||||
/**
|
||||
* 判断是否为菜单类型(Menu/C)
|
||||
*/
|
||||
function isMenuType(menuType: string): boolean {
|
||||
const type = menuType?.toLowerCase();
|
||||
return type === 'c' || type === 'menu';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为目录类型(Catalogue/M)
|
||||
*/
|
||||
function isCatalogueType(menuType: string): boolean {
|
||||
const type = menuType?.toLowerCase();
|
||||
return type === 'm' || type === 'catalogue' || type === 'catalog' || type === 'directory' || type === 'folder';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为按钮类型(Component/F)
|
||||
*/
|
||||
function isComponentType(menuType: string): boolean {
|
||||
const type = menuType?.toLowerCase();
|
||||
return type === 'f' || type === 'component' || type === 'button';
|
||||
}
|
||||
|
||||
// (M目录 C菜单 F按钮)
|
||||
const menuTypes: Record<string, { icon: typeof MenuIcon; value: string }> = {
|
||||
c: { icon: MenuIcon, value: '菜单' },
|
||||
menu: { icon: MenuIcon, value: '菜单' },
|
||||
Menu: { icon: MenuIcon, value: '菜单' },
|
||||
catalog: { icon: FolderIcon, value: '目录' },
|
||||
directory: { icon: FolderIcon, value: '目录' },
|
||||
folder: { icon: FolderIcon, value: '目录' },
|
||||
m: { icon: FolderIcon, value: '目录' },
|
||||
catalogue: { icon: FolderIcon, value: '目录' },
|
||||
component: { icon: OkButtonIcon, value: '按钮' },
|
||||
f: { icon: OkButtonIcon, value: '按钮' },
|
||||
button: { icon: OkButtonIcon, value: '按钮' },
|
||||
};
|
||||
export const columns: VxeGridProps['columns'] = [
|
||||
{
|
||||
title: '菜单名称',
|
||||
field: 'menuName',
|
||||
treeNode: true,
|
||||
width: 200,
|
||||
slots: {
|
||||
// 需要i18n支持 否则返回原始值
|
||||
default: ({ row }) => $t(row.menuName),
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '图标',
|
||||
field: 'menuIcon',
|
||||
width: 80,
|
||||
slots: {
|
||||
default: ({ row }) => {
|
||||
if (row?.menuIcon === '#' || !row?.menuIcon) {
|
||||
return '';
|
||||
}
|
||||
return (
|
||||
<span class={'flex justify-center'}>
|
||||
<VbenIcon icon={row.menuIcon} />
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '排序',
|
||||
field: 'orderNum',
|
||||
width: 120,
|
||||
},
|
||||
{
|
||||
title: '组件类型',
|
||||
field: 'menuType',
|
||||
width: 150,
|
||||
slots: {
|
||||
default: ({ row }) => {
|
||||
const typeKey = `${row.menuType ?? ''}`.toString().trim().toLowerCase();
|
||||
const current = menuTypes[typeKey];
|
||||
if (!current) {
|
||||
return '未知';
|
||||
}
|
||||
return (
|
||||
<span class="flex items-center justify-center gap-1">
|
||||
{h(current.icon, { class: 'size-[18px]' })}
|
||||
<span>{current.value}</span>
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '权限标识',
|
||||
field: 'permissionCode',
|
||||
},
|
||||
{
|
||||
title: '组件路径',
|
||||
field: 'component',
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
field: 'state',
|
||||
width: 100,
|
||||
slots: {
|
||||
default: ({ row }) => {
|
||||
return renderDict(String(row.state), DictEnum.SYS_NORMAL_DISABLE);
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '显示',
|
||||
field: 'isShow',
|
||||
width: 100,
|
||||
slots: {
|
||||
default: ({ row }) => {
|
||||
return row.isShow ? '显示' : '隐藏';
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
field: 'creationTime',
|
||||
},
|
||||
{
|
||||
field: 'action',
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' },
|
||||
title: '操作',
|
||||
resizable: false,
|
||||
width: 'auto',
|
||||
},
|
||||
];
|
||||
|
||||
export const drawerSchema: FormSchemaGetter = () => [
|
||||
{
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
show: () => false,
|
||||
triggerFields: [''],
|
||||
},
|
||||
fieldName: 'id',
|
||||
},
|
||||
{
|
||||
component: 'TreeSelect',
|
||||
defaultValue: 0,
|
||||
fieldName: 'parentId',
|
||||
label: '上级菜单',
|
||||
rules: 'selectRequired',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: menuTypeOptions,
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: 'M',
|
||||
dependencies: {
|
||||
componentProps: (_, api) => {
|
||||
// 切换时清空校验
|
||||
// 直接抄的源码 没有清空校验的方法
|
||||
Object.keys(api.errors.value).forEach((key) => {
|
||||
api.setFieldError(key, undefined);
|
||||
});
|
||||
return {};
|
||||
},
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'menuType',
|
||||
label: '菜单类型',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
// 类型不为按钮时显示
|
||||
show: (values) => !isComponentType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
renderComponentContent: (model) => ({
|
||||
addonBefore: () => <VbenIcon icon={model.menuIcon} />,
|
||||
addonAfter: () => (
|
||||
<a href="https://icon-sets.iconify.design/" target="_blank">
|
||||
搜索图标
|
||||
</a>
|
||||
),
|
||||
}),
|
||||
fieldName: 'menuIcon',
|
||||
help: '点击搜索图标跳转到iconify & 粘贴',
|
||||
label: '菜单图标',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'menuName',
|
||||
label: '菜单名称',
|
||||
help: '支持i18n写法, 如: menu.system.user',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'InputNumber',
|
||||
fieldName: 'orderNum',
|
||||
help: '排序, 数字越小越靠前',
|
||||
label: '显示排序',
|
||||
defaultValue: 0,
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: (model) => {
|
||||
const placeholder = model.isLink
|
||||
? '填写链接地址http(s):// 使用新页面打开'
|
||||
: '填写`路由地址`或者`链接地址` 链接默认使用内部iframe内嵌打开';
|
||||
return {
|
||||
placeholder,
|
||||
};
|
||||
},
|
||||
dependencies: {
|
||||
rules: (model) => {
|
||||
if (!model.isLink) {
|
||||
return z
|
||||
.string({ message: '请输入路由地址' })
|
||||
.min(1, '请输入路由地址')
|
||||
.refine((val) => !val.startsWith('/'), {
|
||||
message: '路由地址不需要带/',
|
||||
});
|
||||
}
|
||||
// 为链接
|
||||
return z
|
||||
.string({ message: '请输入链接地址' })
|
||||
.regex(/^https?:\/\//, { message: '请输入正确的链接地址' });
|
||||
},
|
||||
// 类型不为按钮时显示
|
||||
show: (values) => !isComponentType(values?.menuType),
|
||||
triggerFields: ['isLink', 'menuType'],
|
||||
},
|
||||
fieldName: 'router',
|
||||
help: `路由地址不带/, 如: menu, user\n 链接为http(s)://开头\n 链接默认使用内部iframe打开, 可通过{是否外链}控制打开方式`,
|
||||
label: '路由地址',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: (model) => {
|
||||
return {
|
||||
// 为链接时组件disabled
|
||||
disabled: model.isLink,
|
||||
};
|
||||
},
|
||||
defaultValue: '',
|
||||
dependencies: {
|
||||
rules: (model) => {
|
||||
// 非链接时为必填项
|
||||
if (model.router && !/^https?:\/\//.test(model.router)) {
|
||||
return z
|
||||
.string()
|
||||
.min(1, { message: '非链接时必填组件路径' })
|
||||
.refine((val) => !val.startsWith('/') && !val.endsWith('/'), {
|
||||
message: '组件路径开头/末尾不需要带/',
|
||||
});
|
||||
}
|
||||
// 为链接时非必填
|
||||
return z.string().optional();
|
||||
},
|
||||
// 类型为菜单时显示
|
||||
show: (values) => isMenuType(values.menuType),
|
||||
triggerFields: ['menuType', 'router'],
|
||||
},
|
||||
fieldName: 'component',
|
||||
help: '填写./src/views下的组件路径, 如system/menu/index',
|
||||
label: '组件路径',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: '是', value: true },
|
||||
{ label: '否', value: false },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: false,
|
||||
dependencies: {
|
||||
// 类型不为按钮时显示
|
||||
show: (values) => !isComponentType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'isLink',
|
||||
help: '外链为http(s)://开头\n 选择否时, 使用iframe从内部打开页面, 否则新窗口打开',
|
||||
label: '是否外链',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: '显示', value: true },
|
||||
{ label: '隐藏', value: false },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: true,
|
||||
dependencies: {
|
||||
// 类型不为按钮时显示
|
||||
show: (values) => !isComponentType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'isShow',
|
||||
help: '隐藏后不会出现在菜单栏, 但仍然可以访问',
|
||||
label: '是否显示',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: '启用', value: true },
|
||||
{ label: '禁用', value: false },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: true,
|
||||
dependencies: {
|
||||
// 类型不为按钮时显示
|
||||
show: (values) => !isComponentType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'state',
|
||||
help: '停用后不会出现在菜单栏, 也无法访问',
|
||||
label: '菜单状态',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
// 类型为菜单/按钮时显示
|
||||
show: (values) => !isCatalogueType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'permissionCode',
|
||||
help: `控制器中定义的权限字符\n 如: @SaCheckPermission("system:user:import")`,
|
||||
label: '权限标识',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: (model) => ({
|
||||
// 为链接时组件disabled
|
||||
disabled: model.isLink,
|
||||
placeholder: '必须为json字符串格式',
|
||||
}),
|
||||
dependencies: {
|
||||
// 类型为菜单时显示
|
||||
show: (values) => isMenuType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'query',
|
||||
help: 'vue-router中的query属性\n 如{"name": "xxx", "age": 16}',
|
||||
label: '路由参数',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: '是', value: true },
|
||||
{ label: '否', value: false },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
defaultValue: false,
|
||||
dependencies: {
|
||||
// 类型为菜单时显示
|
||||
show: (values) => isMenuType(values.menuType),
|
||||
triggerFields: ['menuType'],
|
||||
},
|
||||
fieldName: 'isCache',
|
||||
help: '路由的keepAlive属性',
|
||||
label: '是否缓存',
|
||||
},
|
||||
];
|
||||
263
Yi.Vben5.Vue3/apps/web-antd/src/views/system/menu/index.vue
Normal file
263
Yi.Vben5.Vue3/apps/web-antd/src/views/system/menu/index.vue
Normal file
@@ -0,0 +1,263 @@
|
||||
<script setup lang="ts">
|
||||
import type { VbenFormProps } from '@vben/common-ui';
|
||||
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { Menu } from '#/api/system/menu/model';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useAccess } from '@vben/access';
|
||||
import { Fallback, Page, useVbenDrawer } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import { eachTree, getVxePopupContainer, treeToList } from '@vben/utils';
|
||||
|
||||
import { Popconfirm, Space, Switch, Tooltip } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { menuCascadeRemove, menuList, menuRemove } from '#/api/system/menu';
|
||||
|
||||
import { columns, querySchema } from './data';
|
||||
import menuDrawer from './menu-drawer.vue';
|
||||
|
||||
// 空GUID,用于判断根节点
|
||||
const EMPTY_GUID = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
type MenuRow = Omit<Menu, 'parentId'> & {
|
||||
menuId: string;
|
||||
parentId: string | null;
|
||||
};
|
||||
|
||||
const formOptions: VbenFormProps = {
|
||||
commonConfig: {
|
||||
labelWidth: 80,
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
schema: querySchema(),
|
||||
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps<Record<string, any>> = {
|
||||
columns,
|
||||
height: 'auto',
|
||||
keepSource: true,
|
||||
pagerConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async (_, formValues = {}) => {
|
||||
const resp = await menuList({
|
||||
...formValues,
|
||||
});
|
||||
// 统一处理数据:确保 menuId 和 parentId 存在,并将根节点的 parentId 置为 null
|
||||
const items = (resp || []).map((item) => {
|
||||
const menuId = String(item.id ?? '');
|
||||
const parentId = item.parentId ? String(item.parentId) : null;
|
||||
return {
|
||||
...item,
|
||||
menuId,
|
||||
// 将根节点的 parentId 置为 null,以便 vxe-table 正确识别根节点
|
||||
parentId:
|
||||
!parentId ||
|
||||
parentId === EMPTY_GUID ||
|
||||
parentId === menuId
|
||||
? null
|
||||
: parentId,
|
||||
} as MenuRow;
|
||||
});
|
||||
return { items };
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'menuId',
|
||||
},
|
||||
/**
|
||||
* 开启虚拟滚动
|
||||
* 数据量小可以选择关闭
|
||||
* 如果遇到样式问题(空白、错位 滚动等)可以选择关闭虚拟滚动
|
||||
*/
|
||||
scrollY: {
|
||||
enabled: true,
|
||||
gt: 0,
|
||||
},
|
||||
treeConfig: {
|
||||
parentField: 'parentId',
|
||||
rowField: 'menuId',
|
||||
transform: true,
|
||||
},
|
||||
id: 'system-menu-index',
|
||||
};
|
||||
// @ts-expect-error TS2589: MenuRow 与 proxyConfig 组合导致类型实例化层级过深;运行时泛型已被擦除,可控,先压制报错。
|
||||
const [BasicTable, tableApi] = useVbenVxeGrid({
|
||||
formOptions,
|
||||
gridOptions,
|
||||
gridEvents: {
|
||||
cellDblclick: (e) => {
|
||||
const { row = {} } = e;
|
||||
if (!row?.children) {
|
||||
return;
|
||||
}
|
||||
const isExpanded = row?.expand;
|
||||
tableApi.grid.setTreeExpand(row, !isExpanded);
|
||||
row.expand = !isExpanded;
|
||||
},
|
||||
// 需要监听使用箭头展开的情况 否则展开/折叠的数据不一致
|
||||
toggleTreeExpand: (e) => {
|
||||
const { row = {}, expanded } = e;
|
||||
row.expand = expanded;
|
||||
},
|
||||
},
|
||||
});
|
||||
const [MenuDrawer, drawerApi] = useVbenDrawer({
|
||||
connectedComponent: menuDrawer,
|
||||
});
|
||||
|
||||
function handleAdd() {
|
||||
drawerApi.setData({});
|
||||
drawerApi.open();
|
||||
}
|
||||
|
||||
function handleSubAdd(row: MenuRow) {
|
||||
const { menuId } = row;
|
||||
drawerApi.setData({ id: menuId, update: false });
|
||||
drawerApi.open();
|
||||
}
|
||||
|
||||
async function handleEdit(record: MenuRow) {
|
||||
drawerApi.setData({ id: record.menuId, update: true });
|
||||
drawerApi.open();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否级联删除
|
||||
*/
|
||||
const cascadingDeletion = ref(false);
|
||||
async function handleDelete(row: MenuRow) {
|
||||
if (cascadingDeletion.value) {
|
||||
// 级联删除
|
||||
const menuAndChildren = treeToList<MenuRow[]>([row], { id: 'menuId' });
|
||||
const menuIds = menuAndChildren.map((item) => String(item.menuId));
|
||||
await menuCascadeRemove(menuIds);
|
||||
} else {
|
||||
// 单删除
|
||||
await menuRemove([String(row.menuId)]);
|
||||
}
|
||||
await tableApi.query();
|
||||
}
|
||||
|
||||
function removeConfirmTitle(row: MenuRow) {
|
||||
const menuName = $t(row.menuName);
|
||||
if (!cascadingDeletion.value) {
|
||||
return `是否确认删除 [${menuName}] ?`;
|
||||
}
|
||||
const menuAndChildren = treeToList<MenuRow[]>([row], { id: 'menuId' });
|
||||
if (menuAndChildren.length === 1) {
|
||||
return `是否确认删除 [${menuName}] ?`;
|
||||
}
|
||||
return `是否确认删除 [${menuName}] 及 [${menuAndChildren.length - 1}]个子项目 ?`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑/添加成功后刷新表格
|
||||
*/
|
||||
async function afterEditOrAdd() {
|
||||
tableApi.query();
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部展开/折叠
|
||||
* @param expand 是否展开
|
||||
*/
|
||||
function setExpandOrCollapse(expand: boolean) {
|
||||
eachTree(tableApi.grid.getData(), (item) => (item.expand = expand));
|
||||
tableApi.grid?.setAllTreeExpand(expand);
|
||||
}
|
||||
|
||||
/**
|
||||
* 与后台逻辑相同
|
||||
* 只有租户管理和超级管理能访问菜单管理
|
||||
* 注意: 只有超管才能对菜单进行`增删改`操作
|
||||
* 注意: 只有超管才能对菜单进行`增删改`操作
|
||||
* 注意: 只有超管才能对菜单进行`增删改`操作
|
||||
*/
|
||||
const { hasAccessByRoles } = useAccess();
|
||||
const isAdmin = computed(() => {
|
||||
return hasAccessByRoles(['admin', 'superadmin']);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page v-if="isAdmin" :auto-content-height="true">
|
||||
<BasicTable table-title="菜单列表" table-title-help="双击展开/收起子菜单">
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Tooltip title="删除菜单以及子菜单">
|
||||
<div
|
||||
v-access:role="['superadmin']"
|
||||
v-access:code="['system:menu:remove']"
|
||||
class="flex items-center"
|
||||
>
|
||||
<span class="mr-2 text-sm text-[#666666]">级联删除</span>
|
||||
<Switch v-model:checked="cascadingDeletion" />
|
||||
</div>
|
||||
</Tooltip>
|
||||
<a-button @click="setExpandOrCollapse(false)">
|
||||
{{ $t('pages.common.collapse') }}
|
||||
</a-button>
|
||||
<a-button @click="setExpandOrCollapse(true)">
|
||||
{{ $t('pages.common.expand') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
type="primary"
|
||||
v-access:code="['system:menu:add']"
|
||||
v-access:role="['superadmin']"
|
||||
@click="handleAdd"
|
||||
>
|
||||
{{ $t('pages.common.add') }}
|
||||
</a-button>
|
||||
</Space>
|
||||
</template>
|
||||
<template #action="{ row }">
|
||||
<Space>
|
||||
<ghost-button
|
||||
v-access:code="['system:menu:edit']"
|
||||
v-access:role="['superadmin']"
|
||||
@click="handleEdit(row)"
|
||||
>
|
||||
{{ $t('pages.common.edit') }}
|
||||
</ghost-button>
|
||||
<!-- '按钮类型'无法再添加子菜单 -->
|
||||
<ghost-button
|
||||
v-if="row.menuType !== 'F'"
|
||||
class="btn-success"
|
||||
v-access:code="['system:menu:add']"
|
||||
v-access:role="['superadmin']"
|
||||
@click="handleSubAdd(row)"
|
||||
>
|
||||
{{ $t('pages.common.add') }}
|
||||
</ghost-button>
|
||||
<Popconfirm
|
||||
:get-popup-container="getVxePopupContainer"
|
||||
placement="left"
|
||||
:title="removeConfirmTitle(row)"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<ghost-button
|
||||
danger
|
||||
v-access:code="['system:menu:remove']"
|
||||
v-access:role="['superadmin']"
|
||||
@click.stop=""
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</ghost-button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</template>
|
||||
</BasicTable>
|
||||
<MenuDrawer @reload="afterEditOrAdd" />
|
||||
</Page>
|
||||
<Fallback v-else description="您没有菜单管理的访问权限" status="403" />
|
||||
</template>
|
||||
@@ -0,0 +1,159 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenDrawer } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import {
|
||||
addFullName,
|
||||
cloneDeep,
|
||||
getPopupContainer,
|
||||
listToTree,
|
||||
} from '@vben/utils';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import { menuAdd, menuInfo, menuList, menuUpdate } from '#/api/system/menu';
|
||||
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
|
||||
|
||||
import { drawerSchema } from './data';
|
||||
|
||||
// 空GUID,用于判断根节点
|
||||
const EMPTY_GUID = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
interface ModalProps {
|
||||
id?: string;
|
||||
update: boolean;
|
||||
}
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
const isUpdate = ref(false);
|
||||
const title = computed(() => {
|
||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||
});
|
||||
|
||||
const [BasicForm, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
formItemClass: 'col-span-2',
|
||||
labelWidth: 90,
|
||||
},
|
||||
schema: drawerSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
});
|
||||
|
||||
async function setupMenuSelect() {
|
||||
// menu
|
||||
const menuArray = await menuList();
|
||||
// support i18n
|
||||
menuArray.forEach((item) => {
|
||||
item.menuName = $t(item.menuName);
|
||||
});
|
||||
// const folderArray = menuArray.filter((item) => item.menuType === 'M');
|
||||
/**
|
||||
* 这里需要过滤掉按钮类型
|
||||
* 不允许在按钮下添加数据
|
||||
*/
|
||||
const filteredList = menuArray.filter((item) => item.menuType !== 'F');
|
||||
const menuTree = listToTree(filteredList, { id: 'id', pid: 'parentId' });
|
||||
const fullMenuTree = [
|
||||
{
|
||||
id: EMPTY_GUID,
|
||||
menuName: $t('menu.root'),
|
||||
children: menuTree,
|
||||
},
|
||||
];
|
||||
addFullName(fullMenuTree, 'menuName', ' / ');
|
||||
|
||||
formApi.updateSchema([
|
||||
{
|
||||
componentProps: {
|
||||
fieldNames: {
|
||||
label: 'menuName',
|
||||
value: 'id',
|
||||
},
|
||||
getPopupContainer,
|
||||
// 设置弹窗滚动高度 默认256
|
||||
listHeight: 300,
|
||||
showSearch: true,
|
||||
treeData: fullMenuTree,
|
||||
treeDefaultExpandAll: false,
|
||||
// 默认展开的树节点
|
||||
treeDefaultExpandedKeys: [EMPTY_GUID],
|
||||
treeLine: { showLeafIcon: false },
|
||||
// 筛选的字段
|
||||
treeNodeFilterProp: 'menuName',
|
||||
treeNodeLabelProp: 'fullName',
|
||||
},
|
||||
fieldName: 'parentId',
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
|
||||
{
|
||||
initializedGetter: defaultFormValueGetter(formApi),
|
||||
currentGetter: defaultFormValueGetter(formApi),
|
||||
},
|
||||
);
|
||||
|
||||
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
||||
onBeforeClose,
|
||||
onClosed: handleClosed,
|
||||
onConfirm: handleConfirm,
|
||||
async onOpenChange(isOpen) {
|
||||
if (!isOpen) {
|
||||
return null;
|
||||
}
|
||||
drawerApi.drawerLoading(true);
|
||||
|
||||
const { id, update } = drawerApi.getData() as ModalProps;
|
||||
isUpdate.value = update;
|
||||
|
||||
// 加载菜单树选择
|
||||
await setupMenuSelect();
|
||||
if (id) {
|
||||
await formApi.setFieldValue('parentId', id);
|
||||
if (update) {
|
||||
const record = await menuInfo(id);
|
||||
await formApi.setValues(record);
|
||||
}
|
||||
}
|
||||
await markInitialized();
|
||||
|
||||
drawerApi.drawerLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
async function handleConfirm() {
|
||||
try {
|
||||
drawerApi.lock(true);
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
const data = cloneDeep(await formApi.getValues());
|
||||
await (isUpdate.value ? menuUpdate(data) : menuAdd(data));
|
||||
resetInitialized();
|
||||
emit('reload');
|
||||
drawerApi.close();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
drawerApi.lock(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleClosed() {
|
||||
await formApi.resetForm();
|
||||
resetInitialized();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicDrawer :title="title" class="w-[600px]">
|
||||
<BasicForm />
|
||||
</BasicDrawer>
|
||||
</template>
|
||||
Reference in New Issue
Block a user