国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. Basic information of the menu
2. Menu entity class definition
3. Interface implementation of menu management operations
Home WeChat Applet WeChat Development C# develops WeChat portal and applies management operations of WeChat portal menu

C# develops WeChat portal and applies management operations of WeChat portal menu

Mar 31, 2017 pm 03:45 PM

The previous articles continued my own technical exploration and related experience summary of C# developing WeChat portals and applications, and continued to explore WeChatAPI and share related technologies. On the one hand The purpose is to interact and communicate with everyone on this aspect. On the other hand, we are also concentrating on developing the underlying technology of WeChat applications and consolidating the basic modules so that they can be used in future applications. This essay continues to introduce the management operations of the WeChat portal menu.

1. Basic information of the menu

WeChat portal menu, generally both service accounts and subscription accounts can have the development of this module, but the subscription account seems to need to be authenticated before it can be owned, and the service account You can have it without certification. This menu can have edit mode and development mode. The editing mode is mainly on the WeChat portal platform On the menu, edit the menu; in the development mode, users can customize and develop the menu by calling WeChat's API and POST data to the WeChat server to generate the corresponding menu content. This article mainly introduces the menu management operations based on the development mode.

Custom menu can help the official account enrich the interface, allowing users to better and faster understand the functions of the official account. Currently, the custom menu includes up to 3 first-level menus, one for each. The first-level menu contains up to 5 Second-level menu. The first-level menu can contain up to 4 Chinese characters, and the second-level menu can contain up to 7 Chinese characters. The extra parts will be replaced by "...". Interface can implement two types of buttons, as follows:

click:
用戶點擊click類型按鈕后,微信服務器會通過消息接口推送消息類型為
event????
的結(jié)構(gòu)給開發(fā)者(參考消息接口指南),并且?guī)习粹o中開發(fā)者填寫的key值,開發(fā)者可以通過自定義的key值與用戶進行交互;
view:
用戶點擊view類型按鈕后,微信客戶端將會打開開發(fā)者在按鈕中填寫的url值????
(即網(wǎng)頁鏈接),達到打開網(wǎng)頁的目的,建議與網(wǎng)頁授權(quán)獲取用戶基本信息接口結(jié)合,獲得用戶的登入個人信息。

The data submitted by the menu itself is a Json data character String , its official example data is as follows.

 {
     "button":[
     {    
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":"歌手簡介",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":"菜單",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"視頻",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"贊一下我們",
               "key":"V1001_GOOD"
            }]
       }]
 }

From the above we can see that different types of menus have different field contents, such as the type of view has url Attribute , and the type is click, there is a key attribute. The menu can have a sub-menu sub_button attribute. Generally speaking, in order to construct the corresponding menu entity class information, it cannot be analyzed at once

.

I have seen some WeChat interface development codes. The menu is divided into several entity classes, specifying the inheritance relationship, and then corresponding They configure attributes, and the approximate relationship is as follows.

C# develops WeChat portal and applies management operations of WeChat portal menu

This multi-layer relationship inheritance method can solve the problem, but I don't think it is an elegant solution. In fact, combined with Json.NET's own Attribute attribute configuration, you can specify that those empty contents will not be displayed when the serial number is a Json string.

[JsonProperty( NullValueHandling = NullValueHandling.Ignore)]

With this attribute, we can define it uniformly. The entity class information of the menu has more attributes. You can merge the url and key of the menu attributes of the View type and Click type.

/// <summary>
    /// 菜單基本信息
    /// </summary>
    public class MenuInfo
    {
        /// <summary>
        /// 按鈕描述,既按鈕名字,不超過16個字節(jié),子菜單不超過40個字節(jié)
        /// </summary>
        public string name { get; set; }

        /// <summary>
        /// 按鈕類型(click或view)
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string type { get; set; }

        /// <summary>
        /// 按鈕KEY值,用于消息接口(event類型)推送,不超過128字節(jié)
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string key { get; set; }

        /// <summary>
        /// 網(wǎng)頁鏈接,用戶點擊按鈕可打開鏈接,不超過256字節(jié)
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string url { get; set; }

        /// <summary>
        /// 子按鈕數(shù)組,按鈕個數(shù)應為2~5個
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<MenuInfo> sub_button { get; set; }

.......

However, with so much information, I need to specify different types. Attribute type, isn't that very troublesome? What if I set the key attribute in the View type menu?

The solution is that we define several constructors, They are used to construct different menu information, as shown below are the constructors that assign different attributes to different types of menus

  /// <summary>
        /// 參數(shù)化構(gòu)造函數(shù)
        /// </summary>
        /// <param name="name">按鈕名稱</param>
        /// <param name="buttonType">菜單按鈕類型</param>
        /// <param name="value">按鈕的鍵值(Click),或者連接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.view)
            {
                this.url = value;
            }
        }

Okay, there is another problem, the submenu is the attribute sub_button. It is a dispensable thing. If there is one, you need to specify the Name attribute and add its sub_button collection Object. Then we are adding a constructor that constructs the object information of the submenu.

