forked from chanjarster/weixin-java-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
MP_群发消息
Binary Wang edited this page Dec 17, 2022
·
7 revisions
下面用用户列表群发(WxMpMassOpenIdsMessage
)做例子,如果要使用标签群发,则使用me.chanjar.weixin.mp.bean.WxMpMassTagMessage
即可。
更多用例,请参考单元测试类:me.chanjar.weixin.mp.api.impl.WxMpMassMessageServiceImplTest
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MASS_MSG_TEXT);
massMessage.setContent("消息内容");
massMessage.getToUsers().add(openid);
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
WxMediaUploadResult uploadMediaRes = wxMpService.getMaterialService().mediaUpload(WxConsts.MEDIA_VIDEO, "mp4", inputStream);
// 把视频变成可被群发的媒体
WxMpMassVideo video = new WxMpMassVideo();
video.setTitle("测试标题");
video.setDescription("测试描述");
video.setMediaId(uploadMediaRes.getMediaId());
WxMpMassUploadResult uploadResult = wxMpService.getMassMessageService().massVideoUpload(video);
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MASS_MSG_VIDEO);
massMessage.setMediaId(uploadResult.getMediaId());
massMessage.getToUsers().add(openid);
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
WxMediaUploadResult uploadMediaRes = wxMpService.getMaterialService().mediaUpload(WxConsts.MEDIA_IMAGE, "jpg", inputStream);
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MASS_MSG_IMAGE);
massMessage.setMediaId(uploadMediaRes.getMediaId());
massMessage.getToUsers().add(openid);
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
WxMediaUploadResult uploadMediaRes = wxMpService.getMaterialService().mediaUpload(WxConsts.MEDIA_VOICE, "mp3", inputStream);
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MASS_MSG_VOICE);
massMessage.setMediaId(uploadMediaRes.getMediaId());
massMessage.getToUsers().add(openid);
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
// 上传图文消息的封面图片
WxMediaUploadResult uploadMediaRes = wxMpService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, "jpg", inputStream);
// 上传图文消息的正文图片(返回的url拼在正文的<img>标签中)
WxMediaImgUploadResult imagedMediaRes = wxMpService.getMaterialService().mediaImgUpload(file);
String url=imagedMediaRes.getUrl();
WxMpMassNews news = new WxMpMassNews();
WxMpNewsArticle article1 = new WxMpNewsArticle();
article1.setTitle("标题1");
article1.setContent("内容1");
article1.setThumbMediaId(uploadMediaRes.getMediaId());
news.addArticle(article1);
WxMpNewsArticle article2 = new WxMpNewsArticle();
article2.setTitle("标题2");
article2.setContent("内容2");
article2.setThumbMediaId(uploadMediaRes.getMediaId());
article2.setShowCoverPic(true);
article2.setAuthor("作者2");
article2.setContentSourceUrl("www.baidu.com");
article2.setDigest("摘要2");
news.addArticle(article2);
WxMpMassUploadResult massUploadResult = wxMpService.getMassMessageService().massNewsUpload(news);
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MassMsgType.MPNEWS);
massMessage.setMediaId(uploadResult.getMediaId());
massMessage.getToUsers().add(openid);
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
String encType = request.getParameter("encrypt_type");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String msgSignature = request.getParameter("msg_signature");
String signature = request.getParameter("signature");
String openid = request.getParameter("openid");
log.info("接收微信请求:[openid=[{}], [openid=[{}],[signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[{}] ",
appid, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
// 请求里面没有对应信息。 // if (!this.wxMpService.switchover(appid)) { //appid // throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); // }
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
return ResponseEntity.ok("");