private void GetMenu(int account_id) { try { string error = string.Empty; string accessToken = cpp.GetAccessToken(account_id, out error); if (!string.IsNullOrEmpty(error)) { JscriptMsg(error, string.Empty); return; } Senparc.Weixin.MP.Entities.GetMenuResult result = mMrg.GetMenu(accessToken); if (result == null) { return; } var topButtonList = result.menu.button; int topNum = topButtonList.Count; TextBox txtName = new TextBox(); TextBox txtKey = new TextBox(); TextBox txtUrl = new TextBox(); for (int i = 0; i < topNum; i++) { var topButton = topButtonList[i]; if (topButton != null) { txtName = this.FindControl("txtTop" + (i + 1) + "Name") as TextBox; txtKey = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox; txtUrl = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox; txtName.Text = topButton.name; if (topButton.GetType() != typeof(SubButton)) { //下面无子菜单 if (topButton.GetType() == typeof(SingleViewButton)) { txtUrl.Text = ((SingleViewButton)topButton).url; } } else { //下面有子菜单 IList <SingleButton> subButtonList = ((SubButton)topButton).sub_button; if (subButtonList != null && subButtonList.Count > 0) { TextBox txtSubName = new TextBox(); TextBox txtSubKey = new TextBox(); TextBox txtSubUrl = new TextBox(); for (int j = 0; j < subButtonList.Count; j++) { txtSubName = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Name") 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 (subButtonList[j].GetType() == typeof(SingleViewButton)) { SingleViewButton sub = (SingleViewButton)subButtonList[j]; txtSubName.Text = sub.name; txtSubUrl.Text = sub.url; } else { SingleClickButton sub = (SingleClickButton)subButtonList[j]; txtSubName.Text = sub.name; txtSubKey.Text = sub.key; } } } } } } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> public Senparc.Weixin.MP.Entities.GetMenuResult GetMenuFromJsonResult(Senparc.Weixin.MP.GetMenuResultFull resultFull) { Senparc.Weixin.MP.Entities.GetMenuResult result = null; try { //重新整理按钮信息 Senparc.Weixin.MP.Entities.Menu.ButtonGroup bg = new Senparc.Weixin.MP.Entities.Menu.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 Senparc.Weixin.MP.Exceptions.WeixinMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //URL bg.button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } } else { //底部二级菜单 var subButton = new Senparc.Weixin.MP.Entities.Menu.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 Senparc.Weixin.MP.Exceptions.WeixinMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //URL subButton.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } } } } if (bg.button.Count < 1) { throw new Senparc.Weixin.MP.Exceptions.WeixinMenuException("一级菜单按钮至少为1个!"); } result = new Senparc.Weixin.MP.Entities.GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new Senparc.Weixin.MP.Exceptions.WeixinMenuException(ex.Message, ex); } return(result); }
/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> public Senparc.Weixin.MP.Entities.GetMenuResult GetMenuFromJsonResult(Senparc.Weixin.MP.GetMenuResultFull resultFull) { Senparc.Weixin.MP.Entities.GetMenuResult result = null; try { //重新整理按钮信息 Senparc.Weixin.MP.Entities.Menu.ButtonGroup bg = new Senparc.Weixin.MP.Entities.Menu.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 Senparc.Weixin.MP.Exceptions.WeixinMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //URL bg.button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } } else { //底部二级菜单 var subButton = new Senparc.Weixin.MP.Entities.Menu.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 Senparc.Weixin.MP.Exceptions.WeixinMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //URL subButton.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } } } } if (bg.button.Count < 1) { throw new Senparc.Weixin.MP.Exceptions.WeixinMenuException("一级菜单按钮至少为1个!"); } result = new Senparc.Weixin.MP.Entities.GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new Senparc.Weixin.MP.Exceptions.WeixinMenuException(ex.Message, ex); } return result; }