您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關微信公眾號開發中如何設置自定義菜單的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、首先我們需要新建一個數據庫表用于存放menu菜單項
可包含的字段有id、父類id、name、排序、是否顯示、類型(view、click)、鏈接、adddate
注意后臺存menu菜單數據時,parentId=-1為一級菜單,或parendId為一級菜單的id作為該一級菜單下的二級菜單
2、在設置菜單時需要向微信接口傳menuJson字符串,所以要先拼接字符串,后臺定義一個creatMenu()
public bool creatMenu() { string menuJson = ""; //這里默認parentId=-1為最外層菜單,isactive=1為顯示,responseType=1為click類型 DataTable dtAMenu = Service.SelectDataTable("id, name, responseType, jsonStr,url", "WEIXINMENU", " parentId=-1 and isactive=1 order by sort"); if (dtAMenu.Rows.Count > 0) { menuJson = "{\"button\":["; for (int i = 0; i < dtAMenu.Rows.Count; i++) { DataTable dtBMenu = Service.SelectDataTable("id, name, responseType, jsonStr,url", wx, " parentId=" + dtAMenu.Rows[i]["id"].ToString() + " and isactive=1 order by sort"); if (dtBMenu.Rows.Count > 0) { menuJson += "{\"name\":\"" + dtAMenu.Rows[i]["name"].ToString() + "\",\"sub_button\":["; for (int j = 0; j < dtBMenu.Rows.Count; j++) { if (Convert.ToInt32(dtBMenu.Rows[j]["responseType"]) == 2) { menuJson += "{\"type\":\"view\",\"name\":\"" + dtBMenu.Rows[j]["name"].ToString() + "\",\"url\":\"" + dtBMenu.Rows[j]["jsonStr"].ToString() + "\"},"; } else { menuJson += "{\"type\":\"click\",\"name\":\"" + dtBMenu.Rows[j]["name"].ToString() + "\",\"key\":\"eventKey_" + dtBMenu.Rows[j]["id"].ToString() + "\"},"; } } menuJson = menuJson.TrimEnd(','); menuJson += "]},"; } else { //if (Convert.ToInt32(dtAMenu.Rows[i]["responseType"]) == 2) //{ menuJson += "{\"type\":\"view\",\"name\":\"" + dtAMenu.Rows[i]["name"].ToString() + "\",\"url\":\"" + dtAMenu.Rows[i]["jsonStr"].ToString() + "\"},"; //} //else //{ // menuJson += "{\"type\":\"click\",\"name\":\"" + dtAMenu.Rows[i]["name"].ToString() + "\",\"key\":\"eventKey_" + dtAMenu.Rows[i]["id"].ToString() + "\"},"; //} } dtBMenu.Dispose(); } dtAMenu.Dispose(); menuJson = menuJson.TrimEnd(','); menuJson += "]}"; menuJson = menuJson.Trim(); return requstZmToCreatMent(menuJson); } else { return false; } }
3、獲取到menujson字符串后調用微信接口創建菜單,需先獲取assess token,有關assess token獲取可參考:獲取AccessToken
/// <summary> /// 向微信服務器請求創建自定義菜單 /// </summary> /// <param name="jsonStr"></param> /// <returns></returns> /// private bool requstZmToCreatMent(string jsonStr) { try { var AccessToken = "";//accesstoken需例外獲取,一般可開始時獲取后存數據庫,下次從數據庫取,注意accesstoken有效期為7200秒 //聲明一個HttpWebRequest請求 string interfaceUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + AccessToken; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(interfaceUrl); //設置連接超時時間 request.Timeout = 30000; request.KeepAlive = true; Encoding encodeType = Encoding.GetEncoding("UTF-8"); request.Headers.Set("Pragma", "no-cache"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322); Http STdns"; request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; request.CookieContainer = new CookieContainer(); byte[] Bytes = encodeType.GetBytes(jsonStr); request.ContentLength = Bytes.Length; request.AllowAutoRedirect = true; //發送數據 using (Stream writer = request.GetRequestStream()) { writer.Write(Bytes, 0, Bytes.Length); writer.Close(); } StringBuilder strb = new StringBuilder(); //接收數據 using (Stream reader = request.GetResponse().GetResponseStream()) { StreamReader sr = new StreamReader(reader, encodeType); strb.Append(sr.ReadToEnd()); sr.Close(); reader.Close(); } if ((strb.ToString().IndexOf("\"errcode\":42001") != -1) || (strb.ToString().IndexOf("\"errcode\":40001") != -1) || (strb.ToString().IndexOf("\"errcode\":40014") != -1) || (strb.ToString().IndexOf("\"errcode\":41001") != -1)) //access_token錯誤 { // AccessToken = getAccessToken(); getzmAccessToken(); return requstZmToCreatMent(jsonStr); } else { if (strb.ToString() == "{\"errcode\":0,\"errmsg\":\"ok\"}") { return true; } else { return false; } } } catch (Exception exp) { return false; } }
感謝各位的閱讀!關于“微信公眾號開發中如何設置自定義菜單”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。