public string GetMenuJson() { Menu menu = new Menu(); SingleClickButton item = new SingleClickButton { name = "热卖商品", key = "123" }; SingleClickButton item2 = new SingleClickButton { name = "推荐商品", key = "SINGER" }; SingleViewButton item3 = new SingleViewButton { name = "会员卡", url = "www.baidu.com" }; SingleViewButton item4 = new SingleViewButton { name = "积分商城", url = "www.baidu.com" }; SubMenu subMenu = new SubMenu { name = "个人中心" }; subMenu.sub_button.Add(item3); subMenu.sub_button.Add(item4); menu.menu.button.Add(item); menu.menu.button.Add(item2); menu.menu.button.Add(subMenu); return(new JavaScriptSerializer().Serialize(menu.menu)); }
public string GetMenuJson() { Hishop.Weixin.MP.Domain.Menu.Menu menu = new Hishop.Weixin.MP.Domain.Menu.Menu(); SingleClickButton item = new SingleClickButton { name = "热卖商品", key = "123" }; SingleClickButton button2 = new SingleClickButton { name = "推荐商品", key = "SINGER" }; SingleViewButton button3 = new SingleViewButton { name = "会员卡", url = "www.baidu.com" }; SingleViewButton button4 = new SingleViewButton { name = "积分商城", url = "www.baidu.com" }; SubMenu menu2 = new SubMenu { name = "个人中心" }; menu2.sub_button.Add(button3); menu2.sub_button.Add(button4); menu.menu.button.Add(item); menu.menu.button.Add(button2); menu.menu.button.Add(menu2); return(new JavaScriptSerializer().Serialize(menu.menu)); }
public void CreateMenuTest() { return;//已经通过测试 var accessToken = AccessTokenContainer.GetToken(_appId); ButtonGroup bg = new ButtonGroup(); //单击 bg.button.Add(new SingleClickButton() { name = "单击测试", key = "OneClick", type = ButtonType.click.ToString(), //默认已经设为此类型,这里只作为演示 }); //二级菜单 var subButton = new SubButton() { name = "二级菜单" }; subButton.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Text", name = "返回文本" }); subButton.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_News", name = "返回图文" }); subButton.sub_button.Add(new SingleClickButton() { key = "SubClickRoot_Music", name = "返回音乐" }); subButton.sub_button.Add(new SingleViewButton() { url = "http://weixin.senparc.com", name = "Url跳转" }); bg.button.Add(subButton); //url跳转 var viewButton = new SingleViewButton() { name = "Url跳转", url = "http://weixin.senparc.com" }; bg.button.Add(viewButton); var result = CommonApi.CreateMenu(accessToken, bg); Assert.IsNotNull(result); Assert.AreEqual("ok", result.errmsg); }
private SingleButton BuildMenu(MenuInfo menu) { SingleViewButton singleViewButton = new SingleViewButton() { name = menu.Title, url = menu.Url }; return(singleViewButton); }
// 创建菜单 private void BuildMenu() { var x5Meuns = new ButtonGroup(); var indexPage = new SingleViewButton { name = "起步科技", type = ButtonType.view.ToString(), url = "http://www.justep.com" }; x5Meuns.button.Add(indexPage); var demoPage = new SingleViewButton { name = "外卖", type = ButtonType.view.ToString(), url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + ConfigHelper.AppConfig["appId"] + "&redirect_uri=http%3A%2F%2Fwx.justep.com%2Ftakeout%2Findex.html&" + "response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" }; x5Meuns.button.Add(demoPage); var takeout = new SingleViewButton { name = "外卖案例", type = ButtonType.view.ToString(), url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + ConfigHelper.AppConfig["appId"] + "&redirect_uri=http%3A%2F%2Fwx.justep.com%2Fx5%2FUI2%2Ftakeout%2Findex.w&" + "response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" }; Console.WriteLine(takeout.url); x5Meuns.button.Add(takeout); try { CommonApi.DeleteMenu(AccessToken); CommonApi.CreateMenu(AccessToken, x5Meuns); } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
/// <summary> /// 初始化用户的默认菜单 /// </summary> private static void InitMenus() { var buttonGroup = new ButtonGroup(); #region 第一个菜单 var timeGalleryBtn = new SubButton("时光轴"); var pepeWebBtn = new SingleViewButton { name = "裴裴时光轴", url = "http://fengrui358.vicp.cc/TimeGallery" }; timeGalleryBtn.sub_button.Add(pepeWebBtn); buttonGroup.button.Add(timeGalleryBtn); #endregion #region 第二个菜单 var aboutBtn = new SubButton("关于"); var uploadBtn = new SingleViewButton { name = "上传", url = "http://fengrui358.vicp.cc/TimeGallery/Gallery/OAuth2ForBaseUpload" }; var uploadUserBtn = new SingleViewButton { name = "用户信息上传", url = "http://fengrui358.vicp.cc/TimeGallery/Gallery/OAuth2ForUserInfoUpload" }; aboutBtn.sub_button.Add(uploadBtn); aboutBtn.sub_button.Add(uploadUserBtn); buttonGroup.button.Add(aboutBtn); #endregion var result = CommonApi.CreateMenu(AppId, buttonGroup); if (result == null || result.errcode != 0) { throw new Exception("初始化微信默认菜单出错!"); } }
public string GetMenuJson() { Menu menu = new Menu(); SingleClickButton singleClickButton = new SingleClickButton() { name = "热卖商品", key = "123" }; SingleClickButton singleClickButton1 = singleClickButton; SingleClickButton singleClickButton2 = new SingleClickButton() { name = "推荐商品", key = "SINGER" }; SingleClickButton singleClickButton3 = singleClickButton2; SingleViewButton singleViewButton = new SingleViewButton() { name = "会员卡", url = "www.baidu.com" }; SingleViewButton singleViewButton1 = singleViewButton; SingleViewButton singleViewButton2 = new SingleViewButton() { name = "积分商城", url = "www.baidu.com" }; SingleViewButton singleViewButton3 = singleViewButton2; SubMenu subMenu = new SubMenu() { name = "个人中心" }; subMenu.sub_button.Add(singleViewButton1); subMenu.sub_button.Add(singleViewButton3); menu.menu.button.Add(singleClickButton1); menu.menu.button.Add(singleClickButton3); menu.menu.button.Add(subMenu); return((new JavaScriptSerializer()).Serialize(menu.menu)); }
private SingleButton BuildMenu(MenuInfo menu) { SingleButton result; switch (menu.BindType) { case BindType.Key: result = new SingleClickButton { name = menu.Name, key = menu.MenuId.ToString() }; break; case BindType.Topic: case BindType.HomePage: case BindType.ProductCategory: case BindType.ShoppingCar: case BindType.OrderCenter: case BindType.MemberCard: case BindType.Url: result = new SingleViewButton { name = menu.Name, url = menu.Url }; break; default: result = new SingleClickButton { name = menu.Name, key = "None" }; break; } return(result); }
public ActionResult SubmitForm(WxMenu entity, string keyValue) { try { MenuManage mMrg = new MenuManage(); WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); IList <BaseButton> topList = new List <BaseButton>(); IList <SingleButton> subList = new List <SingleButton>(); #region 菜单设置 for (int i = 0; i < 3; i++) { string txtName = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1)); string txtKey = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Key"); string txtUrl = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Url"); if (string.IsNullOrEmpty(txtName)) { // 如果名称为空,则忽略该菜单,以及下面的子菜单 continue; } subList = new List <SingleButton>(); string txtSubName; string txtSubKey; string txtSubUrl; for (int j = 0; j < 5; j++) { #region 子菜单的设置 txtSubName = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1)); txtSubKey = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Key"); txtSubUrl = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Url"); if (string.IsNullOrEmpty(txtSubName)) { continue; } if (!string.IsNullOrEmpty(txtSubUrl)) { SingleViewButton sub = new SingleViewButton(); sub.name = txtSubName.Trim(); sub.url = Utils.UrlDecode(txtSubUrl.Trim()); subList.Add(sub); } else if (!string.IsNullOrEmpty(txtSubKey)) { SingleClickButton sub = new SingleClickButton(); sub.name = txtSubName.Trim(); sub.key = txtSubKey.Trim(); subList.Add(sub); } else { //报错 :子菜单必须有key和name return(Error("二级菜单的名称和key或者url必填!")); ; } #endregion } //子菜单循环结束 if (subList != null && subList.Count > 0) { //有子菜单, 该一级菜单是SubButton if (subList.Count < 1) { return(Error("子菜单个数为2~5个!")); } SubButton topButton = new SubButton(Utils.CutString(txtName.Trim(), 16)); topButton.sub_button.AddRange(subList); topList.Add(topButton); } else { // 无子菜单 if (string.IsNullOrEmpty(txtKey) && string.IsNullOrEmpty(txtUrl)) { return(Error("若无子菜单,必须填写key或者url值!")); } if (!string.IsNullOrEmpty(txtUrl)) { //view 页面跳转 SingleViewButton topSingleButton = new SingleViewButton(); topSingleButton.name = txtName.Trim(); topSingleButton.url = Utils.UrlDecode(txtUrl.Trim()); topList.Add(topSingleButton); } else if (!string.IsNullOrEmpty(txtKey)) { SingleClickButton topSingleButton = new SingleClickButton(); topSingleButton.name = txtName.Trim(); topSingleButton.key = txtKey.Trim(); topList.Add(topSingleButton); } } } #endregion bg.button.AddRange(topList); var result = mMrg.CreateMenu(token.access_token, bg); return(Success("菜单提交成功!")); } catch (Exception ex) { return(Error("报错:" + ex.Message)); } }
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); } }
//保存 protected void btnSubmit_Click(object sender, EventArgs e) { try { string error = string.Empty; string accessToken = cpp.GetAccessToken(this.id, out error); Model.weixin_account accountModel = new BLL.weixin_account().GetModel(this.id); if (!string.IsNullOrEmpty(error)) { JscriptMsg(error, string.Empty); 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>(); //菜单设置 for (int i = 0; i < 3; i++) { 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; 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++) { //子菜单的设置 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 (txtSubName.Text.Trim() == "") { continue; } if (txtSubUrl.Text.Trim() != "") { SingleViewButton sub = new SingleViewButton(); sub.name = txtSubName.Text.Trim(); //2015-6-8 链接跳转加入OAuth授权 //删除OAuth授权 //sub.url = txtSubUrl.Text.Trim().Contains("https://open.weixin.qq.com/") ? txtSubUrl.Text.Trim() : // OAuth.GetAuthorizeUrl(accountModel.appid, txtSubUrl.Text.Trim(), "1", OAuthScope.snsapi_base); 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必填!", string.Empty); return; } } if (subList != null && subList.Count > 0) { //有子菜单, 该一级菜单是SubButton if (subList.Count < 1) { JscriptMsg("子菜单个数必须为2至5个!", string.Empty); 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值!", string.Empty); 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); } } } bg.button.AddRange(topList); var result = mMrg.CreateMenu(accessToken, bg); JscriptMsg("自定义菜单保存成功!", "menu_list.aspx"); } catch (Exception ex) { JscriptMsg("出错了:" + ex.Message, "menu_list.aspx"); } }
private void GetMenu() { try { Model.wx_userweixin weixin = GetWeiXinCode(); string error = ""; string accessToken = WeiXinCRMComm.getAccessToken(weixin.id, out error); if (error != "") { JscriptMsg(error, "", "Error"); return; } GetMenuResult result = mMrg.GetMenu(accessToken); if (result == null) { //JscriptMsg("未获得到菜单,请参考【使用规则】,自行排查问题!", "", "Error"); return; //强制刷新 //accessToken = cpp.FlushAccessToken(weixin.id, out error); //result = CommonApi.GetMenu(accessToken); } 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)) 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)) { //view 页面跳转 txtUrl.Text = ((SingleViewButton)topButton).url; } else { txtKey.Text = ((SingleClickButton)topButton).key; } } 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)) 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) { } }
/// <summary> /// 更新菜单 /// </summary> protected void btnSubmit_Click(object sender, EventArgs e) { try { Model.wx_userweixin weixin = GetWeiXinCode(); string error = ""; string accessToken = WeiXinCRMComm.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"); } }
protected void Page_Load(object sender, EventArgs e) { string signature = Request["signature"]; string timestamp = Request["timestamp"]; string nonce = Request["nonce"]; string echostr = Request["echostr"]; string inputXml = string.Empty; using (StreamReader sr = new StreamReader(Request.InputStream)) { inputXml = sr.ReadToEnd(); inputXml = HttpUtility.UrlDecode(inputXml); } string updateMenu = Request["updatemenu"]; if (!string.IsNullOrWhiteSpace(updateMenu)) { foreach (var menu in Activator.WeChatMenuContainer.WeChatMenus) { try { if (!AccessTokenContainer.CheckRegistered(menu.AppId)) { AccessTokenContainer.Register(menu.AppId, menu.Secret); } AccessTokenResult tokenRes = null; try { tokenRes = AccessTokenContainer.GetTokenResult(menu.AppId); //CommonAPIs.CommonApi.GetToken(appId, appSecret); WriteContent(string.Format("获取到 token 为:{0}, 有效时间为 {1} 秒。", tokenRes.access_token, tokenRes.expires_in)); //var menuRes = CommonApi.GetMenu(tokenRes.access_token); } catch { WriteContent(string.Format("获取到 token 失败, appid: {0},secret: {1}。", menu.AppId, menu.Secret)); } try { if (tokenRes != null) { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var menuButton in menu.MenuButtons) { BaseButton but = null; switch (menuButton.Type) { case ButtonType.Click: but = new SingleClickButton() { name = menuButton.Name, key = menuButton.Key, type = "click" }; break; case ButtonType.View: but = new SingleViewButton() { name = menuButton.Name, url = menuButton.Url, type = "view" }; break; case ButtonType.SubButton: List <SingleButton> subButtons = new List <SingleButton>(); foreach (var subBut in menuButton.MenuSubButtons) { SingleButton singleBut = null; switch (subBut.Type) { case ButtonType.Click: singleBut = new SingleClickButton() { name = subBut.Name, key = subBut.Key, type = "click" }; break; case ButtonType.View: singleBut = new SingleViewButton() { name = subBut.Name, url = subBut.Url, type = "view" }; break; } if (singleBut != null) { subButtons.Add(singleBut); } } but = new SubButton() { name = menuButton.Name, sub_button = subButtons }; break; } if (but != null) { bg.button.Add(but); } } var result = CommonApi.CreateMenu(tokenRes.access_token, bg); WriteContent(string.Format("创建结果信息:{0}, 返回值 {1} ({2})。", result.errmsg, (int)result.errcode, result.errcode.ToString())); } } catch { WriteContent("创建菜单失败!"); } } catch (Exception) { //TODO:为简化代码,这里不处理异常(如Token过期) WriteContent("执行过程发生错误!"); } } } foreach (var proxy in Activator.WeChatProxyContainer.WeChatProxies) { string token = proxy.Token; if (Request.HttpMethod == "GET") { //get method - 仅在微信后台填写URL验证时触发 if (CheckSignature.Check(signature, timestamp, nonce, token)) { WriteContent(echostr); //返回随机字符串则表示验证通过 //如果有多个相同的Token,则第一个验证通过就返回 break; } else { //WriteContent("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, token) + "。" + // "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"); //如果失败应该不返回任何东西,以便循环校验下一个Token continue; } } else { //post method - 当有用户想公众账号发送消息时触发 if (!CheckSignature.Check(signature, timestamp, nonce, token)) { WriteContent("参数错误!"); continue; } //v4.2.2之后的版本,可以设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制 var maxRecordCount = 10; //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 //var messageHandler = new CustomMessageHandler(Request.InputStream, maxRecordCount); IMessageHandler messageHandler = null; using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(inputXml))) { Type type = proxy.Bundle.LoadClass(proxy.Handler); var parameters = new object[] { stream, token, proxy.AppID, proxy.Secret, maxRecordCount }; messageHandler = System.Activator.CreateInstance(type, parameters) as IMessageHandler; } try { if (messageHandler != null) { //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。 messageHandler.RequestDocument.Save( Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" + messageHandler.RequestMessage.FromUserName + ".txt")); //执行微信处理过程 messageHandler.Execute(); //测试时可开启,帮助跟踪数据 messageHandler.ResponseDocument.Save( Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" + messageHandler.ResponseMessage.ToUserName + ".txt")); WriteContent(messageHandler.ResponseDocument.ToString()); continue; } } catch (Exception ex) { using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt"))) { tw.WriteLine(ex.Message); tw.WriteLine(ex.InnerException.Message); if (messageHandler.ResponseDocument != null) { tw.WriteLine(messageHandler.ResponseDocument.ToString()); } tw.Flush(); tw.Close(); } WriteContent(""); } } } Response.End(); }
public ActionResult AddMenu() { //获取accessToken var accessToken = AccessTokenContainer.TryGetAccessToken(AppId, AppSecret); //删除原有菜单 var result1 = CommonApi.DeleteMenu(accessToken); //创建新菜单 ButtonGroup bg = new ButtonGroup(); //第1个一级菜单 var firstButton = new SubButton() { name = "Ai我家" }; firstButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinHome/Index", name = "首页" }); firstButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinPropertyNotice/PropertyNoticeList/", name = "物业公告" }); firstButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinQuestion/QuestionList/", name = "上报问题" }); firstButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinExpenseNotice/ExpenseNoticeList/", name = "缴费通知" }); firstButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinTopic/Index/", name = "社区话题" }); bg.button.Add(firstButton); //第2个一级菜单 var secondButton = new SingleViewButton() { url = "http://weidian.com/s/382033745?wfr=c/", name = "绿色直供" }; bg.button.Add(secondButton); //第3个一级菜单 var threeButton = new SubButton() { name = "更多" }; threeButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinProduct/Index/", name = "产品介绍" }); threeButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinProduct/QrCodeShare/", name = "分享友邻" }); threeButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinSocialCircle/Index/", name = "业主圈子" }); threeButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinPersonalCenter/Index/", name = "个人中心" }); threeButton.sub_button.Add(new SingleViewButton() { url = "http://v.homekeeper.com.cn/WeixinIdentityBind/Index/", name = "绑定小区" }); bg.button.Add(threeButton); var result2 = CommonApi.CreateMenu(accessToken, bg); return(Content("创建菜单成功,请查看。" + result1.errmsg + "+44" + result2.errmsg)); }
/// <summary> /// 测试创建菜单的部分 /// </summary> /// <param name="MemuName"></param> /// <returns></returns> public ActionResult CreateMenuTest() { var accesstoken = AccessTokenContainer.TryGetToken(AppId, AppSecret); var btnGroup = new ButtonGroup(); //二级菜单 var subButton = new SubButton() { name = "羊客商城" }; subButton.sub_button.Add(new SingleViewButton() { url = "http://yk.kerchinsheep.com/home/presaleproduct", name = "羊客商城" }); subButton.sub_button.Add(new SingleViewButton() { url = "http://yk.kerchinsheep.com/home/PreSaleOrderList", name = "商城订单" }); subButton.sub_button.Add(new SingleViewButton() { url = "http://yk.kerchinsheep.com/html/recipeMain.html", name = "羊客食谱" }); btnGroup.button.Add(subButton); var clickButton = new SingleViewButton() { url = "http://yk.kerchinsheep.com/", name = "羊羊得益" }; btnGroup.button.Add(clickButton); var subButton1 = new SubButton() { name = "更多精彩" }; subButton1.sub_button.Add(new SingleViewButton() { url = "http://yk.kerchinsheep.com/html/micro.html", name = "羊客介绍" }); subButton1.sub_button.Add(new SingleClickButton() { key = "learnmore", name = "了解更多" }); var clickButton1 = new SingleViewButton() { url = "http://activity.kerchinsheep.com/app/h5/zy", name = "全民套羊" }; subButton1.sub_button.Add(clickButton1); btnGroup.button.Add(subButton1); var result = CommonApi.CreateMenu(accesstoken, btnGroup); return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult GetMenu() { try { MenuManage mMrg = new MenuManage(); WxBaseConfigEntity weixin = app.GetDefaultConfig(); AccessTokenResult token = AccessTokenContainer.GetAccessTokenResult(weixin.AppId); if (token.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Error(token.errmsg)); } GetMenuResult result = mMrg.GetMenu(token.access_token); if (result == null) { return(Error("未获得到菜单,请参考【使用规则】,自行排查问题!")); } var topButtonList = result.menu.button; int topNum = topButtonList.Count; //List<string> menu = new List<string>(); Dictionary <string, string> menu = new Dictionary <string, string>(); for (int i = 0; i < topNum; i++) { var topButton = topButtonList[i]; if (topButton != null) { menu.Add("Top" + (i + 1), topButton.name); if (topButton.GetType() != typeof(SubButton)) { //下面无子菜单 if (topButton.GetType() == typeof(SingleViewButton)) { //view 页面跳转 menu.Add("Top" + (i + 1) + "Key", ((SingleViewButton)topButton).url); } else { menu.Add("Top" + (i + 1) + "Url", ((SingleClickButton)topButton).key); } } else { //下面有子菜单 IList <SingleButton> subButtonList = ((SubButton)topButton).sub_button; if (subButtonList != null && subButtonList.Count > 0) { for (int j = 0; j < subButtonList.Count; j++) { if (subButtonList[j].GetType() == typeof(SingleViewButton)) { SingleViewButton sub = (SingleViewButton)subButtonList[j]; menu.Add("Menu" + (i + 1) + (j + 1), sub.name); menu.Add("Menu" + (i + 1) + (j + 1) + "Url", sub.url); } else { SingleClickButton sub = (SingleClickButton)subButtonList[j]; menu.Add("Menu" + (i + 1) + (j + 1), sub.name); menu.Add("Menu" + (i + 1) + (j + 1) + "Key", sub.key); } } } } } } return(Success("菜单获取成功", menu)); } catch (Exception ex) { return(Error(ex.Message)); } }
//保存 protected void btnSubmit_Click(object sender, EventArgs e) { try { string error = string.Empty; string accessToken = wxComm.GetAccessToken(out error); if (!string.IsNullOrEmpty(error)) { JscriptMsg(error, string.Empty); 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>(); //菜单设置 for (int i = 0; i < 3; i++) { 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; 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++) { //子菜单的设置 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 (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必填!", string.Empty); return; } } if (subList != null && subList.Count > 0) { //有子菜单, 该一级菜单是SubButton if (subList.Count < 1) { JscriptMsg("子菜单个数必须为2至5个!", string.Empty); 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值!", string.Empty); 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); } } } bg.button.AddRange(topList); var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.CreateMenu(accessToken, bg); JscriptMsg("自定义菜单保存成功!", "menu_edit.aspx"); } catch (Exception ex) { JscriptMsg("出错了:" + ex.Message, string.Empty); } }
/// <summary> /// /// </summary> /// <param name="menuInfo"></param> /// <returns></returns> public SingleButton GetButton(Menu menuInfo) { SingleButton model = null; var type = menuInfo.type.ToLower(); if (type == ButtonType.click.ToStringEx()) { model = new SingleClickButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.location_select.ToStringEx()) { model = new SingleLocationSelectButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.miniprogram.ToStringEx()) { model = new SingleMiniProgramButton() { url = menuInfo.url, appid = "", pagepath = "", name = menuInfo.name }; } else if (type == ButtonType.pic_photo_or_album.ToStringEx()) { model = new SinglePicPhotoOrAlbumButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.pic_sysphoto.ToStringEx()) { model = new SinglePicSysphotoButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.pic_weixin.ToStringEx()) { model = new SinglePicWeixinButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.scancode_push.ToStringEx()) { model = new SingleScancodePushButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.scancode_waitmsg.ToStringEx()) { model = new SingleScancodeWaitmsgButton() { key = menuInfo.key, name = menuInfo.name }; } else if (type == ButtonType.view.ToStringEx()) { model = new SingleViewButton() { url = menuInfo.url, name = menuInfo.name }; } return(model); }