大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂
现在是:2022年7月7日13:46:07
前言
今天有个这样的需求,PC端需要查看一下哪些天有数据,但是哪些有有没有数据我这边还看不出来,得请求别的系统的接口去看。且团组和会员请求的接口和请求方式均不一样。
前端说为了方便,团组和个人都走一个接口吧,让我这边封装一下……
实现
如何区分前台请求的是会员的还是团组的
让前台这样传,如果不传groupId,传memberId,则我就知道了,请求的是查询会员的信息。反之则请求的是团组的接口。
代码如下:
if(Strings.isNotBlank(groupId)){//是团组//查询这个团组下面的人LambdaQueryWrapper<GroupUser> groupUserLambdaQueryWrapper = new LambdaQueryWrapper<>();groupUserLambdaQueryWrapper.eq(GroupUser::getGroupId, groupId);List<GroupUser> groupUserList = groupUserService.list(groupUserLambdaQueryWrapper);List<Long> userList = new ArrayList<>();groupUserList.forEach(group->{userList.add(group.getUserId());});//集合去重List<Long> memberIdList =userList.stream().distinct().collect(Collectors.toList());url = ServicePathConstant.HBV_GATAWAY+"/doctor/api/doctor/getMemberDataDays";body = HttpUtil.createPost(url).contentType("application/json").body(JSON.toJSONString(memberIdList)).execute().body();}else if(Strings.isNotBlank(memberId)){//是会员url = ServicePathConstant.HBV_GATAWAY+"/doctor/api/doctor/getMemberDataDays/"+memberId;body = HttpUtil.createGet(url).execute().body();}
里面涉及到了些别的代码,集合去重:
//集合去重List<Long> memberIdList =userList.stream().distinct().collect(Collectors.toList());
还别说,这个地方用的还挺多的,不过每次用到了之后都是翻之前的代码。。。。。
完整代码如下:
/*** 获取有日期的报告吗,返回一个集合* @param groupId* @return*/@GetMapping("/getReportHaveDateList")public Object getReportHaveDateList(String groupId,String memberId) {cn.hutool.json.JSONObject jsonObjectResult = new cn.hutool.json.JSONObject();//请求接口地址String url = "";String body = "";if(Strings.isNotBlank(groupId)){//是团组//查询这个团组下面的人LambdaQueryWrapper<GroupUser> groupUserLambdaQueryWrapper = new LambdaQueryWrapper<>();groupUserLambdaQueryWrapper.eq(GroupUser::getGroupId, groupId);List<GroupUser> groupUserList = groupUserService.list(groupUserLambdaQueryWrapper);List<Long> userList = new ArrayList<>();groupUserList.forEach(group->{userList.add(group.getUserId());});//集合去重List<Long> memberIdList =userList.stream().distinct().collect(Collectors.toList());url = ServicePathConstant.HBV_GATAWAY+"/doctor/api/doctor/getMemberDataDays";body = HttpUtil.createPost(url).contentType("application/json").body(JSON.toJSONString(memberIdList)).execute().body();}else if(Strings.isNotBlank(memberId)){//是会员url = ServicePathConstant.HBV_GATAWAY+"/doctor/api/doctor/getMemberDataDays/"+memberId;body = HttpUtil.createGet(url).execute().body();}if (StringUtils.isBlank(body)) {jsonObjectResult.putOpt("code", 500);jsonObjectResult.putOpt("data", null);jsonObjectResult.putOpt("msg", "操作失败");return jsonObjectResult;}cn.hutool.json.JSONObject obj = JSONUtil.parseObj(body);if (obj == null) {jsonObjectResult.putOpt("code", 500);jsonObjectResult.putOpt("data", null);jsonObjectResult.putOpt("msg", "操作失败");return jsonObjectResult;}String code = obj.get("code").toString();String msg = obj.get("msg").toString();System.out.println("调用doctor系统返回的信息:"+msg);if ("200".equals(code)) {jsonObjectResult.putOpt("code", 200);jsonObjectResult.putOpt("data", obj.get("data"));jsonObjectResult.putOpt("msg", "操作成功");//标记有没有记录jsonObjectResult.putOpt("count", obj.getJSONArray("data").size());return jsonObjectResult;}else{jsonObjectResult.putOpt("code", 200);jsonObjectResult.putOpt("data", new ArrayList<String>());jsonObjectResult.putOpt("msg", "操作成功");//标记有没有记录jsonObjectResult.putOpt("count", 0);return jsonObjectResult;}}
顺便在记录一下糊涂工具类请求接口的几种方式吧:
post
方法form
传参:
//接口地址String url = "";//请求参数Map<String, Object> paramMap = new HashMap<>();map.put("groupId", "1506254142554785");//请求头HashMap<String, String> headers = new HashMap<>();headers.put("Authorization", token);String result= HttpUtil.createPost(url).addHeaders(headers).form(map).execute().body();
post
方法json
传参:
//接口地址String url = "";//请求参数Map<String, Object> paramMap = new HashMap<>();map.put("groupId", "1506254142554785");//请求头HashMap<String, String> headers = new HashMap<>();headers.put("Authorization", token);String body = HttpUtil.createPost(url).contentType("application/json").body(JSON.toJSONString(projectVo)).execute().body();
projectVo
就是一个json
格式的数据。
get
方法form
传参:
//接口地址String url = "";//请求参数Map<String, Object> paramMap = new HashMap<>();map.put("groupId", "1506254142554785");//请求头HashMap<String, String> headers = new HashMap<>();headers.put("Authorization", token);String body = HttpUtil.createGet(url).header("Authorization", token).form(paramMap).execute().body();
这就是今天分享的内容,谢谢大家的捧场!!!