mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
wip:岗位权限
This commit is contained in:
+19
@@ -73,4 +73,23 @@ public class CompanyMenuController {
|
||||
companyMenuService.batchDeleteCompanyMenu(id, menuIds);
|
||||
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);
|
||||
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")
|
||||
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);
|
||||
|
||||
/**
|
||||
* 获取公司所有菜单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 菜单列表
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
+48
@@ -280,4 +280,52 @@ public class PositionCompanyMenuServiceImpl extends ServiceImpl<PositionCompanyM
|
||||
})
|
||||
.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',
|
||||
`position_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_by` bigint unsigned NOT NULL COMMENT '创建人ID',
|
||||
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">
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { Drawer, Tree, Button, message, Spin } from 'ant-design-vue'
|
||||
import { computed, ref } from 'vue'
|
||||
import { useVbenDrawer } from '@vben/common-ui'
|
||||
import { Button, message, Select, Spin, Tree } from 'ant-design-vue'
|
||||
import { getAllMenusApi } from '#/api/system/menu'
|
||||
import { getCompanyMenuIdsApi, batchUpdateCompanyMenusApi } from '#/api/organization/company-menu'
|
||||
import type { MenuVO } from '#/api/system/menu/types'
|
||||
|
||||
// 响应式数据
|
||||
const visible = ref(false)
|
||||
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 Props {
|
||||
company?: any
|
||||
}
|
||||
|
||||
// 树形数据
|
||||
const treeData = computed(() => {
|
||||
return buildTreeData(allMenus.value)
|
||||
interface Emits {
|
||||
(e: 'success'): void
|
||||
(e: 'close'): void
|
||||
}
|
||||
|
||||
defineOptions({
|
||||
name: 'CompanyMenuDrawer',
|
||||
})
|
||||
|
||||
// 构建树形数据
|
||||
function buildTreeData(menus: MenuVO.MenuInfo[]): any[] {
|
||||
// 如果菜单数据已经是树形结构(有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)
|
||||
}
|
||||
defineProps<Props>()
|
||||
const emit = defineEmits<Emits>()
|
||||
|
||||
// 从扁平数据构建树形结构
|
||||
function buildTreeFromFlatData(menus: MenuVO.MenuInfo[]): any[] {
|
||||
// 系统菜单列表数据
|
||||
const systemMenuList = ref<MenuVO.MenuInfo[]>([])
|
||||
|
||||
// 公司菜单ID列表
|
||||
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 rootMenus: any[] = []
|
||||
|
||||
// 创建所有菜单节点的映射
|
||||
menus.forEach(menu => {
|
||||
|
||||
// 先创建所有节点
|
||||
systemMenuList.value.forEach((menu) => {
|
||||
menuMap.set(menu.id, {
|
||||
key: menu.id,
|
||||
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)!
|
||||
if (menu.parentId && menuMap.has(menu.parentId)) {
|
||||
// 有父级菜单,添加到父级的children中
|
||||
const parent = menuMap.get(menu.parentId)!
|
||||
parent.children.push(node)
|
||||
} else {
|
||||
// 没有父级菜单,是根级菜单
|
||||
rootMenus.push(node)
|
||||
}
|
||||
})
|
||||
|
||||
// 清理空的children数组
|
||||
function cleanEmptyChildren(nodes: any[]): any[] {
|
||||
return nodes.map(node => ({
|
||||
...node,
|
||||
children: node.children.length > 0 ? cleanEmptyChildren(node.children) : undefined
|
||||
}))
|
||||
}
|
||||
|
||||
return cleanEmptyChildren(rootMenus)
|
||||
}
|
||||
|
||||
// 监听抽屉打开状态
|
||||
watch(visible, (newVisible) => {
|
||||
if (newVisible && currentCompany.value) {
|
||||
loadData()
|
||||
return rootMenus
|
||||
})
|
||||
|
||||
// 统计信息
|
||||
const stats = computed(() => {
|
||||
const totalMenus = systemMenuList.value.length
|
||||
const companyMenus = companyMenuIds.value.length
|
||||
const selectedMenus = calculateCorrectSelectedCount(checkedKeys.value)
|
||||
return {
|
||||
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() {
|
||||
if (!currentCompany.value) return
|
||||
|
||||
if (!currentCompany.value?.id) return
|
||||
loading.value = true
|
||||
try {
|
||||
loading.value = true
|
||||
|
||||
// 并行加载所有菜单和公司菜单ID
|
||||
const [menusResult, menuIdsResult] = await Promise.all([
|
||||
const [systemData, companyData] = await Promise.all([
|
||||
getAllMenusApi(),
|
||||
getCompanyMenuIdsApi(currentCompany.value.id)
|
||||
getCompanyMenuIdsApi(currentCompany.value.id),
|
||||
])
|
||||
systemMenuList.value = systemData
|
||||
companyMenuIds.value = companyData
|
||||
|
||||
allMenus.value = menusResult
|
||||
companyMenuIds.value = menuIdsResult
|
||||
checkedKeys.value = [...menuIdsResult]
|
||||
// 清理根节点,只保留叶子节点
|
||||
const leafMenuIds = cleanRootNodes(companyData)
|
||||
checkedKeys.value = leafMenuIds
|
||||
|
||||
// 自动展开所有节点
|
||||
expandedKeys.value = getAllMenuIds(menusResult)
|
||||
|
||||
} catch (error) {
|
||||
console.error('加载数据失败:', error)
|
||||
message.error('加载数据失败')
|
||||
expandedKeys.value = systemData.map((menu) => menu.id)
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '加载数据失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有菜单ID(用于展开)
|
||||
function getAllMenuIds(menus: MenuVO.MenuInfo[]): number[] {
|
||||
const ids: number[] = []
|
||||
|
||||
// 如果菜单数据已经是树形结构
|
||||
if (menus.some(menu => menu.children && menu.children.length > 0)) {
|
||||
menus.forEach(menu => {
|
||||
ids.push(menu.id)
|
||||
if (menu.children) {
|
||||
ids.push(...getAllMenuIds(menu.children))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 如果菜单数据是扁平结构,直接返回所有ID
|
||||
menus.forEach(menu => {
|
||||
ids.push(menu.id)
|
||||
})
|
||||
// 保存所有更改
|
||||
async function handleSave() {
|
||||
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
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
// 处理勾选变化
|
||||
function handleCheck(checkedKeysValue: any, _info: any) {
|
||||
// 处理checkedKeysValue可能是数组或对象的情况
|
||||
const keys = Array.isArray(checkedKeysValue)
|
||||
? checkedKeysValue
|
||||
: checkedKeysValue.checked || []
|
||||
|
||||
checkedKeys.value = keys.map((key: any) => Number(key))
|
||||
}
|
||||
|
||||
// 全选
|
||||
// 一键全选
|
||||
function handleSelectAll() {
|
||||
checkedKeys.value = getAllMenuIds(allMenus.value)
|
||||
// 获取所有可选择的菜单ID(排除禁用的)
|
||||
let selectableMenus = systemMenuList.value.filter(
|
||||
(menu) => menu.status === 1,
|
||||
)
|
||||
|
||||
// 如果选择了分组,则只选择该分组的菜单
|
||||
if (selectedGroup.value) {
|
||||
if (selectedGroup.value === 'super') {
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu === 1)
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
selectableMenus = selectableMenus.filter((menu) => menu.superMenu !== 1)
|
||||
}
|
||||
}
|
||||
|
||||
// 只选择叶子节点(没有子节点的菜单)
|
||||
const leafMenus = selectableMenus.filter((menu) => {
|
||||
// 检查是否有其他菜单将此菜单作为父菜单
|
||||
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
return !hasChildren
|
||||
})
|
||||
|
||||
if (selectedGroup.value) {
|
||||
// 保留其他分组的选中状态,只替换当前分组的选中状态
|
||||
const otherGroupCheckedKeys = checkedKeys.value.filter((id) => {
|
||||
const menu = systemMenuList.value.find((m) => m.id === id)
|
||||
if (!menu) return false
|
||||
|
||||
if (selectedGroup.value === 'super') {
|
||||
return menu.superMenu !== 1
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
return menu.superMenu === 1
|
||||
}
|
||||
return false
|
||||
})
|
||||
checkedKeys.value = [...otherGroupCheckedKeys, ...leafMenus.map((menu) => menu.id)]
|
||||
} else {
|
||||
// 如果没有选择分组,全选所有叶子节点
|
||||
checkedKeys.value = leafMenus.map((menu) => menu.id)
|
||||
}
|
||||
}
|
||||
|
||||
// 取消全选
|
||||
function handleUnselectAll() {
|
||||
checkedKeys.value = []
|
||||
}
|
||||
|
||||
// 展开全部
|
||||
function handleExpandAll() {
|
||||
expandedKeys.value = getAllMenuIds(allMenus.value)
|
||||
}
|
||||
|
||||
// 折叠全部
|
||||
function handleCollapseAll() {
|
||||
expandedKeys.value = []
|
||||
}
|
||||
|
||||
// 提交更新
|
||||
async function handleSubmit() {
|
||||
if (!currentCompany.value) return
|
||||
|
||||
try {
|
||||
submitLoading.value = true
|
||||
function handleSelectNone() {
|
||||
if (selectedGroup.value) {
|
||||
// 如果选择了分组,只取消该分组的叶子节点选中状态
|
||||
const leafMenuIds = systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
const hasChildren = systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
if (hasChildren) return false
|
||||
|
||||
// 根据分组过滤
|
||||
if (selectedGroup.value === 'super') {
|
||||
return menu.superMenu === 1
|
||||
} else if (selectedGroup.value === 'normal') {
|
||||
return menu.superMenu !== 1
|
||||
}
|
||||
return false
|
||||
})
|
||||
.map((menu) => menu.id)
|
||||
|
||||
await batchUpdateCompanyMenusApi(currentCompany.value.id, checkedKeys.value)
|
||||
checkedKeys.value = checkedKeys.value.filter((id) => !leafMenuIds.includes(id))
|
||||
} else {
|
||||
// 如果没有选择分组,取消所有叶子节点的选中状态
|
||||
const allLeafMenuIds = systemMenuList.value
|
||||
.filter((menu) => {
|
||||
// 只处理叶子节点
|
||||
return !systemMenuList.value.some((otherMenu) => otherMenu.parentId === menu.id)
|
||||
})
|
||||
.map((menu) => menu.id)
|
||||
|
||||
message.success('菜单权限更新成功')
|
||||
close()
|
||||
|
||||
} catch (error) {
|
||||
console.error('更新菜单权限失败:', error)
|
||||
message.error('更新菜单权限失败')
|
||||
} finally {
|
||||
submitLoading.value = false
|
||||
checkedKeys.value = checkedKeys.value.filter((id) => !allLeafMenuIds.includes(id))
|
||||
}
|
||||
}
|
||||
|
||||
// 打开抽屉
|
||||
function open(company: any) {
|
||||
currentCompany.value = company
|
||||
visible.value = true
|
||||
// 处理树形控件的选中变化
|
||||
function handleTreeCheckChange(checkedKeysValue: any) {
|
||||
checkedKeys.value = checkedKeysValue
|
||||
}
|
||||
|
||||
// 确保提交时包含必要的父菜单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() {
|
||||
visible.value = false
|
||||
currentCompany.value = null
|
||||
allMenus.value = []
|
||||
companyMenuIds.value = []
|
||||
function handleClose() {
|
||||
mainDrawerApi.close()
|
||||
checkedKeys.value = []
|
||||
expandedKeys.value = []
|
||||
selectedGroup.value = undefined
|
||||
}
|
||||
|
||||
// 暴露方法
|
||||
// 暴露打开方法
|
||||
defineExpose({
|
||||
open,
|
||||
close,
|
||||
open: (company: any) => {
|
||||
// 设置公司信息
|
||||
currentCompany.value = company
|
||||
// 打开抽屉
|
||||
mainDrawerApi.open()
|
||||
// 加载数据
|
||||
if (company?.id) {
|
||||
loadData()
|
||||
}
|
||||
},
|
||||
close: () => mainDrawerApi.close(),
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Drawer
|
||||
v-model:open="visible"
|
||||
title="公司菜单权限管理"
|
||||
width="600px"
|
||||
:closable="true"
|
||||
:body-style="{ display: 'flex', flexDirection: 'column', height: '100%', padding: '16px' }"
|
||||
@close="close"
|
||||
>
|
||||
<div v-if="currentCompany" class="mb-4 flex-shrink-0">
|
||||
<div class="text-lg font-medium">{{ currentCompany.name }}</div>
|
||||
<div class="text-gray-500 text-sm">管理该公司的菜单访问权限</div>
|
||||
</div>
|
||||
|
||||
<Spin :spinning="loading" class="flex-1 flex flex-col min-h-0">
|
||||
<div v-if="!loading && treeData.length > 0" class="flex-1 flex flex-col">
|
||||
<div class="mb-4 flex-shrink-0">
|
||||
<div class="text-sm text-gray-600 mb-2">
|
||||
已选择 {{ checkedKeys.length }} 个菜单
|
||||
<MainDrawer>
|
||||
<div class="permission-drawer">
|
||||
<!-- 头部信息 -->
|
||||
<div class="drawer-header">
|
||||
<div class="company-info">
|
||||
<span class="company-name">{{ currentCompany?.name }}</span>
|
||||
<span class="company-id">(ID: {{ currentCompany?.id }})</span>
|
||||
</div>
|
||||
<div class="stats-section">
|
||||
<div class="stats-left">
|
||||
<div class="stat-item">
|
||||
<span class="label">系统菜单:</span>
|
||||
<span class="value">{{ stats.total }}</span>
|
||||
</div>
|
||||
<div class="stat-item">
|
||||
<span class="label">已配置:</span>
|
||||
<span class="value success">{{ stats.company }}</span>
|
||||
</div>
|
||||
<div class="stat-item">
|
||||
<span class="label">当前选择:</span>
|
||||
<span class="value primary">{{ stats.selected }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-xs text-gray-400 mb-3">
|
||||
勾选菜单后,该公司将拥有对应菜单的访问权限
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<Button size="small" @click="handleSelectAll">
|
||||
全选
|
||||
<div class="stats-right">
|
||||
<Select
|
||||
v-model:value="selectedGroup"
|
||||
placeholder="选择分组"
|
||||
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 size="small" @click="handleUnselectAll">
|
||||
<Button @click="handleSelectNone">
|
||||
取消全选
|
||||
</Button>
|
||||
<Button size="small" @click="handleExpandAll">
|
||||
展开全部
|
||||
</Button>
|
||||
<Button size="small" @click="handleCollapseAll">
|
||||
折叠全部
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 min-h-0">
|
||||
</div>
|
||||
|
||||
<!-- 菜单树 -->
|
||||
<div class="menu-tree-container">
|
||||
<Spin :spinning="loading">
|
||||
<Tree
|
||||
v-model:checkedKeys="checkedKeys"
|
||||
v-model:expandedKeys="expandedKeys"
|
||||
:tree-data="treeData"
|
||||
:checkable="true"
|
||||
:show-line="true"
|
||||
:check-strictly="false"
|
||||
:tree-props="treeProps"
|
||||
:default-expand-all="false"
|
||||
:auto-expand-parent="true"
|
||||
class="h-full overflow-y-auto border border-gray-200 rounded-lg p-4"
|
||||
@check="handleCheck"
|
||||
/>
|
||||
</div>
|
||||
v-model:checked-keys="checkedKeys"
|
||||
v-model:expanded-keys="expandedKeys"
|
||||
:tree-data="menuTreeData"
|
||||
checkable
|
||||
class="menu-tree"
|
||||
@check="handleTreeCheckChange"
|
||||
>
|
||||
<template #title="{ title, status, disabled, superMenu }">
|
||||
<div class="menu-item">
|
||||
<span class="menu-name">{{ title }}</span>
|
||||
<span v-if="superMenu === 1" class="menu-tag super">
|
||||
超管
|
||||
</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 v-else-if="!loading && treeData.length === 0" class="flex-1 flex items-center justify-center text-gray-400">
|
||||
暂无菜单数据
|
||||
</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>
|
||||
</div>
|
||||
</MainDrawer>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.ant-tree) {
|
||||
background: transparent;
|
||||
<style lang="scss" scoped>
|
||||
.permission-drawer {
|
||||
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;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
: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;
|
||||
// 响应式设计
|
||||
@media (max-width: 768px) {
|
||||
.stats-section {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user