Detailed description of WeChat personalized menu development mode

Source: Internet
Author: User
This article explains how to develop a custom menu in the personalized menu development mode. In general, it is quite depressing.

Analyze several interfaces first:

1: query interface. There are two query interfaces. one is a common query interface in development mode, he will only query the menus and personalized menus created through the added interfaces (so that different user groups of public accounts can see different custom menus ), you cannot obtain the menus you have added through the management platform. Another interface is an interface that can query all menus. this interface is a bit painful. if you add a menu on the platform and then call this interface in the background, you can obtain this menu, however, the json returned by calling this interface is different from the added format (that is, the original json model that you send back to you cannot be created after you create the menu ), in addition, there are many types of menus added on the platform, and each type of returned json format is different. it is very troublesome to parse the menus, therefore, I personally think that since the developer mode is selected, it is better to use the common query interface.

2: create an interface. the function of this interface, as its name implies, is to create an interface, but this interface is not created based on the original interface, but re-created all the menus, each call is to process the menus you need to leave into a fixed json format and then pass it to them. so if you want to delete a single menu, you can remove the menu you want to delete, change the remaining data to a json file. replace the data you want to modify and convert all the menus to a json file.

3: Delete interface. this interface is used to delete all menus, but it does not take effect immediately. after calling it, it seems that it will take the next day to see the effect, in addition, you can still query the menus you created earlier.

My task is to integrate the addition, deletion, modification, and query of menus into the background management. here we only talk about the background method. the requested interface returns a json string, my practice is to parse this string, write an object according to certain rules, and add it to the list. when adding, deleting, modifying, and operating the list, then, convert the list to the required json format, and call the interface to create a menu (add, delete, modify, and call the creation interface ). My attributes of the object are id (set the subscript of list to id for easy operation), name (menu name), type (menu type, there are 10 types of custom menus), parent (parent menu name), SecendLvMenu (number of list menus), url (required for link menus ), key (call push function), mediaid (

Set madia_id and sort when calling the clip ).

The following is the code for converting json to list. here, the json package is Alibaba's fastjson package, which is quite useful:

Public static List JsonToList (JSONObject obj ){

List Menulist = new ArrayList ();

Obj = obj. getJSONObject ("menu ");

JSONObject a = new JSONObject ();

Int num = 0;

If (obj! = Null ){

// Obtain the json Array of the button

JSONArray array = obj. getJSONArray ("button ");

If (array! = Null ){

For (int I = 0; I

A = array. getJSONObject (I );

// No type indicates that a level-1 menu with level-2 menu

If (a. get ("type") = null ){

WeixinMenu menu = new weixinMenu ();

Menu. setId (num );

Menu. setSort (num );

Num ++;

Menu. setName (a. getString ("name"); // you can specify the subscript to save the order number to the list.

// Obtain the level-2 menu under the level-1 Menu

JSONArray sub = a. getJSONArray ("sub_button ");

// The number of level-2 Menus. Since the maximum number of level-2 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 level-1 menu.

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 level-1 menu without a level-2 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 the list is converted to weixinJson, and the uploaded 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 level-2 menu and no parent menu, there is no level-1 menu of level-2 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) {no parent menu is a level-2 menu

JSONObject obj = new JSONObject ();

JSONArray sub = new JSONArray ();

Obj. put ("name", menu. getName ());

WeixinMenu submenu = new weixinMenu ();

// Cyclically traverse the entire list and use name matching to find the level-2 menu under the level-1 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 );

// Convert the map directly to json

Return (JSONObject) JSONObject. toJSON (map );

}

This basic set is fixed, and the menu can be parsed into a list. after the operation, the json is converted, and the subsequent operations are much easier.

The above is a detailed description of the personalized menu development mode. For more information, see other related articles on php Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.