接收事件推送https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
@PostMapping
public String handleMessage(@RequestBody String requestBody) {
try {
// 解析XML
Map<String, String> msgMap = parseXml(requestBody);
String msgType = msgMap.get("MsgType");
String event = msgMap.get("Event");
String openId = msgMap.get("FromUserName");
// 处理关注事件
if ("event".equals(msgType) && "subscribe".equals(event)) {
// 新用户关注处理
WxPayProperties properties = wxPayConfiguration.getWxPayProperties();
return String.format(
"<xml>" +
"<ToUserName><![CDATA[%s]]></ToUserName>" +
"<FromUserName><![CDATA[%s]]></FromUserName>" +
"<CreateTime>%s</CreateTime>" +
"<MsgType><![CDATA[text]]></MsgType>" +
"<Content><![CDATA[%s]]></Content>" +
"</xml>",
openId,properties.getAppId(),System.currentTimeMillis() / 1000, "感谢关注!请<a href='https://xxxxx.com/api/wechat.html'>点击授权</a>"
);
}
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
} 用户关注后能接受到信息
但是无法返回消息
到用户 是不是还需要设置什么地方
FromUserName要传公众号的原始ID