mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 11:09:20 +08:00
256 lines
7.4 KiB
Vue
256 lines
7.4 KiB
Vue
<script setup lang="ts">
|
||
import type { CompanyVO } from '#/api/company/types';
|
||
|
||
import { computed, ref } from 'vue';
|
||
|
||
import {
|
||
Button,
|
||
Card,
|
||
Empty,
|
||
message,
|
||
Space,
|
||
Spin,
|
||
Tag,
|
||
Tree,
|
||
} from 'ant-design-vue';
|
||
|
||
import {
|
||
batchUpdateCompanyMenusApi,
|
||
getCompanyMenusApi,
|
||
getCompanyMenuIdsApi,
|
||
} from '#/api/company/menu/apis';
|
||
import { PermissionGate } from '#/components/permission';
|
||
import { usePermission } from '#/composables/use-permission';
|
||
import CompanyColPage from '#/views/company/components/CompanyColPage.vue';
|
||
|
||
defineOptions({
|
||
name: 'CompanyMenuManagePage',
|
||
});
|
||
|
||
const { hasPermission } = usePermission();
|
||
const canAssignMenu = computed(() => hasPermission('org:company:menu:assign'));
|
||
|
||
const selectedCompanyId = ref<number | undefined>(undefined);
|
||
const currentCompany = ref<CompanyVO.CompanyInfoVO | null>(null);
|
||
|
||
const systemMenuList = ref<any[]>([]);
|
||
const companyMenuIds = ref<number[]>([]);
|
||
const selectedMenuIds = ref<number[]>([]);
|
||
const menuExpandedKeys = ref<(number | string)[]>([]);
|
||
const menuLoading = ref(false);
|
||
|
||
const menuTreeData = computed(() => {
|
||
const list = systemMenuList.value;
|
||
const map = new Map<number, any>();
|
||
const roots: any[] = [];
|
||
|
||
list.forEach((menu) => {
|
||
map.set(menu.id, { ...menu, key: menu.id, title: menu.title, children: [] });
|
||
});
|
||
|
||
list.forEach((menu) => {
|
||
const node = map.get(menu.id)!;
|
||
if (menu.parentId && map.has(menu.parentId)) {
|
||
map.get(menu.parentId)!.children.push(node);
|
||
} else {
|
||
roots.push(node);
|
||
}
|
||
});
|
||
|
||
return roots;
|
||
});
|
||
|
||
const stats = computed(() => ({
|
||
total: systemMenuList.value.filter((m) => m.type === 'menu').length,
|
||
company: companyMenuIds.value.length,
|
||
selected: selectedMenuIds.value.length,
|
||
changed: selectedMenuIds.value.length !== companyMenuIds.value.length,
|
||
}));
|
||
|
||
function onCompanySelect(company: CompanyVO.CompanyInfoVO) {
|
||
currentCompany.value = company;
|
||
selectedCompanyId.value = company.id;
|
||
void loadCompanyMenus();
|
||
}
|
||
|
||
async function loadCompanyMenus() {
|
||
if (!currentCompany.value?.id) return;
|
||
menuLoading.value = true;
|
||
try {
|
||
const [allMenus, menuIds] = await Promise.all([
|
||
getCompanyMenusApi(),
|
||
getCompanyMenuIdsApi(currentCompany.value.id),
|
||
]);
|
||
systemMenuList.value = allMenus || [];
|
||
companyMenuIds.value = menuIds || [];
|
||
selectedMenuIds.value = [...companyMenuIds.value];
|
||
menuExpandedKeys.value = (allMenus || []).map((m: { id: number }) => m.id);
|
||
} catch (error: any) {
|
||
message.error(error?.message || '加载菜单失败');
|
||
} finally {
|
||
menuLoading.value = false;
|
||
}
|
||
}
|
||
|
||
function handleTreeCheckChange(
|
||
checked: (number | string)[] | { checked: (number | string)[]; halfChecked: (number | string)[] },
|
||
) {
|
||
const checkedKeys = Array.isArray(checked) ? checked : checked.checked;
|
||
const oldKeys = new Set<number>(selectedMenuIds.value);
|
||
const newKeys = new Set<number>(checkedKeys as number[]);
|
||
|
||
const removedKeys = [...oldKeys].filter((k) => !newKeys.has(k));
|
||
const addedKeys = [...newKeys].filter((k) => !oldKeys.has(k));
|
||
|
||
if (removedKeys.length > 0) {
|
||
const parentToChildren = new Map<number, number[]>();
|
||
for (const menu of systemMenuList.value) {
|
||
if (menu.parentId) {
|
||
const children = parentToChildren.get(menu.parentId) || [];
|
||
children.push(menu.id);
|
||
parentToChildren.set(menu.parentId, children);
|
||
}
|
||
}
|
||
|
||
function removeDescendants(nodeId: number) {
|
||
const children = parentToChildren.get(nodeId) || [];
|
||
for (const childId of children) {
|
||
newKeys.delete(childId);
|
||
removeDescendants(childId);
|
||
}
|
||
}
|
||
|
||
for (const removedId of removedKeys) {
|
||
removeDescendants(removedId);
|
||
}
|
||
}
|
||
|
||
if (addedKeys.length > 0) {
|
||
const menuMap = new Map<number, any>();
|
||
for (const menu of systemMenuList.value) {
|
||
menuMap.set(menu.id, menu);
|
||
}
|
||
|
||
function addAncestors(menuId: number) {
|
||
const menu = menuMap.get(menuId);
|
||
if (menu?.parentId && menu.parentId > 0) {
|
||
newKeys.add(menu.parentId);
|
||
addAncestors(menu.parentId);
|
||
}
|
||
}
|
||
|
||
for (const addedId of addedKeys) {
|
||
addAncestors(addedId);
|
||
}
|
||
}
|
||
|
||
selectedMenuIds.value = [...newKeys];
|
||
}
|
||
|
||
function selectAll() {
|
||
selectedMenuIds.value = systemMenuList.value
|
||
.filter((m) => m.type === 'menu')
|
||
.map((m) => m.id);
|
||
}
|
||
|
||
function selectNone() {
|
||
selectedMenuIds.value = [];
|
||
}
|
||
|
||
async function handleSave() {
|
||
if (!currentCompany.value?.id) return;
|
||
try {
|
||
await batchUpdateCompanyMenusApi(currentCompany.value.id, selectedMenuIds.value);
|
||
message.success('菜单配置保存成功');
|
||
companyMenuIds.value = [...selectedMenuIds.value];
|
||
} catch (error: any) {
|
||
message.error(error?.message || '保存失败');
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<CompanyColPage
|
||
:selected-company-id="selectedCompanyId"
|
||
@select="onCompanySelect"
|
||
>
|
||
<Card
|
||
class="ml-1 flex h-full min-h-0 min-w-0 flex-1 flex-col sm:ml-2"
|
||
:body-style="{
|
||
height: '100%',
|
||
minHeight: 0,
|
||
padding: 0,
|
||
display: 'flex',
|
||
flexDirection: 'column',
|
||
}"
|
||
>
|
||
<template #title>
|
||
<span class="text-base font-semibold">菜单分配</span>
|
||
</template>
|
||
|
||
<template #extra>
|
||
<Space wrap size="small">
|
||
<template v-if="currentCompany">
|
||
<Tag :color="currentCompany.status === 1 ? 'green' : 'red'">
|
||
{{ currentCompany.status === 1 ? '启用' : '禁用' }}
|
||
</Tag>
|
||
<span class="text-xs text-muted-foreground">
|
||
系统菜单 {{ stats.total }} · 已配置
|
||
<strong class="text-green-600">{{ stats.company }}</strong> · 当前
|
||
<strong class="text-blue-600">{{ stats.selected }}</strong>
|
||
<span v-if="stats.changed" class="text-orange-500">(未保存)</span>
|
||
</span>
|
||
</template>
|
||
<PermissionGate code="org:company:menu:assign">
|
||
<Button :disabled="!currentCompany" @click="selectNone">取消全选</Button>
|
||
<Button type="primary" :disabled="!currentCompany" @click="selectAll">
|
||
一键全选
|
||
</Button>
|
||
<Button
|
||
type="primary"
|
||
:disabled="!currentCompany || !stats.changed"
|
||
@click="handleSave"
|
||
>
|
||
保存配置
|
||
</Button>
|
||
</PermissionGate>
|
||
</Space>
|
||
</template>
|
||
|
||
<div
|
||
v-if="!currentCompany"
|
||
class="flex min-h-0 flex-1 items-center justify-center"
|
||
>
|
||
<Empty description="请从左侧选择公司以配置菜单" />
|
||
</div>
|
||
|
||
<div v-else class="min-h-0 flex-1 overflow-y-auto px-4 py-3 sm:px-5">
|
||
<Spin :spinning="menuLoading">
|
||
<Tree
|
||
v-model:checked-keys="selectedMenuIds"
|
||
v-model:expanded-keys="menuExpandedKeys"
|
||
:tree-data="menuTreeData"
|
||
:checkable="canAssignMenu"
|
||
block-node
|
||
:selectable="false"
|
||
@check="handleTreeCheckChange"
|
||
>
|
||
<template #title="{ title, status }">
|
||
<div class="flex min-w-0 items-center gap-2">
|
||
<span class="min-w-0 truncate font-medium">{{ title }}</span>
|
||
<Tag
|
||
v-if="status === 0"
|
||
color="default"
|
||
class="m-0 shrink-0"
|
||
>
|
||
已禁用
|
||
</Tag>
|
||
</div>
|
||
</template>
|
||
</Tree>
|
||
</Spin>
|
||
</div>
|
||
</Card>
|
||
</CompanyColPage>
|
||
</template>
|