protected void SubmitButton_Click(object sender, EventArgs e) { WechatMenuInfo wechatmenu = new WechatMenuInfo(); wechatmenu.Id = RequestHelper.GetQueryString <int>("ID"); wechatmenu.Name = Name.Text; wechatmenu.FatherId = Convert.ToInt32(FatherID.Text); wechatmenu.OrderId = Convert.ToInt32(OrderID.Text); wechatmenu.Type = this.MenuType.Text; if (wechatmenu.Type == "click") { wechatmenu.Key = MenuKey.Text; } else { wechatmenu.Url = MenuKey.Text; } string alertMessage = ShopLanguage.ReadLanguage("AddOK"); //添加 if (wechatmenu.Id <= 0) { if (FatherID.Text == "0" && WechatMenuBLL.ReadRootList().Count >= 3) { ScriptHelper.Alert("最多只能添加3个一级菜单"); return; } if (FatherID.Text != "0" && WechatMenuBLL.ReadChildList(Convert.ToInt32(FatherID.Text)).Count >= 5) { ScriptHelper.Alert("每个一级菜单下最多只能添加5个二级菜单"); return; } int id = WechatMenuBLL.Add(wechatmenu); } else//修改 { //如果更改了父级菜单且原先已有3个一级菜单 string _oldFatherId = WechatMenuBLL.Read(wechatmenu.Id).FatherId.ToString(); if (FatherID.Text == "0" && FatherID.Text != _oldFatherId && WechatMenuBLL.ReadRootList().Count >= 3) { ScriptHelper.Alert("最多只能添加3个一级菜单"); return; } //如果更改了父级菜单且该菜单下原先已有5个二级菜单 if (FatherID.Text != "0" && FatherID.Text != _oldFatherId && WechatMenuBLL.ReadChildList(Convert.ToInt32(FatherID.Text)).Count >= 5) { ScriptHelper.Alert("每个一级菜单下最多只能添加5个二级菜单"); return; } WechatMenuBLL.Update(wechatmenu); alertMessage = ShopLanguage.ReadLanguage("UpdateOK"); } ScriptHelper.Alert(alertMessage, RequestHelper.RawUrl); }
public void ProcessRequest(HttpContext context) { //获取access_token string access_Token = WechatCommon.IsExistAccess_Token(); if (!string.IsNullOrEmpty(access_Token) && access_Token != "error") { #region 除原菜单 string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", access_Token); //HTTP get 请求 WebRequestPostOrGet(url, ""); #endregion #region 创建菜单 url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", access_Token); List <Menu> menus = new List <Menu>(); List <WechatMenuInfo> topMenuList = WechatMenuBLL.ReadRootList(); //subbuttons.Clear(); foreach (var topmenu in topMenuList) { List <SubButton> subbuttons = new List <SubButton>(); foreach (var submenu in WechatMenuBLL.ReadChildList(topmenu.Id)) { //if (submenu.Type == "click") //{ // subbuttons.Add(new SubButton { name = submenu.Name, type = submenu.Type, key = submenu.Key }); //} //else //{ subbuttons.Add(new SubButton { name = submenu.Name, type = submenu.Type, url = submenu.Url, key = submenu.Key }); //} } //if (subbuttons.Count > 0) //{//如果有二级菜单 // menus.Add(new Menu { name = topmenu.Name, sub_button = subbuttons.ToArray<Button>() }); //} //else //{ menus.Add(new Menu { name = topmenu.Name, type = topmenu.Type, key = topmenu.Key, url = topmenu.Url, sub_button = subbuttons.ToArray <Button>() }); //} } #region // menus.Add( // new Menu() // { // name = "微商城", // sub_button = new SubButton[] // { // new SubButton() // { // name = "首页", // url = "http://dzwz18.ncms5.hnjing.net/mobile", // type = "view" // }, // new SubButton() // { // name = "分类", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/ProductClass.html", // type = "view" // } // } // }); // menus.Add( // new Menu() // { // name = "个人中心", // sub_button = new SubButton[] // { // new SubButton() // { // name = "登录", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/User/login.html", // type = "view" // }, // new SubButton() // { // name = "注册", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/User/Register.html", // type = "view" // }, // new SubButton() // { // name = "我的竞网", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/User/index.html", // type = "view" // } // } // }); // menus.Add( //new Menu() //{ // name = "用户服务", // sub_button = new SubButton[] // { // new SubButton() // { // name = "购物车", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/cart.html", // type = "view" // }, // new SubButton() // { // name = "找回密码", // url = "http://dzwz18.ncms5.hnjing.net/Mobile/User/FindPasswordInit.html", // type = "view" // }, // new SubButton() // { // name = "男装搜索", // key="男装", // type = "click" // } // } //}); #endregion WeixinMenu weixinMenu = new WeixinMenu() { button = menus }; //参数 string param = JsonConvert.SerializeObject(weixinMenu); byte[] bs = Encoding.UTF8.GetBytes(param); //参数转化为ascii码 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); //创建request req.Method = "POST"; //确定传值的方式,此处为post方式传值 req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } string responseString = string.Empty; using (HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse()) { StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); responseString = sr.ReadToEnd(); } ReturnMsg msg = JsonConvert.DeserializeObject <ReturnMsg>(responseString); if (msg.errcode != "0") { //context.Response.Write("系统忙,请稍后重试"); context.Response.Write("error|" + msg.errcode + ":" + msg.errmsg); context.Response.End(); } else { context.Response.Write("ok|自定义菜单成功"); context.Response.End(); } #endregion } }