com.zopen.wechat.mp.service.WechatMpTemplateMessageService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.mp.service;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.mp.dto.message.template.TemplateMessage;
import com.zopen.wechat.mp.dto.message.template.TemplateMessageData;
import com.zopen.wechat.mp.dto.message.template.TemplateMessageResp;
import com.zopen.wechat.mp.task.WechatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Component("atoWechatMpTemplateMessageService")
public class WechatMpTemplateMessageService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private WechatMpProperties wechatMpProperties;
// 发送模板消息(使用 application 中配置的 appId)
public Long postTemplateMessage(String openId, String templateId, String url, Map dataMap) {
String appId = wechatMpProperties.getAppId();
return postTemplateMessage(openId, templateId, url, dataMap, appId);
}
// 发送模板消息(使用 application 中配置的 appId)
public Long postTemplateMessage2(String openId, String templateId, String url, Map dataMap) {
String appId = wechatMpProperties.getAppId();
return postTemplateMessage2(openId, templateId, url, dataMap, appId);
}
// 发送模板消息
public Long postTemplateMessage(String openId, String templateId, String url, Map dataMap, String appId) {
String accessToken = WechatInfo.getAccessToken(appId);
return httpPostTemplateMessage(accessToken, openId, templateId, url, dataMap);
}
// 发送模板消息
public Long postTemplateMessage2(String openId, String templateId, String url, Map dataMap, String appId) {
String accessToken = WechatInfo.getAccessToken(appId);
return httpPostTemplateMessage2(accessToken, openId, templateId, url, dataMap);
}
private Long httpPostTemplateMessage2(String accessToken, String openId, String templateId, String url, Map dataMap) {
WechatAssert.notEmpty(dataMap, "data 不能为空");
Map dataMapObj = new HashMap<>(16);
for (Map.Entry entry : dataMap.entrySet()) {
dataMapObj.put(entry.getKey(), new TemplateMessageData(entry.getValue()));
}
return httpPostTemplateMessage(accessToken, openId, templateId, url, dataMapObj);
}
private Long httpPostTemplateMessage(String accessToken, String openId, String templateId, String url, Map dataMap) {
WechatAssert.notNullAndEmpty(accessToken, "accessToken 不能为空");
WechatAssert.notNullAndEmpty(openId, "openId 不能为空");
WechatAssert.notNullAndEmpty(templateId, "模板ID不能为空");
WechatAssert.notEmpty(dataMap, "data 不能为空");
TemplateMessage templateMessage = new TemplateMessage(openId, templateId, url, dataMap);
String postUrl = String.format(WechatHttpUrl.POST_TEMPLATE_MESSAGE, accessToken);
TemplateMessageResp resp = restTemplate.postForObject(postUrl, templateMessage, TemplateMessageResp.class);
WechatAssert.notNull(resp, "发送模板消息失败");
resp.valid("发送模板消息失败");
return resp.getMsgid();
}
}