微信小程序订阅消息推送--Springboot实现

微信小程序订阅消息推送--Springboot实现

2023年7月2日发(作者:)

微信⼩程序订阅消息推送--Springboot实现背景最近做个排号叫号的微信⼩程序,想⽤户在微信⼩程序上进⾏排号,商家在⼩程序上进⾏叫号,叫号的通知发送到⽤户微信⾥.这⾥就要⽤到订阅消息.先看效果图1.创建模板登录微信公众平台创建⼀个⾃⼰需要的模版,具体创建请⾃⾏查阅,今天的重点不在这⾥…发送订阅消息的三个步骤.⼀.获取⽤户的openid⽤户的openid的获取,我是在⽤户使⽤微信登录时进⾏获取的,具体可以查看我微信登录的⽂章:⼆.获取access_token我们⾸先来看看access_token是什么,官⽅的说明是:access_token是公众号的全局唯⼀接⼝调⽤凭据,公众号调⽤各接⼝时都需使⽤access_token。开发者需要进⾏妥善保存。access_token的存储⾄少要保留512个字符空间。access_token的有效期⽬前为2个⼩时,需定时刷新,重复获取将导致上次获取的access_token失效。简单来说,access_token就是⼩程序官⽅给我们提供的⼀个凭证,如果要调⽤官⽅的接⼝,就必须先获取凭证,所以我们先来谈谈怎么获取access_token.还是先看官⽅⽂档从官⽅⽂档我们可以看到,我们需要以下⼏个参数参数grant_typeappidsecret是否必须是是是说明获取access_token填写client_credential第三⽅⽤户唯⼀凭证第三⽅⽤户唯⼀凭证密钥,即appsecretgrant_type是⼀个固定的值,appid和secret是需要我们填⼊的,这两个值在我们的⼩程序后台就可以拿到,具体可以查看我微信登录的⽂章:可以在微信⼩程序后台去寻找appid和secretJava代码部分具体思路就是先向微信提供的这个url发送⼀个get请求.微信官⽅给的正常返回值为新建⼀个pojo来存储返回的参数import ;/** * @author zty * @date 2020/4/23

下午2:26 * @description: */@Datapublic class getAccessTokenModel { private String access_token; private Integer expires_in;}然后利⽤⼀个http⼯具类发送请求,⼀个json转model⼯具类来转换⼀下.这两个⼯具类具体代码后⾯贴出这就是我们获取access_token的代码了,获取之后将他存⼊redis,设置有效时间,超时就重新获取./** *

获取AccessToken * * @return */ public String getAccessToken() { if (("access_token") != null) { return (String) ("access_token"); } //GET /cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET String url = "/cgi-bin/token"; Map param = new HashMap<>(); ("grant_type", "client_credential"); ("appid", "wx7e139fc4dec9fd08"); ("secret", "d483c36cf9f93500a17aa0cb788a0f48"); String vxResult = (url, param); (vxResult); getAccessTokenModel accessTokenModel = Pojo(vxResult, ); ("access_token", ess_token(), ires_in()); return ess_token(); //POST /cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN }使⽤这两个⼯具类需要引⼊俩个maven依赖 mponents httpclient 4.5.7 a fastjson 1.2.56 package ;import ption;import ;import ist;import ;import ;import luePair;import odedFormEntity;import bleHttpResponse;import t;import st;import lder;import tType;import Entity;import bleHttpClient;import ients;import ameValuePair;import Utils;/** * @author zty */public class HttpClientUtil { public static String doGet(String url, Map param) { //

创建Httpclient对象 CloseableHttpClient httpclient = Default(); String resultString = ""; CloseableHttpResponse response = null; try { //

创建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : ()) { ameter(key, (key)); } } URI uri = (); //

创建http GET请求 HttpGet httpGet = new HttpGet(uri); //

执⾏请求 response = e(httpGet); //

判断返回状态是否为200 if (tusLine().getStatusCode() == 200) { resultString = ng(ity(), "UTF-8"); } } catch (Exception e) { tackTrace(); } finally { try { if (response != null) { (); } (); } catch (IOException e) { tackTrace(); } } return resultString; } public static String doGet(String url) { return doGet(url, null); } public static String doPost(String url, Map param) { //

创建Httpclient对象 CloseableHttpClient httpClient = Default(); CloseableHttpResponse response = null; String resultString = ""; try { //

创建Http Post请求 HttpPost httpPost = new HttpPost(url); //

创建参数列表 if (param != null) { List paramList = new ArrayList<>(); for (String key : ()) { (new BasicNameValuePair(key, (key))); } //

模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); ity(entity); } //

执⾏http请求 response = e(httpPost); resultString = ng(ity(), "utf-8"); } catch (Exception e) { tackTrace(); } finally { try { (); } catch (IOException e) { tackTrace(); } } return resultString; }

