Home > Java > javaTutorial > The fourth step of java WeChat API development: WeChat custom personalized menu implementation

The fourth step of java WeChat API development: WeChat custom personalized menu implementation

高洛峰
Release: 2017-01-17 11:26:48
Original
1458 people have browsed it

How WeChat implements customized personalized menus, let me introduce it to you below

1. Global description
Please refer to the first two articles for detailed instructions.

2. Description of this article
This article is divided into five parts:
* Encapsulation of tool class AccessTokenUtils
* Reading and analysis of custom menus and personalized menu documents
* Menu JSON Analyze and build corresponding beans
* Implementation of custom menu
* Implementation of personalized menu
WeChat custom menu provides demonstrations for all types of menus
At the end of this article, the first four articles of this article will be given All demonstration source codes of the article

Encapsulation of tool class AccessTokenUtils
The acquisition and scheduled saving of AccessToken have been introduced in detail above. Here are the encapsulated AccessTokenUtils, implementation principles and documents directly given after processing. Reading is no longer given.
AccessTokenUtils.java

package com.gist.utils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
 
import javax.net.ssl.HttpsURLConnection;
 
import com.gist.bean.Access_token;
import com.google.gson.Gson;
 
/**
 * @author 高远</n> 邮箱:wgyscsf@163.com</n> 博客 http://blog.csdn.net/wgyscsf</n>
 *   编写时期 2016-4-7 下午5:44:33
 */
public class AccessTokenUtils {
 private static final long MAX_TIME = 7200 * 1000;// 微信允许最长Access_token有效时间(ms)
 private static final String TAG = "WeixinApiTest";// TAG
 private static final String APPID = "wx889b020b3666b0b8";// APPID
 private static final String SECERT = "6da7676bf394f0a9f15fbf06027856bb";// 秘钥
 
