
WxJava微信公众号模板消息
这一期需要用到发送微信公众号发送消息,直接用了大佬封装好的
GitHub链接
因为本来刚开始是想手动请求自己拼接参数的,实在是想偷懒哈哈哈,所以还是用已经封装好的波~
添加完依赖 写一个配置类,注入Service
@Configuration
@AllArgsConstructor
class WxConfig {
private final WechatProperties wechatProperties;
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
// 公众号appId
configStorage.setAppId(wechatProperties.getAppId());
// 公众号appSecret
configStorage.setSecret(wechatProperties.getAppSecret());
return configStorage;
}
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
}
当时搞完这里,网上的博客里的微信的access_token,是在这里添加进去,
但是很巧合,我添加token 的时候,故意填了一个错误的token,发现消息一样可以发送??
我去翻源码,看他的token是怎么用的,发现他自己会有一个token管理系统,会先去申请一个access_token, 然后放到内存里保存?这里看他也有写的用redis管理啊,那些那些,但是当时运维那边还没搞好redis的支持,那个项目没有配置redis,所以一定是放在内存里了把。
自己会管理access_token的过期时间,过期了自己再去申请,免去了我们自己去管理access_token的步骤
然后WxMessageUtil
public void sendWxMessage(String openId, Map<String, String> params, String model) {
try {
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.build();
switch (model) {
case "model1":
templateMessage.setTemplateId(wechatProperties.getAuditMessageModel());
templateMessage.addData(new WxMpTemplateData("first", "xxx", "#000000"))
.addData(new WxMpTemplateData("keyword1", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword2", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword3", params.get("xxx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword4", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("remark", "xxxx", "#000000"));
break;
case "model2":
templateMessage.setTemplateId(wechatProperties.getProductPutOnModel());
templateMessage.addData(new WxMpTemplateData("first", "xxx", "#000000"))
.addData(new WxMpTemplateData("keyword1", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword2", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword3", params.get("xxx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword4", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("remark", "xxxx", "#000000"));
break;
case "model3":
templateMessage.setTemplateId(wechatProperties.getAmtAuditMessageModel());
templateMessage.addData(new WxMpTemplateData("first", "xxx", "#000000"))
.addData(new WxMpTemplateData("keyword1", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword2", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword3", params.get("xxx"), "#0000FF"))
.addData(new WxMpTemplateData("keyword4", params.get("xx"), "#0000FF"))
.addData(new WxMpTemplateData("remark", "xxxx", "#000000"));
break;
}
this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
xxxxx
}
}
区分模板发送
同时支持跳转链接和小程序
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.url("")
.miniProgram(new WxMpTemplateMessage.MiniProgram())
同时根据官方文档:
注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Henry's Lib
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果