云信第三方服务回调,就是客户端发消息时,由客户端进行拦截,根据业务验证是否可以发送成功等等。
具体实现如下 控制器代码 @RequestMapping(value = "/interceptHandleMsg", method = {RequestMethod.POST, RequestMethod.GET}) public JSONObject interceptHandleMsg(HttpServletRequest request) { JSONObject result = new JSONObject(); result.put("errCode", 1); result.put("responseCode", 200); result.put("modifyResponse", "{}"); try { // 获取请求体 byte[] body = readBody(request); if (body == null) { return result; } String ContentType = request.getContentType(); String AppKey = request.getHeader("AppKey"); String CurTime = request.getHeader("CurTime"); String MD5 = request.getHeader("MD5"); String CheckSum = request.getHeader("CheckSum"); //log.info("云信回调 request headers: ContentType = {}, AppKey = {}, CurTime = {}, " + // "MD5 = {}, CheckSum = {}", ContentType, AppKey, CurTime, MD5, CheckSum); String requestBody = new String(body, "utf-8"); JSONObject jsonObject = JSON.parseObject(requestBody); log.info("云信回调request body = {}", jsonObject); return imWangyiyunxinService.interceptHandleMsg(AppKey, jsonObject); } catch (Exception e) { e.printStackTrace(); return result; } } private byte[] readBody(HttpServletRequest request) throws IOException { if (request.getContentLength() > 0) { byte[] body = new byte[request.getContentLength()]; IOUtils.readFully(request.getInputStream(), body); return body; } return null; }
具体返回值如下。也可以去参考云信api:https://doc.yunxin.163.com/docs/TM5MzM5Njk/jI3ODc2ODE?platformId=60353
service层代码如下:
@Override public JSONObject interceptHandleMsg(String AppKey, JSONObject paramJson) { JSONObject jsonObject = new JSONObject(); jsonObject.put("errCode", 1); jsonObject.put("responseCode", 200); jsonObject.put("modifyResponse", "{}"); if (!AppKey.equals(wangyiyunxinappkey)) { jsonObject.put("responseCode", 20001); return jsonObject; } //纯文本{"attach":"","body":"给你们","eventType":1,"fromAccount":"ywu20210809000067","fromClientIp":"114.242.33.27", // "fromClientPort":"54038","fromClientType":"IOS","fromDeviceId":"9E5C1A55-CE8A-43B3-BE6D-55965B135356", // "fromNick":"飞翔的丘八6119170614157","msgTimestamp":"1628921211580","msgType":"TEXT","msgidClient":"d519dbe7-19cd-4f67-9152-31c074d28866", // "to":"ywu20210731000005"} //图片{"fromNick":"飞翔的丘八","msgType":"PICTURE","fromClientIp":"114.242.33.27","fromAccount":"ywu20201117000326", // "fromClientType":"IOS","fromDeviceId":"E7488660-94A7-469A-B243-DAB4683A4750","eventType":1,"body":"", // "msgidClient":"5b2d2457-01ab-4f34-83e0-2ed081971e61","fromClientPort":"52959","msgTimestamp":"1630140255286", // "attach":"{\"md5\":\"05ddebf23caa4c0a1937cfa37fee2569\",\"h\":1596,\"ext\":\"png\",\"size\":186059,\"w\":1656, // \"name\":\"thumbnailDisplayName.png\", // \"url\":\"https:\\/\\/nim-nosdn.netease.im\\/MTY4MTUwMjQ=\\/bmltYV8yMjExNDgwODY5NF8xNjMwMTQwMjAxODk2Xzk4MDBhMTc4LTljNmItNDk1NC1iOGExLTVmODA1NGE1NTFkYg==\"}", // "to":"ywu20210703000137"} String msg = paramJson.getString("body"); if (StringUtils.isBlank(msg)) { JSONObject attach = paramJson.getJSONObject("attach"); msg = attach == null ? msg :attach.getString("url"); } String fromAccount = paramJson.getString("fromAccount"); String toAccount = paramJson.getString("to"); if (StringUtils.isBlank(fromAccount) || StringUtils.isBlank(toAccount)) { jsonObject.put("responseCode", 20001); //业务处理 return jsonObject; }
//业务处理
return jsonObject;
}