 /*
  * 该方法实现获取Access_token、保存并且只保存2小时Access_token。如果超过两个小时重新获取;如果没有超过两个小时,直接获取。该方法依赖
  * :public static String getAccessToken();
  * 
  * 思路:将获取到的Access_token和当前时间存储到file里,
  * 取出时判断当前时间和存储里面的记录的时间的时间差,如果大于MAX_TIME,重新获取,并且将获取到的存储到file替换原来的内容
  * ,如果小于MAX_TIME,直接获取。
  */
 // 为了调用不抛异常,这里全部捕捉异常,代码有点长
 public static String getSavedAccess_token() {
  Gson gson = new Gson();// 第三方jar,处理json和bean的转换
  String mAccess_token = null;// 需要获取的Access_token;
  FileOutputStream fos = null;// 输出流
  FileInputStream fis = null;// 输入流
  File file = new File("temp_access_token.temp");// Access_token保存的位置
  try {
   // 如果文件不存在,创建
   if (!file.exists()) {
    file.createNewFile();
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  // 如果文件大小等于0,说明第一次使用,存入Access_token
  if (file.length() == 0) {
   try {
    mAccess_token = getAccessToken();// 获取AccessToken
    Access_token at = new Access_token();
    at.setAccess_token(mAccess_token);
    at.setExpires_in(System.currentTimeMillis() + "");// 设置存入时间
    String json = gson.toJson(at);
    fos = new FileOutputStream(file, false);// 不允许追加
    fos.write((json).getBytes());// 将AccessToken和当前时间存入文件
    fos.close();
    return mAccess_token;
   } catch (Exception e) {
    e.printStackTrace();
   }
  } else {
   // 读取文件内容
   byte[] b = new byte[2048];
   int len = 0;
   try {
    fis = new FileInputStream(file);
    len = fis.read(b);
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   String mJsonAccess_token = new String(b, 0, len);// 读取到的文件内容
   Access_token access_token = gson.fromJson(mJsonAccess_token,
     new Access_token().getClass());
   if (access_token.getExpires_in() != null) {
    long saveTime = Long.parseLong(access_token.getExpires_in());
    long nowTime = System.currentTimeMillis();
    long remianTime = nowTime - saveTime;
    // System.out.println(TAG + "时间差:" + remianTime + "ms");
    if (remianTime < MAX_TIME) {
     Access_token at = gson.fromJson(mJsonAccess_token,
       new Access_token().getClass());
     mAccess_token = at.getAccess_token();
     return mAccess_token;
    } else {
     mAccess_token = getAccessToken();
     Access_token at = new Access_token();
     at.setAccess_token(mAccess_token);
     at.setExpires_in(System.currentTimeMillis() + "");
     String json = gson.toJson(at);
     try {
      fos = new FileOutputStream(file, false);// 不允许追加
      fos.write((json).getBytes());
      fos.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     return mAccess_token;
    }
 
   } else {
    return null;
   }
  }
 
  return mAccess_token;
 }
 
 /*
  * 获取微信服务器AccessToken。该部分和getAccess_token() 一致,不再加注释
  */
 public static String getAccessToken() {
  String urlString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
    + APPID + "&secret=" + SECERT;
  String reslut = null;
  try {
   URL reqURL = new URL(urlString);
   HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
     .openConnection();
   InputStreamReader isr = new InputStreamReader(
     httpsConn.getInputStream());
   char[] chars = new char[1024];
   reslut = "";
   int len;
   while ((len = isr.read(chars)) != -1) {
    reslut += new String(chars, 0, len);
   }
   isr.close();
  } catch (IOException e) {
 
   e.printStackTrace();
  }
  Gson gson = new Gson();
  Access_token access_token = gson.fromJson(reslut,
    new Access_token().getClass());
  if (access_token.getAccess_token() != null) {
   return access_token.getAccess_token();
  } else {
   return null;
  }
 }
}
Copy after login

Reading and analysis of custom menu and personalized menu documents
•Custom menu
◦Custom menu creation interface
◦Custom menu query interface
◦ Custom menu deletion interface
◦ Custom menu event push
◦ Personalized menu interface
◦ Obtain the menu configuration of the public account

•Document address: http://mp .weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html
•The official website documentation gives this explanation:
* The custom menu interface can implement multiple types of buttons, as follows: 1. click: click event.. .;2. view:jump event...;3....(About custom menu)
* Interface call request description http request method: POST (please use https protocol) https://api.weixin .qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN (About custom menu)
* Click and view request example {"button":[...]} (About custom menu)
* Parameter description...(About custom menu)
* Create a personalized menu http request method: POST (please use https protocol) https://api.weixin.qq.com/cgi-bin/menu /addconditional?access_token=ACCESS_TOKEN(About personalized menu)
* Request example: {"button":[...],"matchrule":{...}}(About personalized menu)
* Parameter Description... (About Personalized Menu)
* Developers can set the menu that users see (About Personalized Menu) through the following conditions:
1. User grouping (the developer’s business needs can be determined by Group users to complete)
2. Gender
3. Mobile operating system
4. Region (the region set by the user on the WeChat client)
5. Language (the language set by the user on the WeChat client) )

•Understanding:
◦It is a familiar POST request, but the call seems to be vague and I don’t quite understand it. We just know that we need to use the "?access_token=ACCESS_TOKEN" parameter, which we have already obtained in the previous article. If we replace the "ACCESS_TOKEN" in the request address given by the WeChat document with our own ACCESS_TOKEN we obtained, and visit the URL, we will see "{"errcode":44002,"errmsg":"empty post data hint: [ Gdveda0984vr23]”}”. It probably means empty post request data. Therefore, we need to pass the parameters to the WeChat server in the form of a POST request. The parameter format is also given below the document: {"button":[...]}, so we need to pass the parameters to the WeChat server according to this format.
◦ Regarding parameter description, we can see that there are seven parameters in custom menu creation. In addition to these seven parameters, there are eight more parameters in the personalized menu interface. Simply looking at this part of the document, we can understand that these eight parameters are used for matching and screening of personalized menus.
◦Now, we need to construct json according to the requirements of WeChat documents and send this string of json data to the WeChat server through post requests. The json includes various types of button events we created.

Menu JSON analysis and construction of corresponding beans
Custom menu json analysis (excluding personalized menus). The following code is an example given by WeChat documentation.
Click and view request examples

{
 "button":[
 { 
  "type":"click",
  "name":"今日歌曲",
  "key":"V1001_TODAY_MUSIC"
 },
 {
  "name":"菜单",
  "sub_button":[
  { 
   "type":"view",
   "name":"搜索",
   "url":"http://www.soso.com/"
  },
  {
   "type":"view",
   "name":"视频",
   "url":"http://v.qq.com/"
  },
  {
   "type":"click",
   "name":"赞一下我们",
   "key":"V1001_GOOD"
  }]
 }]
}
Copy after login

经过分析我们可以看到这串json数据分为三层:“”button”:[{…},{…}]”、“[{…},{{“name”:菜单,”sub_button”:[{},{}]}]”、“{“type”:”view”,”name:”:”视频”,”url”:”…”},{},{}”,可能看起来比较晕。
但是,如果我们能够联想起来现实中看到的微信菜单,就会好理解一点:一级:菜单(一个菜单),下包括一到三个父按钮;二级:父按钮(1~3个父按钮),下包括一到五个子按钮;三级:子按钮(1~5个子按钮)。
现在,我们可以看到json和我们理解的“菜单”可以一一对应起来了。现在重点是如何确认每一级的“级名”,在java中也就是对应的javabean对象。
同时,因为一级菜单下会有多个父按钮,所以是一个List<父菜单>的形式。父按钮下可能有多个子菜单,也是一个 List<子菜单>;但是,父按钮也有可能也是一个单独的可以响应的按钮。是一个单独的父按钮对象。子按钮就是一个单独的子按钮对象。
查看关于自定义菜单的参数说明,我们可以看到按钮分为一级按钮(“button”)和二级按钮(“sub_button”)。还有一些公用的数据类型,例如:菜单响应类型(“type”)、菜单标题(“name”)、click类型的参数(“key”)、view类型的参数(“url”)、media_id类型和view_limited类型的参数(“media_id”)。
•数据抽象(没有写setter,getter):

//按钮基类
public class BaseButton {
 private String type;
 private String name;
 private String key;
 private String url;
 private String media_id;
} 
//子按钮
public class SonButton extends BaseButton {
 private String sub_button;
}
//父按钮
public class FatherButton extends BaseButton {
private String button;//可能直接一个父按钮做响应
@SerializedName("sub_button")//为了保证Gson解析后子按钮的名字是“sub_button”,具体用法请搜索
private List<SonButton> sonButtons;//可能有多个子按钮
}
 
public class Menu {
@SerializedName("button")
private List<FatherButton> fatherButtons;
}
Copy after login

以上是完整的自定义菜单的分析以及对应javabean的构建。

对于个性化菜单,如果查看该部分的文档,会发现和自定义菜单大致相同,只是多个一个“配置”的json,格式是这样的:{“button”:[…],”matchrule”:{…}}。
我们发现,“匹配”这段json和“button”是同级的,分析和实现和上面基本等同,直接给出实现的javabean。

//匹配的json对应的json
public class MatchRule {
private String group_id;
private String sex;
private String client_platform_type;
private String country;
private String province;
private String city;
private String language;
}
 
//修改Menu.java
public class Menu {
@SerializedName("button")
private List<FatherButton> fatherButtons;
private MatchRule matchrule;
}
Copy after login


自定义菜单的实现
任务,我们实现所有微信按钮响应类型:
任务(注释:“m-0”表示父按钮;“m-n”表示第m个父按钮,第n个子按钮(m,n≠0)):1-0:名字:click,响应点击事件:点击推事件 。2-0:名字:父按钮2。2-1:名字:view,响应事件:跳转网页;2-2:名字:scancode_push,响应事件:扫码推事件;2-3:名字:scancode_waitmsg,响应事件:扫码推事件且弹出“消息接收中”提示框;2-4:名字:pic_sysphoto,响应事件
:弹出系统拍照发图。2-5:名字:pic_photo_or_album,响应事件:弹出拍照或者相册发图。3-0:名字:父按钮3。3-1:名字
:pic_weixin,响应事件:弹出微信相册发图器;3-2:名字:location_select,响应事件:弹出地理位置选择器;3-3:名字:media_id,响应事件:下发消息(除文本消息);3-4:名字:view_limited,响应事件:跳转图文消息url。

实现源码(引用的AccessTokenUtils.java在第一部分:工具类AccessTokenUtils的封装)

/*
 * 创建自定义菜单。
 */
@Test
public void createCommMenu() {
 String ACCESS_TOKEN = AccessTokenUtils.getAccessToken();// 获取AccessToken,AccessTokenUtils是封装好的类
 // 拼接api要求的httpsurl链接
 String urlString = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
   + ACCESS_TOKEN;
 try {
  // 创建一个url
  URL reqURL = new URL(urlString);
  // 拿取链接
  HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
    .openConnection();
  httpsConn.setDoOutput(true);
  // 取得该连接的输出流,以读取响应内容
  OutputStreamWriter osr = new OutputStreamWriter(
    httpsConn.getOutputStream());
  osr.write(getMenuJson());// 使用本类外部方法getMenuJson()
  osr.close();
 
  // 返回结果
  InputStreamReader isr = new InputStreamReader(
    httpsConn.getInputStream());
  // 读取服务器的响应内容并显示
  char[] chars = new char[1024];
  String reslut = "";
  int len;
  while ((len = isr.read(chars)) != -1) {
   reslut += new String(chars, 0, len);
  }
  System.out.println("返回结果:" + reslut);
  isr.close();
 } catch (IOException e) {
  e.printStackTrace();
 }
}
 
public String getMenuJson() {
 Gson gson = new Gson();// json处理工具
 
 Menu menu = new Menu();// 菜单类
 List<FatherButton> fatherButtons = new ArrayList<FatherButton>();// 菜单中的父按钮集合
 // -----------
 // 父按钮1
 FatherButton fb1 = new FatherButton();
 fb1.setName("click");
 fb1.setType("click");
 fb1.setKey("10");
 // -------------
 // 父按钮2
 FatherButton fb2 = new FatherButton();
 fb2.setName("父按钮2");
 List<SonButton> sonButtons2 = new ArrayList<SonButton>();// 子按钮的集合
 
 // 子按钮2-1
 SonButton sb21 = new SonButton();
 sb21.setName("view");
 sb21.setUrl("http://www.baidu.com");
 sb21.setType("view");
 // 子按钮2-2
 SonButton sb22 = new SonButton();
 sb22.setName("scancode_push");
 sb22.setType("scancode_push");
 sb22.setKey("22");
 // 子按钮2-3
 SonButton sb23 = new SonButton();
 sb23.setName("scancode_waitmsg");
 sb23.setType("scancode_waitmsg");
 sb23.setKey("23");
 // 子按钮2-4
 SonButton sb24 = new SonButton();
 sb24.setName("pic_sysphoto");
 sb24.setType("pic_sysphoto");
 sb24.setKey("24");
 // 子按钮2-5
 SonButton sb25 = new SonButton();
 sb25.setName("pic_photo_or_album");
 sb25.setType("pic_photo_or_album");
 sb25.setKey("25");
 
 // 添加子按钮到子按钮集合
 sonButtons2.add(sb21);
 sonButtons2.add(sb22);
 sonButtons2.add(sb23);
 sonButtons2.add(sb24);
 sonButtons2.add(sb25);
 
 // 将子按钮放到2-0父按钮集合
 fb2.setSonButtons(sonButtons2);
 
 // ------------------
 // 父按钮3
 FatherButton fb3 = new FatherButton();
 fb3.setName("父按钮3");
 List<SonButton> sonButtons3 = new ArrayList<SonButton>();
 
 // 子按钮3-1
 SonButton sb31 = new SonButton();
 sb31.setName("pic_weixin");
 sb31.setType("pic_weixin");
 sb31.setKey("31");
 // 子按钮3-2
 SonButton sb32 = new SonButton();
 sb32.setName("locatselect");
 sb32.setType("location_select");
 sb32.setKey("32");
 // // 子按钮3-3-->测试不了,因为要media_id。这需要调用素材id.
 // SonButton sb33 = new SonButton();
 // sb33.setName("media_id");
 // sb33.setType("media_id");
 // sb33.setMedia_id("???");
 // // 子按钮3-4-->测试不了,因为要media_id。这需要调用素材id.
 // SonButton sb34 = new SonButton();
 // sb34.setName("view_limited");
 // sb34.setType("view_limited");
 // sb34.setMedia_id("???");
 
 // 添加子按钮到子按钮队列
 sonButtons3.add(sb31);
 sonButtons3.add(sb32);
 // sonButtons3.add(sb33);
 // sonButtons3.add(sb34);
 
 // 将子按钮放到3-0父按钮队列
 fb3.setSonButtons(sonButtons3);
 // ---------------------
 
 // 将父按钮加入到父按钮集合
 fatherButtons.add(fb1);
 fatherButtons.add(fb2);
 fatherButtons.add(fb3);
 
 // 将父按钮队列加入到菜单栏
 menu.setFatherButtons(fatherButtons);
 String json = gson.toJson(menu);
 System.out.println(json);// 测试输出
 return json;
 
}
Copy after login


个性化菜单的实现
•任务:根据性别展示不同的按钮显示(可以根据性别、地区、分组手机操作系统等)
•修改代码一,因为是不同的微信后台实现,所以接口也不一样,不过还是POST请求,代码不用改,只要替换原来urlString即可。

// 拼接api要求的httpsurl链接
String urlString = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token="
   + ACCESS_TOKEN;
Copy after login


•修改代码二,只要创建一个MatchRule,设置匹配规则,然后将matchrule加入到menu便可以完成匹配规则。

// -----
// 从此处开始设置个性菜单
MatchRule matchrule = new MatchRule();
matchrule.setSex("2");// 男生
menu.setMatchrule(matchrule);
// ----
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多java微信开发API第四步 微信自定义个性化菜单实现相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template