Java wechat development API step 4 implementation of wechat custom personalized menu

How wechat realizes customized personalized menu is introduced below

1、 Please refer to the previous two articles for detailed description of the global description.

II This paper explains that this paper is divided into five parts: * encapsulation of tool class accesstokenutils * reading and parsing of custom menu and personalized menu documents * analysis of menu JSON and construction of corresponding bean * implementation of custom menu * implementation of personalized menu all types of wechat custom menu will be demonstrated. At the end of this paper, all demonstrations including the first four articles will be given Source code

The encapsulation of the tool class accesstokenutils has been described in detail in the above. The accesstokenutils encapsulated after processing are directly given here. The implementation principle and document reading will not be given. AccessTokenUtils. java

Custom menu and personalized menu document reading analysis. Custom menu, custom menu creation interface, custom menu query interface, custom menu delete interface, custom menu event push, personalized menu interface, access to official account menu configuration

• document address: http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html • the official website document explains as follows: * the user-defined menu interface can realize various 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 customizing menus) * request example of click and view {button ": [...]} (about custom menu) * parameter description (about custom menu) * create 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) * the developer can set the menu seen by the user (about personalized menu) through the following conditions: 1. User grouping (the developer's business needs can be completed with the help of user grouping) 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's a familiar post request again, but it seems vague about the call. We just know that we need to use the parameter "access_token = access_token", which we obtained in the last article. If we replace the "access_token" in the request address given by the wechat document with our own access_ Token, visit the website and you will see "{" errCode ": 44002," errmsg ":" empty post data hint: [gdveda0984vr23]} ". Roughly, an empty post request data. Therefore, we should pass the parameters to the wechat server in the form of post request. The format of the parameters is also given below the document: {"button": [...]}. Therefore, we should pass the parameters to the wechat server in this format. · as for 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. By simply viewing this part of the document, we can see that these eight parameters are used for matching and filtering personalized menus. · now, we need to construct JSON according to the requirements of wechat documents and send this string of JSON data to wechat server through post request. JSON includes various types of button events we create.

Menu JSON analysis and build corresponding bean custom menu JSON analysis (excluding personalized menu). The following code is an example given by wechat documents. Click and view request examples

After analysis, we can see that this string of JSON data is divided into three layers: "button": [{...}, {...}] "," [{...}, {"name": menu, "sub_button": [{}, {}]}] "," {type ":" view "," Name: ":" video "," URL ":"...}, {}, {} ", which may look dizzy. However, if we can think of the wechat menu we see in reality, It will be easier to understand: Level 1: menu (a menu), including one to three parent buttons; Level 2: parent buttons (1 ~ 3 parent buttons), including one to five child buttons; Level 3: child buttons (1 ~ 5 child buttons). Now we can see that JSON and the "menu" we understand can correspond one by one. Now the focus is on how to confirm the "level name" of each level , in Java, that is, the corresponding JavaBean object. At the same time, because there are multiple parent buttons under the first level menu, it is in the form of list < parent menu >. There may be multiple submenus under the parent button, which is also a list < submenu >; However, the parent button may also be a separate button that can respond. Is a separate parent button object. A sub button is a separate sub button object. View the parameter description of the custom menu, We can see that buttons are divided into primary buttons ("buttons") and secondary buttons ("sub_buttons"). There are also some common data types, such as menu response type ("type"), menu title ("name"), click type parameter ("key"), view type parameter ("URL"), media_id type and view_limited type parameter ("media_id") )。 • Data abstraction (setter and getter are not written):

The above is a complete analysis of the user-defined menu and the construction of the corresponding java bean.

For personalized menus, if you view the documents in this section, you will find that they are roughly the same as custom menus, except for multiple "configuration" JSON. The format is as follows: {button ": [...]," matchrule ": {...}}. We found that the "match" JSON and "button" are of the same level. The analysis and implementation are basically the same as those above, and the implemented JavaBeans are given directly.

Customize the implementation task of the menu, We implement all wechat button response types: task (Note: "M-0" represents the parent button; "M-N" represents the m-th parent button and the n-th child button (m, n ≠ 0)): 1-0: Name: click, response click event: click push event. 2-0: Name: parent button 2. 2-1: Name: view, response event: jump to web page; 2-2: Name: scancode_push, response event: scan push event; 2-3: Name: scancode_waitmsg, response event: scan push event and "message receiving" pops up Prompt box; 2-4: Name: PIC_ Sysphoto, response event: the system will pop up to take photos and send pictures. 2-5: Name: PIC_ photo_ or_ Album, response event: pop up to take photos or send pictures from photo albums. 3-0: Name: parent button 3. 3-1: Name: PIC_ Weixin, response event: pop up wechat photo album poster; 3-2: Name: Location_ Select, response event: pop up the geographic location selector; 3-3: Name: Media_ ID, response event: send message (except text message); 3-4: Name: view_limited, response event: jump to graphic message URL.

Implementation source code (the referenced accesstokenutils. Java is in part I: encapsulation of tool class accesstokenutils)

Implementation of personalized menu • task: display different buttons according to gender (you can group mobile phone operating system according to gender, region, etc.) • modify code 1. Because it is implemented in different wechat background, the interface is also different. However, it is still a post request, and the code does not need to be changed. Just replace the original urlstring.

• modify code 2. Just create a matchrule, set the matching rule, and then add the matchrule to the menu to complete the matching rule.

Source code download: http://xiazai.jb51.net/201606/yuanma/WeixinApi (jb51.net). rar

This article has been sorted into Android wechat development tutorial summary and Java wechat development tutorial summary. You are welcome to learn and read.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>