Java发送(服务号)统⼀服务消息(原模板消息)
原模板消息接⼝,已经废弃。改为统⼀服务消息接⼝,需要⽤户⼿动订阅,后端才能发送。
需要注意的是,这个接⼝的能⼒是(仅认证服务号)的能⼒,(原)模板消息需要在配置。
⼩程序端订阅时,需要输⼊模板id,请先⾃⾏在管理后台进⾏配置。
开始
我们这边来使⽤开发者联盟的包来进⾏开发,他们⾥⾯都封装好了调⽤(重试啊,报错啊什么的,都处理过了,不需要咱担⼼),⽤起来⽐较⽅便。
maven引⼊
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.0.0</version>
</dependency>
注意哦,引⽤(包括和服务号):weixin-java-mp这个模块
对外部服务器有验证步骤的,所以我们需要新增⼀个restController去处理的验证
这个类WxPortalController在Demo包⾥有,这边就不贴了。
真正的调⽤步骤也就这⼀步,
//封装的request对象类,发送给⽅的格式就是这样
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.
toUser(...)
.templateId(...)
.url(...)
.build();
//输⼊模板消息每⾏的参数,内容,以及样式
templateMessage.addData(new WxMpTemplateData(name1, value1, color2));
templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
//调⽤发送,获取模板消息模块,发送模板消息
但是我们需要对wxMpService进⾏配置,这就是⼀个封装的接⼝类,⾥⾯就是对⽂档⾥各种接⼝的调⽤。⾥⾯先分了模块,然后再分具体调⽤的⽅法。
贴⼀下配置类
依托springboot
属性
@Data
@ConfigurationProperties(prefix ="wx.mp")
public class WxMpProperties {
/**
* 设置的appid
*/
private String appId;
/**
* 设置的app secret
*/
private String secret;
/**
* 设置的token
*/
private String token;
/**
* 设置的EncodingAESKey
*/
private String aesKey;
}
yml⽂件⾥加上配置,格式⾃⼰调⼀下空格位置
wx:
mp:
appId: #appid
secret: #的app secret
token: #的token
aesKey: #的EncodingAESKey configure引⼊
llect.Maps;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.fig.impl.WxMpDefaultConfigImpl;
import org.t.properties.EnableConfigurationProperties; import t.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
//注⼊配置
@Configuration
//这个跟上⾯的@ConfigurationProperties是配套使⽤的,意思是注⼊⼀下这个类
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfig {
//这个可以不要,这个是⽤来接收发送给服务器的回调消息的
private static Map<String, WxMpMessageRouter> routers = wHashMap();
//类所属属性,每个实例都通⽤的
private static HashMap<String,WxMpService> wHashMap();
public static void init(WxMpProperties properties){
WxMpDefaultConfigImpl configStorage =new WxMpDefaultConfigImpl();
configStorage.AppId());
configStorage.Secret());
configStorage.Token());
configStorage.AesKey());
WxMpService service =new WxMpServiceImpl();
service.setWxMpConfigStorage(configStorage);
routers.AppId(),newRouter(service));
mpServices.AppId(),service);
}
public static Map<String, WxMpMessageRouter>getRouters(){
return routers;
}
public static HashMap<String,WxMpService>getMpServices(){
return mpServices;
}
private static WxMpMessageRouter newRouter(WxMpService wxMpService){
final WxMpMessageRouter newRouter =new WxMpMessageRouter(wxMpService);
return newRouter;
}
}
我们需要调⽤WxMpConfig.init⽅法
使⽤springboot的类周期来进⾏调⽤,对mpServices进⾏注⼊
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 初始化配置
* @author Kevin
* @date 2019-01-16 14:39
*/
@Component
public class WxConfig implements InitializingBean {
开发程序
private final static Logger logger = Logger(WxConfig.class); @Autowired
private WxMpProperties wxMpProperties;
@Override
public void afterPropertiesSet()throws Exception {
initWxConfig();
}
/
**
* 初始化配置
* @param
* @return void
* @author lotus
* @date 2021-03-3 15:01:00
*/
public void initWxConfig(){
//只有⼀个配置对应
WxMpConfig.init( wxMpProperties );
logger.info("========================================");
logger.info("加载配置成功");
logger.info("========================================");
}
}
⾄此,我们就可以使⽤模板消息发送
@Slf4j
public class WxTemplate {
// 模板消息字体颜⾊
private static final String TEMPLATE_FRONT_COLOR ="#32CD32";
public void sendTemplate(String appId,String openId,String templateId,String url){
WxMpTemplateMessage resultTemplate = WxMpTemplateMessage.builder().build();
/
/设置接受者的openId
resultTemplate.setToUser(openId);
//设置模板id
resultTemplate.setTemplateId(templateId);
//模板消息跳转页⾯
resultTemplate.setUrl(url);
//设置模板消息样式以及内容
//        WxMpTemplateData firstData = new WxMpTemplateData("first", "订单状态更新", TEMPLATE_FRONT_COLOR);
//        WxMpTemplateData orderMoneySumData = new WxMpTemplateData("OrderSn", Parameter("OrderSn"), TEMPLATE_FRONT_COLOR) ;
//        WxMpTemplateData orderProductNameData = new WxMpTemplateData("OrderStatus", request.
getParameter("OrderStatus"), TEMPLATE_FRONT _COLOR);
//        WxMpTemplateData remarkData = new WxMpTemplateData("remark", Parameter("remark"), TEMPLATE_FRONT_COLOR);
//        orderPaySuccessTemplate.addData(firstData)
//                .addData(orderMoneySumData)
//                .addData(orderProductNameData)
//                .addData(remarkData);
try{
WxMpService wxMpService= MpServices().get(appId);
.sendTemplateMsg(resultTemplate);
}catch(WxErrorException e){
log.Message());
}
}
}