mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49:19 +08:00
prettier: 代码格式化
This commit is contained in:
@@ -1,56 +1,63 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { useVbenDrawer } from '@vben/common-ui'
|
||||
import { Button, message, Select, Spin, Tree } 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'
|
||||
import type { MenuVO } from '#/api/system/menu/types';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenDrawer } from '@vben/common-ui';
|
||||
|
||||
import { Button, message, Select, Spin, Tree } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
batchUpdateCompanyMenusApi,
|
||||
getCompanyMenuIdsApi,
|
||||
} from '#/api/organization/company-menu';
|
||||
import { getAllMenusApi } from '#/api/system/menu';
|
||||
|
||||
interface Props {
|
||||
company?: any
|
||||
company?: any;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void
|
||||
(e: 'close'): void
|
||||
(e: 'success'): void;
|
||||
(e: 'close'): void;
|
||||
}
|
||||
|
||||
defineOptions({
|
||||
name: 'CompanyMenuDrawer',
|
||||
})
|
||||
});
|
||||
|
||||
defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 系统菜单列表数据
|
||||
const systemMenuList = ref<MenuVO.MenuInfo[]>([])
|
||||
const systemMenuList = ref<MenuVO.MenuInfo[]>([]);
|
||||
|
||||
// 公司菜单ID列表
|
||||
const companyMenuIds = ref<number[]>([])
|
||||
const companyMenuIds = ref<number[]>([]);
|
||||
|
||||
// 加载状态
|
||||
const loading = ref(false)
|
||||
const loading = ref(false);
|
||||
|
||||
// 保存状态
|
||||
const saving = ref(false)
|
||||
const saving = ref(false);
|
||||
|
||||
// 当前公司信息
|
||||
const currentCompany = ref<any>()
|
||||
const currentCompany = ref<any>();
|
||||
|
||||
// 选中的菜单ID
|
||||
const checkedKeys = ref<number[]>([])
|
||||
const checkedKeys = ref<number[]>([]);
|
||||
|
||||
// 展开的节点键
|
||||
const expandedKeys = ref<number[]>([])
|
||||
const expandedKeys = ref<number[]>([]);
|
||||
|
||||
// 分组选择
|
||||
const selectedGroup = ref<'super' | 'normal' | undefined>(undefined)
|
||||
const selectedGroup = ref<'normal' | 'super' | undefined>(undefined);
|
||||
|
||||
// 分组选项
|
||||
const groupOptions = [
|
||||
{ label: '超管菜单', value: 'super' as const },
|
||||
{ label: '普通菜单', value: 'normal' as const },
|
||||
]
|
||||
];
|
||||
|
||||
// 创建抽屉组件
|
||||
const [MainDrawer, mainDrawerApi] = useVbenDrawer({
|
||||
@@ -61,20 +68,20 @@ const [MainDrawer, mainDrawerApi] = useVbenDrawer({
|
||||
confirmText: '保存权限配置',
|
||||
cancelText: '取消',
|
||||
onConfirm: async () => {
|
||||
await handleSave()
|
||||
await handleSave();
|
||||
},
|
||||
onCancel() {
|
||||
handleClose()
|
||||
handleClose();
|
||||
},
|
||||
onClosed() {
|
||||
emit('close')
|
||||
emit('close');
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
// 将菜单列表转换为树形结构
|
||||
const menuTreeData = computed(() => {
|
||||
const menuMap = new Map<number, any>()
|
||||
const rootMenus: any[] = []
|
||||
const menuMap = new Map<number, any>();
|
||||
const rootMenus: any[] = [];
|
||||
|
||||
// 先创建所有节点
|
||||
systemMenuList.value.forEach((menu) => {
|
||||
@@ -86,97 +93,99 @@ const menuTreeData = computed(() => {
|
||||
status: menu.status,
|
||||
disabled: menu.status === 0, // 禁用状态的菜单不可选择
|
||||
children: [],
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
// 构建树形结构
|
||||
systemMenuList.value.forEach((menu) => {
|
||||
const node = menuMap.get(menu.id)!
|
||||
const node = menuMap.get(menu.id)!;
|
||||
if (menu.parentId && menuMap.has(menu.parentId)) {
|
||||
const parent = menuMap.get(menu.parentId)!
|
||||
parent.children.push(node)
|
||||
const parent = menuMap.get(menu.parentId)!;
|
||||
parent.children.push(node);
|
||||
} else {
|
||||
rootMenus.push(node)
|
||||
rootMenus.push(node);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return rootMenus
|
||||
})
|
||||
return rootMenus;
|
||||
});
|
||||
|
||||
// 统计信息
|
||||
const stats = computed(() => {
|
||||
const totalMenus = systemMenuList.value.length
|
||||
const companyMenus = companyMenuIds.value.length
|
||||
const selectedMenus = calculateCorrectSelectedCount(checkedKeys.value)
|
||||
const totalMenus = systemMenuList.value.length;
|
||||
const companyMenus = companyMenuIds.value.length;
|
||||
const selectedMenus = calculateCorrectSelectedCount(checkedKeys.value);
|
||||
return {
|
||||
total: totalMenus,
|
||||
company: companyMenus,
|
||||
selected: selectedMenus,
|
||||
}
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
// 计算正确的选中数量(处理父节点逻辑)
|
||||
function calculateCorrectSelectedCount(menuIds: number[]): number {
|
||||
// 使用与提交时相同的逻辑,确保统计数量与提交数量一致
|
||||
const finalMenuIds = ensureParentMenuIds(menuIds)
|
||||
return finalMenuIds.length
|
||||
const finalMenuIds = ensureParentMenuIds(menuIds);
|
||||
return finalMenuIds.length;
|
||||
}
|
||||
|
||||
// 清理根节点,只保留叶子节点
|
||||
function cleanRootNodes(menuIds: number[]): number[] {
|
||||
return menuIds.filter((menuId) => {
|
||||
const menu = systemMenuList.value.find((m) => m.id === menuId)
|
||||
if (!menu) return false
|
||||
|
||||
const menu = systemMenuList.value.find((m) => m.id === menuId);
|
||||
if (!menu) return false;
|
||||
|
||||
// 检查是否有其他菜单将此菜单作为父菜单
|
||||
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menuId)
|
||||
|
||||
const hasChildren = systemMenuList.value.some(
|
||||
(otherMenu) => otherMenu.parentId === menuId,
|
||||
);
|
||||
|
||||
// 只保留叶子节点(没有子节点的菜单)
|
||||
return !hasChildren
|
||||
})
|
||||
return !hasChildren;
|
||||
});
|
||||
}
|
||||
|
||||
// 加载数据
|
||||
async function loadData() {
|
||||
if (!currentCompany.value?.id) return
|
||||
loading.value = true
|
||||
if (!currentCompany.value?.id) return;
|
||||
loading.value = true;
|
||||
try {
|
||||
const [systemData, companyData] = await Promise.all([
|
||||
getAllMenusApi(),
|
||||
getCompanyMenuIdsApi(currentCompany.value.id),
|
||||
])
|
||||
systemMenuList.value = systemData
|
||||
companyMenuIds.value = companyData
|
||||
|
||||
]);
|
||||
systemMenuList.value = systemData;
|
||||
companyMenuIds.value = companyData;
|
||||
|
||||
// 清理根节点,只保留叶子节点
|
||||
const leafMenuIds = cleanRootNodes(companyData)
|
||||
checkedKeys.value = leafMenuIds
|
||||
|
||||
const leafMenuIds = cleanRootNodes(companyData);
|
||||
checkedKeys.value = leafMenuIds;
|
||||
|
||||
// 自动展开所有节点
|
||||
expandedKeys.value = systemData.map((menu) => menu.id)
|
||||
expandedKeys.value = systemData.map((menu) => menu.id);
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '加载数据失败')
|
||||
message.error(error?.message || '加载数据失败');
|
||||
} finally {
|
||||
loading.value = false
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存所有更改
|
||||
async function handleSave() {
|
||||
if (!currentCompany.value?.id) return
|
||||
saving.value = true
|
||||
if (!currentCompany.value?.id) return;
|
||||
saving.value = true;
|
||||
try {
|
||||
// 确保提交时包含必要的父菜单ID
|
||||
const finalMenuIds = ensureParentMenuIds(checkedKeys.value)
|
||||
await batchUpdateCompanyMenusApi(currentCompany.value.id, finalMenuIds)
|
||||
message.success('权限配置保存成功')
|
||||
emit('success')
|
||||
handleClose()
|
||||
const finalMenuIds = ensureParentMenuIds(checkedKeys.value);
|
||||
await batchUpdateCompanyMenusApi(currentCompany.value.id, finalMenuIds);
|
||||
message.success('权限配置保存成功');
|
||||
emit('success');
|
||||
handleClose();
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '保存失败')
|
||||
throw error // 重新抛出错误,让抽屉知道保存失败
|
||||
message.error(error?.message || '保存失败');
|
||||
throw error; // 重新抛出错误,让抽屉知道保存失败
|
||||
} finally {
|
||||
saving.value = false
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,41 +194,46 @@ function handleSelectAll() {
|
||||
// 获取所有可选择的菜单ID(排除禁用的)
|
||||
let selectableMenus = systemMenuList.value.filter(
|
||||
(menu) => menu.status === 1,
|
||||
)
|
||||
);
|
||||
|
||||
// 如果选择了分组,则只选择该分组的菜单
|
||||
if (selectedGroup.value) {
|
||||
if (selectedGroup.value === 'super') {
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu === 1)
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu === 1);
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu !== 1)
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu !== 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 只选择叶子节点(没有子节点的菜单)
|
||||
const leafMenus = selectableMenus.filter((menu) => {
|
||||
// 检查是否有其他菜单将此菜单作为父菜单
|
||||
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
return !hasChildren
|
||||
})
|
||||
const hasChildren = systemMenuList.value.some(
|
||||
(otherMenu) => otherMenu.parentId === menu.id,
|
||||
);
|
||||
return !hasChildren;
|
||||
});
|
||||
|
||||
if (selectedGroup.value) {
|
||||
// 保留其他分组的选中状态,只替换当前分组的选中状态
|
||||
const otherGroupCheckedKeys = checkedKeys.value.filter((id) => {
|
||||
const menu = systemMenuList.value.find((m) => m.id === id)
|
||||
if (!menu) return false
|
||||
|
||||
const menu = systemMenuList.value.find((m) => m.id === id);
|
||||
if (!menu) return false;
|
||||
|
||||
if (selectedGroup.value === 'super') {
|
||||
return menu.superMenu !== 1
|
||||
return menu.superMenu !== 1;
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
return menu.superMenu === 1
|
||||
return menu.superMenu === 1;
|
||||
}
|
||||
return false
|
||||
})
|
||||
checkedKeys.value = [...otherGroupCheckedKeys, ...leafMenus.map((menu) => menu.id)]
|
||||
return false;
|
||||
});
|
||||
checkedKeys.value = [
|
||||
...otherGroupCheckedKeys,
|
||||
...leafMenus.map((menu) => menu.id),
|
||||
];
|
||||
} else {
|
||||
// 如果没有选择分组,全选所有叶子节点
|
||||
checkedKeys.value = leafMenus.map((menu) => menu.id)
|
||||
checkedKeys.value = leafMenus.map((menu) => menu.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,87 +241,96 @@ function handleSelectAll() {
|
||||
function handleSelectNone() {
|
||||
if (selectedGroup.value) {
|
||||
// 如果选择了分组,只取消该分组的叶子节点选中状态
|
||||
const leafMenuIds = systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
if (hasChildren) return false
|
||||
|
||||
// 根据分组过滤
|
||||
if (selectedGroup.value === 'super') {
|
||||
return menu.superMenu === 1
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
return menu.superMenu !== 1
|
||||
}
|
||||
return false
|
||||
})
|
||||
.map((menu) => menu.id)
|
||||
|
||||
checkedKeys.value = checkedKeys.value.filter((id) => !leafMenuIds.includes(id))
|
||||
const leafMenuIds = new Set(
|
||||
systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
const hasChildren = systemMenuList.value.some(
|
||||
(otherMenu) => otherMenu.parentId === menu.id,
|
||||
);
|
||||
if (hasChildren) return false;
|
||||
|
||||
// 根据分组过滤
|
||||
if (selectedGroup.value === 'super') {
|
||||
return menu.superMenu === 1;
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
return menu.superMenu !== 1;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.map((menu) => menu.id),
|
||||
);
|
||||
|
||||
checkedKeys.value = checkedKeys.value.filter((id) => !leafMenuIds.has(id));
|
||||
} else {
|
||||
// 如果没有选择分组,取消所有叶子节点的选中状态
|
||||
const allLeafMenuIds = systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
return !systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
})
|
||||
.map((menu) => menu.id)
|
||||
|
||||
checkedKeys.value = checkedKeys.value.filter((id) => !allLeafMenuIds.includes(id))
|
||||
const allLeafMenuIds = new Set(
|
||||
systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
return !systemMenuList.value.some(
|
||||
(otherMenu) => otherMenu.parentId === menu.id,
|
||||
);
|
||||
})
|
||||
.map((menu) => menu.id),
|
||||
);
|
||||
|
||||
checkedKeys.value = checkedKeys.value.filter(
|
||||
(id) => !allLeafMenuIds.has(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理树形控件的选中变化
|
||||
function handleTreeCheckChange(checkedKeysValue: any) {
|
||||
checkedKeys.value = checkedKeysValue
|
||||
checkedKeys.value = checkedKeysValue;
|
||||
}
|
||||
|
||||
// 确保提交时包含必要的父菜单ID(递归添加所有层级的父菜单)
|
||||
function ensureParentMenuIds(menuIds: number[]): number[] {
|
||||
const result = new Set(menuIds)
|
||||
|
||||
const result = new Set(menuIds);
|
||||
|
||||
// 递归添加所有父菜单ID
|
||||
function addAllParentIds(menuId: number) {
|
||||
const menu = systemMenuList.value.find((m) => m.id === menuId)
|
||||
const menu = systemMenuList.value.find((m) => m.id === menuId);
|
||||
if (menu && menu.parentId) {
|
||||
result.add(menu.parentId)
|
||||
result.add(menu.parentId);
|
||||
// 递归添加父菜单的父菜单
|
||||
addAllParentIds(menu.parentId)
|
||||
addAllParentIds(menu.parentId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 为每个选中的菜单添加其所有父菜单ID
|
||||
menuIds.forEach((menuId) => {
|
||||
addAllParentIds(menuId)
|
||||
})
|
||||
|
||||
const finalResult = [...result]
|
||||
return finalResult
|
||||
}
|
||||
addAllParentIds(menuId);
|
||||
});
|
||||
|
||||
const finalResult = [...result];
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
// 关闭抽屉
|
||||
function handleClose() {
|
||||
mainDrawerApi.close()
|
||||
checkedKeys.value = []
|
||||
expandedKeys.value = []
|
||||
selectedGroup.value = undefined
|
||||
mainDrawerApi.close();
|
||||
checkedKeys.value = [];
|
||||
expandedKeys.value = [];
|
||||
selectedGroup.value = undefined;
|
||||
}
|
||||
|
||||
// 暴露打开方法
|
||||
defineExpose({
|
||||
open: (company: any) => {
|
||||
// 设置公司信息
|
||||
currentCompany.value = company
|
||||
currentCompany.value = company;
|
||||
// 打开抽屉
|
||||
mainDrawerApi.open()
|
||||
mainDrawerApi.open();
|
||||
// 加载数据
|
||||
if (company?.id) {
|
||||
loadData()
|
||||
loadData();
|
||||
}
|
||||
},
|
||||
close: () => mainDrawerApi.close(),
|
||||
})
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -349,12 +372,8 @@ defineExpose({
|
||||
{{ option.label }}
|
||||
</Select.Option>
|
||||
</Select>
|
||||
<Button type="primary" @click="handleSelectAll">
|
||||
一键全选
|
||||
</Button>
|
||||
<Button @click="handleSelectNone">
|
||||
取消全选
|
||||
</Button>
|
||||
<Button type="primary" @click="handleSelectAll"> 一键全选 </Button>
|
||||
<Button @click="handleSelectNone"> 取消全选 </Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user