提交代码

master
胡锐 7 months ago committed by yuanye
parent 1b9526c6de
commit 0ad4d69b8f
  1. 81
      bweb/src/main/java/com/bweb/controller/SecMenuController.java
  2. 6
      service/src/main/java/com/hfkj/dao/SecRoleMenuRelMapperExt.java
  3. 6
      service/src/main/java/com/hfkj/service/SecRoleMenuRelService.java
  4. 8
      service/src/main/java/com/hfkj/service/impl/SecMenuServiceImpl.java
  5. 7
      service/src/main/java/com/hfkj/service/impl/SecRoleMenuRelServiceImpl.java

@ -170,44 +170,17 @@ public class SecMenuController {
} }
} }
@RequestMapping(value="/queryAssignMenuTree",method = RequestMethod.GET) @RequestMapping(value="/queryRoleMenuArray",method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ApiOperation(value = "查询分配菜单树") @ApiOperation(value = "查询分配菜单树")
public ResponseData queryAssignMenuTree(@RequestParam(value = "roleId" , required = true) Long roleId) { public ResponseData queryRoleMenuArray(@RequestParam(value = "roleId" , required = true) Long roleId) {
try { try {
List<MenuTreeModel> treeModelList = new ArrayList<>(); // 查询角色菜单权限
MenuTreeModel menuTree; List<SecRoleMenuRel> roleMenuList = secRoleMenuRelService.getRelListByRole(roleId);
// 获取系统菜单 return ResponseMsgUtil.success(roleMenuList.stream().map(SecRoleMenuRel::getMenuId).collect(Collectors.toList())
List<SecMenu> sysMenuList = secMenuService.getAllList(); .stream().map(Object::toString).collect(Collectors.toList()));
Map<Long, SecRoleMenuRel> roleMenuRelMap = secRoleMenuRelService.getRelListByRole(roleId)
.stream()
.collect(Collectors.toMap(SecRoleMenuRel::getMenuId, Function.identity()));
// 获取系统顶级菜单
List<SecMenu> topLevelMenuList = sysMenuList.stream().filter(o -> o.getMenuPSid() == null)
.sorted(Comparator.comparing(SecMenu::getMenuSort))
.collect(Collectors.toList());
for (SecMenu topLevelMenu : topLevelMenuList) {
menuTree = new MenuTreeModel();
menuTree.setId(topLevelMenu.getId());
menuTree.setMenuName(topLevelMenu.getMenuName());
menuTree.setMenuType(topLevelMenu.getMenuType());
menuTree.setMenuUrl(topLevelMenu.getMenuUrl());
menuTree.setMenuUrlImg(topLevelMenu.getMenuUrlImg());
menuTree.setMenuPSid(topLevelMenu.getMenuPSid());
menuTree.setMenuSort(topLevelMenu.getMenuSort());
menuTree.setMenuDesc(topLevelMenu.getMenuDesc());
// 递归子菜单
menuTree.setChildMenuList(recursionMenu(sysMenuList, roleMenuRelMap, topLevelMenu.getId()));
// 获取角色菜单
menuTree.setAuthority(roleMenuRelMap.get(topLevelMenu.getId()) != null ? true : false);
treeModelList.add(menuTree);
}
return ResponseMsgUtil.success(treeModelList);
} catch (Exception e) { } catch (Exception e) {
log.error("error!",e); log.error("error!",e);
@ -229,10 +202,10 @@ public class SecMenuController {
} }
} }
@RequestMapping(value="/queryRoleMenuTreeCustomized",method = RequestMethod.POST) @RequestMapping(value="/queryMenuTree",method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "查询角色菜单树") @ApiOperation(value = "查询角色菜单树")
public ResponseData queryRoleMenuTreeCustomized(@RequestParam(value = "roleId" , required = false) Long roleId) { public ResponseData queryMenuTree() {
try { try {
List<Map<String,Object>> mapList = new ArrayList<>(); List<Map<String,Object>> mapList = new ArrayList<>();
Map<String,Object> map; Map<String,Object> map;
@ -252,7 +225,7 @@ public class SecMenuController {
map.put("key", ""+topLevelMenu.getId()); map.put("key", ""+topLevelMenu.getId());
map.put("title", topLevelMenu.getMenuName()); map.put("title", topLevelMenu.getMenuName());
// 获取下级菜单 // 获取下级菜单
map.put("children", recursionMenu2(menuList, topLevelMenu.getId())); map.put("children", recursionMenu(menuList, topLevelMenu.getId()));
mapList.add(map); mapList.add(map);
} }
} }
@ -271,7 +244,7 @@ public class SecMenuController {
* @param parentMenuId 父级菜单id * @param parentMenuId 父级菜单id
* @return * @return
*/ */
public List<Map<String,Object>> recursionMenu2(List<SecMenu> dataSource, Long parentMenuId) { public List<Map<String,Object>> recursionMenu(List<SecMenu> dataSource, Long parentMenuId) {
List<Map<String,Object>> mapList = new ArrayList<>(); List<Map<String,Object>> mapList = new ArrayList<>();
Map<String,Object> map; Map<String,Object> map;
@ -285,7 +258,7 @@ public class SecMenuController {
map.put("key", ""+menu.getId()); map.put("key", ""+menu.getId());
map.put("title", menu.getMenuName()); map.put("title", menu.getMenuName());
// 获取下级菜单 // 获取下级菜单
List<Map<String, Object>> recursioned = recursionMenu2(dataSource, menu.getId()); List<Map<String, Object>> recursioned = recursionMenu(dataSource, menu.getId());
if (recursioned.isEmpty()) { if (recursioned.isEmpty()) {
map.put("isLeaf", true); map.put("isLeaf", true);
} else { } else {
@ -297,36 +270,4 @@ public class SecMenuController {
return mapList; return mapList;
} }
/**
* 递归菜单
* @param sysMenu 系统菜单
* @param roleMenuRel 角色菜单
* @param parentMenuId 父类菜单id
* @return
*/
public List<MenuTreeModel> recursionMenu(List<SecMenu> sysMenu, Map<Long, SecRoleMenuRel> roleMenuRel, Long parentMenuId) {
List<MenuTreeModel> treeModelList = new ArrayList<>();
MenuTreeModel menuTree;
List<SecMenu> collect = sysMenu.stream()
.filter(o -> o.getMenuPSid() != null && o.getMenuPSid().equals(parentMenuId))
.sorted(Comparator.comparing(SecMenu::getMenuSort))
.collect(Collectors.toList());
for (SecMenu menu : collect) {
menuTree = new MenuTreeModel();
menuTree.setId(menu.getId());
menuTree.setMenuName(menu.getMenuName());
menuTree.setMenuType(menu.getMenuType());
menuTree.setMenuUrl(menu.getMenuUrl());
menuTree.setMenuUrlImg(menu.getMenuUrlImg());
menuTree.setMenuPSid(menu.getMenuPSid());
menuTree.setMenuSort(menu.getMenuSort());
menuTree.setMenuDesc(menu.getMenuDesc());
// 获取角色菜单
menuTree.setAuthority(roleMenuRel.get(menuTree.getId()) != null ? true : false);
menuTree.setChildMenuList(recursionMenu(sysMenu, roleMenuRel, menu.getId()));
treeModelList.add(menuTree);
}
return treeModelList;
}
} }

@ -13,9 +13,9 @@ public interface SecRoleMenuRelMapperExt {
@Insert({"<script>" + @Insert({"<script>" +
" insert into sec_role_menu_rel (role_id, menu_id) " + " insert into sec_role_menu_rel (role_id, menu_id) " +
" VALUES " + " values " +
" <foreach collection='list' item='item' index='index' separator=','>" + " <foreach collection='list' item='item' index='index' separator=','>",
" (#{item.roleId, #{menuId})" + " ( #{item.roleId}, #{item.menuId} )" +
" </foreach>" + " </foreach>" +
" </script>"}) " </script>"})
void batchAdd(@Param("list") List<SecRoleMenuRel> list); void batchAdd(@Param("list") List<SecRoleMenuRel> list);

@ -23,6 +23,12 @@ public interface SecRoleMenuRelService {
*/ */
void delete(Long relId); void delete(Long relId);
/**
* 删除
* @param roleId 角色id
*/
void deleteByRole(Long roleId);
/** /**
* 查询角色菜单关系 * 查询角色菜单关系
* @param roleId 角色id * @param roleId 角色id

@ -166,8 +166,8 @@ public class SecMenuServiceImpl implements SecMenuService {
if (secRole == null) { if (secRole == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的角色"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的角色");
} }
// 查询角色菜单 // 删除角色权限旧数据
List<SecRoleMenuRel> roleMenuList = secRoleMenuRelService.getRelListByRole(roleId); secRoleMenuRelService.deleteByRole(roleId);
SecRoleMenuRel rel; SecRoleMenuRel rel;
List<SecRoleMenuRel> relList = new ArrayList<>(); List<SecRoleMenuRel> relList = new ArrayList<>();
@ -177,11 +177,9 @@ public class SecMenuServiceImpl implements SecMenuService {
rel.setMenuId(menuId); rel.setMenuId(menuId);
relList.add(rel); relList.add(rel);
} }
// 数据集合并
relList.addAll(roleMenuList);
// 根据菜单id去重 // 根据菜单id去重
List<SecRoleMenuRel> collectList = relList.stream().filter(StreamUtil.distinctByKey(b -> b.getMenuId())).collect(Collectors.toList()); List<SecRoleMenuRel> collectList = relList.stream().filter(StreamUtil.distinctByKey(b -> b.getMenuId())).collect(Collectors.toList());
// 绑定角色与菜单关系 // 角色新菜单权限
secRoleMenuRelService.batchAdd(collectList); secRoleMenuRelService.batchAdd(collectList);
// 更新缓存 // 更新缓存
redisUtil.hdel(CACHE_ROLE_MENU, ""+roleId); redisUtil.hdel(CACHE_ROLE_MENU, ""+roleId);

@ -30,6 +30,13 @@ public class SecRoleMenuRelServiceImpl implements SecRoleMenuRelService {
secRoleMenuRelMapper.deleteByPrimaryKey(relId); secRoleMenuRelMapper.deleteByPrimaryKey(relId);
} }
@Override
public void deleteByRole(Long roleId) {
SecRoleMenuRelExample example = new SecRoleMenuRelExample();
example.createCriteria().andRoleIdEqualTo(roleId);
secRoleMenuRelMapper.deleteByExample(example);
}
@Override @Override
public List<SecRoleMenuRel> getRelListByRole(Long roleId) { public List<SecRoleMenuRel> getRelListByRole(Long roleId) {
SecRoleMenuRelExample example = new SecRoleMenuRelExample(); SecRoleMenuRelExample example = new SecRoleMenuRelExample();

Loading…
Cancel
Save