using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Autofac; using EMISOnline.Entities; using EMISOnline.Utility; using EMISOnline.CommonLogic.SystemServices; using Bowin.Common; using Bowin.Web.Controls.Mvc; using EMISOnline.CommonLogic.SystemServices; namespace EMISOnline.Web.Controls { public static class MenuExtensions { public static MvcHtmlString MenuTree(this HtmlHelper htmlHelper, AccordionTreeOptions accordiontreeOptions = null, System.Collections.Generic.IDictionary htmlAttributes = null) { List dataItemList = new List(); BaseExtensions.GetContextUser(); var menuList = GetMenuList(htmlHelper); /* foreach (var menu in menuList.Where(x => x.IsVisible == true)) { if (!dataItemList.Exists(x => x.MnuId == menu.MenuNo)) { dataItemList.Add(new AccordionTreeDataItem { MnuId = menu.MenuNo, MnuName = menu.MenuName, MnuOrder = menu.OrderNo, MnuPic = menu.Icon, MnuProgram = ((menu.Url ?? "") != "") ? ("/" + menu.Url + (menu.Url.Contains("?") ? "&" : "?") + "MNU=" + menu.MenuNo) : null, ParentMnuId = menu.ParentMenuNo, state = "close"//(menuList.Exists(x => x.ParentMenuNo == menu.MenuNo)) ? "open" : "close" }); } }*/ //暂时在此生成菜单 var user = htmlHelper.ViewContext.HttpContext.User as EMISOnline.Utility.FormValidate.CustomPrincipal; string rootMenuNo = user.IsStudent ? "MnuStu" : "Mnu"; string html = ""; List mRoot = menuList.Where(p => p.ParentMenuNo == rootMenuNo).ToList(); foreach (Sys_Menu root in mRoot) { html += "\r\n
"; html += "\r\n\t" + root.MenuName + ""; html += createChildMenu(menuList, root.MenuNo); html += "\r\n
"; } return new MvcHtmlString(html); //暂不实现 //return AccordionTreeExtensions.AccordionTree(htmlHelper, dataItemList, accordiontreeOptions, htmlAttributes); } private static string createChildMenu(List list,string parentMenuNo) { string rtn = ""; List mChild = list.Where(p => p.ParentMenuNo == parentMenuNo).ToList(); foreach (Sys_Menu m in mChild) { rtn += "\r\n\t\t
  • " + m.MenuName + "
  • "; } if (rtn.Length > 0) { rtn = "\r\n\t
      " + rtn + "
    "; } return rtn; } private static string ProcessUrl(string url) { if (url == null) return null; string result = url; if (url.StartsWith("/")) { UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); result = urlHelper.Content("~" + url); } return result; } private static List GetMenuList(HtmlHelper htmlHelper) { using (var scope = AutofacHelper.Container.BeginLifetimeScope()) { IMenuServices menuServices = scope.Resolve(); var user = htmlHelper.ViewContext.HttpContext.User as EMISOnline.Utility.FormValidate.CustomPrincipal; var menuList = menuServices.GetMenuByUserID(user.UserID); return menuList.ToList(); } } /// /// 导航栏,根据菜单ID分析出用户的菜单路径 /// /// /// 自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary { /// { "style", "width: 100%;" }, { "name", "ddlUsers" } /// } /// /// public static MvcHtmlString Position(this HtmlHelper htmlHelper, IDictionary htmlAttributes = null) { string menuNo = HttpContext.Current.Request["MNU"]; string path = ""; string html = ""; if (string.IsNullOrEmpty(menuNo)) { path = "系统首页"; } else { IMenuServices menuServices = AutofacHelper.Container.Resolve(); path = menuServices.GetFullMenuPath(menuNo); } if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } html = "
    \n"; html += "
    x.Key + "=\"" + x.Value + "\"")) + ">"; html += "当前位置:" + path + "
    \n"; html += "
    "; return MvcHtmlString.Create(html); } public static string AddMenuParameter(this string url) { return AddMenuParamenter(url, HttpContext.Current.Request["MNU"]); } private static string AddMenuParamenter(string url, string menuNo) { string result; bool hasQuestionMark = url.IndexOf('?') > 0; if (hasQuestionMark) { result = url + "&"; } else { result = url + "?"; } result += "MNU=" + menuNo; return result; } } }