Ztree新增角色和编辑角色回显问题的解决(2)

/** * 编辑角色,回显角色权限 * @param roleId * @return */ @RequestMapping(value = "queryFunByRoleId", method = RequestMethod.POST) public ResponseEntity<List<Map<String, Object>>> queryFunByRoleId(String roleId) { try { if(StringUtils.isBlank(roleId)){ // 返回400 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } return ResponseEntity.ok(sysService.queryFunByRoleId(roleId)); } catch (Exception e) { e.printStackTrace(); } // 出错 500 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); }

service:

由于List中的contains方法校验老是失败,也没纠结什么原因,自己写的根据id校验

/** * zTree v3回显 * 初始化化权限树 * 拼接treeNode属性 */ @Transactional(readOnly=true) public List<Map<String, Object>> queryFunByRoleId(String roleId) { //查询所有权限 List<AuthFunction> functions = queryAllAuthFunction(); //查询指定角色的权限 List<AuthFunction> functionsByRoleId = findFunctionByRoleId(roleId); //包装zTree List<Map<String, Object>> list =new ArrayList<Map<String, Object>>(); Map<String, Object>map=null; for(int i=0;i<functions.size();i++){ map=new HashMap<>(); //Role role=functions.get(i); AuthFunction fun = functions.get(i); map.put("id", fun.getId()); map.put("pId", fun.getpId()); map.put("name", fun.getName()); map.put("isParent", fun.getIsParent()); //判断指定用户的角色是否在所有角色中包含,有则设置checked=true. if(functionsByRoleId != null && functionsByRoleId.size()>0 && ListIsContainsObj(functionsByRoleId,fun)){ map.put("checked",true); }else { map.put("checked",false); } list.add(map); } return list; } //校验全部权限中是否有某个权限,有返回true private boolean ListIsContainsObj(List<AuthFunction> functions, AuthFunction fun) { if(fun == null || functions == null || functions.size()<=0){ return false; } for (AuthFunction authFunction : functions) { if(fun.getId().equals(authFunction.getId())){ return true; } } return false; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzgsjs.html