mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 07:49:20 +08:00
522 lines
16 KiB
Vue
522 lines
16 KiB
Vue
<script setup lang="ts">
|
||
import type { CompanyMenuVO } from '#/api/organization/company-menu/types';
|
||
import type { PositionVO } from '#/api/organization/position/types';
|
||
|
||
import { computed, onMounted, ref } from 'vue';
|
||
|
||
import { Page } from '@vben/common-ui';
|
||
|
||
import {
|
||
Button,
|
||
Card,
|
||
Flex,
|
||
Input,
|
||
message,
|
||
Pagination,
|
||
Select,
|
||
Spin,
|
||
Tree,
|
||
} from 'ant-design-vue';
|
||
|
||
import { getCompanyMenuTreeApi } from '#/api/organization/company-menu';
|
||
import { getPositionPageApi } from '#/api/organization/position';
|
||
import {
|
||
batchUpdatePositionMenusApi,
|
||
getPositionMenuIdsApi,
|
||
} from '#/api/organization/position-permission';
|
||
|
||
defineOptions({
|
||
name: 'PositionPermissionPage',
|
||
});
|
||
|
||
// 搜索表单
|
||
const searchForm = ref({
|
||
name: '',
|
||
status: undefined as number | undefined,
|
||
});
|
||
|
||
// 职位列表
|
||
const positionList = ref<PositionVO.PositionInfoVO[]>([]);
|
||
const positionLoading = ref(false);
|
||
const positionPagination = ref({
|
||
current: 1,
|
||
pageSize: 10,
|
||
total: 0,
|
||
});
|
||
|
||
// 当前选中的职位
|
||
const currentPosition = ref<null | PositionVO.PositionInfoVO>(null);
|
||
|
||
// 公司菜单树数据
|
||
const companyMenuTree = ref<CompanyMenuVO.CompanyMenuInfo[]>([]);
|
||
const menuTreeLoading = ref(false);
|
||
|
||
// 职位权限菜单ID列表
|
||
const positionMenuIds = ref<number[]>([]);
|
||
const permissionLoading = ref(false);
|
||
|
||
// 选中的菜单ID(用于树形控件)
|
||
const checkedKeys = ref<number[]>([]);
|
||
const expandedKeys = ref<number[]>([]);
|
||
|
||
// 不区分超管与普通菜单
|
||
|
||
// 搜索职位列表
|
||
async function searchPositions() {
|
||
positionLoading.value = true;
|
||
try {
|
||
const params = {
|
||
page: positionPagination.value.current,
|
||
pageSize: positionPagination.value.pageSize,
|
||
filters: {
|
||
name: searchForm.value.name || undefined,
|
||
status: searchForm.value.status,
|
||
},
|
||
};
|
||
|
||
const result = await getPositionPageApi(params);
|
||
positionList.value = result.records;
|
||
positionPagination.value.total = result.total;
|
||
} catch (error: any) {
|
||
message.error(error?.message || '获取职位列表失败');
|
||
} finally {
|
||
positionLoading.value = false;
|
||
}
|
||
}
|
||
|
||
// 加载公司菜单树
|
||
async function loadCompanyMenuTree() {
|
||
menuTreeLoading.value = true;
|
||
try {
|
||
const result = await getCompanyMenuTreeApi();
|
||
companyMenuTree.value = result;
|
||
// 自动展开所有节点
|
||
expandedKeys.value = getAllMenuIds(result);
|
||
} catch (error: any) {
|
||
message.error(error?.message || '获取菜单树失败');
|
||
} finally {
|
||
menuTreeLoading.value = false;
|
||
}
|
||
}
|
||
|
||
// 获取所有菜单ID(递归)
|
||
function getAllMenuIds(menus: CompanyMenuVO.CompanyMenuInfo[]): number[] {
|
||
const ids: number[] = [];
|
||
function traverse(menuList: CompanyMenuVO.CompanyMenuInfo[]) {
|
||
menuList.forEach((menu) => {
|
||
ids.push(menu.id);
|
||
if (menu.children && menu.children.length > 0) {
|
||
traverse(menu.children);
|
||
}
|
||
});
|
||
}
|
||
traverse(menus);
|
||
return ids;
|
||
}
|
||
|
||
// 将菜单数据转换为Tree组件需要的格式
|
||
function convertToTreeData(menus: CompanyMenuVO.CompanyMenuInfo[]): any[] {
|
||
return menus.map((menu) => ({
|
||
key: menu.id,
|
||
title: menu.name,
|
||
type: menu.type,
|
||
superMenu: menu.superMenu,
|
||
status: menu.status,
|
||
disabled: menu.status === 0,
|
||
children:
|
||
menu.children && menu.children.length > 0
|
||
? convertToTreeData(menu.children)
|
||
: undefined,
|
||
}));
|
||
}
|
||
|
||
// 选择职位
|
||
async function selectPosition(position: PositionVO.PositionInfoVO) {
|
||
currentPosition.value = position;
|
||
await loadPositionPermissions();
|
||
}
|
||
|
||
// 加载职位权限
|
||
async function loadPositionPermissions() {
|
||
if (!currentPosition.value?.id) return;
|
||
|
||
permissionLoading.value = true;
|
||
try {
|
||
const result = await getPositionMenuIdsApi(currentPosition.value.id);
|
||
positionMenuIds.value = result;
|
||
|
||
// 清理根节点,只保留叶子节点
|
||
const leafMenuIds = cleanRootNodes(result);
|
||
checkedKeys.value = leafMenuIds;
|
||
} catch (error: any) {
|
||
message.error(error?.message || '获取职位菜单失败');
|
||
} finally {
|
||
permissionLoading.value = false;
|
||
}
|
||
}
|
||
|
||
// 清理根节点,只保留叶子节点
|
||
function cleanRootNodes(menuIds: number[]): number[] {
|
||
return menuIds.filter((menuId) => {
|
||
const menu = findMenuById(menuId);
|
||
if (!menu) return false;
|
||
|
||
// 检查是否有其他菜单将此菜单作为父菜单
|
||
const hasChildren = hasMenuChildren(menuId);
|
||
|
||
// 只保留叶子节点(没有子节点的菜单)
|
||
return !hasChildren;
|
||
});
|
||
}
|
||
|
||
// 根据ID查找菜单
|
||
function findMenuById(menuId: number): CompanyMenuVO.CompanyMenuInfo | null {
|
||
function search(
|
||
menus: CompanyMenuVO.CompanyMenuInfo[],
|
||
): CompanyMenuVO.CompanyMenuInfo | null {
|
||
for (const menu of menus) {
|
||
if (menu.id === menuId) return menu;
|
||
if (menu.children && menu.children.length > 0) {
|
||
const found = search(menu.children);
|
||
if (found) return found;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
return search(companyMenuTree.value);
|
||
}
|
||
|
||
// 检查菜单是否有子菜单
|
||
function hasMenuChildren(menuId: number): boolean {
|
||
function search(menus: CompanyMenuVO.CompanyMenuInfo[]): boolean {
|
||
for (const menu of menus) {
|
||
if (menu.children && menu.children.length > 0) {
|
||
if (menu.children.some((child) => child.id === menuId)) return true;
|
||
if (search(menu.children)) return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
return search(companyMenuTree.value);
|
||
}
|
||
|
||
// 确保提交时包含必要的父菜单ID(递归添加所有层级的父菜单)
|
||
function ensureParentMenuIds(menuIds: number[]): number[] {
|
||
const result = new Set(menuIds);
|
||
|
||
// 递归添加所有父菜单ID
|
||
function addAllParentIds(menuId: number) {
|
||
const menu = findMenuById(menuId);
|
||
if (menu && menu.parentId) {
|
||
result.add(menu.parentId);
|
||
// 递归添加父菜单的父菜单
|
||
addAllParentIds(menu.parentId);
|
||
}
|
||
}
|
||
|
||
// 为每个选中的菜单添加其所有父菜单ID
|
||
menuIds.forEach((menuId) => {
|
||
addAllParentIds(menuId);
|
||
});
|
||
|
||
return [...result];
|
||
}
|
||
|
||
// 保存职位权限
|
||
async function savePositionPermissions() {
|
||
if (!currentPosition.value?.id) return;
|
||
|
||
try {
|
||
// 确保提交时包含必要的父菜单ID
|
||
const finalMenuIds = ensureParentMenuIds(checkedKeys.value);
|
||
await batchUpdatePositionMenusApi(currentPosition.value.id, finalMenuIds);
|
||
message.success('权限配置保存成功');
|
||
|
||
// 重新加载权限数据
|
||
await loadPositionPermissions();
|
||
} catch (error: any) {
|
||
message.error(error?.message || '保存菜单失败');
|
||
}
|
||
}
|
||
|
||
// 一键全选
|
||
function handleSelectAll() {
|
||
// 获取所有可选择的菜单ID(排除禁用的)
|
||
const selectableMenus = getAllSelectableMenus();
|
||
|
||
// 只选择叶子节点(没有子节点的菜单)
|
||
const leafMenus = selectableMenus.filter((menu) => {
|
||
return !hasMenuChildren(menu.id);
|
||
});
|
||
|
||
// 全选所有叶子节点
|
||
checkedKeys.value = leafMenus.map((menu) => menu.id);
|
||
}
|
||
|
||
// 取消全选
|
||
function handleSelectNone() {
|
||
// 取消所有叶子节点的选中状态
|
||
const allLeafMenuIds = new Set(
|
||
getAllSelectableMenus()
|
||
.filter((menu) => !hasMenuChildren(menu.id))
|
||
.map((menu) => menu.id),
|
||
);
|
||
|
||
checkedKeys.value = checkedKeys.value.filter((id) => !allLeafMenuIds.has(id));
|
||
}
|
||
|
||
// 获取所有可选择的菜单
|
||
function getAllSelectableMenus(): CompanyMenuVO.CompanyMenuInfo[] {
|
||
const result: CompanyMenuVO.CompanyMenuInfo[] = [];
|
||
|
||
function traverse(menus: CompanyMenuVO.CompanyMenuInfo[]) {
|
||
menus.forEach((menu) => {
|
||
if (menu.status === 1) {
|
||
// 只包含启用的菜单
|
||
result.push(menu);
|
||
}
|
||
if (menu.children && menu.children.length > 0) {
|
||
traverse(menu.children);
|
||
}
|
||
});
|
||
}
|
||
|
||
traverse(companyMenuTree.value);
|
||
return result;
|
||
}
|
||
|
||
// 处理树形控件的选中变化
|
||
function handleTreeCheckChange(checkedKeysValue: any) {
|
||
checkedKeys.value = checkedKeysValue;
|
||
}
|
||
|
||
// 计算统计信息
|
||
const stats = computed(() => {
|
||
const totalMenus = getAllSelectableMenus().length;
|
||
const positionMenus = positionMenuIds.value.length;
|
||
const selectedMenus = ensureParentMenuIds(checkedKeys.value).length;
|
||
return {
|
||
total: totalMenus,
|
||
position: positionMenus,
|
||
selected: selectedMenus,
|
||
};
|
||
});
|
||
|
||
// 重置搜索
|
||
function resetSearch() {
|
||
searchForm.value = {
|
||
name: '',
|
||
status: undefined,
|
||
};
|
||
positionPagination.value.current = 1;
|
||
searchPositions();
|
||
}
|
||
|
||
// 分页变化
|
||
function handlePageChange(page: number, pageSize: number) {
|
||
positionPagination.value.current = page;
|
||
positionPagination.value.pageSize = pageSize;
|
||
searchPositions();
|
||
}
|
||
|
||
// 初始化
|
||
onMounted(() => {
|
||
searchPositions();
|
||
loadCompanyMenuTree();
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<Page :auto-content-height="true">
|
||
<Flex gap="middle" class="h-full">
|
||
<!-- 左栏:职位列表 -->
|
||
<Card
|
||
title="职位列表"
|
||
class="flex h-full max-w-[400px] flex-col"
|
||
:body-style="{
|
||
height: '100%',
|
||
display: 'flex',
|
||
flexDirection: 'column',
|
||
gap: '10px',
|
||
}"
|
||
>
|
||
<!-- 搜索区域 -->
|
||
<Flex class="flex-wrap gap-2">
|
||
<Input
|
||
v-model:value="searchForm.name"
|
||
placeholder="请输入职位名称"
|
||
allow-clear
|
||
style="width: 200px; margin-right: 8px"
|
||
/>
|
||
<Select
|
||
v-model:value="searchForm.status"
|
||
placeholder="请选择状态"
|
||
allow-clear
|
||
style="width: 120px; margin-right: 8px"
|
||
>
|
||
<Select.Option :value="1">启用</Select.Option>
|
||
<Select.Option :value="0">禁用</Select.Option>
|
||
</Select>
|
||
<Button type="primary" @click="searchPositions"> 搜索 </Button>
|
||
<Button @click="resetSearch"> 重置 </Button>
|
||
</Flex>
|
||
|
||
<!-- 职位列表区域 -->
|
||
<Flex vertical class="flex-1">
|
||
<!-- 职位列表 -->
|
||
<Flex vertical class="flex-1">
|
||
<Spin :spinning="positionLoading">
|
||
<Card
|
||
v-for="position in positionList"
|
||
:key="position.id"
|
||
hoverable
|
||
size="small"
|
||
:body-style="{ padding: '10px 12px' }"
|
||
class="group mb-2 cursor-pointer transition-all duration-200"
|
||
:class="[
|
||
currentPosition?.id === position.id &&
|
||
'ring-primary shadow ring-1',
|
||
]"
|
||
@click="selectPosition(position)"
|
||
>
|
||
<div class="flex items-start justify-between gap-2">
|
||
<div class="min-w-0">
|
||
<div class="mb-1 flex items-center gap-2">
|
||
<span
|
||
class="inline-block h-2 w-2 rounded-full"
|
||
:class="
|
||
position.status === 1 ? 'bg-green-500' : 'bg-red-500'
|
||
"
|
||
></span>
|
||
<span
|
||
class="truncate text-sm font-semibold text-[var(--text-primary)]"
|
||
>
|
||
{{ position.name }}
|
||
</span>
|
||
</div>
|
||
<div class="mb-1 text-xs text-[var(--text-secondary)]">
|
||
编码:{{ position.code }}
|
||
</div>
|
||
<div
|
||
v-if="position.remark"
|
||
class="truncate text-xs text-[var(--text-secondary)]"
|
||
>
|
||
{{ position.remark }}
|
||
</div>
|
||
</div>
|
||
<Tag :color="position.status === 1 ? 'green' : 'red'">
|
||
{{ position.status === 1 ? '启用' : '禁用' }}
|
||
</Tag>
|
||
</div>
|
||
</Card>
|
||
</Spin>
|
||
</Flex>
|
||
<!-- 分页器 -->
|
||
<Flex justify="center">
|
||
<Pagination
|
||
v-model:current="positionPagination.current"
|
||
v-model:page-size="positionPagination.pageSize"
|
||
:total="positionPagination.total"
|
||
:show-size-changer="true"
|
||
:show-quick-jumper="true"
|
||
:show-total="(total) => `共 ${total} 条`"
|
||
@change="handlePageChange"
|
||
/>
|
||
</Flex>
|
||
</Flex>
|
||
</Card>
|
||
|
||
<!-- 右栏:权限管理 -->
|
||
<Card
|
||
title="职位菜单分配"
|
||
class="flex h-full flex-1 flex-col"
|
||
:body-style="{ height: '100%' }"
|
||
>
|
||
<template v-if="currentPosition">
|
||
<!-- 职位信息 + 统计与操作 -->
|
||
<div class="mb-4 rounded bg-gray-50 p-4 dark:bg-neutral-800">
|
||
<div class="mb-3 flex items-center justify-between gap-2">
|
||
<div class="flex items-center gap-2">
|
||
<span class="text-base font-semibold">{{
|
||
currentPosition.name
|
||
}}</span>
|
||
</div>
|
||
<div class="flex items-center gap-2">
|
||
<Button @click="handleSelectNone">取消全选</Button>
|
||
<Button type="primary" @click="handleSelectAll">
|
||
一键全选
|
||
</Button>
|
||
<Button type="primary" @click="savePositionPermissions">
|
||
保存菜单
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
<div class="flex items-center gap-6 text-xs text-gray-500">
|
||
<span>编码:{{ currentPosition.code }}</span>
|
||
<span class="flex items-center gap-1">
|
||
<span class="text-[var(--text-secondary)]">系统菜单:</span>
|
||
<span class="text-sm font-semibold">{{ stats.total }}</span>
|
||
</span>
|
||
<span class="flex items-center gap-1">
|
||
<span class="text-[var(--text-secondary)]">已配置:</span>
|
||
<span class="text-sm font-semibold text-[var(--success)]">{{
|
||
stats.position
|
||
}}</span>
|
||
</span>
|
||
<span class="flex items-center gap-1">
|
||
<span class="text-[var(--text-secondary)]">当前选择:</span>
|
||
<span class="text-sm font-semibold text-[var(--primary)]">{{
|
||
stats.selected
|
||
}}</span>
|
||
</span>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 菜单树 -->
|
||
<div class="min-h-0 flex-1 overflow-y-auto">
|
||
<Spin :spinning="permissionLoading">
|
||
<Tree
|
||
v-model:checked-keys="checkedKeys"
|
||
v-model:expanded-keys="expandedKeys"
|
||
:tree-data="convertToTreeData(companyMenuTree)"
|
||
checkable
|
||
block-node
|
||
class="rounded border"
|
||
@check="handleTreeCheckChange"
|
||
>
|
||
<template #title="{ title, status, disabled }">
|
||
<div class="flex items-center gap-2">
|
||
<span class="font-medium">{{ title }}</span>
|
||
<span
|
||
v-if="status === 0"
|
||
class="rounded bg-red-50 px-1.5 py-0.5 text-xs text-red-600 dark:bg-red-900/30"
|
||
>
|
||
已禁用
|
||
</span>
|
||
<span
|
||
v-if="disabled"
|
||
class="rounded bg-red-50 px-1.5 py-0.5 text-xs text-red-600 dark:bg-red-900/30"
|
||
>
|
||
不可选择
|
||
</span>
|
||
</div>
|
||
</template>
|
||
</Tree>
|
||
</Spin>
|
||
</div>
|
||
</template>
|
||
|
||
<div v-else class="flex h-full flex-1 items-center justify-center">
|
||
<div class="text-center text-gray-500">
|
||
<div class="mb-4 text-5xl">👈</div>
|
||
<div class="text-base">请先选择一个职位</div>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
</Flex>
|
||
<!-- <div class="flex h-full gap-4 max-lg:flex-col">
|
||
|
||
</div> -->
|
||
</Page>
|
||
</template>
|