/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> /// <param name="resultFull"></param> /// <returns></returns> public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull) { GetMenuResult result = null; try { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var rootButton in resultFull.menu.button) { if (rootButton.name == null) { continue; //没有设置一级菜单 } var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key)) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //URL bg.button.Add(new SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } } else if (availableSubButton < 2) { throw new WeixinMenuException("子菜单至少需要填写2个!"); } else { //底部二级菜单 var subButton = new SubButton(rootButton.name); bg.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton.name == null) { continue; //没有设置菜单 } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subSubButton.key)) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //URL subButton.sub_button.Add(new SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } } } } if (bg.button.Count < 2) { throw new WeixinMenuException("一级菜单按钮至少为2个!"); } result = new GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new WeixinMenuException(ex.Message, ex); } return(result); }
/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> /// <param name="resultFull"></param> /// <returns></returns> public GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull) { GetMenuResult result = null; try { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var rootButton in resultFull.menu.button) { if (rootButton.name == null) { continue;//没有设置一级菜单 } var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name));//可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key)) { throw new WeChatMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //URL bg.button.Add(new SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } } //else if (availableSubButton < 1) //{ // throw new WeChatMenuException("子菜单至少需要填写1个!"); //} else { //底部二级菜单 var subButton = new SubButton(rootButton.name); bg.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton.name == null) { continue; //没有设置菜单 } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subSubButton.key)) { throw new WeChatMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //URL subButton.sub_button.Add(new SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } } } } if (bg.button.Count < 1) { throw new WeChatMenuException("一级菜单按钮至少为1个!"); } result = new GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new WeChatMenuException(ex.Message, ex); } return result; }
/// <summary> /// 接受请求的网站是UTF-8编码,Http Post请求参数也需要用UTF-8编码 /// HttpUtility.UrlEncode(merId, myEncoding) /// </summary> /// <param name="url">访问地址,不带参数</param> /// <param name="para">参数字符串</param> /// <returns></returns> public static string HtmlFromUrl(string url, string type, string aid) { String sResult = ""; try { List<Button> menus = new List<Button>(); List<WEC_MENU> listObj = new List<WEC_MENU>(); WEC_MENU conf = new WEC_MENU(); conf.PID = "0"; conf.AID = Convert.ToDecimal(aid ); listObj = BLLTable<WEC_MENU>.Select(new WEC_MENU(), conf); foreach (WEC_MENU me in listObj) { if (me.STATUS == 1) { continue; } Menu m = new Menu(); m.name = me.MENU_NAME; conf.PID = me.ID; List<WEC_MENU> listObj2 = new List<WEC_MENU>(); listObj2 = BLLTable<WEC_MENU>.Select(new WEC_MENU(), conf); foreach (WEC_MENU me2 in listObj2) { if (me2.STATUS == 1) { continue; } SubButton sub_btn = new SubButton(); sub_btn.type = me2.MENU_TYPE; sub_btn.name = me2.MENU_NAME; if (me2.MENU_TYPE.Equals("click")) { WEC_REQUEST conf2 = new WEC_REQUEST(); conf2.TID = Convert.ToDecimal(me2.MENU_KEY); sub_btn.key = BLLTable<WEC_REQUEST>.GetRowData(new WEC_REQUEST(),conf2).KEYWORD; sub_btn.url = ""; } else if (me2.MENU_TYPE.Equals("view")) { sub_btn.key = ""; sub_btn.url = me2.URL; } m.sub_button.Add(sub_btn); } if (m.sub_button.Count == 0) { SubButton sub = new SubButton(); sub.name = me.MENU_NAME; // sub.key = me.MENU_KEY; sub.type = me.MENU_TYPE; // sub.url = me.URL; if (me.MENU_TYPE.Equals("click")) { WEC_REQUEST conf2 = new WEC_REQUEST(); conf2.TID = Convert.ToDecimal(me.MENU_KEY); sub.key = BLLTable<WEC_REQUEST>.GetRowData(new WEC_REQUEST(), conf2).KEYWORD; sub.url = ""; } else if (me.MENU_TYPE.Equals("view")) { sub.key = ""; sub.url = me.URL; } menus.Add(sub); } else { menus.Add(m); } } //menus.Add( // new Menu() // { // name = "求职招聘", // sub_button = new SubButton[] // { // new SubButton() // { // name = "求职", // key = "menu_qiuzhi", // type = "click" // }, // new SubButton() // { // name = "招聘", // key = "menu_zhaopin", // type = "click" // } // } // }); WeixinMenu weixinMenu = new WeixinMenu() { button = menus }; JavaScriptSerializer jss = new JavaScriptSerializer(); string menusString = jss.Serialize(weixinMenu); // byte[] postBytes = Encoding.UTF8.GetBytes(menusString); // HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); // request.Method = "POST"; // request.ContentType = "application/x-www-form-urlencoded"; // request.ContentLength = postBytes.Length; // using (Stream reqStream = request.GetRequestStream()) // { // reqStream.Write(postBytes, 0, postBytes.Length); // } // HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // System.IO.Stream responseStream = response.GetResponseStream(); // System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); // string responseText = sr.ReadToEnd(); // sr.Close(); // sr.Dispose(); // responseStream.Close(); // string jsonData = responseText; // //MenuResult menuResult = jss.Deserialize<MenuResult>(jsonData); // //return url + "\n" + menuResult.Errcode + "\n" + menuResult.Errmsg; // return url; //ASCIIEncoding encoding = new ASCIIEncoding(); return httpForm(menusString,url,type); } catch (Exception e) { sResult = e.Message; return sResult; } }
private static void GetButtonGroup(List <MenuFull_RootButton> rootButtonList, ButtonGroupBase buttonGroup) { foreach (var rootButton in rootButtonList) { if (rootButton == null || string.IsNullOrEmpty(rootButton.name)) { continue; //没有设置一级菜单 } var availableSubButton = rootButton.sub_button == null ? 0 : rootButton.sub_button.Count(z => z != null && !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (rootButton.type == null || (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key))) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 buttonGroup.button.Add(new SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase)) { //URL buttonGroup.button.Add(new SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } else if (rootButton.type.Equals("MINIPROGRAM", StringComparison.OrdinalIgnoreCase)) { //小程序 buttonGroup.button.Add(new SingleMiniProgramButton() { name = rootButton.name, url = rootButton.url, appid = rootButton.appid, pagepath = rootButton.pagepath, type = rootButton.type }); } else if (rootButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase)) { //弹出地理位置选择器 buttonGroup.button.Add(new SingleLocationSelectButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase)) { //弹出拍照或者相册发图 buttonGroup.button.Add(new SinglePicPhotoOrAlbumButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase)) { //弹出系统拍照发图 buttonGroup.button.Add(new SinglePicSysphotoButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase)) { //弹出微信相册发图器 buttonGroup.button.Add(new SinglePicWeixinButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 buttonGroup.button.Add(new SingleScancodePushButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("SCANCODE_WAITMSG", StringComparison.OrdinalIgnoreCase)) { //扫码推事件且弹出“消息接收中”提示框 buttonGroup.button.Add(new SingleScancodeWaitmsgButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("MEDIA_ID", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 buttonGroup.button.Add(new SingleMediaIdButton() { name = rootButton.name, media_id = rootButton.media_id, type = rootButton.type }); } else if (rootButton.type.Equals("VIEW_LIMITED", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 buttonGroup.button.Add(new SingleViewLimitedButton() { name = rootButton.name, media_id = rootButton.media_id, type = rootButton.type }); } else { throw new WeixinMenuException("菜单类型无法处理:" + rootButton.type); } } //else if (availableSubButton < 2) //{ // throw new WeixinMenuException("子菜单至少需要填写2个!"); //} else { //底部二级菜单 var subButton = new SubButton(rootButton.name); buttonGroup.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton == null || string.IsNullOrEmpty(subSubButton.name)) { continue; //没有设置菜单 } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subSubButton.key)) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase)) { //URL subButton.sub_button.Add(new SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } else if (subSubButton.type.Equals("MINIPROGRAM", StringComparison.OrdinalIgnoreCase)) { //小程序 subButton.sub_button.Add(new SingleMiniProgramButton() { name = subSubButton.name, url = subSubButton.url, appid = subSubButton.appid, pagepath = subSubButton.pagepath, type = subSubButton.type }); } else if (subSubButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase)) { //弹出地理位置选择器 subButton.sub_button.Add(new SingleLocationSelectButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase)) { //弹出拍照或者相册发图 subButton.sub_button.Add(new SinglePicPhotoOrAlbumButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase)) { //弹出系统拍照发图 subButton.sub_button.Add(new SinglePicSysphotoButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase)) { //弹出微信相册发图器 subButton.sub_button.Add(new SinglePicWeixinButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 subButton.sub_button.Add(new SingleScancodePushButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("MEDIA_ID", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 subButton.sub_button.Add(new SingleMediaIdButton() { name = subSubButton.name, media_id = subSubButton.media_id, type = subSubButton.type }); } else if (subSubButton.type.Equals("VIEW_LIMITED", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 subButton.sub_button.Add(new SingleViewLimitedButton() { name = subSubButton.name, media_id = subSubButton.media_id, type = subSubButton.type }); } else { //扫码推事件且弹出“消息接收中”提示框 subButton.sub_button.Add(new SingleScancodeWaitmsgButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } } } } }
//[TestMethod]//已经通过测试 public void CreateMenuTest() { //var accessToken = AccessTokenContainer.GetAccessToken(_appId); ButtonGroup bg = new ButtonGroup(); //二级菜单 var subButton = new SubButton() { name = "二级菜单" }; bg.button.Add(subButton); subButton.sub_button.Add(new SingleViewButton() { url = "https://book.weixin.senparc.com/book/link?code=SenparcRobotMenu", name = "《微信开发深度解析》" }); subButton.sub_button.Add(new SingleViewButton() { url = "https://sdk.weixin.senparc.com/TenpayV3/ProductList", name = "微信支付" }); subButton.sub_button.Add(new SingleClickButton() { key = "Description", name = "测试使用说明" }); subButton.sub_button.Add(new SingleClickButton() { key = "OneClick", name = "单击测试" }); subButton.sub_button.Add(new SingleViewButton() { url = "https://weixin.senparc.com/", name = "Url跳转" }); //二级菜单 var subButton2 = new SubButton() { name = "二级菜单" }; bg.button.Add(subButton2); subButton2.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Text", name = "返回文本" }); subButton2.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_News", name = "返回图文" }); subButton2.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Music", name = "返回音乐" }); subButton2.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Image", name = "返回图片" }); subButton2.sub_button.Add(new SingleClickButton() { key = "OAuth", name = "OAuth2.0授权测试" }); //二级菜单 var subButton3 = new SubButton() { name = "更多" }; bg.button.Add(subButton3); subButton3.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Agent", name = "代理消息-返回图文" }); var result = CommonApi.CreateMenu(_appId, bg); Assert.IsNotNull(result); Assert.AreEqual("ok", result.errmsg); }
/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> /// <param name="resultFull"></param> /// <returns></returns> public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull) { GetMenuResult result = null; try { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var rootButton in resultFull.button) { if (rootButton.name == null) { continue; //没有设置一级菜单 } var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (rootButton.type == null || (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key))) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase)) { //URL bg.button.Add(new SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } else if (rootButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase)) { //弹出地理位置选择器 bg.button.Add(new SingleLocationSelectButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase)) { //弹出拍照或者相册发图 bg.button.Add(new SinglePicPhotoOrAlbumButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase)) { //弹出系统拍照发图 bg.button.Add(new SinglePicSysphotoButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase)) { //弹出微信相册发图器 bg.button.Add(new SinglePicWeixinButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else if (rootButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 bg.button.Add(new SingleScancodePushButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //扫码推事件且弹出“消息接收中”提示框 bg.button.Add(new SingleScancodeWaitmsgButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } } else { //底部二级菜单 var subButton = new SubButton(rootButton.name); bg.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton.name == null) { continue; //没有设置菜单 } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subSubButton.key)) { throw new WeixinMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase)) { //URL subButton.sub_button.Add(new SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } else if (subSubButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase)) { //弹出地理位置选择器 subButton.sub_button.Add(new SingleLocationSelectButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase)) { //弹出拍照或者相册发图 subButton.sub_button.Add(new SinglePicPhotoOrAlbumButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase)) { //弹出系统拍照发图 subButton.sub_button.Add(new SinglePicSysphotoButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase)) { //弹出微信相册发图器 subButton.sub_button.Add(new SinglePicWeixinButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else if (subSubButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase)) { //扫码推事件 subButton.sub_button.Add(new SingleScancodePushButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //扫码推事件且弹出“消息接收中”提示框 subButton.sub_button.Add(new SingleScancodeWaitmsgButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } } } } result = new GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new WeixinMenuException(ex.Message, ex); } return(result); }
// GET: Menu public ActionResult Index() { var accessToken = AccessTokenContainer.GetAccessToken(Common.CommonConst.AppID); ButtonGroup bg = new ButtonGroup(); ////单击 //bg.button.Add(new SingleClickButton() //{ // name = "废品回收", // key = "OneClick", // type = ButtonType.click.ToString(),//默认已经设为此类型,这里只作为演示 //}); ////单击 //bg.button.Add(new SingleClickButton() //{ // name = "废品回收", // key = "OneClick", // type = ButtonType.click.ToString(),//默认已经设为此类型,这里只作为演示 //}); ////单击 //bg.button.Add(new SingleClickButton() //{ // name = "废品回收", // key = "OneClick", // type = ButtonType.click.ToString(),//默认已经设为此类型,这里只作为演示 //}); //二级菜单 var subButton = new SubButton() { name = "废品回收" }; subButton.sub_button.Add(new SingleViewButton() { name = "关于收收", url = "http://weixin.shoushouto.com/about.html" }); subButton.sub_button.Add(new SingleViewButton() { name = "企业下单", url = "http://weixin.shoushouto.com/User/Index?type=2" }); subButton.sub_button.Add(new SingleViewButton() { name = "个人下单", url = "http://weixin.shoushouto.com/User/Index?type=1" }); //二级菜单 var subSecButton = new SubButton() { name = "回收加盟" }; subSecButton.sub_button.Add(new SingleViewButton() { name = "加入收收", url = "http://weixin.shoushouto.com/SS_WeiUser/Index" }); var subTheButton = new SubButton() { name = "我的收收" }; subTheButton.sub_button.Add(new SingleViewButton() { name = "我的订单", url = "http://weixin.shoushouto.com/Order/History" }); subTheButton.sub_button.Add(new SingleViewButton() { name = "我的资料", url = "http://weixin.shoushouto.com/UserProfile/Index" }); subTheButton.sub_button.Add(new SingleViewButton() { name = "我的推荐", url = "http://weixin.shoushouto.com/Poster/Index" }); bg.button.Add(subButton); bg.button.Add(subSecButton); bg.button.Add(subTheButton); var result = CommonApi.CreateMenu(accessToken, bg); //获取菜单 //var result = CommonApi.GetMenu(accessToken); //删除菜单 //var result = CommonApi.DeleteMenu(accessToken); return(View()); }
/// <summary> /// 更新菜单 /// </summary> protected void btnSubmit_Click(object sender, EventArgs e) { try { Model.wx_userweixin weixin = GetWeiXinCode(); string error = ""; string accessToken = cpp.getAccessToken(weixin.id, out error); if (error != "") { JscriptMsg(error, "", "Error"); return; } //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); TextBox txtName = new TextBox(); TextBox txtKey = new TextBox(); TextBox txtUrl = new TextBox(); IList <BaseButton> topList = new List <BaseButton>(); IList <SingleButton> subList = new List <SingleButton>(); #region 菜单设置 for (int i = 0; i < 3; i++) { txtName = this.FindControl("txtTop" + (i + 1)) as TextBox; txtKey = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox; txtUrl = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox; if (txtName.Text.Trim() == "") { // 如果名称为空,则忽略该菜单,以及下面的子菜单 continue; } subList = new List <SingleButton>(); TextBox txtSubName = new TextBox(); TextBox txtSubKey = new TextBox(); TextBox txtSubUrl = new TextBox(); for (int j = 0; j < 5; j++) { #region 子菜单的设置 txtSubName = this.FindControl("txtMenu" + (i + 1) + (j + 1)) as TextBox; txtSubKey = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Key") as TextBox; txtSubUrl = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Url") as TextBox; if (txtSubName.Text.Trim() == "") { continue; } if (txtSubUrl.Text.Trim() != "") { SingleViewButton sub = new SingleViewButton(); sub.name = txtSubName.Text.Trim(); sub.url = txtSubUrl.Text.Trim(); subList.Add(sub); } else if (txtSubKey.Text.Trim() != "") { SingleClickButton sub = new SingleClickButton(); sub.name = txtSubName.Text.Trim(); sub.key = txtSubKey.Text.Trim(); subList.Add(sub); } else { //报错 :子菜单必须有key和name JscriptMsg("二级菜单的名称和key或者url必填!", "", "Error"); return; } #endregion } //子菜单循环结束 if (subList != null && subList.Count > 0) { //有子菜单, 该一级菜单是SubButton if (subList.Count < 1) { JscriptMsg("子菜单个数为2~5个!", "", "Error"); return; } SubButton topButton = new SubButton(Utils.CutString(txtName.Text.Trim(), 16)); topButton.sub_button.AddRange(subList); topList.Add(topButton); } else { // 无子菜单 if (txtKey.Text.Trim() == "" && txtUrl.Text.Trim() == "") { JscriptMsg("若无子菜单,必须填写key或者url值!", "", "Error"); return; } if (txtUrl.Text.Trim() != "") { //view 页面跳转 SingleViewButton topSingleButton = new SingleViewButton(); topSingleButton.name = txtName.Text.Trim(); topSingleButton.url = txtUrl.Text.Trim(); topList.Add(topSingleButton); } else if (txtKey.Text.Trim() != "") { SingleClickButton topSingleButton = new SingleClickButton(); topSingleButton.name = txtName.Text.Trim(); topSingleButton.key = txtKey.Text.Trim(); topList.Add(topSingleButton); } } } #endregion bg.button.AddRange(topList); var result = mMrg.CreateMenu(accessToken, bg); JscriptMsg("菜单提交成功!", "wxMenu.aspx", "Success"); } catch (Exception ex) { JscriptMsg("报错:" + ex.Message, "", "Error"); } }
public ActionResult CreateMenu(string token, MenuPost menuPost, FormCollection fc) { try { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var rootButton in menuPost.menu.button) { if (rootButton.name == null) { continue; //没有设置一级菜单 } var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (string.IsNullOrEmpty(rootButton.key)) { throw new Exception("单击按钮的key不能为空!"); } bg.button.Add(new SingleButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type //目前只有click }); } else if (availableSubButton < 2) { throw new Exception("子菜单至少需要填写2个!"); } else { //底部二级菜单 var subButton = new SubButton(rootButton.name); bg.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton.name == null) { continue; //没有设置菜单 } if (string.IsNullOrEmpty(subSubButton.key)) { throw new Exception("单击按钮的key不能为空!"); } subButton.sub_button.Add(new SingleButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type //目前只有click }); }); } } } if (bg.button.Count < 2) { throw new Exception("一级菜单按钮至少为2个!"); } var result = CommonAPIs.CommonApi.CreateMenu(token, bg); var json = new { Success = result.errmsg == "ok", Message = result.errmsg }; return(Json(json)); } catch (Exception ex) { var json = new { Success = false, Message = ex.Message }; return(Json(json)); } }