mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 08:29: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>
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
<script setup lang="ts">
|
||||
import type { PermissionVO } from '#/api/system/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>
|
||||
@@ -0,0 +1,446 @@
|
||||
<script setup lang="ts">
|
||||
import type { CompanyVO } from '#/api/organization/company/types';
|
||||
import type { PermissionVO } from '#/api/system/permission/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,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
batchUpdateCompanyPermissionsApi,
|
||||
getCompanyPermissionIdsApi,
|
||||
} from '#/api/organization/company-permission/apis';
|
||||
import { getCompanyPageApi } from '#/api/organization/company';
|
||||
import { getPermissionTreeApi } from '#/api/system/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');
|
||||
|
||||
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 permissionTree = ref<PermissionVO.PermissionInfoVO[]>([]);
|
||||
const permissionLoading = ref(false);
|
||||
const companyPermissionIds = ref<number[]>([]);
|
||||
const selectedPermissionIds = ref<number[]>([]);
|
||||
const expandedIds = ref<Set<number>>(new Set());
|
||||
|
||||
const selectedCompanyLabel = computed(() => {
|
||||
if (!currentCompany.value) {
|
||||
return '请先选择公司';
|
||||
}
|
||||
return currentCompany.value.name;
|
||||
});
|
||||
|
||||
const stats = computed(() => ({
|
||||
total: countLeafPermissions(permissionTree.value),
|
||||
company: companyPermissionIds.value.length,
|
||||
selected: selectedPermissionIds.value.length,
|
||||
changed: !sameArray(selectedPermissionIds.value, companyPermissionIds.value),
|
||||
}));
|
||||
|
||||
function sameArray(a: number[], b: number[]): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
const sa = new Set(a);
|
||||
return b.every((id) => sa.has(id));
|
||||
}
|
||||
|
||||
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 loadCompanyPermissions();
|
||||
}
|
||||
|
||||
async function loadCompanyPermissions() {
|
||||
if (!currentCompany.value?.id) return;
|
||||
permissionLoading.value = true;
|
||||
try {
|
||||
const result = await getCompanyPermissionIdsApi(currentCompany.value.id);
|
||||
companyPermissionIds.value = result || [];
|
||||
selectedPermissionIds.value = [...companyPermissionIds.value];
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '加载权限失败');
|
||||
} finally {
|
||||
permissionLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPermissionTree() {
|
||||
try {
|
||||
const result = await getPermissionTreeApi();
|
||||
permissionTree.value = result || [];
|
||||
expandedIds.value = new Set(
|
||||
result?.map((n: PermissionVO.PermissionInfoVO) => n.id) || [],
|
||||
);
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '获取权限树失败');
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
try {
|
||||
await batchUpdateCompanyPermissionsApi(currentCompany.value.id, leafIds);
|
||||
message.success('权限配置保存成功');
|
||||
companyPermissionIds.value = [...leafIds];
|
||||
selectedPermissionIds.value = [...leafIds];
|
||||
} 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 countLeafPermissions(nodes: PermissionVO.PermissionInfoVO[]): number {
|
||||
let count = 0;
|
||||
for (const node of nodes) {
|
||||
if (node.children && node.children.length > 0) {
|
||||
count += countLeafPermissions(node.children);
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
function getAllLeafIds(nodes: PermissionVO.PermissionInfoVO[]): number[] {
|
||||
const result: number[] = [];
|
||||
for (const node of nodes) {
|
||||
if (!node.children || node.children.length === 0) result.push(node.id);
|
||||
else result.push(...getAllLeafIds(node.children));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function selectAll() {
|
||||
selectedPermissionIds.value = getAllLeafIds(permissionTree.value);
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
</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="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"
|
||||
/>
|
||||
</template>
|
||||
</div>
|
||||
<Empty
|
||||
v-else-if="!permissionLoading"
|
||||
class="py-8"
|
||||
description="暂无可分配的权限"
|
||||
/>
|
||||
</Spin>
|
||||
</div>
|
||||
</Card>
|
||||
</Flex>
|
||||
</Page>
|
||||
</template>
|
||||
Reference in New Issue
Block a user