java 微信 推送

编程入门 行业动态 更新时间:2024-10-23 01:47:14

<a href=https://www.elefans.com/category/jswz/34/1770091.html style=java 微信 推送"/>

java 微信 推送

公众号配置模板示例

推送接口

/**

*

* @title: sendTemplate

* @author: daixiaowei

* @date: 2019年11月09日 下午 12:59:27

* @description: 支付微信模板推送

* @param: fillData

* @return:

*/

public String sendTemplate(String openid,String templateId,String[] fillData,String orderId,String vendorId) {

String orderIds = "";

if (StringUtils.isNotEmpty(orderId)) {

orderIds = orderId;

}

String tepUrl = "="

+ getAccessToken();

String frontUrl = conf.get("weixinpay.msgDetailUrl")+"/"

+vendorId +"/"+orderIds+"/"+openid;

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(tepUrl);

// 装配post请求参数

JSONObject json = new JSONObject();

json.put("touser", openid);//消息接收者的openid

json.put("template_id", templateId);//消息模板id

json.put("url", frontUrl);//模板里详情页链接

json.put("emphasis_keyword", "keyword1.DATA");

JSONObject dataJson = new JSONObject();

//数据插入

for (int i = 0; i < fillData.length; i++) {

JSONObject sonDateJson = new JSONObject();

sonDateJson.put("value", fillData[i]);

dataJson.put("keyword" + (i + 1), sonDateJson);

}

json.put("data", dataJson);

String resultStr = "发送失败";

try {

StringEntity myEntity = new StringEntity(json.toJSONString(), "UTF-8");

// 设置post求情参数

httpPost.setEntity(myEntity);

HttpResponse httpResponse = httpClient.execute(httpPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

// 发送成功

String resutlEntity = EntityUtils.toString(httpResponse.getEntity());

ResultTemplateDate resultTemplateDate = JSONObject.parseObject(resutlEntity, ResultTemplateDate.class);

if (resultTemplateDate.getErrcode().equals("40001")) {

resultStr = "token失效";

}

if (resultTemplateDate.getErrcode().equals("40037")) {

resultStr = "template_id不正确";

}

if (resultTemplateDate.getErrcode().equals("41030")) {

resultStr = "page不正确";

}

if (resultTemplateDate.getErrcode().equals("45009")) {

resultStr = "接口调用超过限额(目前默认每个帐号日调用限额为10万)";

}

resultStr = "ok";

return resultStr;

} else {

// 发送失败

return resultStr;

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (httpClient != null) {

// 释放资源

httpClient.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return resultStr;

}

获取token方法

/**

*

* @title: getAccessToken

* @author: daixiaowei

* @date: 2019年11月09日 上午 11:00:33

* @description: 获取token

* @param:

* @return:

*/

public String getAccessToken() {

String tmpUrl = "=client_credential&appid=" + conf.get("weixinpay.appid")

+ "&secret=" + conf.get("weixinpay.secret") + "";

CloseableHttpClient httpCilent = HttpClients.createDefault();

HttpGet httpGet = new HttpGet(tmpUrl);

try {

HttpResponse httpResponse = httpCilent.execute(httpGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

String entity = EntityUtils.toString(httpResponse.getEntity());

AccessToken accessToken = JSONObject.parseObject(entity, AccessToken.class);

return accessToken.getAccess_token();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 释放资源

httpCilent.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return "";

}

模板返回值类

package com.jkr.common.util;

/**

* 模板返回消息

* @author daixiaowei

*/

public class ResultTemplateDate {

private String errcode;

private String errmsg;

public String getErrcode() {

return errcode;

}

public void setErrcode(String errcode) {

this.errcode = errcode;

}

public String getErrmsg() {

return errmsg;

}

public void setErrmsg(String errmsg) {

this.errmsg = errmsg;

}

}

更多推荐

java 微信 推送

本文发布于:2023-06-25 22:02:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/884996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!