public JsonResult PublishMenu(string companyId) { try { WX_ApiConfig config = WXApiConfigServices.QueryWXApiConfig(companyId); if (config == null || string.IsNullOrWhiteSpace(config.AppId) || string.IsNullOrWhiteSpace(config.AppSecret) || string.IsNullOrWhiteSpace(config.SystemName)) { throw new MyException("获取微信基础信息失败,请确认微信基础信息已配置"); } var accessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, false); var buttonGroup = ToButtonGroup(WXMenuServices.GetMenus(companyId)); TxtLogServices.WriteTxtLogEx("PublishMenu", JsonHelper.GetJsonString(buttonGroup)); var result = WxApi.CreateMenu(companyId, accessToken, buttonGroup); if (!result) { throw new MyException("发布菜单失败"); } return(Json(MyResult.Success())); } catch (MyException ex) { return(Json(MyResult.Error(ex.Message))); } catch (Exception ex) { ExceptionsServices.AddExceptions(ex, "发布菜单失败"); return(Json(MyResult.Error("发布菜单失败"))); } }
public JsonResult Delete(int id, string companyId) { try { bool result = WXMenuServices.Delete(companyId, id); if (!result) { throw new MyException("删除菜单失败"); } return(Json(MyResult.Success())); } catch (MyException ex) { return(Json(MyResult.Error(ex.Message))); } catch (Exception ex) { ExceptionsServices.AddExceptions(ex, "删除菜单失败"); return(Json(MyResult.Error("删除菜单失败"))); } }
public JsonResult GetMasterMenu(string companyId) { Dictionary <string, string> dicMenu = new Dictionary <string, string>(); dicMenu.Add("-1", "一级菜单"); JsonResult json = new JsonResult(); List <WX_Menu> menus = WXMenuServices.GetMenus(companyId).Where(p => p.MasterID == null || p.MasterID == 0).ToList(); foreach (var item in menus) { dicMenu.Add(item.ID.ToString(), item.MenuName); } var result = from p in dicMenu select new { id = p.Key, text = p.Value }; json.Data = result; return(json); }
private void CheckMenu(int?masterId, int id, string companyId) { if (id > 1) { return; } List <WX_Menu> menus = WXMenuServices.GetMenus(companyId); if (masterId.HasValue) { if (menus.Count(p => p.MasterID == masterId) >= 5) { throw new MyException("最多只能添加5个二级菜单"); } } else { if (menus.Count(p => p.MasterID == null || p.MasterID == 0) >= 3) { throw new MyException("最多只能添加3个一级级菜单"); } } }
public string GetMenuData(string companyId) { try { List <WX_Menu> menus = WXMenuServices.GetMenus(companyId); string json = "{\"rows\":["; List <WX_Menu> topMenus = menus.Where(p => p.MasterID == 0).OrderBy(p => p.Sort).ToList(); foreach (WX_Menu dr in topMenus) { var childs = menus.Where(p => p.MasterID == dr.ID).OrderBy(o => o.Sort); string masterId = dr.MasterID != 0 ? dr.MasterID.ToString() : string.Empty; string hasChildMenu = childs.Count() > 0 ? "1" : "0"; json += "{\"id\":\"" + dr.ID + "\""; json += ",\"MenuName\":\"" + dr.MenuName + "\""; json += ",\"Url\":\"" + dr.Url + "\""; json += ",\"KeywordId\":\"" + GetKeywordId(dr) + "\""; json += ",\"ModuleId\":\"" + GetModuleId(dr) + "\""; json += ",\"CompanyID\":\"" + dr.CompanyID + "\""; json += ",\"MenuType\":" + (int)dr.MenuType + ""; json += ",\"MenuTypeDes\":\"" + GetMenuTypeDescription(menus, dr.ID) + "\""; json += ",\"MenuTypeValue\":\"" + GetMenuTypeValue(menus, dr.ID) + "\""; json += ",\"Sort\":\"" + dr.Sort + "\""; json += ",\"MasterID\":\"" + masterId + "\""; json += ",\"MinIprogramAppId\":\"" + dr.MinIprogramAppId + "\""; json += ",\"MinIprogramPagePath\":\"" + dr.MinIprogramPagePath + "\""; json += ",\"HasChildMenu\":\"" + hasChildMenu + "\""; json += ",\"iconCls\":\"my-menu-icon\"},"; foreach (var obj in childs) { string childmasterId = obj.MasterID == 0 ? obj.MasterID.ToString() : string.Empty; json += "{\"id\":\"" + obj.ID + "\""; json += ",\"MenuName\":\"" + obj.MenuName + "\""; json += ",\"Url\":\"" + obj.Url + "\""; json += ",\"KeywordId\":\"" + GetKeywordId(obj) + "\""; json += ",\"ModuleId\":\"" + GetModuleId(obj) + "\""; json += ",\"CompanyID\":\"" + dr.CompanyID + "\""; json += ",\"MenuType\":" + (int)obj.MenuType + ""; json += ",\"MenuTypeDes\":\"" + GetMenuTypeDescription(menus, obj.ID) + "\""; json += ",\"MenuTypeValue\":\"" + GetMenuTypeValue(menus, obj.ID) + "\""; json += ",\"Sort\":\"" + obj.Sort + "\""; json += ",\"MasterID\":\"" + obj.MasterID + "\""; json += ",\"MinIprogramAppId\":\"" + obj.MinIprogramAppId + "\""; json += ",\"MinIprogramPagePath\":\"" + obj.MinIprogramPagePath + "\""; json += ",\"HasChildMenu\":\"0\""; json += ",\"_parentId\":\"" + dr.ID + "\""; json += ",\"iconCls\":\"my-menu-icon\"},"; } } if (topMenus.Count() > 0) { json = json.Substring(0, json.Length - 1); } json += "]}"; return(json); } catch (Exception ex) { ExceptionsServices.AddExceptions(ex, "微信菜单管理获取菜单失败"); return("{\"rows\":[]}"); } }
public JsonResult AddOrUpdate(WX_Menu model) { try { model.MasterID = model.MasterID == -1 ? 0 : model.MasterID; CheckMenu(model.MasterID, model.ID, model.CompanyID); if (model.MenuType == Common.Entities.Enum.MenuType.Url) { if (string.IsNullOrWhiteSpace(model.Url)) { throw new MyException("请填写链接地址"); } model.KeywordId = 0; } if (model.MenuType == Common.Entities.Enum.MenuType.GKeyValue) { if (model.KeywordId == 0) { throw new MyException("请选择关键字"); } model.Url = string.Empty; } if (model.MenuType == Common.Entities.Enum.MenuType.WeiXinModule) { if (string.IsNullOrWhiteSpace(Request["Module"])) { throw new MyException("请选择系统模块"); } model.KeywordId = int.Parse(Request["Module"].ToString()); model.Url = string.Empty; } if (model.MenuType == Common.Entities.Enum.MenuType.MinIprogram) { if (string.IsNullOrWhiteSpace(model.Url)) { throw new MyException("请填写链接地址"); } if (string.IsNullOrWhiteSpace(model.MinIprogramAppId)) { throw new MyException("请填写小程序APPID"); } if (string.IsNullOrWhiteSpace(model.MinIprogramPagePath)) { throw new MyException("请填写小程序的页面路径"); } } if (model.ID < 1) { bool result = WXMenuServices.Create(model); if (!result) { throw new MyException("添加失败"); } } else { bool result = WXMenuServices.Update(model); if (!result) { throw new MyException("修改失败"); } } return(Json(MyResult.Success())); } catch (MyException ex) { return(Json(MyResult.Error(ex.Message))); } catch (Exception ex) { ExceptionsServices.AddExceptions(ex, "保存菜单信息失败"); return(Json(MyResult.Error("保存失败"))); } }