/// <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 == 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 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 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; }
public static GetMenuResult GetMenuFromJson(string jsonString) { var finalResult = new GetMenuResult(); try { //@"{""menu"":{""button"":[{""type"":""click"",""name"":""单击测试"",""key"":""OneClick"",""sub_button"":[]},{""name"":""二级菜单"",""sub_button"":[{""type"":""click"",""name"":""返回文本"",""key"":""SubClickRoot_Text"",""sub_button"":[]},{""type"":""click"",""name"":""返回图文"",""key"":""SubClickRoot_News"",""sub_button"":[]},{""type"":""click"",""name"":""返回音乐"",""key"":""SubClickRoot_Music"",""sub_button"":[]}]}]}}" object jsonResult = null; JavaScriptSerializer js = new JavaScriptSerializer(); jsonResult = js.Deserialize<object>(jsonString); var fullResult = jsonResult as Dictionary<string, object>; if (fullResult != null && fullResult.ContainsKey("menu")) { //得到菜单 var menu = fullResult["menu"]; var buttons = (menu as Dictionary<string, object>)["button"] as object[]; foreach (var rootButton in buttons) { var fullButton = rootButton as Dictionary<string, object>; if (fullButton.ContainsKey("key") && !string.IsNullOrEmpty(fullButton["key"] as string)) { //按钮,无下级菜单 finalResult.menu.button.Add(GetSingleButtonFromJsonObject(fullButton)); } else { //二级菜单 var subButton = new SubButton(fullButton["name"] as string); finalResult.menu.button.Add(subButton); foreach (var sb in fullButton["sub_button"] as object[]) { subButton.sub_button.Add(GetSingleButtonFromJsonObject(sb as Dictionary<string, object>)); } } } } else if (fullResult != null && fullResult.ContainsKey("errmsg")) { //菜单不存在 throw new ErrorJsonResultException(fullResult["errmsg"] as string, null, null); } } catch (ErrorJsonResultException ex) { finalResult = null; //如果没有惨淡会返回错误代码:46003:menu no exist } catch (Exception ex) { //其他异常 finalResult = null; } return finalResult; }
public JsonResult syncMenuOnline(List<List<_poseMenu>> menus) { WxJsonResult result = new WxJsonResult(); ButtonGroup bg = new ButtonGroup(); IList<BaseButton> topList = new List<BaseButton>(); IList<SingleButton> subList = new List<SingleButton>(); for (int i = 0; i < menus.Count; i++) { List<_poseMenu> list = menus[i].OrderBy(m => m.sort).ToList(); //过滤掉不符合条件的 for (int x = list.Count - 1; x >= 1; x--) { if (string.IsNullOrEmpty(list[x].title)) list.RemoveAt(x); if (string.IsNullOrEmpty(list[x].key) && string.IsNullOrEmpty(list[x].url)) list.RemoveAt(x); } if (!string.IsNullOrEmpty(list[0].title)) { if (list.Count() == 1) //没有子菜单 { if (!string.IsNullOrEmpty(list.First().key))//click { SingleClickButton topSingleButton = new SingleClickButton(); topSingleButton.name = list.First().title; topSingleButton.key = list.First().key; topList.Add(topSingleButton); } else if (!string.IsNullOrEmpty(list[0].url))//view { SingleViewButton topSingleButton = new SingleViewButton(); topSingleButton.name = list.First().title; topSingleButton.url = list.First().url; topList.Add(topSingleButton); } } else//有子菜单 { subList = new List<SingleButton>(); SubButton topButton = new SubButton(list.First().title); list.Remove(list.First()); for (int j = 0; j < list.Count(); j++) { if (!string.IsNullOrEmpty(list[j].url))//view { SingleViewButton sub = new SingleViewButton(); sub.name = list[j].title; sub.url = list[j].url; subList.Add(sub); } else if (!string.IsNullOrEmpty(list[j].key))//click { SingleClickButton sub = new SingleClickButton(); sub.name = list[j].title; sub.key = list[j].key; subList.Add(sub); } } if (subList.Count > 0) { topButton.sub_button.AddRange(subList); topList.Add(topButton); } } } } bg.button.AddRange(topList); if (bg.button.Count() > 0) result = WeChartAPI.CreateMenu(GetToken(), bg); return Json(result, JsonRequestBehavior.AllowGet); }