|
|
@ -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()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|