先配置好第三方平台的消息相关配置
注意消息报文中的appid都是通知给第三方平台的appid
需要在接收消息的接口中拼接/$APPID$/ 便可获取到是这条消息给哪个小程序的
下载微信官方消息加解密源码 下载
打开Java项目
将圈出来的拉到你的项目中
@RequestMapping("/appNotify")
public class AppNotifyController {
/**
* 这里的路径需要与配置的路径相同
*/
@RequestMapping("/{appId}")
public String eventMsg(@PathVariable String appId, HttpServletRequest request) throws AesException, IOException {
// 这里需要通过request获取到原文再进行解密
StringBuffer sb = new StringBuffer();
try (ServletInputStream inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String requestBody = sb.toString();
// 初始化微信加解密类
WXBizMsgCrypt WXBizMsgCrypt = new WXBizMsgCrypt(WxConfig.msgToken, WxConfig.msgKey, WxConfig.templateAppId);
// 提取出xml数据包中的加密消息
Object[] encrypt = XMLParse.extract(requestBody);
// 对密文进行解密,拿到解密后的消息字符串
String decrypt = WXBizMsgCrypt.decrypt(encrypt[1].toString());
}
}
最后通过event来区分消息事件类型,某些消息是通过MsgType区分的,注意查看文档中消息说明