mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 19:09:20 +08:00
refactor: 调整页面组件位置
This commit is contained in:
@@ -0,0 +1,410 @@
|
||||
<script setup lang="ts">
|
||||
import type { CompanyVO } from '#/api/organization/company/types';
|
||||
|
||||
import { computed, onMounted, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Card,
|
||||
Empty,
|
||||
Flex,
|
||||
Input,
|
||||
message,
|
||||
Pagination,
|
||||
Select,
|
||||
Space,
|
||||
Spin,
|
||||
Tag,
|
||||
Tree,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
batchUpdateCompanyMenusApi,
|
||||
getCompanyMenusApi,
|
||||
getCompanyMenuIdsApi,
|
||||
} from '#/api/organization/company-menu/apis';
|
||||
import { getCompanyPageApi } from '#/api/organization/company';
|
||||
import {
|
||||
SIDEBAR_LIST_SHELL_CLASS,
|
||||
sidebarListRowClass,
|
||||
} from '#/utils/sidebar-list-styles';
|
||||
|
||||
defineOptions({
|
||||
name: 'CompanyMenuManagePage',
|
||||
});
|
||||
|
||||
const BuildingIcon = createIconifyIcon('lucide:building-2');
|
||||
|
||||
const companySearchForm = ref({
|
||||
name: '',
|
||||
status: undefined as number | undefined,
|
||||
});
|
||||
|
||||
const companyList = ref<CompanyVO.CompanyInfoVO[]>([]);
|
||||
const companyLoading = ref(false);
|
||||
const companyPagination = ref({
|
||||
current: 1,
|
||||
pageSize: 20,
|
||||
total: 0,
|
||||
});
|
||||
|
||||
const currentCompany = ref<CompanyVO.CompanyInfoVO | null>(null);
|
||||
|
||||
const systemMenuList = ref<any[]>([]);
|
||||
const companyMenuIds = ref<number[]>([]);
|
||||
const selectedMenuIds = ref<number[]>([]);
|
||||
const menuLoading = ref(false);
|
||||
|
||||
const selectedCompanyLabel = computed(() => {
|
||||
if (!currentCompany.value) {
|
||||
return '请先选择公司';
|
||||
}
|
||||
return currentCompany.value.name;
|
||||
});
|
||||
|
||||
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,
|
||||
}));
|
||||
|
||||
async function searchCompanies() {
|
||||
companyLoading.value = true;
|
||||
try {
|
||||
const result = await getCompanyPageApi({
|
||||
page: companyPagination.value.current,
|
||||
pageSize: companyPagination.value.pageSize,
|
||||
filters: {
|
||||
name: companySearchForm.value.name || undefined,
|
||||
status: companySearchForm.value.status,
|
||||
},
|
||||
});
|
||||
companyList.value = result.records;
|
||||
companyPagination.value.total = result.total;
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '获取公司列表失败');
|
||||
} finally {
|
||||
companyLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function selectCompany(company: CompanyVO.CompanyInfoVO) {
|
||||
currentCompany.value = company;
|
||||
await 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];
|
||||
} 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 || '保存失败');
|
||||
}
|
||||
}
|
||||
|
||||
function resetCompanySearch() {
|
||||
companySearchForm.value = { name: '', status: undefined };
|
||||
companyPagination.value.current = 1;
|
||||
searchCompanies();
|
||||
}
|
||||
|
||||
function handleCompanyPageChange(page: number, pageSize: number) {
|
||||
companyPagination.value.current = page;
|
||||
companyPagination.value.pageSize = pageSize;
|
||||
searchCompanies();
|
||||
}
|
||||
|
||||
function companyRowClass(companyId: number) {
|
||||
const active = currentCompany.value?.id === companyId;
|
||||
return sidebarListRowClass(active, 'w-full cursor-pointer text-left');
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
searchCompanies();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page title="公司菜单管理" :auto-content-height="true">
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
左侧选择公司,右侧勾选并保存该公司可访问的菜单。
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #extra>
|
||||
<Space>
|
||||
<Button :disabled="!currentCompany" @click="selectNone">取消全选</Button>
|
||||
<Button type="primary" :disabled="!currentCompany" @click="selectAll">
|
||||
一键全选
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
:disabled="!currentCompany || !stats.changed"
|
||||
@click="handleSave"
|
||||
>
|
||||
保存配置
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
<Flex gap="middle" class="h-full min-h-0">
|
||||
<!-- 左:公司 -->
|
||||
<Card
|
||||
class="flex h-full w-[420px] shrink-0 flex-col"
|
||||
:body-style="{
|
||||
height: '100%',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
gap: '8px',
|
||||
minHeight: 0,
|
||||
padding: '12px',
|
||||
}"
|
||||
>
|
||||
<template #title>
|
||||
<span class="flex items-center gap-2 text-base font-semibold">
|
||||
<BuildingIcon class="h-4 w-4" />
|
||||
公司
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<Flex wrap="wrap" align="center" class="shrink-0 gap-2">
|
||||
<Input
|
||||
v-model:value="companySearchForm.name"
|
||||
placeholder="公司名称"
|
||||
allow-clear
|
||||
class="min-w-[140px] flex-1"
|
||||
@press-enter="searchCompanies"
|
||||
/>
|
||||
<Select
|
||||
v-model:value="companySearchForm.status"
|
||||
placeholder="状态"
|
||||
allow-clear
|
||||
class="w-[88px]"
|
||||
:options="[
|
||||
{ label: '启用', value: 1 },
|
||||
{ label: '禁用', value: 0 },
|
||||
]"
|
||||
/>
|
||||
<Button type="primary" size="small" @click="searchCompanies">
|
||||
搜索
|
||||
</Button>
|
||||
<Button size="small" @click="resetCompanySearch">重置</Button>
|
||||
</Flex>
|
||||
|
||||
<Spin :spinning="companyLoading" class="flex min-h-0 flex-1 flex-col">
|
||||
<div :class="SIDEBAR_LIST_SHELL_CLASS">
|
||||
<Empty
|
||||
v-if="!companyLoading && companyList.length === 0"
|
||||
class="py-8"
|
||||
description="暂无公司"
|
||||
/>
|
||||
<div v-else class="space-y-1">
|
||||
<button
|
||||
v-for="company in companyList"
|
||||
:key="company.id"
|
||||
type="button"
|
||||
:class="companyRowClass(company.id)"
|
||||
@click="selectCompany(company)"
|
||||
>
|
||||
<span class="block truncate">{{ company.name }}</span>
|
||||
<span
|
||||
class="mt-0.5 block truncate text-xs font-normal text-muted-foreground"
|
||||
>
|
||||
{{ company.status === 1 ? '启用' : '禁用' }}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Flex justify="center" class="mt-2 shrink-0 pt-1">
|
||||
<Pagination
|
||||
v-model:current="companyPagination.current"
|
||||
v-model:page-size="companyPagination.pageSize"
|
||||
:total="companyPagination.total"
|
||||
size="small"
|
||||
:show-size-changer="true"
|
||||
:show-quick-jumper="true"
|
||||
@change="handleCompanyPageChange"
|
||||
/>
|
||||
</Flex>
|
||||
</Spin>
|
||||
</Card>
|
||||
|
||||
<!-- 右:菜单分配 -->
|
||||
<Card
|
||||
class="flex h-full min-h-0 min-w-0 flex-1 flex-col"
|
||||
:body-style="{
|
||||
height: '100%',
|
||||
minHeight: 0,
|
||||
padding: 0,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
}"
|
||||
>
|
||||
<template #title>
|
||||
<span class="text-base font-semibold">菜单分配</span>
|
||||
</template>
|
||||
|
||||
<div
|
||||
class="shrink-0 border-t border-border px-4 py-2 text-xs text-muted-foreground"
|
||||
>
|
||||
当前:{{ selectedCompanyLabel }}
|
||||
<template v-if="currentCompany">
|
||||
<Tag
|
||||
class="ml-2"
|
||||
:color="currentCompany.status === 1 ? 'green' : 'red'"
|
||||
>
|
||||
{{ currentCompany.status === 1 ? '启用' : '禁用' }}
|
||||
</Tag>
|
||||
<span class="ml-3">
|
||||
系统菜单 {{ 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>
|
||||
</div>
|
||||
|
||||
<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"
|
||||
:tree-data="menuTreeData"
|
||||
checkable
|
||||
:default-expand-all="true"
|
||||
class="company-menu-tree"
|
||||
@check="handleTreeCheckChange"
|
||||
>
|
||||
<template #title="{ title, status }">
|
||||
<span>
|
||||
{{ title }}
|
||||
<span v-if="status === 0" class="ml-2 text-xs text-gray-400">
|
||||
(已禁用)
|
||||
</span>
|
||||
</span>
|
||||
</template>
|
||||
</Tree>
|
||||
</Spin>
|
||||
</div>
|
||||
</Card>
|
||||
</Flex>
|
||||
</Page>
|
||||
</template>
|
||||
Reference in New Issue
Block a user