C # develop WeChat portals and applications (6) -- manage WeChat portal menus

Source: Internet
Author: User
Tags tojson

1. The basic information portal menu of the menu. Generally, both the service number and subscription number can have the development of this module, but the subscription number seems to have to be available only after authentication, the service number is available without authentication. This menu can have the editing mode and development mode. The editing mode is to edit the menu on the portal platform. The development mode is, that is, you can customize menus by calling APIs and POST data to the server to generate corresponding menu content. This document describes menu management operations based on the development mode. The custom menu can help users enrich the interface of public accounts and help them better understand the functions of public accounts. Currently, a custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus. A level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. The extra content will be replaced. Currently, the custom menu interface supports two types of buttons: click: After you click the click type button, the server pushes the structure of the Message Type as event to the developer (reference message Interface Guide) through the message interface, and carries the key value filled by the developer in the button, developers can interact with users through custom key values. view: After a user clicks the view type button, the client will open the url value (Web Page Link) filled in by the developer in the button ), to open a webpage, we recommend that you use the webpage authorization interface to obtain the user's personal login information. The data submitted by the menu is a Json data string. Its official example data is as follows. Copy the code {"button": [{"type": "click", "name": "Today's song", "key": "V1001_TODAY_MUSIC" },{ "type ": "click", "name": "", "key": "V1001_TODAY_SINGER" },{ "name": "menu", "sub_button ": [{"type": "view", "name": "Search", "url": "http://www.soso.com/" },{ "type": "view ", "name": "video", "url": "http://v.qq.com/"}, {"type": "click", "name": "like us ", "key": "V1001_GOOD"}]} copy the code from the above we can see that different types of menus have different fields, such Url attribute. If type is click, the key attribute is available. The menu can have the sub_button attribute. In general, in order to construct the corresponding menu entity information, it can be analyzed without a moment. 2. The menu object class definition I have read some interface development code, divide the menu into multiple object classes, specify the inheritance relationship, and then configure their properties separately, the approximate relationship is as follows. This multi-layer relationship inheritance method can solve the problem, but I don't think it is an elegant solution. In fact, combined with the Attribute configuration of Json. NET itself, you can specify the content that is null when the serial number is a Json string and it is not displayed. [JsonProperty (NullValueHandling = NullValueHandling. ignore)] with this attribute, we can define more attributes of the object class information in the menu, you can combine the url and key of the View and Click menu attributes. Copy the code /// <summary> /// basic menu information /// </summary> public class MenuInfo {/// <summary> /// button description, button name, no more than 16 bytes, sub-menu no more than 40 bytes // </summary> public string name {get; set ;} /// <summary> /// button type (click or view) /// </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public string type {get; set ;}/// <summary> // button KEY value, used for message interface (event type) push, no more than 128 bytes // </summary> [JsonProperty (NullValu EHandling = NullValueHandling. ignore)] public string key {get; set ;}/// <summary> // webpage link. You can click the button to open the link, no more than 256 bytes // </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public string url {get; set ;}/// <summary> // the array of subbuttons. The number of buttons should be 2 ~ Five /// </summary> [JsonProperty (NullValueHandling = NullValueHandling. ignore)] public List <MenuInfo> sub_button {get; set ;}....... copy the Code. However, it is not difficult to specify different attribute types for different types of information. In case I set the key attribute in the View menu, what should we do? The solution is to define several constructor functions to construct different menu information, as shown below: assign values to constructor of different properties for different menu types. Copy the code /// <summary> /// parameterized constructor /// </summary> /// <param name = "name"> button name </param> // /<param name = "buttonType"> menu button type </param> // <param name = "value"> Click ), or connect URL (View) </param> public MenuInfo (string name, ButtonType buttonType, string value) {this. name = name; this. type = buttonType. toString (); if (buttonType = ButtonType. click) {this. key = value;} else if (buttonType = ButtonType. vie W) {this. url = value ;}} the code has been copied. Another problem is that the submenu, that is, the attribute sub_button, is dispensable. If yes, you need to specify the Name attribute, you can add its sub_button set object, so we are adding a constructor to construct the object information of the sub-menu. Copy the code /// <summary> /// parameterized constructor, used to construct a sub-menu // </summary> /// <param name = "name"> button name </param> /// <param name = "sub_button"> sub-menu menu set </param> public MenuInfo (string name, IEnumerable <MenuInfo> sub_button) {this. name = name; this. sub_button = new List <MenuInfo> (); this. sub_button.AddRange (sub_button);} copy the code. Because only the attribute content of Name and sub_button is specified, if the other content is null, the naturally constructed Json will not contain them, which is perfect! To obtain menu information, we also need to define two object objects, as shown below. Copy the code /// <summary> /// Json String object of the menu /// </summary> public class MenuJson {public List <MenuInfo> button {get; set ;} public MenuJson () {button = new List <MenuInfo> ();}} /// <summary> // Json object in the menu list /// </summary> public class MenuListJson {public MenuJson menu {get; set ;}} copy code 3. The menu management operation interface is implemented from the definition. We can see that we can obtain menu information, create menus, and delete menus through the API, then we define their interfaces as follows. Copy the code /// <summary> /// menu related operations /// </summary> public interface IMenuApi {/// <summary> /// obtain menu data // // </summary> /// <param name = "accessToken"> call interface creden< </param> /// <returns> </returns> MenuJson GetMenu (string accessToken ); /// <summary> /// create a menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> /// <param name = "menuJson"> menu object </param> // <returns> </returns> CommonResult CreateMenu (string CcessToken, MenuJson menuJson ); /// <summary> /// Delete menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> /// <returns> </returns> CommonResult DeleteMenu (string accessToken );} the specific implementation of copying the code to obtain menu information is as follows. Copy the code /// <summary> /// obtain menu data /// </summary> /// <param name = "accessToken"> call interface creden< </param>/ // <returns> </returns> public MenuJson GetMenu (string accessToken) {MenuJson menu = null; var url = string. format ("https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = {0} ", accessToken); MenuListJson list = JsonHelper <MenuListJson>. ConvertJson (url); if (list! = Null) {menu = list. menu;} return menu;} copy the code here to convert the returned Json data to the entity information we need in one step. The call code is as follows. Copy the code private void btnGetMenuJson_Click (object sender, EventArgs e) {IMenuApi menuBLL = new MenuApi (); MenuJson menu = menuBLL. GetMenu (token); if (menu! = Null) {Console. WriteLine (menu. ToJson () ;}} copy the code to create and delete a menu object. Copy the code /// <summary> /// create a menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> // /<param name = "menuJson"> menu object </param> // <returns> </returns> public CommonResult CreateMenu (string accessToken, menuJson menuJson) {var url = string. format ("https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = {0} ", accessToken); string postData = menuJson. toJson (); return Helper. getExecuteResult (url, postData );} /// <summary> /// Delete menu /// </summary> /// <param name = "accessToken"> call interface creden< </param> /// <returns> </returns> public CommonResult DeleteMenu (string accessToken) {var url = string. format ("https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = {0} ", accessToken); return Helper. getExecuteResult (url);} copy the Code and some people may ask, if you simplified the entity class, it is quite troublesome to create a menu, in particular, how should we construct the corresponding information? Didn't we introduce different constructor functions in the previous section? They just get it done. You don't have to write down too many entity classes and their inheritance relationships to process menu information. Copy the code private void btnCreateMenu_Click (object sender, EventArgs e) {MenuInfo productInfo = new MenuInfo ("software product", new MenuInfo [] {new MenuInfo ("Patient Data Management System ", buttonType. click, "patient"), new MenuInfo ("Customer Relationship Management System", ButtonType. click, "crm"), new MenuInfo ("Hotel Management System", ButtonType. click, "hotel"), new MenuInfo ("Water Delivery Management System", ButtonType. click, "water")}); MenuInfo frameworkInfo = new MenuInfo ("framework product", new MenuInfo [] {New MenuInfo ("Win Development Framework", ButtonType. click, "win"), new MenuInfo ("WCF Development Framework", ButtonType. click, "wcf"), new MenuInfo ("Hybrid Framework", ButtonType. click, "mix"), new MenuInfo ("Web Development Framework", ButtonType. click, "web"), new MenuInfo ("code generation tool", ButtonType. click, "database2sharp")}); MenuInfo relatedInfo = new MenuInfo ("related link", new MenuInfo [] {new MenuInfo ("Company Introduction", ButtonType. click, "Event_Company"), new MenuInfo ("Official Website ", ButtonType. view, "http://www.iqidi.com"), new MenuInfo ("Recommendations", ButtonType. click, "Event_Suggestion"), new MenuInfo ("Contact customer service", ButtonType. click, "Event_Contact"), new MenuInfo ("email", ButtonType. view, "http://mail.qq.com/cgi-bin/qm_share? T = qm_mailme & email = S31yfX15fn8LOjplKCQm ")}); MenuJson menuJson = new MenuJson (); menuJson. button. addRange (new MenuInfo [] {productInfo, frameworkInfo, relatedInfo}); // Console. writeLine (menuJson. toJson (); if (MessageUtil. showYesNoAndWarning ("are you sure you want to create a menu?") = System. windows. forms. dialogResult. yes) {IMenuApi menuBLL = new MenuApi (); CommonResult result = menuBLL. createMenu (token, menuJson); Console. WriteLine ("create menu:" + (result. Success? "Successful": "failed:" + result. errorMessage) ;}} copy the Code. This is the menu operation in my portal. For the specific effect, follow my portal: Guangzhou aiqidi. You can also scan the following QR code to learn more.

Related Article

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.