mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:49:20 +08:00
wip:岗位权限
This commit is contained in:
+19
@@ -73,4 +73,23 @@ public class CompanyMenuController {
|
|||||||
companyMenuService.batchDeleteCompanyMenu(id, menuIds);
|
companyMenuService.batchDeleteCompanyMenu(id, menuIds);
|
||||||
return ApiResponse.success();
|
return ApiResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "获取公司所有菜单ID", description = "获取指定公司的所有菜单ID列表")
|
||||||
|
@GetMapping("/menu-ids/{id}")
|
||||||
|
@RequireRole(description = "获取公司菜单ID", allowUser = false)
|
||||||
|
public ApiResponse<List<Long>> getCompanyMenuIds(
|
||||||
|
@Parameter(description = "公司ID") @PathVariable Long id) {
|
||||||
|
List<Long> menuIds = companyMenuService.getCompanyMenuIds(id);
|
||||||
|
return ApiResponse.success(menuIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "批量更新公司菜单", description = "批量更新公司菜单状态,新增缺失的菜单,停用多余的菜单,启用已有的菜单")
|
||||||
|
@PostMapping("/batch-update/{id}")
|
||||||
|
@RequireRole(description = "批量更新公司菜单", allowUser = false)
|
||||||
|
public ApiResponse<Void> batchUpdateCompanyMenus(
|
||||||
|
@Parameter(description = "公司ID") @PathVariable Long id,
|
||||||
|
@Parameter(description = "菜单ID列表") @RequestBody List<Long> menuIds) {
|
||||||
|
companyMenuService.batchUpdateCompanyMenus(id, menuIds);
|
||||||
|
return ApiResponse.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
@@ -65,4 +65,21 @@ public class PositionPermissionController {
|
|||||||
boolean hasPermission = positionCompanyMenuService.hasPositionMenu(positionId, companyMenuId);
|
boolean hasPermission = positionCompanyMenuService.hasPositionMenu(positionId, companyMenuId);
|
||||||
return ApiResponse.success(hasPermission);
|
return ApiResponse.success(hasPermission);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "获取岗位所有公司菜单ID", description = "获取指定岗位的所有公司菜单ID列表")
|
||||||
|
@GetMapping("/company-menu-ids/{positionId}")
|
||||||
|
public ApiResponse<List<Long>> getPositionCompanyMenuIds(
|
||||||
|
@Parameter(description = "岗位ID") @PathVariable Long positionId) {
|
||||||
|
List<Long> companyMenuIds = positionCompanyMenuService.getPositionCompanyMenuIds(positionId);
|
||||||
|
return ApiResponse.success(companyMenuIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "批量更新岗位公司菜单", description = "批量更新岗位公司菜单权限,新增缺失的权限,删除多余的权限")
|
||||||
|
@PostMapping("/batch-update/{positionId}")
|
||||||
|
public ApiResponse<Void> batchUpdatePositionCompanyMenus(
|
||||||
|
@Parameter(description = "岗位ID") @PathVariable Long positionId,
|
||||||
|
@Parameter(description = "公司菜单ID列表") @RequestBody List<Long> companyMenuIds) {
|
||||||
|
positionCompanyMenuService.batchUpdatePositionCompanyMenus(positionId, companyMenuIds);
|
||||||
|
return ApiResponse.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -32,6 +32,12 @@ public class PositionCompanyMenuPO {
|
|||||||
@TableField("company_menu_id")
|
@TableField("company_menu_id")
|
||||||
private Long companyMenuId;
|
private Long companyMenuId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态(0-停用,1-启用)
|
||||||
|
*/
|
||||||
|
@TableField("status")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建时间
|
* 创建时间
|
||||||
*/
|
*/
|
||||||
|
|||||||
+16
@@ -62,4 +62,20 @@ public interface CompanyMenuService extends IService<CompanyMenuPO> {
|
|||||||
*/
|
*/
|
||||||
List<MenuVO> getCompanyMenusForRoute(Long companyId);
|
List<MenuVO> getCompanyMenusForRoute(Long companyId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取公司所有菜单ID列表
|
||||||
|
*
|
||||||
|
* @param companyId 公司ID
|
||||||
|
* @return 菜单ID列表
|
||||||
|
*/
|
||||||
|
List<Long> getCompanyMenuIds(Long companyId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新公司菜单
|
||||||
|
*
|
||||||
|
* @param companyId 公司ID
|
||||||
|
* @param menuIds 菜单ID列表
|
||||||
|
*/
|
||||||
|
void batchUpdateCompanyMenus(Long companyId, List<Long> menuIds);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -58,4 +58,20 @@ public interface PositionCompanyMenuService {
|
|||||||
* @return 菜单列表
|
* @return 菜单列表
|
||||||
*/
|
*/
|
||||||
List<MenuVO> getPositionMenusForRoute(Long positionId);
|
List<MenuVO> getPositionMenusForRoute(Long positionId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取岗位所有公司菜单ID列表
|
||||||
|
*
|
||||||
|
* @param positionId 岗位ID
|
||||||
|
* @return 公司菜单ID列表
|
||||||
|
*/
|
||||||
|
List<Long> getPositionCompanyMenuIds(Long positionId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新岗位公司菜单
|
||||||
|
*
|
||||||
|
* @param positionId 岗位ID
|
||||||
|
* @param companyMenuIds 公司菜单ID列表
|
||||||
|
*/
|
||||||
|
void batchUpdatePositionCompanyMenus(Long positionId, List<Long> companyMenuIds);
|
||||||
}
|
}
|
||||||
|
|||||||
+58
@@ -234,6 +234,64 @@ public class CompanyMenuServiceImpl extends ServiceImpl<CompanyMenuMapper, Compa
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Long> getCompanyMenuIds(Long companyId) {
|
||||||
|
// 查询公司所有菜单(包括启用和禁用的)
|
||||||
|
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(CompanyMenuPO::getCompanyId, companyId);
|
||||||
|
wrapper.eq(CompanyMenuPO::getStatus, 1);
|
||||||
|
|
||||||
|
List<CompanyMenuPO> companyMenus = this.list(wrapper);
|
||||||
|
|
||||||
|
// 返回菜单ID列表
|
||||||
|
return companyMenus.stream()
|
||||||
|
.map(CompanyMenuPO::getMenuId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void batchUpdateCompanyMenus(Long companyId, List<Long> menuIds) {
|
||||||
|
// 查询公司现有菜单
|
||||||
|
LambdaQueryWrapper<CompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(CompanyMenuPO::getCompanyId, companyId);
|
||||||
|
List<CompanyMenuPO> existingMenus = this.list(wrapper);
|
||||||
|
|
||||||
|
// 构建现有菜单ID到CompanyMenuPO的映射
|
||||||
|
Map<Long, CompanyMenuPO> existingMenuMap = existingMenus.stream()
|
||||||
|
.collect(Collectors.toMap(CompanyMenuPO::getMenuId, cm -> cm));
|
||||||
|
|
||||||
|
// 1. 处理新增的菜单(接收到的菜单ID在公司现有菜单中没有的)
|
||||||
|
for (Long menuId : menuIds) {
|
||||||
|
if (!existingMenuMap.containsKey(menuId)) {
|
||||||
|
// 新增菜单
|
||||||
|
CompanyMenuPO newCompanyMenu = new CompanyMenuPO();
|
||||||
|
newCompanyMenu.setCompanyId(companyId);
|
||||||
|
newCompanyMenu.setMenuId(menuId);
|
||||||
|
newCompanyMenu.setStatus(1); // 启用状态
|
||||||
|
this.save(newCompanyMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 处理现有菜单的状态
|
||||||
|
for (CompanyMenuPO existingMenu : existingMenus) {
|
||||||
|
Long menuId = existingMenu.getMenuId();
|
||||||
|
|
||||||
|
if (menuIds.contains(menuId)) {
|
||||||
|
// 接收到的菜单ID列表中有这个菜单,启用它
|
||||||
|
if (existingMenu.getStatus() == 0) {
|
||||||
|
existingMenu.setStatus(1);
|
||||||
|
this.updateById(existingMenu);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 接收到的菜单ID列表中没有这个菜单,停用它
|
||||||
|
if (existingMenu.getStatus() == 1) {
|
||||||
|
existingMenu.setStatus(0);
|
||||||
|
this.updateById(existingMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为VO
|
* 转换为VO
|
||||||
|
|||||||
+48
@@ -280,4 +280,52 @@ public class PositionCompanyMenuServiceImpl extends ServiceImpl<PositionCompanyM
|
|||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Long> getPositionCompanyMenuIds(Long positionId) {
|
||||||
|
// 查询岗位菜单权限关联记录
|
||||||
|
LambdaQueryWrapper<PositionCompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(PositionCompanyMenuPO::getPositionId, positionId);
|
||||||
|
|
||||||
|
List<PositionCompanyMenuPO> positionMenus = this.list(wrapper);
|
||||||
|
|
||||||
|
// 返回公司菜单ID列表
|
||||||
|
return positionMenus.stream()
|
||||||
|
.map(PositionCompanyMenuPO::getCompanyMenuId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void batchUpdatePositionCompanyMenus(Long positionId, List<Long> companyMenuIds) {
|
||||||
|
// 查询岗位现有菜单权限
|
||||||
|
LambdaQueryWrapper<PositionCompanyMenuPO> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(PositionCompanyMenuPO::getPositionId, positionId);
|
||||||
|
List<PositionCompanyMenuPO> existingMenus = this.list(wrapper);
|
||||||
|
|
||||||
|
// 构建现有公司菜单ID到PositionCompanyMenuPO的映射
|
||||||
|
Map<Long, PositionCompanyMenuPO> existingMenuMap = existingMenus.stream()
|
||||||
|
.collect(Collectors.toMap(PositionCompanyMenuPO::getCompanyMenuId, pm -> pm));
|
||||||
|
|
||||||
|
// 1. 处理新增的菜单权限(接收到的公司菜单ID在岗位现有菜单权限中没有的)
|
||||||
|
for (Long companyMenuId : companyMenuIds) {
|
||||||
|
if (!existingMenuMap.containsKey(companyMenuId)) {
|
||||||
|
// 新增菜单权限
|
||||||
|
PositionCompanyMenuPO newPositionMenu = new PositionCompanyMenuPO();
|
||||||
|
newPositionMenu.setPositionId(positionId);
|
||||||
|
newPositionMenu.setCompanyMenuId(companyMenuId);
|
||||||
|
this.save(newPositionMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 处理现有菜单权限的删除(岗位现有菜单权限中有但接收列表中没有的)
|
||||||
|
for (PositionCompanyMenuPO existingMenu : existingMenus) {
|
||||||
|
Long companyMenuId = existingMenu.getCompanyMenuId();
|
||||||
|
|
||||||
|
if (!companyMenuIds.contains(companyMenuId)) {
|
||||||
|
// 接收到的公司菜单ID列表中没有这个菜单权限,删除它
|
||||||
|
this.removeById(existingMenu.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ CREATE TABLE `org_position_company_menu_map` (
|
|||||||
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
||||||
`position_id` bigint unsigned NOT NULL COMMENT '岗位ID',
|
`position_id` bigint unsigned NOT NULL COMMENT '岗位ID',
|
||||||
`company_menu_id` bigint unsigned NOT NULL COMMENT '公司菜单权限ID',
|
`company_menu_id` bigint unsigned NOT NULL COMMENT '公司菜单权限ID',
|
||||||
|
`status` tinyint unsigned NOT NULL DEFAULT '1' COMMENT '状态(0-停用,1-启用)',
|
||||||
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||||
`create_by` bigint unsigned NOT NULL COMMENT '创建人ID',
|
`create_by` bigint unsigned NOT NULL COMMENT '创建人ID',
|
||||||
PRIMARY KEY (`id`) USING BTREE,
|
PRIMARY KEY (`id`) USING BTREE,
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { requestClient } from '#/api/request';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 岗位权限管理API
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取岗位所有公司菜单ID
|
||||||
|
*/
|
||||||
|
export async function getPositionCompanyMenuIdsApi(positionId: number) {
|
||||||
|
return requestClient.get<number[]>(`/position-permission/company-menu-ids/${positionId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新岗位公司菜单
|
||||||
|
*/
|
||||||
|
export async function batchUpdatePositionCompanyMenusApi(positionId: number, companyMenuIds: number[]) {
|
||||||
|
return requestClient.post<void>(`/position-permission/batch-update/${positionId}`, companyMenuIds);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './apis';
|
||||||
@@ -1,314 +1,521 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watch } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import { Drawer, Tree, Button, message, Spin } from 'ant-design-vue'
|
import { useVbenDrawer } from '@vben/common-ui'
|
||||||
|
import { Button, message, Select, Spin, Tree } from 'ant-design-vue'
|
||||||
import { getAllMenusApi } from '#/api/system/menu'
|
import { getAllMenusApi } from '#/api/system/menu'
|
||||||
import { getCompanyMenuIdsApi, batchUpdateCompanyMenusApi } from '#/api/organization/company-menu'
|
import { getCompanyMenuIdsApi, batchUpdateCompanyMenusApi } from '#/api/organization/company-menu'
|
||||||
import type { MenuVO } from '#/api/system/menu/types'
|
import type { MenuVO } from '#/api/system/menu/types'
|
||||||
|
|
||||||
// 响应式数据
|
interface Props {
|
||||||
const visible = ref(false)
|
company?: any
|
||||||
const loading = ref(false)
|
|
||||||
const submitLoading = ref(false)
|
|
||||||
const currentCompany = ref<any>(null)
|
|
||||||
const allMenus = ref<MenuVO.MenuInfo[]>([])
|
|
||||||
const companyMenuIds = ref<number[]>([])
|
|
||||||
const checkedKeys = ref<number[]>([])
|
|
||||||
const expandedKeys = ref<number[]>([])
|
|
||||||
|
|
||||||
// 树形控件配置
|
|
||||||
const treeProps = {
|
|
||||||
title: 'name',
|
|
||||||
key: 'id',
|
|
||||||
children: 'children',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 树形数据
|
interface Emits {
|
||||||
const treeData = computed(() => {
|
(e: 'success'): void
|
||||||
return buildTreeData(allMenus.value)
|
(e: 'close'): void
|
||||||
|
}
|
||||||
|
|
||||||
|
defineOptions({
|
||||||
|
name: 'CompanyMenuDrawer',
|
||||||
})
|
})
|
||||||
|
|
||||||
// 构建树形数据
|
defineProps<Props>()
|
||||||
function buildTreeData(menus: MenuVO.MenuInfo[]): any[] {
|
const emit = defineEmits<Emits>()
|
||||||
// 如果菜单数据已经是树形结构(有children字段),直接转换
|
|
||||||
if (menus.some(menu => menu.children && menu.children.length > 0)) {
|
|
||||||
return menus.map(menu => ({
|
|
||||||
key: menu.id,
|
|
||||||
title: menu.name,
|
|
||||||
children: menu.children ? buildTreeData(menu.children) : undefined,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果菜单数据是扁平结构(只有parentId字段),需要构建树形结构
|
// 系统菜单列表数据
|
||||||
return buildTreeFromFlatData(menus)
|
const systemMenuList = ref<MenuVO.MenuInfo[]>([])
|
||||||
}
|
|
||||||
|
|
||||||
// 从扁平数据构建树形结构
|
// 公司菜单ID列表
|
||||||
function buildTreeFromFlatData(menus: MenuVO.MenuInfo[]): any[] {
|
const companyMenuIds = ref<number[]>([])
|
||||||
|
|
||||||
|
// 加载状态
|
||||||
|
const loading = ref(false)
|
||||||
|
|
||||||
|
// 保存状态
|
||||||
|
const saving = ref(false)
|
||||||
|
|
||||||
|
// 当前公司信息
|
||||||
|
const currentCompany = ref<any>()
|
||||||
|
|
||||||
|
// 选中的菜单ID
|
||||||
|
const checkedKeys = ref<number[]>([])
|
||||||
|
|
||||||
|
// 展开的节点键
|
||||||
|
const expandedKeys = ref<number[]>([])
|
||||||
|
|
||||||
|
// 分组选择
|
||||||
|
const selectedGroup = ref<'super' | 'normal' | undefined>(undefined)
|
||||||
|
|
||||||
|
// 分组选项
|
||||||
|
const groupOptions = [
|
||||||
|
{ label: '超管菜单', value: 'super' as const },
|
||||||
|
{ label: '普通菜单', value: 'normal' as const },
|
||||||
|
]
|
||||||
|
|
||||||
|
// 创建抽屉组件
|
||||||
|
const [MainDrawer, mainDrawerApi] = useVbenDrawer({
|
||||||
|
title: '公司菜单权限管理',
|
||||||
|
placement: 'right',
|
||||||
|
class: 'w-[800px]',
|
||||||
|
zIndex: 1001,
|
||||||
|
confirmText: '保存权限配置',
|
||||||
|
cancelText: '取消',
|
||||||
|
onConfirm: async () => {
|
||||||
|
await handleSave()
|
||||||
|
},
|
||||||
|
onCancel() {
|
||||||
|
handleClose()
|
||||||
|
},
|
||||||
|
onClosed() {
|
||||||
|
emit('close')
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 将菜单列表转换为树形结构
|
||||||
|
const menuTreeData = computed(() => {
|
||||||
const menuMap = new Map<number, any>()
|
const menuMap = new Map<number, any>()
|
||||||
const rootMenus: any[] = []
|
const rootMenus: any[] = []
|
||||||
|
|
||||||
// 创建所有菜单节点的映射
|
// 先创建所有节点
|
||||||
menus.forEach(menu => {
|
systemMenuList.value.forEach((menu) => {
|
||||||
menuMap.set(menu.id, {
|
menuMap.set(menu.id, {
|
||||||
key: menu.id,
|
key: menu.id,
|
||||||
title: menu.name,
|
title: menu.name,
|
||||||
children: []
|
type: menu.type,
|
||||||
|
superMenu: menu.superMenu,
|
||||||
|
status: menu.status,
|
||||||
|
disabled: menu.status === 0, // 禁用状态的菜单不可选择
|
||||||
|
children: [],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// 构建父子关系
|
// 构建树形结构
|
||||||
menus.forEach(menu => {
|
systemMenuList.value.forEach((menu) => {
|
||||||
const node = menuMap.get(menu.id)!
|
const node = menuMap.get(menu.id)!
|
||||||
if (menu.parentId && menuMap.has(menu.parentId)) {
|
if (menu.parentId && menuMap.has(menu.parentId)) {
|
||||||
// 有父级菜单,添加到父级的children中
|
|
||||||
const parent = menuMap.get(menu.parentId)!
|
const parent = menuMap.get(menu.parentId)!
|
||||||
parent.children.push(node)
|
parent.children.push(node)
|
||||||
} else {
|
} else {
|
||||||
// 没有父级菜单,是根级菜单
|
|
||||||
rootMenus.push(node)
|
rootMenus.push(node)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 清理空的children数组
|
return rootMenus
|
||||||
function cleanEmptyChildren(nodes: any[]): any[] {
|
})
|
||||||
return nodes.map(node => ({
|
|
||||||
...node,
|
|
||||||
children: node.children.length > 0 ? cleanEmptyChildren(node.children) : undefined
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
return cleanEmptyChildren(rootMenus)
|
// 统计信息
|
||||||
}
|
const stats = computed(() => {
|
||||||
|
const totalMenus = systemMenuList.value.length
|
||||||
// 监听抽屉打开状态
|
const companyMenus = companyMenuIds.value.length
|
||||||
watch(visible, (newVisible) => {
|
const selectedMenus = calculateCorrectSelectedCount(checkedKeys.value)
|
||||||
if (newVisible && currentCompany.value) {
|
return {
|
||||||
loadData()
|
total: totalMenus,
|
||||||
|
company: companyMenus,
|
||||||
|
selected: selectedMenus,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 计算正确的选中数量(处理父节点逻辑)
|
||||||
|
function calculateCorrectSelectedCount(menuIds: number[]): number {
|
||||||
|
// 使用与提交时相同的逻辑,确保统计数量与提交数量一致
|
||||||
|
const finalMenuIds = ensureParentMenuIds(menuIds)
|
||||||
|
return finalMenuIds.length
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理根节点,只保留叶子节点
|
||||||
|
function cleanRootNodes(menuIds: number[]): number[] {
|
||||||
|
return menuIds.filter((menuId) => {
|
||||||
|
const menu = systemMenuList.value.find((m) => m.id === menuId)
|
||||||
|
if (!menu) return false
|
||||||
|
|
||||||
|
// 检查是否有其他菜单将此菜单作为父菜单
|
||||||
|
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menuId)
|
||||||
|
|
||||||
|
// 只保留叶子节点(没有子节点的菜单)
|
||||||
|
return !hasChildren
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 加载数据
|
// 加载数据
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
if (!currentCompany.value) return
|
if (!currentCompany.value?.id) return
|
||||||
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
loading.value = true
|
const [systemData, companyData] = await Promise.all([
|
||||||
|
|
||||||
// 并行加载所有菜单和公司菜单ID
|
|
||||||
const [menusResult, menuIdsResult] = await Promise.all([
|
|
||||||
getAllMenusApi(),
|
getAllMenusApi(),
|
||||||
getCompanyMenuIdsApi(currentCompany.value.id)
|
getCompanyMenuIdsApi(currentCompany.value.id),
|
||||||
])
|
])
|
||||||
|
systemMenuList.value = systemData
|
||||||
|
companyMenuIds.value = companyData
|
||||||
|
|
||||||
allMenus.value = menusResult
|
// 清理根节点,只保留叶子节点
|
||||||
companyMenuIds.value = menuIdsResult
|
const leafMenuIds = cleanRootNodes(companyData)
|
||||||
checkedKeys.value = [...menuIdsResult]
|
checkedKeys.value = leafMenuIds
|
||||||
|
|
||||||
// 自动展开所有节点
|
// 自动展开所有节点
|
||||||
expandedKeys.value = getAllMenuIds(menusResult)
|
expandedKeys.value = systemData.map((menu) => menu.id)
|
||||||
|
} catch (error: any) {
|
||||||
} catch (error) {
|
message.error(error?.message || '加载数据失败')
|
||||||
console.error('加载数据失败:', error)
|
|
||||||
message.error('加载数据失败')
|
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有菜单ID(用于展开)
|
// 保存所有更改
|
||||||
function getAllMenuIds(menus: MenuVO.MenuInfo[]): number[] {
|
async function handleSave() {
|
||||||
const ids: number[] = []
|
if (!currentCompany.value?.id) return
|
||||||
|
saving.value = true
|
||||||
|
try {
|
||||||
|
// 确保提交时包含必要的父菜单ID
|
||||||
|
const finalMenuIds = ensureParentMenuIds(checkedKeys.value)
|
||||||
|
await batchUpdateCompanyMenusApi(currentCompany.value.id, finalMenuIds)
|
||||||
|
message.success('权限配置保存成功')
|
||||||
|
emit('success')
|
||||||
|
handleClose()
|
||||||
|
} catch (error: any) {
|
||||||
|
message.error(error?.message || '保存失败')
|
||||||
|
throw error // 重新抛出错误,让抽屉知道保存失败
|
||||||
|
} finally {
|
||||||
|
saving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 如果菜单数据已经是树形结构
|
// 一键全选
|
||||||
if (menus.some(menu => menu.children && menu.children.length > 0)) {
|
function handleSelectAll() {
|
||||||
menus.forEach(menu => {
|
// 获取所有可选择的菜单ID(排除禁用的)
|
||||||
ids.push(menu.id)
|
let selectableMenus = systemMenuList.value.filter(
|
||||||
if (menu.children) {
|
(menu) => menu.status === 1,
|
||||||
ids.push(...getAllMenuIds(menu.children))
|
)
|
||||||
}
|
|
||||||
})
|
// 如果选择了分组,则只选择该分组的菜单
|
||||||
} else {
|
if (selectedGroup.value) {
|
||||||
// 如果菜单数据是扁平结构,直接返回所有ID
|
if (selectedGroup.value === 'super') {
|
||||||
menus.forEach(menu => {
|
selectableMenus = selectableMenus.filter((menu) => menu.superMenu === 1)
|
||||||
ids.push(menu.id)
|
} else if (selectedGroup.value === 'normal') {
|
||||||
})
|
selectableMenus = selectableMenus.filter((menu) => menu.superMenu !== 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ids
|
// 只选择叶子节点(没有子节点的菜单)
|
||||||
}
|
const leafMenus = selectableMenus.filter((menu) => {
|
||||||
|
// 检查是否有其他菜单将此菜单作为父菜单
|
||||||
|
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||||
|
return !hasChildren
|
||||||
|
})
|
||||||
|
|
||||||
// 处理勾选变化
|
if (selectedGroup.value) {
|
||||||
function handleCheck(checkedKeysValue: any, _info: any) {
|
// 保留其他分组的选中状态,只替换当前分组的选中状态
|
||||||
// 处理checkedKeysValue可能是数组或对象的情况
|
const otherGroupCheckedKeys = checkedKeys.value.filter((id) => {
|
||||||
const keys = Array.isArray(checkedKeysValue)
|
const menu = systemMenuList.value.find((m) => m.id === id)
|
||||||
? checkedKeysValue
|
if (!menu) return false
|
||||||
: checkedKeysValue.checked || []
|
|
||||||
|
|
||||||
checkedKeys.value = keys.map((key: any) => Number(key))
|
if (selectedGroup.value === 'super') {
|
||||||
}
|
return menu.superMenu !== 1
|
||||||
|
} else if (selectedGroup.value === 'normal') {
|
||||||
// 全选
|
return menu.superMenu === 1
|
||||||
function handleSelectAll() {
|
}
|
||||||
checkedKeys.value = getAllMenuIds(allMenus.value)
|
return false
|
||||||
|
})
|
||||||
|
checkedKeys.value = [...otherGroupCheckedKeys, ...leafMenus.map((menu) => menu.id)]
|
||||||
|
} else {
|
||||||
|
// 如果没有选择分组,全选所有叶子节点
|
||||||
|
checkedKeys.value = leafMenus.map((menu) => menu.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取消全选
|
// 取消全选
|
||||||
function handleUnselectAll() {
|
function handleSelectNone() {
|
||||||
checkedKeys.value = []
|
if (selectedGroup.value) {
|
||||||
}
|
// 如果选择了分组,只取消该分组的叶子节点选中状态
|
||||||
|
const leafMenuIds = systemMenuList.value
|
||||||
|
.filter((menu) => {
|
||||||
|
// 只处理叶子节点
|
||||||
|
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||||
|
if (hasChildren) return false
|
||||||
|
|
||||||
// 展开全部
|
// 根据分组过滤
|
||||||
function handleExpandAll() {
|
if (selectedGroup.value === 'super') {
|
||||||
expandedKeys.value = getAllMenuIds(allMenus.value)
|
return menu.superMenu === 1
|
||||||
}
|
} else if (selectedGroup.value === 'normal') {
|
||||||
|
return menu.superMenu !== 1
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
.map((menu) => menu.id)
|
||||||
|
|
||||||
// 折叠全部
|
checkedKeys.value = checkedKeys.value.filter((id) => !leafMenuIds.includes(id))
|
||||||
function handleCollapseAll() {
|
} else {
|
||||||
expandedKeys.value = []
|
// 如果没有选择分组,取消所有叶子节点的选中状态
|
||||||
}
|
const allLeafMenuIds = systemMenuList.value
|
||||||
|
.filter((menu) => {
|
||||||
|
// 只处理叶子节点
|
||||||
|
return !systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||||
|
})
|
||||||
|
.map((menu) => menu.id)
|
||||||
|
|
||||||
// 提交更新
|
checkedKeys.value = checkedKeys.value.filter((id) => !allLeafMenuIds.includes(id))
|
||||||
async function handleSubmit() {
|
|
||||||
if (!currentCompany.value) return
|
|
||||||
|
|
||||||
try {
|
|
||||||
submitLoading.value = true
|
|
||||||
|
|
||||||
await batchUpdateCompanyMenusApi(currentCompany.value.id, checkedKeys.value)
|
|
||||||
|
|
||||||
message.success('菜单权限更新成功')
|
|
||||||
close()
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('更新菜单权限失败:', error)
|
|
||||||
message.error('更新菜单权限失败')
|
|
||||||
} finally {
|
|
||||||
submitLoading.value = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打开抽屉
|
// 处理树形控件的选中变化
|
||||||
function open(company: any) {
|
function handleTreeCheckChange(checkedKeysValue: any) {
|
||||||
currentCompany.value = company
|
checkedKeys.value = checkedKeysValue
|
||||||
visible.value = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保提交时包含必要的父菜单ID(递归添加所有层级的父菜单)
|
||||||
|
function ensureParentMenuIds(menuIds: number[]): number[] {
|
||||||
|
const result = new Set(menuIds)
|
||||||
|
|
||||||
|
// 递归添加所有父菜单ID
|
||||||
|
function addAllParentIds(menuId: number) {
|
||||||
|
const menu = systemMenuList.value.find((m) => m.id === menuId)
|
||||||
|
if (menu && menu.parentId) {
|
||||||
|
result.add(menu.parentId)
|
||||||
|
// 递归添加父菜单的父菜单
|
||||||
|
addAllParentIds(menu.parentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 为每个选中的菜单添加其所有父菜单ID
|
||||||
|
menuIds.forEach((menuId) => {
|
||||||
|
addAllParentIds(menuId)
|
||||||
|
})
|
||||||
|
|
||||||
|
const finalResult = [...result]
|
||||||
|
return finalResult
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 关闭抽屉
|
// 关闭抽屉
|
||||||
function close() {
|
function handleClose() {
|
||||||
visible.value = false
|
mainDrawerApi.close()
|
||||||
currentCompany.value = null
|
|
||||||
allMenus.value = []
|
|
||||||
companyMenuIds.value = []
|
|
||||||
checkedKeys.value = []
|
checkedKeys.value = []
|
||||||
expandedKeys.value = []
|
expandedKeys.value = []
|
||||||
|
selectedGroup.value = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// 暴露方法
|
// 暴露打开方法
|
||||||
defineExpose({
|
defineExpose({
|
||||||
open,
|
open: (company: any) => {
|
||||||
close,
|
// 设置公司信息
|
||||||
|
currentCompany.value = company
|
||||||
|
// 打开抽屉
|
||||||
|
mainDrawerApi.open()
|
||||||
|
// 加载数据
|
||||||
|
if (company?.id) {
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
close: () => mainDrawerApi.close(),
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Drawer
|
<MainDrawer>
|
||||||
v-model:open="visible"
|
<div class="permission-drawer">
|
||||||
title="公司菜单权限管理"
|
<!-- 头部信息 -->
|
||||||
width="600px"
|
<div class="drawer-header">
|
||||||
:closable="true"
|
<div class="company-info">
|
||||||
:body-style="{ display: 'flex', flexDirection: 'column', height: '100%', padding: '16px' }"
|
<span class="company-name">{{ currentCompany?.name }}</span>
|
||||||
@close="close"
|
<span class="company-id">(ID: {{ currentCompany?.id }})</span>
|
||||||
>
|
</div>
|
||||||
<div v-if="currentCompany" class="mb-4 flex-shrink-0">
|
<div class="stats-section">
|
||||||
<div class="text-lg font-medium">{{ currentCompany.name }}</div>
|
<div class="stats-left">
|
||||||
<div class="text-gray-500 text-sm">管理该公司的菜单访问权限</div>
|
<div class="stat-item">
|
||||||
</div>
|
<span class="label">系统菜单:</span>
|
||||||
|
<span class="value">{{ stats.total }}</span>
|
||||||
<Spin :spinning="loading" class="flex-1 flex flex-col min-h-0">
|
</div>
|
||||||
<div v-if="!loading && treeData.length > 0" class="flex-1 flex flex-col">
|
<div class="stat-item">
|
||||||
<div class="mb-4 flex-shrink-0">
|
<span class="label">已配置:</span>
|
||||||
<div class="text-sm text-gray-600 mb-2">
|
<span class="value success">{{ stats.company }}</span>
|
||||||
已选择 {{ checkedKeys.length }} 个菜单
|
</div>
|
||||||
|
<div class="stat-item">
|
||||||
|
<span class="label">当前选择:</span>
|
||||||
|
<span class="value primary">{{ stats.selected }}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-xs text-gray-400 mb-3">
|
<div class="stats-right">
|
||||||
勾选菜单后,该公司将拥有对应菜单的访问权限
|
<Select
|
||||||
</div>
|
v-model:value="selectedGroup"
|
||||||
<div class="flex space-x-2">
|
placeholder="选择分组"
|
||||||
<Button size="small" @click="handleSelectAll">
|
allow-clear
|
||||||
全选
|
style="width: 120px; margin-right: 8px"
|
||||||
|
>
|
||||||
|
<Select.Option
|
||||||
|
v-for="option in groupOptions"
|
||||||
|
:key="option.value || 'all'"
|
||||||
|
:value="option.value"
|
||||||
|
>
|
||||||
|
{{ option.label }}
|
||||||
|
</Select.Option>
|
||||||
|
</Select>
|
||||||
|
<Button type="primary" @click="handleSelectAll">
|
||||||
|
一键全选
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="small" @click="handleUnselectAll">
|
<Button @click="handleSelectNone">
|
||||||
取消全选
|
取消全选
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="small" @click="handleExpandAll">
|
|
||||||
展开全部
|
|
||||||
</Button>
|
|
||||||
<Button size="small" @click="handleCollapseAll">
|
|
||||||
折叠全部
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="flex-1 min-h-0">
|
<!-- 菜单树 -->
|
||||||
|
<div class="menu-tree-container">
|
||||||
|
<Spin :spinning="loading">
|
||||||
<Tree
|
<Tree
|
||||||
v-model:checkedKeys="checkedKeys"
|
v-model:checked-keys="checkedKeys"
|
||||||
v-model:expandedKeys="expandedKeys"
|
v-model:expanded-keys="expandedKeys"
|
||||||
:tree-data="treeData"
|
:tree-data="menuTreeData"
|
||||||
:checkable="true"
|
checkable
|
||||||
:show-line="true"
|
class="menu-tree"
|
||||||
:check-strictly="false"
|
@check="handleTreeCheckChange"
|
||||||
:tree-props="treeProps"
|
>
|
||||||
:default-expand-all="false"
|
<template #title="{ title, status, disabled, superMenu }">
|
||||||
:auto-expand-parent="true"
|
<div class="menu-item">
|
||||||
class="h-full overflow-y-auto border border-gray-200 rounded-lg p-4"
|
<span class="menu-name">{{ title }}</span>
|
||||||
@check="handleCheck"
|
<span v-if="superMenu === 1" class="menu-tag super">
|
||||||
/>
|
超管
|
||||||
</div>
|
</span>
|
||||||
|
<span v-if="status === 0" class="menu-status disabled">
|
||||||
|
已禁用
|
||||||
|
</span>
|
||||||
|
<span v-if="disabled" class="menu-status disabled">
|
||||||
|
不可选择
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</Tree>
|
||||||
|
</Spin>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<div v-else-if="!loading && treeData.length === 0" class="flex-1 flex items-center justify-center text-gray-400">
|
</MainDrawer>
|
||||||
暂无菜单数据
|
|
||||||
</div>
|
|
||||||
</Spin>
|
|
||||||
|
|
||||||
<template #footer>
|
|
||||||
<div class="flex justify-end space-x-2">
|
|
||||||
<Button @click="close">
|
|
||||||
取消
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="primary"
|
|
||||||
:loading="submitLoading"
|
|
||||||
@click="handleSubmit"
|
|
||||||
>
|
|
||||||
确定
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</Drawer>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style lang="scss" scoped>
|
||||||
:deep(.ant-tree) {
|
.permission-drawer {
|
||||||
background: transparent;
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
|
||||||
|
.drawer-header {
|
||||||
|
padding: 16px 0;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
margin-bottom: 16px;
|
||||||
|
background-color: var(--background-color);
|
||||||
|
flex-shrink: 0;
|
||||||
|
|
||||||
|
.company-info {
|
||||||
|
margin-bottom: 12px;
|
||||||
|
|
||||||
|
.company-name {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.company-id {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
margin-left: 8px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.stats-section {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
gap: 16px;
|
||||||
|
padding: 12px 16px;
|
||||||
|
background-color: var(--background-secondary);
|
||||||
|
border-radius: 6px;
|
||||||
|
|
||||||
|
.stats-left {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stats-right {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
|
||||||
|
.label {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.value {
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 14px;
|
||||||
|
|
||||||
|
&.success {
|
||||||
|
color: var(--success);
|
||||||
|
}
|
||||||
|
|
||||||
|
&.primary {
|
||||||
|
color: var(--primary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-tree-container {
|
||||||
|
flex: 1;
|
||||||
|
overflow-y: auto;
|
||||||
|
min-height: 0;
|
||||||
|
|
||||||
|
.menu-tree {
|
||||||
|
padding: 12px;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 6px;
|
||||||
|
|
||||||
|
.menu-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
|
||||||
|
.menu-name {
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-tag {
|
||||||
|
font-size: 12px;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
|
||||||
|
&.super {
|
||||||
|
color: var(--warning);
|
||||||
|
background-color: var(--warning-light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-status {
|
||||||
|
font-size: 12px;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
|
||||||
|
&.disabled {
|
||||||
|
color: var(--error);
|
||||||
|
background-color: var(--error-light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:deep(.ant-tree-node-content-wrapper) {
|
// 响应式设计
|
||||||
padding: 4px 8px;
|
@media (max-width: 768px) {
|
||||||
border-radius: 4px;
|
.stats-section {
|
||||||
}
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
:deep(.ant-tree-node-content-wrapper:hover) {
|
|
||||||
background-color: #f5f5f5;
|
|
||||||
}
|
|
||||||
|
|
||||||
:deep(.ant-tree-checkbox) {
|
|
||||||
margin-right: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
:deep(.ant-tree-title) {
|
|
||||||
font-size: 14px;
|
|
||||||
color: #333;
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user