Home  >  Article  >  WeChat Applet  >  Detailed explanation of WeChat personalized menu development model

Detailed explanation of WeChat personalized menu development model

高洛峰
高洛峰Original
2017-03-22 16:15:061380browse

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 jsonToList(JSONObject obj){

List menulist=new ArrayList();

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 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=new HashMap();

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 map=null ;

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=new HashMap();

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!

Statement:
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