Java对接模板消息推送maven依赖
<!--的SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.9.8.BETA</version>
</dependency>
<!--3.0.0版本失效的话使⽤3.6.0-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.0.0</version>
</dependency>
1. 配置类
import Data;
import ConfigurationProperties;
import RefreshScope;
import Configuration;
@ConfigurationProperties(prefix ="wx.mp")
@Data
@Configuration
@RefreshScope//实现配置⽂件修改,bean⾃动刷新功能
public class WxMpProperties {
/**
* 设置的appid
*/
private String appId;
/**
* 设置的app secret
*/
private String secret;
/**
* 设置的token
*/
private String token;
/**
* 设置的EncodingAESKey
*/
private String aesKey;
}
2. 初始化消息模板,数据库进⾏配置
import JSON;
import ApplicationContextProvider;
import WxMpTemplateDto;
import WxMpTemplateService;
import Slf4j;
import WxMpTemplateData;
import WxMpTemplateMessage;
import CollectionUtils;
import StringUtils;
import Autowired;
import Component;
import PostConstruct;
import HashMap;
import List;
import Map;
@Component
@Slf4j
public class WxMpTemplateConfig {
private static Map<String, WxMpTemplateMessage> wxMpTemplateMap =new HashMap<>(8);
@Autowired
private WxMpTemplateService wxMpTemplateService = Bean(WxMpTemplateService.class);
@PostConstruct
private void init(){
log.info("初始化消息模板map============");
List<WxMpTemplateDto> list = List();
if(CollectionUtils.isEmpty(list)){
return;
}
list.forEach(dto ->{
String wxMpTemplateDataJson = WxMpTemplateDataJson();
List<WxMpTemplateData> wxMpTemplateData = JSON.parseArray(wxMpTemplateDataJson, WxMpTemplateData.class);            WxMpTemplateMessage config =
WxMpTemplateMessage.builder()
开发程序.TemplateId())
.Url())
.data(wxMpTemplateData)
.build();
wxMpTemplateMap.KeyName(), config);
});
}
public static WxMpTemplateMessage getTemplate(String key, String openId){
WxMpTemplateMessage wxMpTemplateMessage = (key);
if(null== wxMpTemplateMessage){
return null;
}
if(StringUtils.isNotEmpty(openId)){
wxMpTemplateMessage.setToUser(openId);
}
return wxMpTemplateMessage;
}
}
3. 消息模板 WxMpTemplateData
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain =true)
@Builder
public class WxMpTemplateData implements Serializable {
private String name;
private String value;
private String color;
}
4. WxMpTemplateMsgConfig
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain =true)
@Builder
public class WxMpTemplateMsgConfig implements Serializable {
private static final long serialVersionUID =5063374783759519418L;
/
**
* 接收者openid.
*/
private String toUser;
/**
* 模板ID.
*/
private String templateId;
/**
* 模板跳转链接.
* <pre>
* url和miniprogram都是⾮必填字段,若都不传则模板⽆跳转;若都传,会优先跳转⾄⼩程序。
* 开发者可根据实际需要选择其中⼀种跳转⽅式即可。当⽤户的客户端版本不⽀持跳⼩程序时,将会跳转⾄url。    * </pre>
*/
private String url;
/**
* 跳⼩程序所需数据,不需跳⼩程序可不⽤传该数据.
*
* @see #url
*/
private WxMpTemplateMessage.MiniProgram miniProgram;
}
5. 配置⽂件注⼊
import WxMpService;
import WxMpServiceImpl;
import WxMpConfigStorage;
import WxMpDefaultConfigImpl;
import Autowired;
import Value;
import Bean;
import Configuration;
import Resource;
@Configuration
public class WxConfig {
@Autowired
private WxMpProperties wxMpProperties;
/**
* 客户端配置存储
*
* @return
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpDefaultConfigImpl configStorage =new WxMpDefaultConfigImpl();
/
/ appId
configStorage.AppId());
// appSecret
configStorage.Secret());
// Token
configStorage.Token());
// EncodingAESKey
configStorage.AesKey());
return configStorage;
}
/**
* 声明实例
*
* @return
*/
@Bean
public WxMpService wxMpService(){
WxMpService wxMpService =new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
}
6. 实体类
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain =true)
@Builder
@TableName("wx_mp_template")
public class WxMpTemplate implements Serializable {
private static final long serialVersionUID =337800069603349L;
@TableId
private Long id;
/
**
* key
*/
private String keyName;
/**
* 描述
*/
private String description;
/**
* 模板id
*/
private String templateId;
/**
* 跳转url
*/
private String url;
/**
* 模板
*/
private String wxMpTemplateDataJson;
private Integer status;
}
7. api实现 (其他的跳过 entity->dto->vo等转换,)
import JSON;
import JSONObject;
import CommonConfigDto;
import CommonConfigClient; import CommonConfigKeyEnum; import MemberErrCode; import WxMpErrCode;
import RestResult;
import OkHttpUtil;
import WxMphelperServer;
import Slf4j;
import WxMpService;
import Autowired; import Service;
import StringUtils;
import Resource;
import IOException;
import HashMap;
@Service
@Slf4j

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。