经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
公众号开发笔记二
来源:cnblogs  作者:达叔小生  时间:2018/10/29 9:38:30  对本文有异议

标题图

前言

微信公众平台开发模板消息,用于公众号向用户发送服务通知,如学生进校门,用校卡滴,就可以在公众号接收服务通知,表明学生进校.在公众号内申请功能,添加模板消息.

只有认证后的服务号才能申请模板消息,需要选择2个行业,MP(维基百科,自由的百科全书),模板消息需要模板的ID,和模板中各种参数,内容以".DATA"结尾,否则视为保留字,模板保留符号"{{ }}".

设置行业可以在公众平台后台完成,接口调用:

这个步骤需要access_token

  1. // 请求方式: POST
  2. https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN

数据:

  1. {
  2.     "industry_id1":"1",
  3.     "industry_id2":"4"
  4. }

参数说明

参数说明
access_token接口调用凭证
industry_id1行业编号
industry_id2行业编号

行业代码查询:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

获取设置的行业信息:

  1. // 请求方式:GET
  2. https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN

需要access_token接口调用凭证,返回示例:

  1. {
  2. "primary_industry":{"first_class":"运输与仓储","second_class":"快递"},
  3. "secondary_industry":{"first_class":"IT科技","second_class":"互联网|电子商务"}
  4. }

primary_industry: 主营行业
secondary_industry: 副营行业

获取模板ID: 可以在公众平台后获取模板ID:

  1. // 请求方式: POST
  2. https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN

需要access_token

post后的数据:

  1. {
  2.  "template_id_short":"TM00015"
  3. }

template_id_short: 是模板库中模板的编号.

返回模板消息的接口,获取json数据:

  1. {
  2.  "errcode":0,
  3.  "errmsg":"ok",
  4.  "template_id":"Doclyl5uP7Aciu-qZ7mJNPtWkbkYnWBWVja26EGbNyk"
  5. }

获取模板列表

获取帐号下所有模板信息:

提供的接口:

  1. // 请求方式:GET
  2. https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN

返回的数据:

图1

调用接口进行删除某账号下的模板:

  1. // 请求方式:POST
  2.  
  3. https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN
  4.  
  5. POST数据说明如下:
  6.  
  7.  {
  8.      "template_id" : "Dyvp3-Ff0cnail_CDSzk1fIc6-9lOkxsQE7exTJbwUE"
  9.  }

template_id: 模板消息ID

删除成功:

  1. {
  2.    "errcode" : 0,
  3.    "errmsg" : "ok"
  4. }

发送模板消息

调用接口:

  1. // 请求方式: POST
  2. https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

图2

需要:

  1. OPENID
  2. template_id
  3. url
  4. miniprogram

图3

调用模板消息接口成功:

  1. {
  2. "errcode":0,
  3. "errmsg":"ok",
  4. "msgid":200228332
  5. }

事件推送

消息模板发送成功到公众号,微信服务器会将是否发送成功作为通知到开发者中心的服务器配置地址中.

推送xml代码:

图4

图5

图6

模版消息接口文档

https://www.cnblogs.com/jiqing9006/p/5220571.html

对模板进行查看详情:

图7

# 可向指定openID群发

https://segmentfault.com/a/1190000012828138

php的实现

https://www.cnblogs.com/jiqing9006/p/5220571.html

  1. 实例化 --> 获取appid,appsecret

  2. 获取access_token

  3. 发送模板消息

  4. 组合消息数据

  5. 获取openid, 并发送消息

发送模板消息的接口:

https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

发送模板的方法:

  1.     //发送模板消息的接口
  2.     public static final String SEND_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
  3.     /**
  4.      * 发送模板
  5.      *
  6.      */
  7.     public static void sendTemplate(String data){
  8.         String result = HttpUtil.post(SEND_TEMPLATE_URL.replace("ACCESS_TOKEN", getAccessToken()),data);
  9.         System.out.println(result);
  10.     }

getAccessToken的方法:

验证接入

图8

微信公众平台接口测试帐号申请

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

公众号开发:

appidappsecret

配置参数: URL(自己的服务器地址)和Token(可任意填写)

图9

  1. 使用java语言,SpringMVC+Spring+MyBatis框架

保证url的有效性:

图10

图11

代码示例:

  1. @Controller
  2. public class WeChatController {
  3.         /**
  4.          * 微信URL接入验证
  5.          * @param signature
  6.          * @param timestamp
  7.          * @param nonce
  8.          * @param echostr
  9.          * @return
  10.          */
  11.         @RequestMapping(value="/weChat",method= RequestMethod.GET)
  12.         @ResponseBody
  13.         public String validate(String signature,String timestamp,String nonce,String echostr){
  14.             //1. 将token、timestamp、nonce三个参数进行字典序排序
  15.             String[] arr = {timestamp,nonce,WeChatUtil.TOKEN};
  16.             Arrays.sort(arr);
  17.             //2. 将三个参数字符串拼接成一个字符串进行sha1加密
  18.             StringBuilder sb = new StringBuilder();
  19.             for (String temp : arr) {
  20.                sb.append(temp);
  21.             }
  22.             //3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
  23.             if(SecurityUtil.SHA1(sb.toString()).equals(signature)){
  24.                 //接入成功
  25.                 return echostr;
  26.             }
  27.             //接入失败
  28.             return null;
  29.         }
  30. }
  1. WeChatUtil.TOKEN是常量要与填入的token值相同
  2. SecurityUtil是工具类,提供sha1加密方法

