解决办法:
检查一下微信回调URL是否转发了客服消息。
/**
* 处理具体的回调信息
*/
@PostMapping("/callback")
public void callback(@RequestParam(name = "signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr,
@RequestParam(name = "openid", required = false) String openid,
@RequestBody String msg,
HttpServletResponse response) {
log.info("处理具体的回调信息");
log.info("======微信回调msg======:" +msg);
try {
weChatService.handleWxCallBackMsg(msg, response);
} catch (Exception e) {
log.info("handleWxCallBackMsg", e);
}
}
public void handleWxCallBackMsg(String msg, HttpServletResponse response) {
try {
Document document = XmlUtil.readXML(msg);
String msgType = String.valueOf(XmlUtil.getByXPath("//MsgType", document, XPathConstants.STRING));
if ("text".equals(msgType)) {
handleWxTextCallBack(document, response);
}
} catch (Exception e) {
log.info("handleWxCallBackMsg_error:", e);
}
}
private void handleWxTextCallBack(Document document, HttpServletResponse response) throws Exception {
log.info("======进入微信消息处理回调======");
String fromUserName = String.valueOf(XmlUtil.getByXPath("//FromUserName", document, XPathConstants.STRING));
String toUserName = String.valueOf(XmlUtil.getByXPath("//ToUserName", document, XPathConstants.STRING));
String createTime = String.valueOf(XmlUtil.getByXPath("//CreateTime", document, XPathConstants.STRING));
String xml = String.format("""
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>
""", fromUserName, toUserName, createTime);
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getOutputStream().print(xml);
}