推送模板消息,推送个⼈消息,给指定的⼈发送模
板消息
推送个⼈消息
开发时,需要写推送消息,从⽹上了⼀⼤堆,都不是很全,,通过这次把代码全都总结起来,⽅便他⼈寻问题的时候,不会跟作者⼀样,各种⽂档,耽误⼯作时间。最主要的就是的推送模板消息的设置,在后头进⾏把需要的模板推送给关注的,需要把openId与⽤户关联起来,这样就⽅便进⾏发送指定的⼈
开发⽂档
设置模板消息
maven⼯程l需要的包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>4.6.5</version>
</dependency>
<!-- mvnrepository/artifact/cn.hutool/hutool-log -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-log</artifactId>
<version>4.1.17</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
controller⾥的⽅法
// 获得access_token的接⼝
String getTokenUrl = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=" + AppConstants.APPID
+ "&secret=" + AppConstants.APPSECRET ;
String returnvalue = GET(getTokenUrl);
JSONObject jsonObjects = JSONObject.fromObject(returnvalue);
String access_token = ("access_token").toString();
//获取⽤户信息
String getUserUrl = "api.weixin.qq/cgi-bin/user/get?access_token=" + access_token;
String returnvalues = GET(getUserUrl);
JSONObject jsonObject3 = JSONObject.fromObject(returnvalues);
String openid = "";
if(lhVipDetail!=null) {
openid =OpenId();
}else {
return 501;
}
if(!StringUtils.isNotBlank(openid)) {
return 501;
}
// object[0];
// 发送的内容
WechatTemplate wechatTemplate = new WechatTemplate();
// 跳转⼩程序配置
// Miniprogram miniprogram = new Miniprogram();
// 设置模板id
wechatTemplate.setTemplate_id("DA4443rT1UORhell5vqiskNXLbx3oIzm0TmmdQ4kviU"); wechatTemplate.setTouser(openid);
// 模板消息点击跳转路径, 如果不配置跳转⼩程序,可以设置跳转该路径
// wechatTemplate.setUrl("www.baidu");
Map<String, TemplateData> m = new HashMap<String, TemplateData>();
TemplateData first = new TemplateData();
first.setColor("#173177");
first.setValue("订单成功⽀付通知");//标题
m.put("first", first);
TemplateData keyword1 = new TemplateData();
keyword1.setColor("#173177");
Timestamp changeTime = Tool.currentDate();
keyword1.setValue(Tool.changeTimeStampToString(changeTime));//时间
m.put("keyword1", keyword1);
TemplateData keyword2 = new TemplateData();
keyword2.setColor("#173177");
keyword2.setValue(shop);//商品标题
m.put("keyword2", keyword2);
TemplateData keyword3 = new TemplateData();
keyword3.setColor("#173177");
if (type == 1) {
keyword3.PracticalPrice().toString());
} else if (type == 2) {
keyword3.ServerPrice().toString()); //价格
}
m.put("keyword3", keyword3);
TemplateData keyword4 = new TemplateData();
keyword4.setColor("#173177");
if (type == 1) {
keyword4.OrderNum()); //订单号
} else if (type == 2) {
keyword4.ServerNum());
}
m.put("keyword4", keyword4);
TemplateData keyword5 = new TemplateData();
keyword5.setColor("#173177");
if (type == 1) {
keyword5.VipName() + "先⽣/⼥⽣");
} else if (type == 2) {
keyword5.VipName() + "先⽣/⼥⽣"); //姓名
}
m.put("keyword5", keyword5);
TemplateData remark = new TemplateData();
remark.setColor("#173177");
remark.setValue("感谢您的光临!"); //结尾
m.put("remark", remark);
wechatTemplate.setData(m);
Integer result = 0;
if(lhVipDetail!=null) {
//发送通知
String sendUrl = "api.weixin.qq/cgi-bin/message/template/send?access_token=" + access_token; String jsonString = JSONString(wechatTemplate);
String json2 = HttpUtil.post(sendUrl, jsonString);
JSONObject jsonObject2 = JSONObject.fromObject(json2);
System.out.println(jsonString);
System.out.println(json2);
System.out.println(jsonObject2);
if (null != jsonObject2) {
if (0 != Int("errcode")) {
result = Int("errcode");
<("错误 errcode:{} errmsg:{}", Int("errcode"),
<("errmsg").toString());
+ ("errmsg").toString());
}else {
result = Int("errcode");
}
}
StaticLog.info("模板消息发送结果:" + result);
}
return result;
需要的实体类
* 包含Miniprogram, TemplateData
* @Description * 参数的类, 调⽤发送模板消息的参数类 * @author scf
* @Date 2019-09-16 15:24:03
* @version 1.0.0
*/
public class WechatTemplate {
private String touser;
private Miniprogram miniprogram;
private String template_id;
private String url;
private Map<String, TemplateData> data;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, TemplateData> getData() {
return data;
}
public Miniprogram getMiniprogram() {
return miniprogram;
}
public void setMiniprogram(Miniprogram miniprogram) { this.miniprogram = miniprogram;
}
public void setData(Map<String, TemplateData> data) { this.data = data;
}
}
* @ClassName TemplateData
* @Description 模板消息的内容, 封装模板消息具体内容的类 * @author scf
* @Date 2019-09-16 15:00:30
* @version 1.0.0
*/
public class TemplateData {
private String value;
private String color;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
}
}
/**
* 模板消息跳转连接, 封装参数的类
* @author scf
* @Date 2019-09-16 15:10:30
* @version 1.0.0
*/
public class Miniprogram {
private String appid;
private String pagepath;
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
开发程序
}
public String getPagepath() {
return pagepath;
}
public void setPagepath(String pagepath) {
this.pagepath = pagepath;
}
}
http请求封装的类
/**
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论