feat: 优化前端系统菜单和系统权限页UI和交互

This commit is contained in:
2026-08-15 14:42:43 +08:00
parent 5f6b96e662
commit 64c38a4928
3 changed files with 312 additions and 358 deletions
@@ -13,7 +13,6 @@ import {
Flex,
Input,
message,
Pagination,
Select,
Space,
Spin,
@@ -27,10 +26,6 @@ import {
getCompanyMenuIdsApi,
} from '#/api/company/menu/apis';
import { getCompanyPageApi } from '#/api/company';
import {
SIDEBAR_LIST_SHELL_CLASS,
sidebarListRowClass,
} from '#/utils/sidebar-list-styles';
defineOptions({
name: 'CompanyMenuManagePage',
@@ -56,6 +51,7 @@ 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 selectedCompanyLabel = computed(() => {
@@ -129,6 +125,7 @@ async function loadCompanyMenus() {
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 {
@@ -224,9 +221,17 @@ function handleCompanyPageChange(page: number, pageSize: number) {
searchCompanies();
}
function companyRowClass(companyId: number) {
const active = currentCompany.value?.id === companyId;
return sidebarListRowClass(active, 'w-full cursor-pointer text-left');
const companyTotalPages = computed(() => {
const { total, pageSize } = companyPagination.value;
return Math.max(1, Math.ceil(total / pageSize) || 1);
});
function goCompanyPage(page: number) {
const next = Math.min(companyTotalPages.value, Math.max(1, page));
if (next === companyPagination.value.current) {
return;
}
handleCompanyPageChange(next, companyPagination.value.pageSize);
}
onMounted(() => {
@@ -258,16 +263,18 @@ onMounted(() => {
</Space>
</template>
<Flex gap="middle" class="h-full min-h-0">
<Flex gap="middle" class="h-full min-h-0 overflow-hidden">
<!-- 左:公司 -->
<Card
class="flex h-full w-[420px] shrink-0 flex-col"
class="flex h-full w-[320px] shrink-0 flex-col overflow-hidden"
:body-style="{
height: '100%',
display: 'flex',
flexDirection: 'column',
gap: '8px',
minHeight: 0,
minWidth: 0,
overflow: 'hidden',
padding: '12px',
}"
>
@@ -278,67 +285,101 @@ onMounted(() => {
</span>
</template>
<Flex wrap="wrap" align="center" class="shrink-0 gap-2">
<div class="flex min-w-0 shrink-0 flex-col gap-2">
<Input
v-model:value="companySearchForm.name"
placeholder="公司名称"
allow-clear
class="min-w-[140px] flex-1"
class="w-full min-w-0"
@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 class="flex min-w-0 items-center gap-2">
<Select
v-model:value="companySearchForm.status"
placeholder="状态"
allow-clear
class="min-w-0 flex-1"
:options="[
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 },
]"
/>
<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"
<Button type="primary" class="shrink-0" @click="searchCompanies">
搜索
</Button>
<Button class="shrink-0" @click="resetCompanySearch">重置</Button>
</div>
</div>
<div class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
<div class="min-h-0 flex-1 overflow-y-auto">
<Spin :spinning="companyLoading">
<Empty
v-if="!companyLoading && companyList.length === 0"
class="py-8"
description="暂无公司"
/>
<template v-else>
<Card
v-for="company in companyList"
:key="company.id"
size="small"
:body-style="{ padding: '10px 12px' }"
class="mb-2 cursor-pointer !shadow-none last:mb-0 hover:border-primary"
:class="
currentCompany?.id === company.id
? 'border-primary bg-primary/5'
: undefined
"
@click="selectCompany(company)"
>
{{ company.status === 1 ? '启用' : '禁用' }}
</span>
</button>
</div>
<div class="flex items-center justify-between gap-2">
<span class="min-w-0 truncate text-sm font-medium">
{{ company.name }}
</span>
<Tag
class="m-0 shrink-0"
:color="company.status === 1 ? 'green' : 'red'"
>
{{ company.status === 1 ? '启用' : '禁用' }}
</Tag>
</div>
</Card>
</template>
</Spin>
</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"
<div
class="mt-2 flex w-full min-w-0 shrink-0 items-center justify-between gap-2"
>
<Button
size="small"
:show-size-changer="true"
:show-quick-jumper="true"
@change="handleCompanyPageChange"
/>
</Flex>
</Spin>
class="shrink-0"
:disabled="companyPagination.current <= 1 || companyLoading"
@click="goCompanyPage(companyPagination.current - 1)"
>
上一页
</Button>
<span
class="min-w-0 flex-1 truncate text-center text-xs text-muted-foreground"
>
{{ companyPagination.current }} / {{ companyTotalPages }}
<span class="ml-1 opacity-70"
>({{ companyPagination.total }})</span
>
</span>
<Button
size="small"
class="shrink-0"
:disabled="
companyPagination.current >= companyTotalPages || companyLoading
"
@click="goCompanyPage(companyPagination.current + 1)"
>
下一页
</Button>
</div>
</div>
</Card>
<!-- 右:菜单分配 -->
@@ -387,19 +428,24 @@ onMounted(() => {
<Spin :spinning="menuLoading">
<Tree
v-model:checked-keys="selectedMenuIds"
v-model:expanded-keys="menuExpandedKeys"
:tree-data="menuTreeData"
checkable
:default-expand-all="true"
class="company-menu-tree"
block-node
:selectable="false"
@check="handleTreeCheckChange"
>
<template #title="{ title, status }">
<span>
{{ title }}
<span v-if="status === 0" class="ml-2 text-xs text-gray-400">
(已禁用)
</span>
</span>
<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>
@@ -1,128 +0,0 @@
<script setup lang="ts">
import type { PermissionVO } from '#/api/sys/permission/types';
import { createIconifyIcon } from '@vben/icons';
import { Checkbox, Tag } from 'ant-design-vue';
import type { CheckboxChangeEvent } from 'ant-design-vue/es/checkbox/interface';
const ChevronDownIcon = createIconifyIcon('lucide:chevron-down');
const ChevronRightIcon = createIconifyIcon('lucide:chevron-right');
defineOptions({ name: 'PermissionTreeNode' });
const props = defineProps<{
node: PermissionVO.PermissionInfoVO;
level: number;
expandedIds: Set<number>;
selectedIds: number[];
}>();
const emit = defineEmits<{
toggle: [id: number];
check: [id: number, checked: boolean];
}>();
function isChecked(): boolean {
if (!props.node.children || props.node.children.length === 0) {
return props.selectedIds.includes(props.node.id);
}
const allIds = getDescendantIds(props.node);
return allIds.length > 0 && allIds.every((id) => props.selectedIds.includes(id));
}
function isIndeterminate(): boolean {
if (!props.node.children || props.node.children.length === 0) return false;
const allIds = getDescendantIds(props.node);
const selectedCount = allIds.filter((id) => props.selectedIds.includes(id)).length;
return selectedCount > 0 && selectedCount < allIds.length;
}
function getDescendantIds(node: PermissionVO.PermissionInfoVO): number[] {
const result: number[] = [];
function collect(n: PermissionVO.PermissionInfoVO) {
if (n.children) {
for (const child of n.children) {
result.push(child.id);
collect(child);
}
}
}
if (node.children) {
for (const child of node.children) {
result.push(child.id);
collect(child);
}
}
return result;
}
function onCheckboxChange(e: CheckboxChangeEvent) {
const checked = e.target.checked;
emit('check', props.node.id, checked);
}
function handleToggle(id: number) {
emit('toggle', id);
}
function handleCheck(id: number, checked: boolean) {
emit('check', id, checked);
}
</script>
<template>
<div class="tree-node">
<!-- 节点行 -->
<div
class="flex items-center gap-2 py-1.5 px-2 rounded cursor-pointer hover:bg-gray-50 transition-colors"
:style="{ paddingLeft: (level * 20 + 8) + 'px' }"
>
<!-- 展开/收起图标(antd 默认图标) -->
<span
v-if="node.children && node.children.length > 0"
class="flex items-center justify-center w-4 h-4 cursor-pointer"
@click="emit('toggle', node.id)"
>
<ChevronDownIcon v-if="expandedIds.has(node.id)" class="text-xs text-gray-400" />
<ChevronRightIcon v-else class="text-xs text-gray-400" />
</span>
<span v-else class="w-4"></span>
<!-- Checkbox -->
<Checkbox
:checked="isChecked()"
:indeterminate="isIndeterminate()"
@change="onCheckboxChange"
/>
<!-- 名称 -->
<span
class="flex-1 text-sm leading-5"
:class="node.type === 'module' ? 'font-semibold text-[var(--text-primary)]' : 'text-[var(--text-secondary)]'"
>
{{ node.name }}
</span>
<!-- Code -->
<span class="text-xs text-gray-400 mr-2">{{ node.code }}</span>
<!-- 类型标签 -->
<Tag v-if="node.type === 'module'" color="purple" class="text-xs">模块</Tag>
</div>
<!-- 子节点 -->
<template v-if="node.children && node.children.length > 0 && expandedIds.has(node.id)">
<TreeNodeContent
v-for="child in node.children"
:key="child.id"
:node="child"
:level="level + 1"
:expanded-ids="expandedIds"
:selected-ids="selectedIds"
@toggle="handleToggle"
@check="handleCheck"
/>
</template>
</div>
</template>
@@ -14,11 +14,11 @@ import {
Flex,
Input,
message,
Pagination,
Select,
Space,
Spin,
Tag,
Tree,
} from 'ant-design-vue';
import {
@@ -28,16 +28,18 @@ import {
import { getCompanyPageApi } from '#/api/company';
import { getPermissionTreeApi } from '#/api/sys/permission';
import TreeNodeContent from './components/TreeNodeContent.vue';
import {
SIDEBAR_LIST_SHELL_CLASS,
sidebarListRowClass,
} from '#/utils/sidebar-list-styles';
defineOptions({ name: 'CompanyPermissionManagePage' });
const BuildingIcon = createIconifyIcon('lucide:building-2');
type PermissionTreeNode = {
key: number;
title: string;
code?: string;
type?: string;
children?: PermissionTreeNode[];
};
const companySearchForm = ref({
name: '',
status: undefined as number | undefined,
@@ -53,7 +55,7 @@ const permissionTree = ref<PermissionVO.PermissionInfoVO[]>([]);
const permissionLoading = ref(false);
const companyPermissionIds = ref<number[]>([]);
const selectedPermissionIds = ref<number[]>([]);
const expandedIds = ref<Set<number>>(new Set());
const expandedKeys = ref<number[]>([]);
const selectedCompanyLabel = computed(() => {
if (!currentCompany.value) {
@@ -62,11 +64,26 @@ const selectedCompanyLabel = computed(() => {
return currentCompany.value.name;
});
const permissionTreeData = computed(() =>
mapPermissionToTreeData(permissionTree.value),
);
const selectedLeafCount = computed(() => {
const selected = new Set(selectedPermissionIds.value);
return getAllLeafIds(permissionTree.value).filter((id) => selected.has(id))
.length;
});
const stats = computed(() => ({
total: countLeafPermissions(permissionTree.value),
company: companyPermissionIds.value.length,
selected: selectedPermissionIds.value.length,
changed: !sameArray(selectedPermissionIds.value, companyPermissionIds.value),
selected: selectedLeafCount.value,
changed: !sameArray(
getAllLeafIds(permissionTree.value).filter((id) =>
selectedPermissionIds.value.includes(id),
),
companyPermissionIds.value,
),
}));
function sameArray(a: number[], b: number[]): boolean {
@@ -75,6 +92,32 @@ function sameArray(a: number[], b: number[]): boolean {
return b.every((id) => sa.has(id));
}
function mapPermissionToTreeData(
nodes: PermissionVO.PermissionInfoVO[],
): PermissionTreeNode[] {
return nodes.map((node) => ({
key: node.id,
title: node.name,
code: node.code,
type: node.type,
children:
node.children && node.children.length > 0
? mapPermissionToTreeData(node.children)
: undefined,
}));
}
function collectExpandableKeys(nodes: PermissionVO.PermissionInfoVO[]): number[] {
const keys: number[] = [];
for (const node of nodes) {
if (node.children && node.children.length > 0) {
keys.push(node.id);
keys.push(...collectExpandableKeys(node.children));
}
}
return keys;
}
async function searchCompanies() {
companyLoading.value = true;
try {
@@ -118,20 +161,26 @@ async function loadPermissionTree() {
try {
const result = await getPermissionTreeApi();
permissionTree.value = result || [];
expandedIds.value = new Set(
result?.map((n: PermissionVO.PermissionInfoVO) => n.id) || [],
);
expandedKeys.value = collectExpandableKeys(result || []);
} catch (error: any) {
message.error(error?.message || '获取权限树失败');
}
}
function handleTreeCheckChange(
checked:
| (number | string)[]
| { checked: (number | string)[]; halfChecked: (number | string)[] },
) {
const checkedKeys = Array.isArray(checked) ? checked : checked.checked;
selectedPermissionIds.value = checkedKeys.map((k) => Number(k));
}
async function handleSave() {
if (!currentCompany.value?.id) return;
const leafIds = selectedPermissionIds.value.filter((id) => {
const node = findNode(permissionTree.value, id);
return node && (!node.children || node.children.length === 0);
});
const leafIds = getAllLeafIds(permissionTree.value).filter((id) =>
selectedPermissionIds.value.includes(id),
);
try {
await batchUpdateCompanyPermissionsApi(currentCompany.value.id, leafIds);
message.success('权限配置保存成功');
@@ -154,6 +203,19 @@ function handleCompanyPageChange(page: number, pageSize: number) {
searchCompanies();
}
const companyTotalPages = computed(() => {
const { total, pageSize } = companyPagination.value;
return Math.max(1, Math.ceil(total / pageSize) || 1);
});
function goCompanyPage(page: number) {
const next = Math.min(companyTotalPages.value, Math.max(1, page));
if (next === companyPagination.value.current) {
return;
}
handleCompanyPageChange(next, companyPagination.value.pageSize);
}
function countLeafPermissions(nodes: PermissionVO.PermissionInfoVO[]): number {
let count = 0;
for (const node of nodes) {
@@ -183,87 +245,6 @@ function selectNone() {
selectedPermissionIds.value = [];
}
function onCheck(permissionId: number, checked: boolean) {
if (checked) {
const toAdd = new Set<number>([permissionId]);
getParentChain(permissionTree.value, permissionId).forEach((id) =>
toAdd.add(id),
);
getDescendantIds(permissionId).forEach((id) => toAdd.add(id));
const newSet = new Set(selectedPermissionIds.value);
toAdd.forEach((id) => newSet.add(id));
selectedPermissionIds.value = Array.from(newSet);
} else {
const toRemove = new Set<number>([
permissionId,
...getDescendantIds(permissionId),
]);
selectedPermissionIds.value = selectedPermissionIds.value.filter(
(id) => !toRemove.has(id),
);
}
}
function getParentChain(
nodes: PermissionVO.PermissionInfoVO[],
targetId: number,
path: number[] = [],
): number[] {
for (const node of nodes) {
if (node.id === targetId) return [...path, node.id];
if (node.children) {
const found = getParentChain(node.children, targetId, [...path, node.id]);
if (found.length > 0) return found;
}
}
return [];
}
function getDescendantIds(permissionId: number): number[] {
const node = findNode(permissionTree.value, permissionId);
if (!node || !node.children) return [];
const result: number[] = [];
function collect(n: PermissionVO.PermissionInfoVO) {
if (n.children) {
for (const child of n.children) {
result.push(child.id);
collect(child);
}
}
}
for (const child of node.children) {
result.push(child.id);
collect(child);
}
return result;
}
function findNode(
nodes: PermissionVO.PermissionInfoVO[],
id: number,
): PermissionVO.PermissionInfoVO | null {
for (const node of nodes) {
if (node.id === id) return node;
if (node.children) {
const found = findNode(node.children, id);
if (found) return found;
}
}
return null;
}
function toggleExpand(nodeId: number) {
const newSet = new Set(expandedIds.value);
if (newSet.has(nodeId)) newSet.delete(nodeId);
else newSet.add(nodeId);
expandedIds.value = newSet;
}
function companyRowClass(companyId: number) {
const active = currentCompany.value?.id === companyId;
return sidebarListRowClass(active, 'w-full cursor-pointer text-left');
}
onMounted(() => {
searchCompanies();
loadPermissionTree();
@@ -294,16 +275,18 @@ onMounted(() => {
</Space>
</template>
<Flex gap="middle" class="h-full min-h-0">
<Flex gap="middle" class="h-full min-h-0 overflow-hidden">
<!-- 左:公司 -->
<Card
class="flex h-full w-[420px] shrink-0 flex-col"
class="flex h-full w-[320px] shrink-0 flex-col overflow-hidden"
:body-style="{
height: '100%',
display: 'flex',
flexDirection: 'column',
gap: '8px',
minHeight: 0,
minWidth: 0,
overflow: 'hidden',
padding: '12px',
}"
>
@@ -314,67 +297,101 @@ onMounted(() => {
</span>
</template>
<Flex wrap="wrap" align="center" class="shrink-0 gap-2">
<div class="flex min-w-0 shrink-0 flex-col gap-2">
<Input
v-model:value="companySearchForm.name"
placeholder="公司名称"
allow-clear
class="min-w-[140px] flex-1"
class="w-full min-w-0"
@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 class="flex min-w-0 items-center gap-2">
<Select
v-model:value="companySearchForm.status"
placeholder="状态"
allow-clear
class="min-w-0 flex-1"
:options="[
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 },
]"
/>
<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"
<Button type="primary" class="shrink-0" @click="searchCompanies">
搜索
</Button>
<Button class="shrink-0" @click="resetCompanySearch">重置</Button>
</div>
</div>
<div class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
<div class="min-h-0 flex-1 overflow-y-auto">
<Spin :spinning="companyLoading">
<Empty
v-if="!companyLoading && companyList.length === 0"
class="py-8"
description="暂无公司"
/>
<template v-else>
<Card
v-for="company in companyList"
:key="company.id"
size="small"
:body-style="{ padding: '10px 12px' }"
class="mb-2 cursor-pointer !shadow-none last:mb-0 hover:border-primary"
:class="
currentCompany?.id === company.id
? 'border-primary bg-primary/5'
: undefined
"
@click="selectCompany(company)"
>
{{ company.status === 1 ? '启用' : '禁用' }}
</span>
</button>
</div>
<div class="flex items-center justify-between gap-2">
<span class="min-w-0 truncate text-sm font-medium">
{{ company.name }}
</span>
<Tag
class="m-0 shrink-0"
:color="company.status === 1 ? 'green' : 'red'"
>
{{ company.status === 1 ? '启用' : '禁用' }}
</Tag>
</div>
</Card>
</template>
</Spin>
</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"
<div
class="mt-2 flex w-full min-w-0 shrink-0 items-center justify-between gap-2"
>
<Button
size="small"
:show-size-changer="true"
:show-quick-jumper="true"
@change="handleCompanyPageChange"
/>
</Flex>
</Spin>
class="shrink-0"
:disabled="companyPagination.current <= 1 || companyLoading"
@click="goCompanyPage(companyPagination.current - 1)"
>
上一页
</Button>
<span
class="min-w-0 flex-1 truncate text-center text-xs text-muted-foreground"
>
{{ companyPagination.current }} / {{ companyTotalPages }}
<span class="ml-1 opacity-70"
>({{ companyPagination.total }})</span
>
</span>
<Button
size="small"
class="shrink-0"
:disabled="
companyPagination.current >= companyTotalPages || companyLoading
"
@click="goCompanyPage(companyPagination.current + 1)"
>
下一页
</Button>
</div>
</div>
</Card>
<!-- 右:权限分配 -->
@@ -407,7 +424,9 @@ onMounted(() => {
全部叶子权限 {{ 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 v-if="stats.changed" class="text-orange-500"
>(有未保存更改)</span
>
</span>
</template>
</div>
@@ -421,18 +440,35 @@ onMounted(() => {
<div v-else class="min-h-0 flex-1 overflow-y-auto px-4 py-3 sm:px-5">
<Spin :spinning="permissionLoading">
<div v-if="permissionTree.length > 0" class="py-2">
<template v-for="node in permissionTree" :key="node.id">
<TreeNodeContent
:node="node"
:level="0"
:expanded-ids="expandedIds"
:selected-ids="selectedPermissionIds"
@toggle="toggleExpand"
@check="onCheck"
/>
<Tree
v-if="permissionTreeData.length > 0"
v-model:checked-keys="selectedPermissionIds"
v-model:expanded-keys="expandedKeys"
:tree-data="permissionTreeData"
checkable
block-node
:selectable="false"
@check="handleTreeCheckChange"
>
<template #title="{ title, code, type }">
<div class="flex min-w-0 items-center gap-2">
<span class="min-w-0 truncate font-medium">{{ title }}</span>
<span
v-if="code"
class="shrink-0 text-xs text-muted-foreground"
>
{{ code }}
</span>
<Tag
v-if="type === 'module'"
color="purple"
class="m-0 shrink-0"
>
模块
</Tag>
</div>
</template>
</div>
</Tree>
<Empty
v-else-if="!permissionLoading"
class="py-8"