/// <summary>
        /// 參數(shù)化構(gòu)造函數(shù),用于構(gòu)造子菜單
        /// </summary>
        /// <param name="name">按鈕名稱</param>
        /// <param name="sub_button">子菜單集合</param>
        public MenuInfo(string name, IEnumerable<MenuInfo> sub_button)
        {
            this.name = name;
            this.sub_button = new List<MenuInfo>();
            this.sub_button.AddRange(sub_button);
        }

Since only the attribute contents of Name and sub_button are specified, and if the other contents are null, the naturally constructed Json will not contain them, which is perfect!

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

    /// <summary>
    /// 菜單的Json字符串對象    
    /// </summary>
    public class MenuJson
    {        public List<MenuInfo> button { get; set; }        
    public MenuJson()
        {
            button = new List<MenuInfo>();
        }
    }    /// <summary>
    /// 菜單列表的Json對象    /// </summary>
    public class MenuListJson
    {        public MenuJson menu { get; set; }
    }

3. Interface implementation of menu management operations

From the definition of WeChat, we can see that we can obtain menu information, create menus, and delete through the API menu, then we define their interfaces as follows.

 /// <summary>
    /// 菜單的相關(guān)操作
    /// </summary>
    public interface IMenuApi
    {              
        /// <summary>
        /// 獲取菜單數(shù)據(jù)
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</param>
        /// <returns></returns>
        MenuJson GetMenu(string accessToken);
                       
        /// <summary>
        /// 創(chuàng)建菜單
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</param>
        /// <param name="menuJson">菜單對象</param>
        /// <returns></returns>
        CommonResult CreateMenu(string accessToken, MenuJson menuJson);
                       
        /// <summary>
        /// 刪除菜單
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</param>
        /// <returns></returns>
        CommonResult DeleteMenu(string accessToken);
    }

The specific implementation of obtaining menu information is as follows.

/// <summary>
        /// 獲取菜單數(shù)據(jù)
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</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;
        }

這里就是把返回的Json數(shù)據(jù),統(tǒng)一轉(zhuǎn)換為我們需要的實體信息了,一步到位。

調(diào)用代碼如下所示。

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

創(chuàng)建和刪除菜單對象的操作實現(xiàn)如下所示。

/// <summary>
        /// 創(chuàng)建菜單
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</param>
        /// <param name="menuJson">菜單對象</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>
        /// 刪除菜單
        /// </summary>
        /// <param name="accessToken">調(diào)用接口憑證</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);
        }

看到這里,有些人可能會問,實體類你簡化了,那么創(chuàng)建菜單是不是挺麻煩的,特別是構(gòu)造對應的信息應該如何操作呢?前面不是介紹了不同的構(gòu)造函數(shù)了嗎,通過他們簡單就搞定了,不用記下太多的實體類及它們的繼承關(guān)系來處理菜單信息。

private void btnCreateMenu_Click(object sender, EventArgs e)
        {                       
            MenuInfo productInfo = new MenuInfo("軟件產(chǎn)品", new MenuInfo[] { 
                new MenuInfo("病人資料管理系統(tǒng)", ButtonType.click, "patient"), 
                new MenuInfo("客戶關(guān)系管理系統(tǒng)", ButtonType.click, "crm"), 
                new MenuInfo("酒店管理系統(tǒng)", ButtonType.click, "hotel"), 
                new MenuInfo("送水管理系統(tǒng)", ButtonType.click, "water")
            });                                    

            MenuInfo frameworkInfo = new MenuInfo("框架產(chǎn)品", new MenuInfo[] { 
                new MenuInfo("Win開發(fā)框架", ButtonType.click, "win"),
                new MenuInfo("WCF開發(fā)框架", ButtonType.click, "wcf"),
                new MenuInfo("混合式框架", ButtonType.click, "mix"), 
                new MenuInfo("Web開發(fā)框架", ButtonType.click, "web"),
                new MenuInfo("代碼生成工具", ButtonType.click, "database2sharp")
            });

            MenuInfo relatedInfo = new MenuInfo("相關(guān)鏈接", new MenuInfo[] { 
                new MenuInfo("公司介紹", ButtonType.click, "Event_Company"),
                new MenuInfo("官方網(wǎng)站", ButtonType.view, "http://www.iqidi.com"),
                new MenuInfo("提點建議", ButtonType.click, "Event_Suggestion"),
              new MenuInfo("聯(lián)系客服", ButtonType.click, "Event_Contact"),
new MenuInfo("發(fā)郵件", 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("您確認要創(chuàng)建菜單嗎") == System.Windows.Forms.DialogResult.Yes)
            {
                IMenuApi menuBLL = new MenuApi();
                CommonResult result = menuBLL.CreateMenu(token, menuJson);
   Console.WriteLine("創(chuàng)建菜單:" + (result.Success ? "成功" : "失敗:" + result.ErrorMessage));
            }
        }

菜單的效果如下:

C# develops WeChat portal and applies management operations of WeChat portal menu

?更多C# develops WeChat portal and applies management operations of WeChat portal menu相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276