C#微信开发之旅(五):操作公众号菜单

菜单操作类:

 1 #region 创建、查询、删除菜单
 2 
 3         /// <summary>
 4         /// 创建菜单
 5         /// </summary>
 6         /// <param name="menuData">菜单字符串</param>
 7         /// <returns>ReturnMsg.ErrCode为0则创建菜单成功</returns>
 8         public static ErrorMessage CreateMenu(string menuData)
 9         {
10             string url = string.Format(WeiXinConst.WeiXin_Menu_CreateUrl, AccessToken.Instance.Access_Token);
11             ErrorMessage msg = HttpClientHelper.PostResponse<ErrorMessage>(url, menuData);
12             return msg;
13         }
14 
15         /// <summary>
16         /// 获取菜单信息
17         /// </summary>
18         /// <returns>菜单详细信息的字符串(失败则返回 null)</returns>
19         public static string GetMenu()
20         {
21             string url = string.Format(WeiXinConst.WeiXin_Menu_GetUrl, AccessToken.Instance.Access_Token);
22             string result = HttpClientHelper.GetResponse(url);
23             return result;
24         }
25 
26         /// <summary>
27         /// 删除自定义菜单
28         /// </summary>
29         /// <returns></returns>
30         public static bool DeleteMenu()
31         {
32             string url = string.Format(WeiXinConst.WeiXin_Menu_DeleteUrl, AccessToken.Instance.Access_Token);
33             ErrorMessage msg = HttpClientHelper.GetResponse<ErrorMessage>(url);
34             return msg.ErrCode == "0";
35         }

有了上面的方法,一切都是那么简单

一、自定义菜单创建

menu.txt是存储菜单的文本文件,ErrorMessage 是对微信返回结果的封装:

 1     public class ErrorMessage
 2     {
 3         //{"errcode":40001,"errmsg":"invalid credential"} AppId AppSecret   配置错误,或AccessToken 过期
 4 
 5         public string ErrCode { get; set; }
 6 
 7         public string ErrMsg { get; set; }
 8 
 9         public bool TokenExpired
10         {
11             get { return ErrCode == "40001"; }
12         }
13     }
1        string menuFile = Server.MapPath("~/Document/menu.txt");
2             string menuString = System.IO.File.ReadAllText(menuFile);
3 
4             ErrorMessage message = WeiXinHelper.CreateMenu(menuString);

二、自定义菜单查询

           string menuString =WeiXinHelper.GetMenu();

三、自定义菜单删除

            bool result = WeiXinHelper.DeleteMenu();

 

posted @ 2014-10-24 16:19  Nuclear  阅读(1653)  评论(0编辑  收藏  举报

我只存在于我的世界!