/// <summary> /// 创建菜单 /// </summary> /// <param name="accessToken"></param> /// <param name="buttonData">菜单内容</param> /// <returns></returns> public static WxJsonResult CreateMenu(string accessToken, ButtonGroup buttonData) { var jsonString = JsonConvert.SerializeObject(buttonData); CookieContainer cookieContainer = null;// new CookieContainer(); using (MemoryStream ms = new MemoryStream()) { var bytes = Encoding.UTF8.GetBytes(jsonString); ms.Write(bytes, 0, bytes.Length); ms.Seek(0, SeekOrigin.Begin); var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", accessToken); var result = Post.PostGetJson<WxJsonResult>(url, cookieContainer, ms); return result; } }
public void CreateMenuTest() { ButtonGroup bg = new ButtonGroup(); var list = new List<Button>(); for (int i = 1; i < 4; i++) { Button b = new Button(); b.name = i.ToString(); list.Add(b); } foreach (var item in list) { SingleClickButton bb = new SingleClickButton(); bb.name = item.name; bb.key = "click"; bg.button.Add(bb); } string token = AccessTokenContainer.TryGetToken("wx00f1a2e4c8da9fff", "d0cecf2fa0b11a67077a4cbeb4c127ab"); WxJsonResult wx = CommonApi.CreateMenu(token, bg); Assert.IsTrue(wx != null); }
/// <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; }
public GetMenuResult() { menu = new ButtonGroup(); }