简述在线推广网站的方法,免费下载ps素材网站,作文网站哪个平台好,商河县建设局网站效果图见测试结果#xff0c;有问题评论 模板消息
开通模板消息功能
登录微信公众平台开通模板消息功能 (未开通时) 添加模板消息
审核通过后#xff0c;登录微信公众平台#xff0c;点击 广告与服务 -- 模板消息公众号如果未设置服务类目#xff0c;则需要先设置服… 效果图见测试结果有问题评论 模板消息
开通模板消息功能
登录微信公众平台开通模板消息功能 (未开通时) 添加模板消息
审核通过后登录微信公众平台点击 广告与服务 -- 模板消息公众号如果未设置服务类目则需要先设置服务类目。然后在模板库中挑选合适的模板消息即可如下图。 对接开发
模板消息官方文档建议看完少走弯路
引入依赖
参考https://github.com/Wechat-Group/weixin-java-tools
!-- 微信框架 --
dependencygroupIdcom.github.binarywang/groupIdartifactIdweixin-java-mp/artifactIdversion3.6.0/version
/dependency添加模板消息工具类
在业务类中注入工具类即可 commonSendTemplateMsg公共消息通知发送 模板相同的消息 sendTemplateMsg 发送不同的模板消息需要在业务类中构建消息内容。
/*** 微信公众号消息通知工具类*/
Slf4j
Component
public class WeChatTemplateMsgUtils{Autowiredprivate WxMpService wxMpService;//公共消息通知public boolean commonSendTemplateMsg(String openId) {//实例化模板对象WxMpTemplateMessage wxMpTemplateMessage new WxMpTemplateMessage();//设置模板IDwxMpTemplateMessage.setTemplateId(TEMPLATE_ID);//设置详情跳转链接wxMpTemplateMessage.setUrl(WX_TEMPLATE_DETAILS_URL);//设置发送给哪个用户wxMpTemplateMessage.setToUser(openId);//构建消息格式//消息格式自行修改ListWxMpTemplateData listData new ArrayList();//WxMpTemplateDatakey (time4) 对应 模板消息中的关键词 {{time4.DATA}}listData.addAll(Arrays.asList(new WxMpTemplateData(time4,DateUtils.parseDateToStr(yyyy-MM-dd hh:mm:ss,sessionInfo.getBiddingStartTime())),new WxMpTemplateData(time5,DateUtils.parseDateToStr(yyyy-MM-dd hh:mm:ss,sessionInfo.getBiddingEndTime()))));//放进模板对象。准备发送wxMpTemplateMessage.setData(listData);//接收发送模板消息结果,就是msgIdboolean flag false;try {//发送模板String json wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);if (StringUtils.isNotEmpty(json)){flag true;}} catch (WxErrorException e) {log.error(发送模板消息异常{}, e.getMessage());}return flag;}/*** 设置所属行业*/public boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException {Boolean flag wxMpService.getTemplateMsgService().setIndustry(wxMpIndustry);return flag;}/*** 获取设置的行业信息*/public WxMpTemplateIndustry getIndustry() throws WxErrorException {WxMpTemplateIndustry wxMpTemplateIndustry wxMpService.getTemplateMsgService().getIndustry();return wxMpTemplateIndustry;}/*** 发送模板消息*/public boolean sendTemplateMsg(WxMpTemplateMessage templateMessage) {//标识消息是否发送成功true 成功false 失败boolean flag false;try {// result 为消息idString result wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);if (StringUtils.isNotEmpty(result)){flag true;}} catch (WxErrorException e) {log.error(发送模板消息异常{}, e.getMessage());}return flag;}/*** 获得模板ID*/public String addTemplate(String shortTemplateId) throws WxErrorException {String result wxMpService.getTemplateMsgService().addTemplate(shortTemplateId);return result;}/*** 获得模板列表*/ListWxMpTemplate getAllPrivateTemplate() throws WxErrorException {ListWxMpTemplate templateList wxMpService.getTemplateMsgService().getAllPrivateTemplate();return templateList;}/*** 删除模板* templateId: 公众帐号下模板消息ID*/boolean delPrivateTemplate(String templateId) throws WxErrorException {Boolean flag wxMpService.getTemplateMsgService().delPrivateTemplate(templateId);return flag;}}
wxMpService 注入为空解决
详情见demo 参考https://github.com/Wechat-Group/weixin-java-tools 完成下面3步即可。 1、application.yml 添加配置
# 微信公众号配置
wx:mp:configs:- appId: xxx # 公众号appidsecret: xxx # 公众号密钥2、添加 WxMpProperties 类
Data
ConfigurationProperties(prefix wx.mp)
public class WxMpProperties {/*** 多个公众号配置信息*/private ListMpConfig configs;Datapublic static class MpConfig {/*** 设置微信公众号的appid*/private String appId;/*** 设置微信公众号的app secret*/private String secret;}Overridepublic String toString() {return JSONUtil.toJsonStr(this);}
}3、添加 WxMpConfiguration 类
AllArgsConstructor
Configuration
EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {private final WxMpProperties properties;Beanpublic WxMpService wxMpService() {final ListWxMpProperties.MpConfig configs this.properties.getConfigs();if (configs null) {throw new RuntimeException(添加下相关配置注意别配错了);}WxMpService service new WxMpServiceImpl();service.setMultiConfigStorages(configs.stream().map(a - {WxMpDefaultConfigImpl configStorage new WxMpDefaultConfigImpl();;configStorage.setAppId(a.getAppId());configStorage.setSecret(a.getSecret());return configStorage;}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a - a, (o, n) - o)));return service;}
}测试结果
手机显示结果 Postman测试结果
access_token可以从微信公众平台接口调试工具中获取如下图
若获取不成功需要在公众号平台IP白名单中添加本机IP地址。设置与开发 --- 安全中心 ---- IP白名单