收藏
回答

第三方授权小程序 ,转发客服消息,无法在客服小助手上面收到消息?

下面直接贴代码,就这个方法,我也返回xml格式了啊,为什么服务器日志也输出了对应的log,但是客服小助手小程序就是收不到?

还有已经授权了客服信息了,这个如果不授权的话,没办法通过接口给客户添加客服,不授权的话只能让客户登录MP后台自己添加的,太麻烦了。

/**
 * 消息与事件接收URL:用于接收公众号或小程序消息和事件推送。
 * 简单理解为,同时推送给服务商和授权公众号或者小程序的的消息就会推送到该URL
 */
@RequestMapping(value = "{appid}/receiveMsg", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/xml; charset=UTF-8")
public void receiveMsg(@RequestBody String xml,
                       @RequestParam(value = "signature", required = false) String signature,
                       @RequestParam(value = "timestamp", required = false) String timeStamp,
                       @RequestParam(value = "nonce", required = false) String nonce,
                       @RequestParam(value = "encrypt_type", required = false) String encryptType,
                       @RequestParam(value = "msg_signature", required = false) String msgSignature,
                       @PathVariable("appid") String appid,
                       HttpServletResponse httpServletResponse) throws Exception {
    log.info("------- 消息与事件接收URL 开始 --------- ");
    log.info("-------appid: --------- " + appid + " ----------------");
    log.info("-------xml: --------- " + xml + " ----------------");
    log.info("-------signature: --------- " + signature + " ----------------");
    log.info("-------timeStamp: --------- " + timeStamp + " ----------------");
    log.info("-------nonce: --------- " + nonce + " ----------------");
    log.info("-------encryptType: --------- " + encryptType + " ----------------");
    log.info("-------msgSignature: --------- " + msgSignature + " ----------------");
    log.info("------- 消息与事件接收URL 结束 --------- ");
    WXBizMsgCrypt pc = null;
    try {
        pc = new WXBizMsgCrypt(WxConst.TOKEN, WxConst.ENCODING_AES_KEY, WxConst.THIRD_APP_ID);
        String requestMessage = pc.decryptMsg(msgSignature, timeStamp, nonce, xml);
        cn.hutool.json.JSONObject object = XML.toJSONObject(requestMessage);
        cn.hutool.json.JSONObject objectXml = (cn.hutool.json.JSONObject) object.get("xml");
        log.info(objectXml.get("MsgType") + "事件");
        if ("event".equals(objectXml.get("MsgType"))) {
            httpServletResponse.getWriter().write("success");
            return;
        } else if ("text".equals(objectXml.get("MsgType")) || "image".equals(objectXml.get("MsgType"))){
            // 拼接回包
            String responseMessage = "<xml>" +
                    "<ToUserName><![CDATA[" + objectXml.get("FromUserName") + "]]></ToUserName>" +
                    "<FromUserName><![CDATA[" + objectXml.get("ToUserName") + "]]></FromUserName>" +
                    "<CreateTime>" + timeStamp + "</CreateTime>" +
                    "<MsgType><![CDATA[transfer_customer_service]]></MsgType>" +
                    "</xml>";
            log.info(responseMessage);
            // 返回消息
            httpServletResponse.getWriter().write(responseMessage);
            return;
        } else {
            log.info("test");
        }
    } catch (AesException e) {
        e.printStackTrace();
    }
}


回答关注问题邀请回答
收藏

1 个回答

  • captain😼
    captain😼
    2022-07-28

    已解决,返回的xml字符串,一定要加密!!!!!!!!!用官方下载的加密代码

    2022-07-28
    有用
    回复
登录 后发表内容