自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。 自定义菜单的介绍,可以看官方开发文档http://mp.weixin.qq.com/wiki/home/index.html 下面是我自己写的一个实现自定义菜单的小Dome 一、先定义了一个Json字符串,格式要按照微信提供的Json格式
//创建微信菜单JSON字符串 private string GetWXMenuStr() { string weixin1 = ""; weixin1 += "{\n"; weixin1 += "\"button\":[\n"; weixin1 += "{\n"; // weixin1 += "\"type\":\"click\",\n"; //第一个菜单 weixin1 += "\"name\":\"公共信息\",\n"; weixin1 += "\"sub_button\":[\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"通知公告\",\n"; weixin1 += "\"key\":\"11\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"工作动态\",\n"; weixin1 += "\"key\":\"12\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"政策法规\",\n"; weixin1 += "\"key\":\"13\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"经济视野\",\n"; weixin1 += "\"key\":\"14\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"专题报道\",\n"; weixin1 += "\"key\":\"15\"\n"; weixin1 += "}]\n"; weixin1 += "},\n"; //第二个菜单 weixin1 += "{\n"; //weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"公共服务\",\n"; weixin1 += "\"sub_button\":[\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"企业之窗\",\n"; weixin1 += "\"key\":\"21\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"金融服务\",\n"; weixin1 += "\"key\":\"22\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"创业指导\",\n"; weixin1 += "\"key\":\"23\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"管理服务\",\n"; weixin1 += "\"key\":\"24\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"click\",\n"; weixin1 += "\"name\":\"法律服务\",\n"; weixin1 += "\"key\":\"25\"\n"; weixin1 += "}]\n"; weixin1 += "},\n"; //第三个菜单(view类型的) weixin1 += "{\n"; weixin1 += "\"name\":\"互动交流\",\n"; weixin1 += "\"sub_button\":[\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"view\",\n"; weixin1 += "\"name\":\"注册\",\n"; weixin1 += "\"url\":\"http://1532618rl0.iok.la/Register.aspx\"\n"; weixin1 += "},\n"; weixin1 += "{\n"; weixin1 += "\"type\":\"view\",\n"; weixin1 += "\"name\":\"登录\",\n"; weixin1 += "\"url\":\"http://1532618rl0.iok.la\"\n"; weixin1 += "}]\n"; weixin1 += "}\n"; weixin1 += "}]\n"; weixin1 += "}\n"; return weixin1; }
二、定义执行菜单请求方法
////// 执行菜单请求 /// /// 微信提供的自定义菜单接口地址 /// 实现自定义菜单的Json字符串 public void PostMenuData(string url, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; } catch (Exception ex) { string err = ex.Message; } }
三、实现自定义菜单
private string postUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="; //创建自定义菜单 public void CreateWxMenu() { string weixin1 = GetWXMenuStr();//获取菜单Json字符串 string AccessToken = Helper.IsExistAccess_Token();//获取AccessToken PostMenuData(postUrl + AccessToken, weixin1);//通过接口实现自定义菜单 }
四、在程序运行的起始页调用CreateWxMenu()方法即可。
其中获取AccessToken的方法是调用前面写的文章封装的一个Helper方法实现的,地址http://www.cnblogs.com/zhangjd/p/5694737.html