微信公众号平台接口开发:菜单管理

官方菜单功能介绍

微信公众号平台接口开发:菜单管理

微信公众号平台接口开发:菜单管理

请求接口:https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

 微信公众号平台接口开发:菜单管理

微信公众号平台接口开发:菜单管理

新增菜单管理类

 1     public class MenuFirstLayerModel
 2     {
 3         public string name { get; set; }
 4         public List<MenuTwoLayerModel> sub_button { get; set; }
 5     }
 6 
 7     public class MenuTwoLayerModel
 8     {
 9         public string type { get; set; }
10         public string name { get; set; }
11         public string key { get; set; }
12         public string url { get; set; }
13     }
14 
15     public class WXMenu 
16     {
17         public List<MenuFirstLayerModel> button { get; set; }
18 
19         public string Create()
20         {
21             try
22             {
23                 var requestUri = string.Format(@"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", WeCharBase.AccessToken);
24                 
25                 return WeCharBase.Post(requestUri, new StringContent(JsonConvert.SerializeObject(new
26                 {
27                     button = button
28                 })));
29             }
30             catch (Exception ex)
31             {
32                 return ex.Message;
33             }
34         }
35     }

新增控制器MenuController.cs

 1         public ActionResult ViewMenu()
 2         {
 3             return View();
 4         }
 5 
 6         public ActionResult CreateMenu()
 7         {
 8             var wxMenu = new WXMenu()
 9             {
10                 button = new List<MenuFirstLayerModel>()
11                 {
12                     new MenuFirstLayerModel()
13                     {
14                          name="扫码",
15                          sub_button = new List<MenuTwoLayerModel>()
16                          {
17                             new MenuTwoLayerModel()
18                             {
19                                 type = "scancode_waitmsg",
20                                 name = "扫码带提示", 
21                                 key = "rselfmenu_0_0"
22                             },
23                             new MenuTwoLayerModel()
24                             {
25                                 type = "scancode_push",
26                                 name = "扫码推事件", 
27                                 key = "rselfmenu_0_1"
28                             }
29                          },
30                     },
31                     new MenuFirstLayerModel()
32                     {
33                          name = "发图",
34                          sub_button = new List<MenuTwoLayerModel>()
35                          {
36                             new MenuTwoLayerModel()
37                             {
38                                 type = "pic_sysphoto",
39                                 name = "系统拍照发图", 
40                                 key = "rselfmenu_1_0"
41                             },
42                             new MenuTwoLayerModel()
43                             {
44                                 type = "pic_photo_or_album",
45                                 name = "拍照或者相册发图", 
46                                 key = "rselfmenu_1_1"
47                             },
48                             new MenuTwoLayerModel()
49                             {
50                                 type = "pic_weixin",
51                                 name = "微信相册发图", 
52                                 key = "rselfmenu_1_2"
53                             }
54                          }
55                     },
56                     new MenuFirstLayerModel()
57                     {
58                         name = "其他",
59                         sub_button = new List<MenuTwoLayerModel>()
60                         {
61                             new MenuTwoLayerModel()
62                             {
63                                 type = "location_select",
64                                 name = "发送位置", 
65                                 key = "rselfmenu_2_0"
66                             },
67                             new MenuTwoLayerModel()
68                             {
69                                 type = "click",
70                                 name = "今日歌曲", 
71                                 key = "V1001_TODAY_MUSIC"
72                             },
73                             new MenuTwoLayerModel()
74                             {
75                                 type = "view",
76                                 name = "百度", 
77                                 url = "http://www.baidu.com"
78                             }
79                         }
80                     }
81                 }
82             };
83 
84             return Content(wxMenu.Create());
85         }

新增视图ViewMenu.cshtml

 1 <script type="text/javascript" language="javascript">
 2     $(document).ready(function () {
 3         $("#btnCreate").click(function () {
 4             $.ajax({
 5                 type: "POST",
 6                 url: "/Menu/CreateMenu",
 7                 data: { id: $("#textContent").val() },
 8                 success: function (responseTest) {
 9                     $("#resultMesage").text(responseTest);
10                 }
11             });
12         });
13     });
14 </script>
15 
16 <table>
17     <tr>
18         <td>
19             <div class="title">菜单创建</div>
20             <textarea id="textContent" name="textContent" rows="20" cols="100">
21                 button = new[]
22                 {
23                     new
24                     {
25                         name="扫码",
26                         sub_button = new[]
27                         {
28                             new
29                             {
30                                 type = "scancode_waitmsg",
31                                 name = "扫码带提示",
32                                 key = "rselfmenu_0_0"
33                             },
34                             new
35                             {
36                                 type = "scancode_push",
37                                 name = "扫码推事件",
38                                 key = "rselfmenu_0_1"
39                             }
40                         }
41                     },
42                     new
43                     {
44                         name = "发图",
45                         sub_button = new[]
46                         {
47                             new
48                             {
49                                 type = "pic_sysphoto",
50                                 name = "系统拍照发图",
51                                 key = "rselfmenu_1_0"
52                             },
53                             new
54                             {
55                                 type = "pic_photo_or_album",
56                                 name = "拍照或者相册发图",
57                                 key = "rselfmenu_1_1"
58                             },
59                             new
60                             {
61                                 type = "pic_weixin",
62                                 name = "微信相册发图",
63                                 key = "rselfmenu_1_2"
64                             }
65                         }
66                     },
67                     new
68                     {
69                         name = "其他",
70                         sub_button = new[]
71                         {
72                             new
73                             {
74                                 type = "location_select",
75                                 name = "发送位置",
76                                 key = "rselfmenu_2_0"
77                             },
78                             new
79                             {
80                                 type = "click",
81                                 name = "今日歌曲",
82                                 key = "V1001_TODAY_MUSIC"
83                             },
84                             new
85                             {
86                                 type = "view",
87                                 name = "百度",
88                                 url = "http://www.baidu.com"
89                             }
90                         }
91                     }
92                 }
93 
94             </textarea>
95         </td>
96         <td><input class="btncss" id="btnCreate" type="button" value=" 创建 " /></td>
97         <td id="resultMesage" class="resultMesage"></td>
98     </tr>
99 </table>

有效代码写完了,看看效果

微信公众号平台接口开发:菜单管理

微信公众号平台接口开发:菜单管理

 

成功了哦。

 

微信公众号平台接口开发系列

  1. 微信公众号平台接口开发:成为开发者 
  2. 微信公众号平台接口开发:基础支持,获取access_token
  3. 微信公众号平台接口开发:基础支持,获取微信服务器IP地址
  4. 微信公众号平台接口开发:发送客服消息
  5. 微信公众号平台接口开发:菜单管理
  6. 微信公众号平台接口开发:待续

 

微信公众号平台接口开发:菜单管理

上一篇:如何评价张小龙在2016微信公开课的演讲


下一篇:微信 Mustache