菜单权限控制

This commit is contained in:
zhangchenbiao
2023-06-29 19:27:20 +08:00
parent 7a5497a7f2
commit 63054bed46
6 changed files with 77 additions and 4 deletions

View File

@@ -2,14 +2,20 @@ package com.cool.store.service.impl;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.SysMenuDAO;
import com.cool.store.dao.SysRoleMenuDAO;
import com.cool.store.entity.SysMenuDO;
import com.cool.store.entity.SysRoleMenuDO;
import com.cool.store.enums.MenuTypeEnum;
import com.cool.store.enums.PlatFormTypeEnum;
import com.cool.store.service.MenuService;
import com.cool.store.vo.menu.MenuTreeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author zhangchenbiao
@@ -22,11 +28,35 @@ public class MenuServiceImpl implements MenuService {
@Resource
private SysMenuDAO sysMenuDAO;
@Resource
private SysRoleMenuDAO sysRoleMenuDAO;
@Override
public List<MenuTreeVO> getUserMenus(String userId, String roleId) {
List<SysMenuDO> menuList= sysMenuDAO.selectMenuAll(null, PlatFormTypeEnum.PC.getCode());
List<SysRoleMenuDO> roleMenuList = sysRoleMenuDAO.getRoleMenuByRoleId(roleId, PlatFormTypeEnum.PC.getCode());
List<Long> anthMenuIdList = menuList.stream().filter(o -> MenuTypeEnum.AUTH.getCode().equals(o.getMenuType())).map(SysMenuDO::getId).collect(Collectors.toList());
List<Long> roleMenuIdList = roleMenuList.stream().map(SysRoleMenuDO::getMenuId).filter(anthMenuIdList::contains).collect(Collectors.toList());
Set<Long> authMenuSet = new HashSet<>();
//1倒推菜单列表 2.转换成树`
if (CollectionUtils.isEmpty(roleMenuIdList)) {
return new ArrayList<>();
}
authMenuSet.addAll(roleMenuIdList);
Map<Long, Long> idMap = menuList.stream().filter(a -> a.getId() != null && a.getParentId() != null).collect(Collectors.toMap(SysMenuDO::getId, SysMenuDO::getParentId));
for (Long menuId : roleMenuIdList) {
getParentNode(menuId, idMap, authMenuSet);
}
menuList = menuList.stream().filter(data -> authMenuSet.contains(data.getId())).collect(Collectors.toList());
//根据角色获取菜单
return MenuTreeVO.dealMenuTree(CommonConstants.ZERO_LONG, menuList);
}
private void getParentNode(Long menuId, Map<Long, Long> idMap, Set<Long> authMenuList) {
Long parentId = idMap.get(menuId);
authMenuList.add(parentId);
if (parentId!=null&&parentId != 0) {
getParentNode(parentId, idMap, authMenuList);
}
}
}