Browse Source

发送消息

master
zhizhi wu 3 years ago
parent
commit
caf695b92b
  1. 47
      wechatutil/src/main/java/com/ccsens/wechatutil/wxofficial/OfficialAccountMessageUtil.java

47
wechatutil/src/main/java/com/ccsens/wechatutil/wxofficial/OfficialAccountMessageUtil.java

@ -85,22 +85,39 @@ public class OfficialAccountMessageUtil {
if (CollectionUtil.isNotEmpty(openIdList)) {
openIdList.forEach(openid -> {
//拼接访问路径
String url = String.format(WxConstant.MESSAGE_TEMPLATE_SEND, WxCommonUtil.getAccessToken(appId, secret));
//调用微信接口
String response = RestTemplateUtil.postBody(url, templateMessage);
log.info("url: {}\nresponse: {}", url, response);
WxBaseEntity wxBaseEntity;
try {
if (StrUtil.isEmpty(response) || null == (wxBaseEntity = JacksonUtil.jsonToBean(response, WxBaseEntity.class))) {
throw new BaseException(WxCodeError.WX_HTTP_ERROR);
}
} catch (IOException e) {
throw new BaseException(e.getMessage());
}
if (null != wxBaseEntity.getErrcode()) {
throw new BaseException(wxBaseEntity.getErrcode(), wxBaseEntity.getErrmsg());
}
sendMsg(appId, secret, templateMessage);
});
}
}
/**
* 公众号发送订阅消息传入openId
* @param templateMessages 消息
*/
public static void officialMessage(List<WxTemplateMessage> templateMessages, String appId, String secret) {
//判断是否有openId
if (CollectionUtil.isEmpty(templateMessages)) {
return;
}
templateMessages.forEach(templateMessage -> sendMsg(appId, secret, templateMessage));
}
private static void sendMsg(String appId, String secret, WxTemplateMessage templateMessage) {
//拼接访问路径
String url = String.format(WxConstant.MESSAGE_TEMPLATE_SEND, WxCommonUtil.getAccessToken(appId, secret));
//调用微信接口
String response = RestTemplateUtil.postBody(url, templateMessage);
log.info("url: {}\nresponse: {}", url, response);
WxBaseEntity wxBaseEntity;
try {
if (StrUtil.isEmpty(response) || null == (wxBaseEntity = JacksonUtil.jsonToBean(response, WxBaseEntity.class))) {
throw new BaseException(WxCodeError.WX_HTTP_ERROR);
}
} catch (IOException e) {
throw new BaseException(e.getMessage());
}
if (null != wxBaseEntity.getErrcode()) {
throw new BaseException(wxBaseEntity.getErrcode(), wxBaseEntity.getErrmsg());
}
}
}

Loading…
Cancel
Save