@RequestMapping("userTreeMenu")
@ResponseBody
public JSONObject userTreeMenu() {
// 获取当前登录人id
Subject loginUser = SecurityUtils.getSubject();
String json = this.jedisService.get(EnumRedisDB.LOGIN_USER.getDbindex(),
"loginUser_" + loginUser.getSession().getId());
JSONObject jsons = new JSONObject();
List<Object> list = new ArrayList<Object>();
if (json != null && json.length() > 0) {
UserInfo user = JSONObject.parseObject(json, UserInfo.class);
HashMap<String, Object> params = new HashMap<>();
params.put("userId", user.getUserId());
params.put("isTree", 1);
jsons.put("code", 0);
jsons.put("msg", "");
//查询该用户所有的菜单ID
List<UserRight> rightIds = userAdminService.getRightIds(user.getUserId(),1);
String ids = "";
if(rightIds != null) {
for(int i = 0;i<rightIds.size();i++) {
ids+= rightIds.get(i).getId()+",";
}
}
ids = ids.substring(0, ids.length()-1);
params.put("ids", ids);
// 查询最外层的菜单
List<UserRight> all = userAdminService.userMenu(params);
//params.put("parentId", '0');
//查询该用户所有的父菜单ID
List<UserRight> rightFatherIds = userAdminService.getRightIds(user.getUserId(),0);
if(rightFatherIds != null) {
ids = "";
for(int i = 0;i<rightFatherIds.size();i++) {
ids+= rightFatherIds.get(i).getId()+",";
}
}
ids = ids.substring(0, ids.length()-1);
params.put("ids",ids);
List<UserRight> yiList = userAdminService.userMenu(params);
this.menuCommon = all;
for (UserRight menu : yiList) {
Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
mapArr.put("name", menu.getRightCode());
mapArr.put("icon", menu.getIcon());
mapArr.put("title", menu.getRightName());
mapArr.put("jump", menu.getTreeUrl());
if (menu.getChildCount() > 0) {
mapArr.put("list", menuChild(menu.getId()));
}
list.add(mapArr);
}
// data
jsons.put("data", list);
} else {
jsons.put("code", 0);
jsons.put("msg", "");
jsons.put("data", list);
}
System.out.println(jsons);
return jsons;
}
public List<?> menuChild(Integer id) {
List<Object> lists = new ArrayList<Object>();
for (UserRight a : menuCommon) {
Map<String, Object> childArray = new LinkedHashMap<String, Object>();
if (a.getParentid().equals(id)) {
childArray.put("name", a.getRightCode());
childArray.put("icon", a.getIcon());
childArray.put("title", a.getRightName());
childArray.put("jump", a.getTreeUrl());
if (a.getChildCount() > 0) {
childArray.put("list", menuChild(a.getId()));
}
lists.add(childArray);
}
}
return lists;
}
优化完之后的
@GetMapping("menuTree")
public ResultModel menuTree(){
// 根据当前登录用户查询所拥有的菜单
Subject loginUser = SecurityUtils.getSubject();
String userInfoJson = this.jedisService.get(EnumRedisDB.LOGIN_USER.getDbindex(),
"loginUser_" + loginUser.getSession().getId());
List<Map<String, Object>> tree = new ArrayList<Map<String, Object>>();
if(!StringUtils.isEmpty(userInfoJson)){
UserInfo user = JSONObject.parseObject(userInfoJson, UserInfo.class);
// 根据用户查询菜单
List<UserRight> haveTree = userRightService.queryTree(user.getUserId());
// 一级菜单
List<UserRight> oneTree = haveTree.stream().filter(h -> h.getPId() == 0).collect(Collectors.toList());
tree.addAll(addTree(oneTree,haveTree));
}
return new ResultModel(tree);
}
public List<Map<String,Object>> addTree(List<UserRight> oneTree,List<UserRight> haveTree){
List<Map<String, Object>> tree = new ArrayList<Map<String, Object>>();
oneTree.stream().forEach(o -> {
Map<String,Object> linkMap = new LinkedHashMap<String,Object>();
linkMap.put("name",o.getMenuCode());
linkMap.put("icon",o.getIcon());
linkMap.put("title",o.getMenuName());
linkMap.put("jump",o.getMenuUrl());
// 有子菜单
if(o.getChildCount() > 0){
linkMap.put("list",menuChild(haveTree,o.getMenuId()));
}
tree.add(linkMap);
});
return tree;
}
public List<Map<String,Object>> menuChild(List<UserRight> haveTree,Long menuId) {
List<Map<String,Object>> sonTree = new ArrayList<Map<String,Object>>();
sonTree.addAll(addTree(haveTree.stream().filter(s -> s.getPId() == menuId).collect(Collectors.toList()),haveTree));
return sonTree;
}
这是一个登录用户获取自己所拥有菜单权限的一个方法
使用了jdk8新特性
- list.stream().filter()进行筛选列表
- list.stream().forEach进行遍历列表
写得不好的地方欢迎指出。
记录于 2020年3月26日 下午4:19