


Detailed explanation of WeChat personalized menu development model
I have been working on the development of a WeChat custom menu in the past week. Overall, it is quite depressing.
Let’s first analyze a few interfaces:
1: Query interface; the query interface is divided into two, one is the ordinary query interface in development mode, it will only query what you pass Add the menus created by the interface and personalized menus (allowing different user groups of the official account to see different customized menus), but the menus you added through the WeChat management platform cannot be obtained. There is also an interface that can query all menus. This interface is a bit painful. If you first add a menu to the platform, and then call this interface in the background, you can get the menu, but the json returned by calling this interface is different from when adding. The format is different (that is, you cannot create a menu if you send it back to your json as it is to WeChat), and there are many kinds of menus added to the WeChat platform, and each type of returned json The formats are all different, and it is very troublesome to parse them, so I personally think that since I have chosen the developer mode, it is better to use the ordinary query interface.
2: Create an interface; the function of this interface, as the name suggests, is to create an interface, but this interface is not created on the basis of the original, but re-creates all the menus. Each time it is called, you need The remaining menus are processed into fixed-format json and then sent to WeChat, so if you want to delete them individually, then you can remove the menu you want to delete and change the rest into a json. The same is true for modifications. Replace the data you need to change, and then convert the entire menu into a json and send it to WeChat.
3: Delete interface; this interface is used to delete all menus, but it will not take effect immediately. After calling it, it seems that you will not see the effect until the next day, and during this period you still You can query the menu you created before.
My task is to integrate the addition, deletion, modification and query of the menu into the backend management. Here I will only talk about how to get out of my backend. The request to the WeChat interface returns a json string. My approach is Parse this string, write it into an object according to certain rules and add it to the list. Operate the list when adding, deleting or modifying, then convert the list back to json in the format required by WeChat, and call the WeChat interface to create the menu. (The creation interface is called for additions, deletions and modifications). My attributes in the object part are id (set the subscript of the list to id for easy operation), name (menu name), type (menu type, there are 10 types of WeChat custom menus), parent (parent menu name) ), SecendLvMenu (number of secondary menus), url (required for link menu), key (call push function), mediaid (
must set madia_id when calling materials), sort (sort number).
The following is the code for converting json to list. The json package here is Alibaba's fastjson package, which is quite easy to use:
public static List
List
obj=obj.getJSONObject("menu");
JSONObject a=new JSONObject();
int num=0;
if(obj!=null){
//Get the json array of button
JSONArray array=obj.getJSONArray(" button");
if(array!=null){
for(int i=0;i a= array.getJSONObject(i); //No type description is a first-level menu with a second-level menu type if(a.get("type")==null){ weixinMenu menu=new weixinMenu(); menu.setId(num); menu.setSort(num); num++; menu.setName(a.getString("name"));//Set the subscript, set the sort number and store it in the list //Get the second-level menu under the first-level menu JSONArray sub=a.getJSONArray("sub_button"); //The number of secondary menus, since the upper limit of secondary menus is 5, it is easy to judge when adding a number menu.setSecendLvMenuNum(sub.size()); menulist.add(menu); JSONObject b=new JSONObject(); for(int j =0;j weixinMenu menu2=new weixinMenu(); b=sub.getJSONObject(j); menu2 .setId(num); menu2.setSort(num); num++; menu2.setName(b.getString("name")); menu2.setParent(a.getString("name"));//The parent menu is the name of the first-level menu just now menu2.setType(b.getString("type")); menu2.setUrl(b.getString("url")); menu2.setKey(b.getString("key")); menu2.setMediaId(b .getString("media_id")); menulist.add(menu2); } }else{//Otherwise it is a first-level menu without a second-level menu , click to jump directly or trigger the corresponding event weixinMenu menu=new weixinMenu(); menu.setId(num); menu.setSort(num); num++; menu.setName(a.getString("name")); menu.setParent(null); menu.setSecendLvMenuNum( 0); menu.setType(a.getString("type")); menu.setUrl(a.getString("url")); menu.setKey(a.getString("key")); menu.setMediaId(a.getString("media_id")); menulist.add(menu); } } } } return menulist; } Then convert list to weixinJson. Uploading WeChat’s json only requires the button part: public static JSONObject listToWxJson(List JSONArray array=new JSONArray(); weixinMenu menu=new weixinMenu(); int size=list.size(); for(int i=0;i menu=list.get(i); //If there is no second-level menu and no parent menu, it is a first-level menu with no second-level type if(menu.getSecendLvMenuNum( )==0&&menu.getParent()==null){ Map map.put("name" , menu.getName()); map.put("type", menu.getType()); if(menu.getUrl()!=null){ map.put("url", menu.getUrl()); } if(menu.getKey()!=null){ map .put("key", menu.getKey()); } if(menu.getMediaId()!=null){ map.put(" media_id", menu.getMediaId()); } array.add(map); }else if(menu.getParent()==null){ A menu without a parent is a first-level menu with a second level JSONObject obj=new JSONObject(); JSONArray sub=new JSONArray(); obj.put( "name",menu.getName()); weixinMenu submenu=new weixinMenu(); //Loop through the entire list and use name matching to find the second-level menu under the first-level menu for(int j=0;j submenu=list.get(j); Map if((menu.getName()).equals(submenu.getParent())){ map=new HashMap map.put("name", submenu.getName()); map.put("type", submenu.getType()); if(submenu.getUrl() !=null){ map.put("url", submenu.getUrl()); } if(submenu.getKey()!=null) { map.put("key", submenu.getKey()); } if(submenu.getMediaId()!=null){ map.put("media_id", submenu.getMediaId()); } sub.add(map); } } obj.put("sub_button",sub); array.add(obj); } } Map map.put("button",array); //Finally convert the map directly into json return (JSONObject)JSONObject.toJSON(map); } This set is basically fixed, and it can be parsed into list, after the operation is completed, convert json, and the subsequent operations will be much easier. The above is the detailed content of Detailed explanation of WeChat personalized menu development model. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

With the popularity of mobile Internet, more and more people are using WeChat as a social software, and the WeChat open platform has also brought many opportunities to developers. In recent years, with the development of artificial intelligence technology, speech recognition technology has gradually become one of the popular technologies in mobile terminal development. In WeChat development, how to implement speech recognition has become a concern for many developers. This article will introduce how to use PHP to develop WeChat applications to implement speech recognition functions. 1. Principles of Speech Recognition Before introducing how to implement speech recognition, let us first understand the language

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform
