自定義菜單最多包括3個一級菜單,每一個一級菜單最多包含5個二級菜單。一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以「...」代替。
自定義菜單的介紹,能夠看官方開發文檔http://mp.weixin.qq.com/wiki/home/index.html
下面是我本身寫的一個實現自定義菜單的小Dome
1、先定義了一個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; }
2、定義執行菜單請求方法html
/// <summary>
/// 執行菜單請求
/// </summary>
/// <param name="url">微信提供的自定義菜單接口地址</param>
/// <param name="postData">實現自定義菜單的Json字符串</param>
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;
}
}
3、實現自定義菜單api
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);//經過接口實現自定義菜單 }
4、在程序運行的起始頁調用CreateWxMenu()方法便可。微信
其中獲取AccessToken的方法是調用前面寫的文章封裝的一個Helper方法實現的,地址http://www.cnblogs.com/zhangjd/p/5694737.htmlcookie