wip: 公司菜单管理

This commit is contained in:
ah--desktop
2025-10-28 01:28:02 +08:00
parent 25977c568f
commit 332c1b190c
4 changed files with 347 additions and 0 deletions
@@ -0,0 +1,19 @@
import { requestClient } from '#/api/request';
/**
* 公司菜单管理API
*/
/**
* 获取公司所有菜单ID
*/
export async function getCompanyMenuIdsApi(id: number) {
return requestClient.get<number[]>(`/company-menu/menu-ids/${id}`);
}
/**
* 批量更新公司菜单
*/
export async function batchUpdateCompanyMenusApi(id: number, menuIds: number[]) {
return requestClient.post<void>(`/company-menu/batch-update/${id}`, menuIds);
}
@@ -0,0 +1 @@
export * from './apis';
@@ -0,0 +1,314 @@
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { Drawer, Tree, Button, message, Spin } from 'ant-design-vue'
import { getAllMenusApi } from '#/api/system/menu'
import { getCompanyMenuIdsApi, batchUpdateCompanyMenusApi } from '#/api/organization/company-menu'
import type { MenuVO } from '#/api/system/menu/types'
// 响应式数据
const visible = ref(false)
const loading = ref(false)
const submitLoading = ref(false)
const currentCompany = ref<any>(null)
const allMenus = ref<MenuVO.MenuInfo[]>([])
const companyMenuIds = ref<number[]>([])
const checkedKeys = ref<number[]>([])
const expandedKeys = ref<number[]>([])
// 树形控件配置
const treeProps = {
title: 'name',
key: 'id',
children: 'children',
}
// 树形数据
const treeData = computed(() => {
return buildTreeData(allMenus.value)
})
// 构建树形数据
function buildTreeData(menus: MenuVO.MenuInfo[]): any[] {
// 如果菜单数据已经是树形结构(有children字段),直接转换
if (menus.some(menu => menu.children && menu.children.length > 0)) {
return menus.map(menu => ({
key: menu.id,
title: menu.name,
children: menu.children ? buildTreeData(menu.children) : undefined,
}))
}
// 如果菜单数据是扁平结构(只有parentId字段),需要构建树形结构
return buildTreeFromFlatData(menus)
}
// 从扁平数据构建树形结构
function buildTreeFromFlatData(menus: MenuVO.MenuInfo[]): any[] {
const menuMap = new Map<number, any>()
const rootMenus: any[] = []
// 创建所有菜单节点的映射
menus.forEach(menu => {
menuMap.set(menu.id, {
key: menu.id,
title: menu.name,
children: []
})
})
// 构建父子关系
menus.forEach(menu => {
const node = menuMap.get(menu.id)!
if (menu.parentId && menuMap.has(menu.parentId)) {
// 有父级菜单,添加到父级的children中
const parent = menuMap.get(menu.parentId)!
parent.children.push(node)
} else {
// 没有父级菜单,是根级菜单
rootMenus.push(node)
}
})
// 清理空的children数组
function cleanEmptyChildren(nodes: any[]): any[] {
return nodes.map(node => ({
...node,
children: node.children.length > 0 ? cleanEmptyChildren(node.children) : undefined
}))
}
return cleanEmptyChildren(rootMenus)
}
// 监听抽屉打开状态
watch(visible, (newVisible) => {
if (newVisible && currentCompany.value) {
loadData()
}
})
// 加载数据
async function loadData() {
if (!currentCompany.value) return
try {
loading.value = true
// 并行加载所有菜单和公司菜单ID
const [menusResult, menuIdsResult] = await Promise.all([
getAllMenusApi(),
getCompanyMenuIdsApi(currentCompany.value.id)
])
allMenus.value = menusResult
companyMenuIds.value = menuIdsResult
checkedKeys.value = [...menuIdsResult]
// 自动展开所有节点
expandedKeys.value = getAllMenuIds(menusResult)
} catch (error) {
console.error('加载数据失败:', error)
message.error('加载数据失败')
} finally {
loading.value = false
}
}
// 获取所有菜单ID(用于展开)
function getAllMenuIds(menus: MenuVO.MenuInfo[]): number[] {
const ids: number[] = []
// 如果菜单数据已经是树形结构
if (menus.some(menu => menu.children && menu.children.length > 0)) {
menus.forEach(menu => {
ids.push(menu.id)
if (menu.children) {
ids.push(...getAllMenuIds(menu.children))
}
})
} else {
// 如果菜单数据是扁平结构,直接返回所有ID
menus.forEach(menu => {
ids.push(menu.id)
})
}
return ids
}
// 处理勾选变化
function handleCheck(checkedKeysValue: any, _info: any) {
// 处理checkedKeysValue可能是数组或对象的情况
const keys = Array.isArray(checkedKeysValue)
? checkedKeysValue
: checkedKeysValue.checked || []
checkedKeys.value = keys.map((key: any) => Number(key))
}
// 全选
function handleSelectAll() {
checkedKeys.value = getAllMenuIds(allMenus.value)
}
// 取消全选
function handleUnselectAll() {
checkedKeys.value = []
}
// 展开全部
function handleExpandAll() {
expandedKeys.value = getAllMenuIds(allMenus.value)
}
// 折叠全部
function handleCollapseAll() {
expandedKeys.value = []
}
// 提交更新
async function handleSubmit() {
if (!currentCompany.value) return
try {
submitLoading.value = true
await batchUpdateCompanyMenusApi(currentCompany.value.id, checkedKeys.value)
message.success('菜单权限更新成功')
close()
} catch (error) {
console.error('更新菜单权限失败:', error)
message.error('更新菜单权限失败')
} finally {
submitLoading.value = false
}
}
// 打开抽屉
function open(company: any) {
currentCompany.value = company
visible.value = true
}
// 关闭抽屉
function close() {
visible.value = false
currentCompany.value = null
allMenus.value = []
companyMenuIds.value = []
checkedKeys.value = []
expandedKeys.value = []
}
// 暴露方法
defineExpose({
open,
close,
})
</script>
<template>
<Drawer
v-model:open="visible"
title="公司菜单权限管理"
width="600px"
:closable="true"
:body-style="{ display: 'flex', flexDirection: 'column', height: '100%', padding: '16px' }"
@close="close"
>
<div v-if="currentCompany" class="mb-4 flex-shrink-0">
<div class="text-lg font-medium">{{ currentCompany.name }}</div>
<div class="text-gray-500 text-sm">管理该公司的菜单访问权限</div>
</div>
<Spin :spinning="loading" class="flex-1 flex flex-col min-h-0">
<div v-if="!loading && treeData.length > 0" class="flex-1 flex flex-col">
<div class="mb-4 flex-shrink-0">
<div class="text-sm text-gray-600 mb-2">
已选择 {{ checkedKeys.length }} 个菜单
</div>
<div class="text-xs text-gray-400 mb-3">
勾选菜单后,该公司将拥有对应菜单的访问权限
</div>
<div class="flex space-x-2">
<Button size="small" @click="handleSelectAll">
全选
</Button>
<Button size="small" @click="handleUnselectAll">
取消全选
</Button>
<Button size="small" @click="handleExpandAll">
展开全部
</Button>
<Button size="small" @click="handleCollapseAll">
折叠全部
</Button>
</div>
</div>
<div class="flex-1 min-h-0">
<Tree
v-model:checkedKeys="checkedKeys"
v-model:expandedKeys="expandedKeys"
:tree-data="treeData"
:checkable="true"
:show-line="true"
:check-strictly="false"
:tree-props="treeProps"
:default-expand-all="false"
:auto-expand-parent="true"
class="h-full overflow-y-auto border border-gray-200 rounded-lg p-4"
@check="handleCheck"
/>
</div>
</div>
<div v-else-if="!loading && treeData.length === 0" class="flex-1 flex items-center justify-center text-gray-400">
暂无菜单数据
</div>
</Spin>
<template #footer>
<div class="flex justify-end space-x-2">
<Button @click="close">
取消
</Button>
<Button
type="primary"
:loading="submitLoading"
@click="handleSubmit"
>
确定
</Button>
</div>
</template>
</Drawer>
</template>
<style scoped>
:deep(.ant-tree) {
background: transparent;
}
:deep(.ant-tree-node-content-wrapper) {
padding: 4px 8px;
border-radius: 4px;
}
:deep(.ant-tree-node-content-wrapper:hover) {
background-color: #f5f5f5;
}
:deep(.ant-tree-checkbox) {
margin-right: 8px;
}
:deep(.ant-tree-title) {
font-size: 14px;
color: #333;
}
</style>
@@ -15,6 +15,7 @@ import {
} from '#/api/organization/company';
import type { PageQueryDTO } from '#/types/common';
import CompanyFormModal from './components/CompanyFormModal.vue';
import CompanyMenuDrawer from '#/components/CompanyMenuDrawer.vue';
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
@@ -24,6 +25,7 @@ defineOptions({ name: 'CompanyManagement' });
// 响应式数据
const loading = ref(false);
const companyFormModalRef = ref();
const companyMenuDrawerRef = ref();
// 搜索表单
const searchForm = ref({
@@ -261,6 +263,11 @@ function handleModalSuccess() {
refreshTable();
}
// 处理菜单管理
function handleMenuManage(record: any) {
companyMenuDrawerRef.value?.open(record);
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
@@ -332,6 +339,9 @@ const [Grid, gridApi] = useVbenVxeGrid({
<Button size="small" @click="handleEdit(row)">
编辑
</Button>
<Button size="small" type="primary" @click="handleMenuManage(row)">
菜单
</Button>
<Button size="small" danger @click="handleDelete(row)">
删除
</Button>
@@ -344,5 +354,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
ref="companyFormModalRef"
@success="handleModalSuccess"
/>
<!-- 公司菜单管理抽屉 -->
<CompanyMenuDrawer ref="companyMenuDrawerRef" />
</Page>
</template>