<!--微信模版消息推送三方sdk-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
1.配置文件
## 微信公众号
wechat:
appId: wxb49343432a366034c68543495376
secret: 3243243254354
IN:
templateId: UBSycw0USbnMni32NOH042x43ZCPUHW3UcqCsYJNYo
url: http://123.net/
OUT:
templateId: UBSycw0USbnMn123iNOH5350xZCPUHW3UcqCsYJINYo
url: http://123.net/
2.推送接口
@Async
@Override
public void push(WechatDto wechatDto) {
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(env.getProperty("wechat.appId"));
wxStorage.setSecret(env.getProperty("wechat.secret"));
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
if (null == wechatDto) {
return;
}
List<String> openIdList = getOpenIdList(wechatDto.getCustomerId());
for (String openId : openIdList) {
String templateId = env.getProperty("wechat." + wechatDto.getWay() + ".templateId");
String url = env.getProperty("wechat." + wechatDto.getWay() + ".url");
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)//要推送的用户openid
.templateId(templateId)
.url(url)
.build();
//3,如果是正式版发送模版消息,这里需要配置你的信息
templateMessage.addData(new WxMpTemplateData("first", wechatDto.getFirst(), "#000000"));
templateMessage.addData(new WxMpTemplateData("keyword1", wechatDto.getKeyword1(), "#000000"));
templateMessage.addData(new WxMpTemplateData("keyword2", wechatDto.getKeyword2(), "#22295F"));
templateMessage.addData(new WxMpTemplateData("keyword3", wechatDto.getKeyword3(), "#22295F"));
templateMessage.addData(new WxMpTemplateData("keyword4", wechatDto.getKeyword4(), "#22295F"));
templateMessage.addData(new WxMpTemplateData("remark", wechatDto.getRemark(), "#22295F"));
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (Exception e) {
System.out.println("推送失败:" + e.getMessage());
e.printStackTrace();
}
}
}