2024-08-07 08:57:56 +08:00
|
|
|
<script setup lang="ts">
|
2024-10-05 15:35:58 +08:00
|
|
|
import type { Recordable } from '@vben/types';
|
2024-09-22 21:37:32 +08:00
|
|
|
|
2024-10-06 16:41:54 +08:00
|
|
|
import { computed } from 'vue';
|
|
|
|
|
|
|
|
import { useAccess } from '@vben/access';
|
2024-10-05 15:35:58 +08:00
|
|
|
import { Page, useVbenDrawer, type VbenFormProps } from '@vben/common-ui';
|
2024-10-06 16:41:54 +08:00
|
|
|
import { Fallback } from '@vben/common-ui';
|
2024-10-07 16:56:32 +08:00
|
|
|
import {
|
|
|
|
eachTree,
|
|
|
|
getPopupContainer,
|
|
|
|
listToTree,
|
|
|
|
removeEmptyChildren,
|
|
|
|
} from '@vben/utils';
|
2024-09-24 11:23:02 +08:00
|
|
|
|
2024-10-25 08:09:53 +08:00
|
|
|
import { Popconfirm, Space } from 'ant-design-vue';
|
2024-09-24 11:23:02 +08:00
|
|
|
|
2024-10-17 15:16:22 +08:00
|
|
|
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter/vxe-table';
|
2024-10-05 15:35:58 +08:00
|
|
|
import { menuList, menuRemove } from '#/api/system/menu';
|
|
|
|
|
|
|
|
import { columns, querySchema } from './data';
|
2024-09-22 21:37:32 +08:00
|
|
|
import menuDrawer from './menu-drawer.vue';
|
|
|
|
|
2024-10-07 11:14:21 +08:00
|
|
|
/**
|
|
|
|
* 不要问为什么有两个根节点 v-if会控制只会渲染一个
|
|
|
|
*/
|
|
|
|
|
2024-10-05 15:35:58 +08:00
|
|
|
const formOptions: VbenFormProps = {
|
2024-10-05 21:04:44 +08:00
|
|
|
commonConfig: {
|
|
|
|
labelWidth: 80,
|
2024-10-20 18:16:47 +08:00
|
|
|
componentProps: {
|
|
|
|
allowClear: true,
|
|
|
|
},
|
2024-10-05 21:04:44 +08:00
|
|
|
},
|
2024-10-05 15:35:58 +08:00
|
|
|
schema: querySchema(),
|
|
|
|
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
|
|
|
};
|
|
|
|
|
|
|
|
const gridOptions: VxeGridProps = {
|
|
|
|
columns,
|
|
|
|
height: 'auto',
|
|
|
|
keepSource: true,
|
|
|
|
pagerConfig: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
proxyConfig: {
|
|
|
|
ajax: {
|
2024-10-06 09:53:00 +08:00
|
|
|
query: async (_, formValues = {}) => {
|
2024-10-05 15:35:58 +08:00
|
|
|
const resp = await menuList({
|
|
|
|
...formValues,
|
|
|
|
});
|
|
|
|
const treeData = listToTree(resp, {
|
|
|
|
id: 'menuId',
|
|
|
|
pid: 'parentId',
|
|
|
|
children: 'children',
|
|
|
|
});
|
2024-10-07 11:14:21 +08:00
|
|
|
removeEmptyChildren(treeData);
|
2024-10-05 15:35:58 +08:00
|
|
|
return { rows: treeData };
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rowConfig: {
|
|
|
|
isHover: true,
|
|
|
|
keyField: 'menuId',
|
|
|
|
},
|
2024-10-11 17:28:56 +08:00
|
|
|
|
2024-10-05 15:35:58 +08:00
|
|
|
treeConfig: {
|
|
|
|
parentField: 'parentId',
|
|
|
|
rowField: 'menuId',
|
|
|
|
transform: false,
|
|
|
|
},
|
2024-10-10 09:13:50 +08:00
|
|
|
id: 'system-menu-index',
|
2024-10-05 15:35:58 +08:00
|
|
|
};
|
|
|
|
|
2024-10-07 11:14:21 +08:00
|
|
|
const [BasicTable, tableApi] = useVbenVxeGrid({
|
|
|
|
formOptions,
|
|
|
|
gridOptions,
|
|
|
|
gridEvents: {
|
|
|
|
cellDblclick: (e: any) => {
|
|
|
|
const { row = {} } = e;
|
|
|
|
if (!row?.children) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const isExpanded = row?.expand;
|
|
|
|
tableApi.grid.setTreeExpand(row, !isExpanded);
|
|
|
|
row.expand = !isExpanded;
|
|
|
|
},
|
2024-10-07 11:19:14 +08:00
|
|
|
// 需要监听使用箭头展开的情况 否则展开/折叠的数据不一致
|
|
|
|
toggleTreeExpand: (e: any) => {
|
|
|
|
const { row = {}, expanded } = e;
|
|
|
|
row.expand = expanded;
|
|
|
|
},
|
2024-10-07 11:14:21 +08:00
|
|
|
},
|
|
|
|
});
|
2024-09-22 21:37:32 +08:00
|
|
|
const [MenuDrawer, drawerApi] = useVbenDrawer({
|
|
|
|
connectedComponent: menuDrawer,
|
|
|
|
});
|
|
|
|
|
|
|
|
function handleAdd() {
|
2024-10-05 15:35:58 +08:00
|
|
|
drawerApi.setData({});
|
2024-09-22 21:37:32 +08:00
|
|
|
drawerApi.open();
|
|
|
|
}
|
2024-09-24 11:23:02 +08:00
|
|
|
|
2024-10-07 20:07:33 +08:00
|
|
|
function handleSubAdd(row: Recordable<any>) {
|
|
|
|
const { menuId } = row;
|
|
|
|
drawerApi.setData({ id: menuId, update: false });
|
|
|
|
drawerApi.open();
|
|
|
|
}
|
|
|
|
|
2024-10-05 15:35:58 +08:00
|
|
|
async function handleEdit(record: Recordable<any>) {
|
2024-10-08 10:36:00 +08:00
|
|
|
drawerApi.setData({ id: record.menuId, update: true });
|
2024-10-05 15:35:58 +08:00
|
|
|
drawerApi.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleDelete(row: Recordable<any>) {
|
|
|
|
await menuRemove(row.menuId);
|
2024-10-05 21:04:44 +08:00
|
|
|
await tableApi.query();
|
2024-10-05 15:35:58 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 11:14:21 +08:00
|
|
|
/**
|
|
|
|
* 全部展开/折叠
|
|
|
|
* @param expand 是否展开
|
|
|
|
*/
|
|
|
|
function setExpandOrCollapse(expand: boolean) {
|
|
|
|
eachTree(tableApi.grid.getData(), (item) => (item.expand = expand));
|
|
|
|
tableApi.grid?.setAllTreeExpand(expand);
|
2024-10-05 15:35:58 +08:00
|
|
|
}
|
2024-10-06 16:41:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 与后台逻辑相同
|
|
|
|
* 只有租户管理和超级管理能访问菜单管理
|
|
|
|
*/
|
|
|
|
const { hasAccessByRoles } = useAccess();
|
|
|
|
const isAdmin = computed(() => {
|
|
|
|
return hasAccessByRoles(['admin', 'superadmin']);
|
|
|
|
});
|
2024-08-07 08:57:56 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2024-10-06 16:41:54 +08:00
|
|
|
<Page v-if="isAdmin" :auto-content-height="true">
|
2024-10-25 08:09:53 +08:00
|
|
|
<BasicTable table-title="菜单列表" table-title-help="双击展开/收起子菜单">
|
2024-10-05 15:35:58 +08:00
|
|
|
<template #toolbar-tools>
|
|
|
|
<Space>
|
2024-10-07 11:14:21 +08:00
|
|
|
<a-button @click="setExpandOrCollapse(false)">
|
2024-10-05 15:35:58 +08:00
|
|
|
{{ $t('pages.common.collapse') }}
|
|
|
|
</a-button>
|
2024-10-07 11:14:21 +08:00
|
|
|
<a-button @click="setExpandOrCollapse(true)">
|
2024-10-05 15:35:58 +08:00
|
|
|
{{ $t('pages.common.expand') }}
|
|
|
|
</a-button>
|
|
|
|
<a-button
|
|
|
|
type="primary"
|
|
|
|
v-access:code="['system:menu:add']"
|
|
|
|
@click="handleAdd"
|
|
|
|
>
|
|
|
|
{{ $t('pages.common.add') }}
|
|
|
|
</a-button>
|
|
|
|
</Space>
|
|
|
|
</template>
|
|
|
|
<template #action="{ row }">
|
2024-10-07 16:56:32 +08:00
|
|
|
<Space>
|
|
|
|
<ghost-button
|
|
|
|
v-access:code="['system:menu:edit']"
|
|
|
|
@click="handleEdit(row)"
|
2024-10-05 15:35:58 +08:00
|
|
|
>
|
2024-10-07 16:56:32 +08:00
|
|
|
{{ $t('pages.common.edit') }}
|
|
|
|
</ghost-button>
|
2024-10-07 20:07:33 +08:00
|
|
|
<!-- '按钮类型'无法再添加子菜单 -->
|
|
|
|
<ghost-button
|
|
|
|
v-if="row.menuType !== 'F'"
|
2024-10-30 15:27:39 +08:00
|
|
|
class="btn-success"
|
2024-10-07 20:07:33 +08:00
|
|
|
v-access:code="['system:menu:add']"
|
|
|
|
@click="handleSubAdd(row)"
|
|
|
|
>
|
|
|
|
{{ $t('pages.common.add') }}
|
|
|
|
</ghost-button>
|
2024-10-07 16:56:32 +08:00
|
|
|
<Popconfirm
|
|
|
|
:get-popup-container="getPopupContainer"
|
|
|
|
placement="left"
|
|
|
|
title="确认删除?"
|
|
|
|
@confirm="handleDelete(row)"
|
|
|
|
>
|
|
|
|
<ghost-button
|
|
|
|
danger
|
|
|
|
v-access:code="['system:menu:remove']"
|
|
|
|
@click.stop=""
|
|
|
|
>
|
|
|
|
{{ $t('pages.common.delete') }}
|
|
|
|
</ghost-button>
|
|
|
|
</Popconfirm>
|
|
|
|
</Space>
|
2024-10-05 15:35:58 +08:00
|
|
|
</template>
|
|
|
|
</BasicTable>
|
2024-10-05 21:04:44 +08:00
|
|
|
<MenuDrawer @reload="tableApi.query()" />
|
2024-09-22 21:37:32 +08:00
|
|
|
</Page>
|
2024-10-06 16:41:54 +08:00
|
|
|
<Fallback v-else description="您没有菜单管理的访问权限" status="403" />
|
2024-08-07 08:57:56 +08:00
|
|
|
</template>
|