/// <summary> /// 创建微信菜单 /// </summary> /// <param name="siteCode"></param> public static void CreateWeiXinMenu(string wxConfigID) { if (!string.IsNullOrEmpty(wxConfigID)) { WXConfigDAL dal = new WXConfigDAL(); WXConfig wxConfig = dal.GetWXConfigByID(wxConfigID); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); MenuDAL dal2 = CreateInstance(); List <Menu> menus = dal2.GetWeiXinMenu(wxConfig.ID); MenuInfo menuInfo = new MenuInfo(); var buttons = from x in menus where x.MenuType.ToLower() == "button" orderby x.OrderNum ascending select x; foreach (Menu button in buttons) { if (!string.IsNullOrEmpty(button.ButtonType)) { switch (button.ButtonType.ToLower()) { case "view": string url = button.AccessLink; if (!string.IsNullOrEmpty(button.RedirectScope)) { url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), button.RedirectScope, button.RedirectState); } menuInfo.Buttons.Add(new MenuViewBtton() { Name = button.ButtonName, Type = button.ButtonType, Url = url }); break; case "click": menuInfo.Buttons.Add(new MenuClickButton() { Name = button.ButtonName, Type = button.ButtonType, Key = button.ButtonKey }); break; default: menuInfo.Buttons.Add(new MenuEventButton() { Name = button.ButtonName, Type = button.ButtonType, Key = button.ButtonKey }); break; } } else { MenuSubButton subButton = new MenuSubButton() { Name = button.ButtonName }; var subButtons = from x in menus where x.MenuType.ToLower() == "sub_button" && x.ParentMenuID == button.ID orderby x.OrderNum ascending select x; foreach (Menu subButton1 in subButtons) { switch (subButton1.ButtonType.ToLower()) { case "view": string url = subButton1.AccessLink; if (!string.IsNullOrEmpty(subButton1.RedirectScope)) { url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), subButton1.RedirectScope, subButton1.RedirectState); } subButton.SubButtons.Add(new MenuViewBtton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Url = url }); break; case "click": subButton.SubButtons.Add(new MenuClickButton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Key = subButton1.ButtonKey }); break; default: subButton.SubButtons.Add(new MenuEventButton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Key = subButton1.ButtonKey }); break; } } menuInfo.Buttons.Add(subButton); } } string strJosn = menuInfo.ToJson(); weixin.CreateCustomMenu(menuInfo); } } }