public void CreateMenuTest() { return;//已经通过测试 LoadToken(); 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(tokenResult.access_token, bg); Assert.IsNotNull(result); Assert.AreEqual("ok", result.errmsg); }
static void Main(string[] args) { #if SETTING string appId = System.Configuration.ConfigurationManager.AppSettings["AppId"].ToString(); string appSecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString(); string appWebUrl = System.Configuration.ConfigurationManager.AppSettings["AppWebUrl"].ToString(); #else string appId = "wx4df221a4e9845aea"; string appSecret = "c2c6e3220de9609c79e3d1f425bba0fb"; string appHost = "http://recruit.casco.com.cn"; #endif var appOauth2 = appHost + "/oauth"; var appUrl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}#wechat_redirect", appId, appOauth2.UrlEncode(), "code", "snsapi_userinfo", "1"); ButtonGroup btGroup = new ButtonGroup(); var serviceButton = new SingleViewButton() { name = "在线服务", url = appUrl }; var userButton = new SubButton() { name = "个人中心" }; userButton.sub_button.Add(new SingleClickButton() { name = "账号绑定", type = ButtonType.click.ToString(), key = "UserBind" }); userButton.sub_button.Add(new SingleClickButton() { name = "个人信息", type = ButtonType.click.ToString(), key = "UserInfo" }); btGroup.button.Add(serviceButton); btGroup.button.Add(userButton); var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret); WxJsonResult result = CommonApi.CreateMenu(accessToken, btGroup); if (result.errmsg != null && result.errmsg != "ok") { Console.WriteLine(result.errmsg); Console.WriteLine("------------------->Build Error!"); Console.ReadKey(); } else { Console.WriteLine("Success!"); Console.ReadKey(); } }
public void CreateMenuFesco() { //Console.WriteLine("************************************开始创建微信菜单*******************************************************\n"); string appid = "wx82315287d9fc52ae"; string secret = "00dbe4ae7304f61ef9f77c7bd8fc0f48"; string PROJECT_NAME = "FESCOAsecco"; string PROJECT_URL = "weixin.hrone.cn"; var accessToken = AccessTokenContainer.TryGetAccessToken(appid, secret); Console.WriteLine(" accessToken:" + accessToken + "\n"); ButtonGroup bg = new ButtonGroup(); //首页 var About = new SingleClickButton() { name = "账户绑定", key = "bind_account", type = ButtonType.click.ToString(), }; bg.button.Add(About); //我的 var AboutMe = new SubButton() { name = "我的信息" }; AboutMe.sub_button.Add(new SingleViewButton() { url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http%3A%2F%2F" + PROJECT_URL + "%2F" + PROJECT_NAME + "%2FView%2FCommUtil%2FLoading.html?pageType=10009&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", name = "个人信息" }); AboutMe.sub_button.Add(new SingleViewButton() { url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http%3A%2F%2F" + PROJECT_URL + "%2F" + PROJECT_NAME + "%2FView%2FCommUtil%2FLoading.html?pageType=10011&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", name = "银行信息" }); bg.button.Add(AboutMe); //申请&签核 var ApplyAndApprove = new SingleViewButton() { name = "进入系统", type = ButtonType.view.ToString(), url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http%3A%2F%2F" + PROJECT_URL + "%2F" + PROJECT_NAME + "%2FView%2FCommUtil%2FMenuLoading.html&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" }; bg.button.Add(ApplyAndApprove); var result = CommonApi.CreateMenu(accessToken, bg); Console.WriteLine(" Errcode: " + result.errcode + "\n\n Errmsg: " + result.errmsg + "\n\n P2PData: " + result.P2PData + "\n"); Console.WriteLine("************************************创建微信菜单结束*******************************************************\n"); Console.ReadKey(); }
/// <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); }
/// <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/html/micro.html", name = "羊客介绍" }); subButton.sub_button.Add(new SingleClickButton() { key = "learnmore", name = "了解更多" }); btnGroup.button.Add(subButton); var clickButton = new SingleViewButton() { url = "http://yk.kerchinsheep.com/", name = "羊羊得益" }; btnGroup.button.Add(clickButton); var result = CommonApi.CreateMenu(accesstoken, btnGroup); return Json(result,JsonRequestBehavior.AllowGet); }
MenuMgr mMrg = new MenuMgr(); //创建菜单类 #endregion Fields #region Methods //保存 protected void btnSubmit_Click(object sender, EventArgs e) { try { string error = string.Empty; string accessToken = cpp.GetAccessToken(this.id, 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 = mMrg.CreateMenu(accessToken, bg); JscriptMsg("自定义菜单保存成功!", "menu_list.aspx"); } catch (Exception ex) { JscriptMsg("出错了:" + ex.Message, string.Empty); } }
/// <summary> /// 更新菜单 /// </summary> protected void btnSubmit_Click(object sender, EventArgs e) { try { Model.wx_userweixin weixin = GetWeiXinCode(); string error = ""; string accessToken = cpp.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(); }
private void GetMenu() { BuysingooShop.Web.AppCode.MenuMgr mMrg = new BuysingooShop.Web.AppCode.MenuMgr(); try { string error = ""; string accessToken = BuysingooShop.Web.AppCode.CWeixinService.IsExistAccess_Token(); //检测access_token是否失效; if (error != "") { JscriptMsg(error, "", "Error"); return; } Senparc.Weixin.MP.Entities.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(Senparc.Weixin.MP.Entities.Menu.SubButton)) { //下面无子菜单 if (topButton.GetType() == typeof(Senparc.Weixin.MP.Entities.Menu.SingleViewButton)) { //view 页面跳转 txtUrl.Text = ((Senparc.Weixin.MP.Entities.Menu.SingleViewButton)topButton).url; } else { txtKey.Text = ((Senparc.Weixin.MP.Entities.Menu.SingleClickButton)topButton).key; } } else { //下面有子菜单 IList <Senparc.Weixin.MP.Entities.Menu.SingleButton> subButtonList = ((Senparc.Weixin.MP.Entities.Menu.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(Senparc.Weixin.MP.Entities.Menu.SingleViewButton)) { Senparc.Weixin.MP.Entities.Menu.SingleViewButton sub = (Senparc.Weixin.MP.Entities.Menu.SingleViewButton)subButtonList[j]; txtSubName.Text = sub.name; txtSubUrl.Text = sub.url; } else { Senparc.Weixin.MP.Entities.Menu.SingleClickButton sub = (Senparc.Weixin.MP.Entities.Menu.SingleClickButton)subButtonList[j]; txtSubName.Text = sub.name; txtSubKey.Text = sub.key; } } } } } } } catch (Exception ex) { } }