获取access_token

  1. public class WeChatUtil {
  2.     //URL验证时使用的token
  3.     public static final String TOKEN = "wolfcode";
  4.     //appid
  5.     public static final String APPID = "wx59687be81dd3d388";
  6.     //secret
  7.     public static final String SECRET = "d4624c36b6795d1d99dcf0547af5443d";
  8.         //创建菜单接口地址
  9.     public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
  10.     //获取access_token的接口地址
  11.     public static final String GET_ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
  12.     //缓存的access_token
  13.     private static String accessToken;
  14.     //access_token的失效时间
  15.     private static long expiresTime;
  16.  
  17.     /**
  18.      * 获取accessToken
  19.      * @return
  20.      */
  21.     public static String getAccessToken(){
  22.         //判断accessToken是否已经过期,如果过期需要重新获取
  23.         if(accessToken==null||expiresTime < new Date().getTime()){
  24.             //发起请求获取accessToken
  25.             String result = HttpUtil.get(GET_ACCESSTOKEN_URL.replace("APPID", APPID).replace("APPSECRET", SECRET));
  26.             //把json字符串转换为json对象
  27.             JSONObject json = JSON.parseObject(result);
  28.             //缓存accessToken
  29.             accessToken = json.getString("access_token");
  30.             //设置accessToken的失效时间
  31.             long expires_in = json.getLong("expires_in");
  32.             //失效时间 = 当前时间 + 有效期(提前一分钟)
  33.             expiresTime = new Date().getTime()+ (expires_in-60) * 1000;
  34.         }
  35.         return accessToken;
  36.     }
  37. }

HttpUtil是发起http请求的工具类

https://blog.csdn.net/frankcheng5143/article/details/50070591

官方文档中的示例:

  1. //1.获得一个httpclient对象
  2. CloseableHttpClient httpclient = HttpClients.createDefault();
  3. //2.生成一个get请求
  4. HttpGet httpget = new HttpGet("http://localhost/");
  5. //3.执行get请求并返回结果
  6. CloseableHttpResponse response = httpclient.execute(httpget);
  7. try {
  8.     //4.处理结果
  9. } finally {
  10.     response.close();
  11. }

HttpGetHttpPost

发送HttpGet

  1.     /**
  2.      * 发送HttpGet请求
  3.      * @param url
  4.      * @return
  5.      */
  6.     public static String sendGet(String url) {
  7.         //1.获得一个httpclient对象
  8.         CloseableHttpClient httpclient = HttpClients.createDefault();
  9.         //2.生成一个get请求
  10.         HttpGet httpget = new HttpGet(url);
  11.         CloseableHttpResponse response = null;
  12.         try {
  13.             //3.执行get请求并返回结果
  14.             response = httpclient.execute(httpget);
  15.         } catch (IOException e1) {
  16.             e1.printStackTrace();
  17.         }
  18.         String result = null;
  19.         try {
  20.             //4.处理结果,这里将结果返回为字符串
  21.             HttpEntity entity = response.getEntity();
  22.             if (entity != null) {
  23.                 result = EntityUtils.toString(entity);
  24.             }
  25.         } catch (ParseException | IOException e) {
  26.             e.printStackTrace();
  27.         } finally {
  28.             try {
  29.                 response.close();
  30.             } catch (IOException e) {
  31.                 e.printStackTrace();
  32.             }
  33.         }
  34.         return result;
  35.     }

发送HttpPost

  1.     /**
  2.      * 发送不带参数的HttpPost请求
  3.      * @param url
  4.      * @return
  5.      */
  6.     public static String sendPost(String url) {
  7.         //1.获得一个httpclient对象
  8.         CloseableHttpClient httpclient = HttpClients.createDefault();
  9.         //2.生成一个post请求
  10.         HttpPost httppost = new HttpPost(url);
  11.         CloseableHttpResponse response = null;
  12.         try {
  13.             //3.执行get请求并返回结果
  14.             response = httpclient.execute(httppost);
  15.         } catch (IOException e) {
  16.             e.printStackTrace();
  17.         }
  18.         //4.处理结果,这里将结果返回为字符串
  19.         HttpEntity entity = response.getEntity();
  20.         String result = null;
  21.         try {
  22.             result = EntityUtils.toString(entity);
  23.         } catch (ParseException | IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.         return result;
  27.     }

HttpClient通过UrlEncodedFormEntity提交带参数的请求

图12

使用apacheHttpClient发送post请求

https://blog.csdn.net/xiaoyaoyulinger/article/details/77315694

JAVA http请求工具类http-request

https://www.jianshu.com/p/e955b01e2e16

获取微信用户信息

https://blog.csdn.net/wolfcode_cn/article/details/80755359

图13

两种scope授权作用域
配置授权域名

引导用户打开授权界面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

图14

code来获取网页授权专用的access_token凭据

地址:

https://blog.csdn.net/wolfcode_cn/article/details/80755359

达叔小生:往后余生,唯独有你
You and me, we are family !
90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生
https://www.jianshu.com/u/c785ece603d1

结语

  • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注

  • 小礼物走一走 or 点赞

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号