public void MenusApiTest_Create() { var menuJson = "{\"button\":[{\"type\":\"click\",\"name\":\"今日歌曲\",\"key\":\"V1001_TODAY_MUSIC\"},{\"name\":\"菜单\",\"sub_button\":[{\"type\":\"view\",\"name\":\"搜索\",\"url\":\"http://www.soso.com/\"},{\"type\":\"view\",\"name\":\"视频\",\"url\":\"http://v.qq.com/\"},{\"type\":\"click\",\"name\":\"赞一下我们\",\"key\":\"V1001_GOOD\"}]}]}"; var model = JsonConvert.DeserializeObject <MenuInfo>(menuJson); var result = weChatApi.Create(model); if (!result.IsSuccess()) { Assert.Fail("创建菜单数据失败,返回结果如下:" + result.DetailResult); } }
public ApiResult SaveAndPublishMenu(string json, UserContext userContext, DomainContext domainContext) { MenuEntity menuEntity = new MenuEntity(); menuEntity.DomainId = userContext.User.Domain; menuEntity.Menu = json; _dataBase.Remove(menuEntity); _dataBase.Insert(menuEntity); ApiResult result = new ApiResult(); //未认证订阅号没有自定义菜单权限 if (domainContext.Authorizer == null || domainContext.Authorizer.AuthorizationType == EnumAuthorizationType.UnauthorizedSubscription) { result.Success = false; result.Message = "未认证订阅号没有自定义菜单权限。"; return(result); } List <ButtonBase> _buttonList = new List <ButtonBase>(); JObject jObject = JObject.Parse(json); #region 解析 JArray buttonArray = (JArray)jObject["button"]; foreach (var button in buttonArray) { if (button["sub_button"] == null || button["sub_button"].Count() == 0) { ButtonBase newButton = null; switch (button["type"].ToString()) { case "wxc_function": newButton = GetWxcFunctionButton(button, domainContext); break; case "click": newButton = GetClickButton(button, domainContext); break; case "view": newButton = JsonHelper.Deserialize <ButtonView>(button.ToString()); break; case "wxc_campaign": newButton = GetWxcCampaignButton(button, domainContext); break; case "wxc_information": newButton = GetWxcInformationButton(button, domainContext); break; case "wxc_customForm": newButton = GetWxcCustomFormButton(button, domainContext); break; case "mediaId": newButton = GetMediaIdButton(button); break; } _buttonList.Add(newButton); } else { ParentButton parentButton = JsonHelper.Deserialize <ParentButton>(button.ToString()); parentButton.SubButton = new List <ButtonBase>(); JArray subButtonArray = (JArray)button["sub_button"]; foreach (var subButton in subButtonArray) { ButtonBase newSubButton = null; switch (subButton["type"].ToString()) { case "wxc_function": newSubButton = GetWxcFunctionButton(subButton, domainContext); break; case "click": newSubButton = GetClickButton(subButton, domainContext); break; case "view": newSubButton = JsonHelper.Deserialize <ButtonView>(subButton.ToString()); break; case "wxc_campaign": newSubButton = GetWxcCampaignButton(subButton, domainContext); break; case "wxc_information": newSubButton = GetWxcInformationButton(subButton, domainContext); break; case "wxc_customForm": newSubButton = GetWxcCustomFormButton(subButton, domainContext); break; case "mediaId": newSubButton = GetMediaIdButton(subButton); break; } parentButton.SubButton.Add(newSubButton); } _buttonList.Add(parentButton); } } #endregion ButtonWrapper menu = new ButtonWrapper(); menu.Button = _buttonList; RequestApiResult apiResult = MenuApi.Create(menu, domainContext.AccessToken); result.Success = apiResult.Success; result.Message = apiResult.Message; return(result); }