Custom menu, custom menu
Although the self-written methods for constructing custom menus are somewhat repetitive, they feel clearer after all! Do you have any good comments!
SubButton (sub menu) and ParentButton (parent menu ).
public class ParentButton{ public string name; public List<SubButton> list;}public class SubButton{ public string name; public string type; public string url; public string key;}
The constructed menu is passed to getmmmmenujsondata to obtain the correct Json format data.
Public string GetCustomMenuJsonData (List <ParentButton> list) {string jsonStr = ""; string str = ""; if (list! = Null & list. Count> 0) {foreach (ParentButton parentButton in list) {if (parentButton! = Null) {string subStr = ""; if (parentButton. list! = Null & parentButton. list. Count> 0) {foreach (SubButton button in parentButton. list) {if (button! = Null) {if (subStr! = "") {SubStr + = ",";} if (button. type = "click") {subStr + = "{\" type \ ": \" click \ ", \" name \ ": \" "+ button. name + "\", \ "key \": \ "" + button. key + "\"} ";} else {subStr + =" {\ "type \": \ "view \", \ "name \": \ "" + button. name + "\", \ "url \": \ "" + button. url + "\"} ";}}} if (str! = "") {Str + = ",";} str + = "{\" name \ ": \" "+ parentButton. name + "\", \ "sub_button \": ["+ subStr +"]} ";}} jsonStr = (" {\ "button \": ["+ str +"]} ");} return jsonStr ;}View Code
Example:
Publish List <ParentButton> Centers () {ParentButton button1 = new ParentButton (); List <SubButton> subButtonList1 = new List <SubButton> (); SubButton subButton = new SubButton (); subButton. name = "Baidu"; subButton. type = "view"; subButton. url = "www.baidu.com"; subButtonList1.Add (subButton); subButton = new SubButton (); subButton. name = "google"; subButton. type = "view"; subButton. url = "www.google.com"; subButtonList1.Add (subButton); button1.name = "Search"; button1.list = subButtonList1; ParentButton button2 = new ParentButton (); button2.name = "other "; list <ParentButton> list = new List <ParentButton> (); list. add (button1); list. add (button2); return list ;}View Code
After obtaining json data, use
Https://api.wexin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = AppId & secret = AppSecret
Obtain the accesstoken, and then use
Https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = AccessToken
Submit the json data of the custom menu to generate the menu!