Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the menu Management Operation of Wechat Portal developed by C#

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you the content of a sample analysis of the menu management operation of Wechat's Wechat portal developed by C#. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Basic information of the menu

Wechat portal menu, the general service number and Subscription account can have the development of this module, but Subscription account seems to need authentication to have, and the service number does not need authentication to have it. This menu can have editing mode and development mode, the editing mode is mainly to edit the menu on the platform of Wechat portal; and the development mode is that users can customize the menu by calling Wechat's API, and then send the POST data to Wechat server to generate the corresponding menu content. This paper mainly introduces the menu management operation based on the development mode.

Custom menus can help official accounts enrich the interface and enable users to understand the functions of official accounts better and faster. At present, the custom menu includes up to three first-level menus, and each first-level menu contains up to five second-level menus. The first-level menu has a maximum of 4 Chinese characters, and the second-level menu has up to 7 Chinese characters. Instead. Currently, the custom menu interface can implement two types of buttons, as follows:

Click: after the user clicks the click type button, the Wechat server will push the structure with the message type event to the developer through the message interface (refer to the message interface guide). With the key value entered by the developer in the button, the developer can interact with the user through the custom key value. View: after the user clicks the view button, the Wechat client will open the URL value (that is, the web link) entered by the developer in the button to open the web page. It is recommended to combine with the API for web page authorization to obtain the user's basic information to obtain the user's login personal information.

The data submitted by the menu is itself a Json data string, and its official example data is shown below.

{"button": [{"type": "click", "name": "Today's song", "key": "V1001_TODAY_MUSIC"}, {"type": "click", "name": "singer profile", "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 ":" praise us "," key ":" V1001_GOOD "}]}

From the above, we can see that different type types of menus have different field contents. For example, if type is view, there is a url attribute, while type is click, there is a key attribute. While menus can have submenu sub_button attributes, generally speaking, in order to construct the corresponding menu entity class information, it can not be analyzed at once.

2. Entity class definition of menu

I have seen some Wechat interface development code, divided the menu into many entity classes, specified the inheritance relationship, and then configured their properties separately, the approximate relationship is as follows.

This inheritance of multi-tier relationships can solve the problem, but I don't think it's an elegant solution. In fact, combined with the Attribute attribute configuration of Json.NET itself, you can specify that the empty content will not be displayed when the sequence number is a Json string.

[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]

With this attribute, we can uniformly define more attributes of the entity class information of the menu, and we can combine the url and key of menu properties of type View and type Click.

/ basic menu information / public class MenuInfo {/ button description, button name, no more than 16 bytes, and submenu no more than 40 bytes / public string name {get; set } / Button type (click or view) / [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] public string type {get; set } / button key value, used for message API (event type) push, no more than 128bytes / [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] public string key {get; set } / links to web pages. Users can click the button to open the link, with no more than 256 bytes / [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] public string url {get; set } / Sub-button array, the number of buttons should be 2'5 / [JsonProperty (NullValueHandling = NullValueHandling.Ignore)] public List sub_button {get; set;}.

However, with so much information, I need to specify different attribute types for different types, which is not troublesome. What if I set the key property in the menu of View type?

The solution is to define several constructors that are used to construct different menu information, as shown below is the constructor that assigns values to different properties for different types of menus.

/ parameterized constructor / button name / menu button type / button key value (Click), or connect URL (View) 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.view) {this.url = value;}}

Well, there is another problem, the submenu, that is, the property sub_button is optional, if there is, we need to specify the Name property and add its sub_button collection object, so we are adding a constructor to construct the object information of the submenu.

/ / parameterized constructor for constructing submenu / Button name / submenu collection public MenuInfo (string name, IEnumerable sub_button) {this.name = name; this.sub_button = new List (); this.sub_button.AddRange (sub_button) }

Since you only specify the attribute content of Name and sub_button, if the other content is null, the naturally constructed Json does not contain them, which is perfect!

In order to get the menu information, we also need to define two entity objects, as shown below.

/ Json string object of the menu / public class MenuJson {public List button {get; set;} public MenuJson () {button = new List ();}} / Json object of the menu list / public class MenuListJson {public MenuJson menu {get; set }} 3. Interface implementation of menu management operation

We can see from the definition of Wechat that we can get menu information, create menus and delete menus through API, so the interfaces we define them are as follows.

/ related operation of menu / public interface IMenuApi {/ get menu data / call API credential / MenuJson GetMenu (string accessToken) / create menu / call API credential / menu object / CommonResult CreateMenu (string accessToken, MenuJson menuJson) / / delete menu / call API credential / CommonResult DeleteMenu (string accessToken);}

The specific implementation of obtaining menu information is as follows.

/ get menu data / call API credential / public MenuJson GetMenu (string accessToken) {MenuJson menu = null; var url = string.Format ("http://www.php.cn/{0}", accessToken); MenuListJson list = JsonHelper.ConvertJson (url) If (list! = null) {menu = list.menu;} return menu;}

Here is the return of Json data, unified conversion into the physical information we need, one step in place.

The calling code is as follows.

Private void btnGetMenuJson_Click (object sender, EventArgs e) {IMenuApi menuBLL = new MenuApi (); MenuJson menu = menuBLL.GetMenu (token); if (menu! = null) {Console.WriteLine (menu.ToJson ());}}

The operation to create and delete menu objects is as follows.

/ create menu / call API credential / menu object / public CommonResult CreateMenu (string accessToken, MenuJson menuJson) {var url = string.Format ("http://www.php.cn/{0}", accessToken); string postData = menuJson.ToJson () Return Helper.GetExecuteResult (url, postData); delete menu / call API credential / public CommonResult DeleteMenu (string accessToken) {var url = string.Format ("http://www.php.cn/{0}", accessToken) Return Helper.GetExecuteResult (url);}

Seeing here, some people may ask, you have simplified the entity class, so is it troublesome to create a menu, especially how to construct the corresponding information? Didn't we introduce different constructors, which can be easily done through them, without having to write down too many entity classes and their inheritance relationships to deal with menu information.

Private void btnCreateMenu_Click (object sender, EventArgs e) {MenuInfo productInfo = new MenuInfo ("Software products", 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 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 links", new MenuInfo [] {new MenuInfo ("Company introduction", ButtonType.click, "Event_Company"), new MenuInfo ("official website", ButtonType.view, "http://www.php.cn/"), new MenuInfo" ("suggestions", ButtonType.click, "Event_Suggestion") New MenuInfo ("contact customer Service", ButtonType.click, "Event_Contact"), new MenuInfo ("email", ButtonType.view, "http://www.php.cn/")}) 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? "success": "failure:" + result.ErrorMessage);}} Thank you for reading! On the "C# development of Wechat Wechat portal menu management example analysis," this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report