IM云信第三方回调服务器端拦截DEMO

        云信第三方服务回调,就是客户端发消息时,由客户端进行拦截,根据业务验证是否可以发送成功等等。

具体实现如下
控制器代码
@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;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的丘八

如果有用,请不要吝啬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值