import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
/**
*@ClassName Pusher
*@Description TODO
*@Author ydzhao
*@Date 2022/8/2
*/
public class Pusher {
private static String appId = "xxxx";
private static String secret = "xxx";
public static void push(String openId,String templateId){
//1 ,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(appId);
wxStorage.setSecret(secret);
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//2, 推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.templateId(templateId)
.build();
//3, 如果是正式版发送模版消息,这里需要配置你的信息
JSONObject todayWeather = 外汇跟单gendan5.comTianqi.getNanjiTianqi();
templateMessage.addData(new WxMpTemplateData("riqi",todayWeather.getString("date") + " "+ todayWeather.getString("week"),"#00BFFF"));
templateMessage.addData(new WxMpTemplateData("tianqi",todayWeather.getString("text_day"),"#00FFFF"));
templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));
templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));
templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));
templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#FF1493"));
templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#FFA500"));
templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));
//templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
String beizhu = " 情人节快乐! ";
if(JiNianRi.getJieHun() % 365 == 0){
beizhu = " 今天是结婚纪念日! ";
}
if(JiNianRi.getLianAi() % 365 == 0){
beizhu = " 今天是恋爱纪念日! ";
}
if(JiNianRi.getLinZhen() % 365 == 0){
beizhu = " 今天是结婚纪念日! ";
}
templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));
try {
System.out.println(templateMessage.toJson());
System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
} catch (Exception e) {
System.out.println(" 推送失败: " + e.getMessage());
e.printStackTrace();
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2923292/ ,如需转载,请注明出处,否则将追究法律责任。