一、公众号开启推送功能
1、
二、编写后台代码
1、实体类:
package com.atrunning.web.controller.lele.api;
import com.atrunning.web.controller.lele.util.WeiXinUtil;
import net.sf.json.JSONObject;
public class WeiXinMessage {
//接受者openId
private String touser;
// 模版id
private String templateId;
//标题
// private String title;
//店铺名称
private String thing1;
//姓名
private String thing3;
private String phone_number4;
private String time5;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThing1() {
return thing1;
}
public void setThing1(String thing1) {
this.thing1 = thing1;
}
public String getThing3() {
return thing3;
}
public void setThing3(String thing3) {
this.thing3 = thing3;
}
public String getPhone_number4() {
return phone_number4;
}
public void setPhone_number4(String phone_number4) {
this.phone_number4 = phone_number4;
}
public String getTime5() {
return time5;
}
public void setTime5(String time5) {
this.time5 = time5;
}
/**
* 公众号推送封装
* @param vo
* @param ACCESS_TOKEN
* @throws Exception
*/
public static String sendMessagePush(WeiXinMessage message , String ACCESS_TOKEN){
JSONObject json=new JSONObject();
JSONObject text=new JSONObject();
if (message.getTouser()==null){
return "touser不能为空";
}
if (message.getTemplateId()==null){
return "TemplateId不能为空";
}
json.put("touser",message.getTouser());
json.put("template_id",message.getTemplateId());
if (message.getThing1()!=null){
JSONObject thing1=new JSONObject();
thing1.put("value",message.getThing1());
text.put("thing1",thing1);
}
if (message.getThing3()!=null){
JSONObject thing3=new JSONObject();
thing3.put("value",message.getThing3());
text.put("thing3",thing3);
}
if (message.getPhone_number4()!=null){
JSONObject phone_number4=new JSONObject();
phone_number4.put("value",message.getPhone_number4());
text.put("phone_number4",phone_number4);
}
if (message.getTime5()!=null){
JSONObject time5=new JSONObject();
time5.put("value",message.getTime5());
text.put("time5",time5);
}
json.put("touser",message.getTouser());
json.put("template_id",message.getTemplateId());
json.put("appid","wx089701e2e1178178");
json.put("data", text);
//发送模板消息
String sendUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
sendUrl=sendUrl.replace("ACCESS_TOKEN",ACCESS_TOKEN);
String jsonstringParm =json.toString();
System.out.println("模版消息----------------"+jsonstringParm);
JSONObject object= WeiXinUtil.httpsRequest(sendUrl,"POST",jsonstringParm);
return object.toString();
}
}
二、controller代码
// 公众号订餐预约通知服务
JSONObject ret1 = WeiXinUtil.requestHttps(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+WeiXinUtil.APPID+"&secret="+WeiXinUtil.APPSECRET, "GET", null);
String token = ret1.getString("access_token");
// String openid = users.getUserOpenid();
System.out.println("模版推送");
WeiXinMessage msgVo = new WeiXinMessage();
msgVo.setTemplateId(WeiXinUtil.MESSAGEID);
msgVo.setTouser("oTatj6YATWU-AR-pOQvAfBO4FRFw");
msgVo.setThing1("挖掘机哪家强");
msgVo.setThing3(tReserve.getName());
msgVo.setPhone_number4(tReserve.getPhone());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
msgVo.setTime5(sdf.format(tReserve.getReserveTime()));
String json = WeiXinMessage.sendMessagePush(msgVo,token);
System.out.println(json);
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(json);
if(jsonObject.getIntValue("errcode") == 0) {
System.out.println("发送成功");
}
return AjaxResult.success("取消预约成功");
} else {
return AjaxResult.error("取消预约失败");
}
}