public static String doPost(String url) { return doPost(url, null); }

public static String doPostJson(String url, String json) { //

创建Httpclient对象 CloseableHttpClient httpClient = Default(); CloseableHttpResponse response = null; String resultString = ""; try { //

创建Http Post请求 HttpPost httpPost = new HttpPost(url); //

创建请求内容 StringEntity entity = new StringEntity(json, ATION_JSON); ity(entity); //

执⾏http请求 response = e(httpPost); resultString = ng(ity(), "utf-8"); } catch (Exception e) { tackTrace(); } finally { try { (); } catch (IOException e) { tackTrace(); } } return resultString; }}package ;import ;import ocessingException;import pe;import Mapper;/** *

* @Title: * @Package * @Description:

⾃定义响应结构,

转换类 * Copyright: Copyright (c) 2016 * Company:ore *

* @author zty * @date 2020年4⽉15⽇

下午11:05:03 * @version V1.0 */public class JsonUtils { //

定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** *

将对象转换成json字符串。 *

Title: pojoToJson

*

Description:

* @param data * @return */ public static String objectToJson(Object data) { try { String string = alueAsString(data); return string; } catch (JsonProcessingException e) { tackTrace(); } return null; }

/** *

将json结果集转化为对象 *

* @param jsonData json数据 * @param clazz

对象中的object类型 * @return */ public static T jsonToPojo(String jsonData, Class beanType) { try { T t = lue(jsonData, beanType); return t; } catch (Exception e) { tackTrace(); } return null; }

/** *

将json数据转换成pojo对象list *

Title: jsonToList

*

Description:

* @param jsonData * @param beanType * @return */ public static List jsonToList(String jsonData, Class beanType) { JavaType javaType = eFactory().constructParametricType(, beanType); try { List list = lue(jsonData, javaType); return list; } catch (Exception e) { tackTrace(); }

return null; }

}好了,这样我我们就获取到了access_token,注意我是存在redis⾥的三.发送消息到微信⼩程序端获取授权我这⾥就不在介绍了,具体看微信的官⽅⽂档我们通过上⾯第⼆步,成功的获取到了access_token。下⾯就要调⽤⼩程序官⽅为我们提供的发送消息的接⼝了,先看下官⽅⽂档。上⾯有些参数是我们发送消息所需要的.这⾥需要注意的是,我们要向⽤户发送订阅消息,⼩程序端就要去诱导⽤户授权正常来说,⼀次授权只允许发送⼀条消息.java代码先创建⼀个类,注意Thing要与你模板对应import sConstructor;import ;/** * @author zty * @date 2020/4/23

下午8:31 * @description: */@Datapublic class SubscribeMessageVO { //具体的订阅消息的key {{}}

则key为thing4 private Thing4 thing4; private Thing6 thing6; private Thing7 thing7; @Data @AllArgsConstructor public static class Thing4{ private String value; } @Data @AllArgsConstructor public static class Thing6{ private String value; } @Data @AllArgsConstructor public static class Thing7{ private String value; }}我这⾥发送⽤到了消息队列,可以忽略/** *

发送订阅消息 * * @param peopleSet */ @RabbitListener(bindings = { @QueueBinding( value = @Queue,//创建临时队列 exchange = @Exchange(value = "updateHistory", type = "fanout") ) }) public void sendMessage(String peopleSet) { String[] temp; temp = ("+"); String p1 = temp[0]; Long p2 = f(temp[1]); Long p3 = f(temp[2]); ActivityUserHistory history = ByOpenId2(getUserIdByOpenId(p1), p2, p3); SubscribeMessageVO bean = new SubscribeMessageVO(); ng4(new 4(ivityName() + ":" + e())); ng6(new 6(ress())); ng7(new 7("请到服务处联系⼯作⼈员")); WxMssVO wxMssVO = new WxMssVO(); ser(p1); plate_id("***");//模板id号码 a(bean); push(wxMssVO); } public void push(WxMssVO wxMssVO) { String url = "/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(); String json = ToJson(wxMssVO); String vxResult = Json(url, json); ("返回的内容:" + vxResult); }将信息加⼊SubscribeMessageVO这个类的⼀个对象中,然后转为json,继续调⽤我们刚刚的http⼯具类发送.这样就能得到我们开始时的订阅消息了

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688279097a112469.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信