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

Developing Wechat Portal menu Management of Wechat with C# and sample Analysis of submitting it to Wechat Server

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you about the C# development of Wechat Wechat portal menu management and submitted to the Wechat server example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The official Wechat account (including the service number and Subscription account) can customize the menu. In order to facilitate management, we usually manage and maintain the menu data locally. When we need to update them, just update them to the Wechat server. Based on this way, this paper introduces the operation of submitting menus to Wechat server in my Wechat portal platform management system. Wechat portal application management system, using MVC+EasyUI-based route, because most domain name servers can only support .NET 4.0, so MVC3,C#4.0 as the basis for development, basically can be deployed on any .NET server.

1. The requirements of Wechat menu and related interface design.

We can manage the menus of Wechat official account locally through the website and maintain the hierarchical relationship between them. As Wechat has relatively strict requirements for custom menus, the following are Wechat's requirements for custom menus:

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.

So according to our own agreement, do not cross the line, otherwise submit the menu to the server, may return some errors, these details, we create local menu management, just pay attention to it. In an early article, I also introduced some contents of the custom menu. I need to be able to review "C # Development of Wechat Portal and applications (6)-- Management Operation of Wechat Portal menu". This article mainly introduces the operation of submitting the menu to the server by calling the menu interface API introduced earlier in my platform management system.

According to the custom menu requirements of Wechat, I am in the management system, the menu of Wechat several basic interface design as follows.

The main menu management interface is shown below.

The interface design for adding menus is as follows

The modification interface for the Wechat menu is as follows

The Wechat menu definition is stored in the database. If you need to submit it to the Wechat server and take effect, you need to call the Wechat API API for processing. I added a processing method to submit to the server in the Controller controller of the page.

2. Submit the menu to the Wechat server

The above interfaces are mainly based on the properties of the Wechat menu to maintain and manage the menu. our ultimate goal is to put them on the server for us to deal with customer-related events.

For the operation of submitting the menu, we can submit it using JQuery's Ajax in the View page of MVC (as long as we add the corresponding processing to the controller, which will be described later). The interface script code is shown below.

/ bind the click event of the submit button function BindSubmitEvent () {$("# btnSubmit") .click (function () {$.messager.submit) ("submit menu confirmation", "are you sure you want to submit the menu to the Wechat server?" , function (action) {if (action) {/ / submit data $.ajax ({url:'/ Menu/UpdateWeixinMenu', type: 'post', dataType:' json' Success: function (data) {if (data.Success) {$.messager.alert (prompt, Wechat menu submitted successfully) } else {$.messager.alert ("prompt", "failed to submit Wechat menu:" + data.ErrorMessage) }, data:''});})

The red code above is the method we defined in the controller of MVC. We only need to use the POST method to call the controller method to submit the menu to the Wechat server. As for the details inside, we can move it to the controller or lower level to deal with it, the page does not need to involve too much logic.

The method code for the UpdateWeixinMenu of the Menu controller above is shown below (mainly based on the development model I introduced earlier).

/ / update Wechat menu / public ActionResult UpdateWeixinMenu () {string token = base.GetAccessToken (); MenuListJson menuJson = GetWeixinMenu (); IMenuApi menuApi = new MenuApi (); CommonResult result = menuApi.CreateMenu (token, menuJson); return ToJsonContent (result);}

The above methods are introduced here one by one. The main purpose of GetAccessToken is to obtain the access token of the current operation. Here, the operation can be cached with cache, otherwise the AccessToken can be frequently obtained. After reaching the specified number of times per day, it can no longer be used on the same day.

The GetWeixinMenu method, mainly for convenience, encapsulates the custom menu data for the construction of Wechat with a function, as shown below.

/ / generate Json data for Wechat menus / private MenuListJson GetWeixinMenu () {MenuListJson menuJson = new MenuListJson (); List menuList = BLLFactory.Instance.GetTree (); foreach (MenuNodeInfo info in menuList) {ButtonType type = (info.Type = = "click")? ButtonType.click: ButtonType.view; string value = (type = = ButtonType.click)? Info.Key: info.Url; MenuJson weiInfo = new MenuJson (info.Name, type, value); AddSubMenuButton (weiInfo, info.Children); menuJson.button.Add (weiInfo);} return menuJson } private void AddSubMenuButton (MenuJson menu, List menuList) {if (menuList.Count > 0) {menu.sub_button = new List ();} foreach (MenuNodeInfo info in menuList) {ButtonType type = (info.Type = = "click")? ButtonType.click: ButtonType.view; string value = (type = = ButtonType.click)? Info.Key: info.Url; MenuJson weiInfo = new MenuJson (info.Name, type, value); menu.sub_button.Add (weiInfo); AddSubMenuButton (weiInfo, info.Children);}}

The above code is to convert the locally stored MenuNodeInfo data into Wechat's custom menu entity MenuJson through recursive traversal, so that it is very convenient for us to call API. This function is mainly responsible for constructing the corresponding entity information. As for the matter of calling Wechat API to submit the menu, it is better to let API handle it himself, their code is shown below (that is, part of the code of the above function).

IMenuApi menuApi = new MenuApi (); CommonResult result = menuApi.CreateMenu (token, menuJson); return ToJsonContent (result)

The final result is to return a general result CommonResult, this result object, very convenient for script processing, if there is an error, then prompt the error, otherwise it is also convenient to determine the Boolean value, that is, the above page code script.

Success: function (data) {if (data.Success) {$.messager.alert ("prompt", "Wechat menu submitted successfully");} else {$.messager.alert ("prompt", "failed to submit Wechat menu:" + data.ErrorMessage);}}

Through the above parts of the code, we can implement the view interface of the foreground MVC, call the Wechat API encapsulated in the background, and realize the menu submission processing.

The above is all the contents of the article "Wechat Portal menu Management of Wechat developed by C# and sample Analysis submitted to Wechat Server". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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