using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Autofac; using EMIS.Entities; using EMIS.Utility; using EMIS.CommonLogic.SystemServices; using Bowin.Common; using Bowin.Web.Controls.Mvc; namespace EMIS.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.StartsWith("http://") ? "" : "/") + menu.Url + (menu.Url.Contains("?") ? "&" : "?") + "MNU=" + menu.MenuNo) : null, ParentMnuId = menu.ParentMenuNo, state = "close"//(menuList.Exists(x => x.ParentMenuNo == menu.MenuNo)) ? "open" : "close" }); } } return AccordionTreeExtensions.AccordionTree(htmlHelper, dataItemList, accordiontreeOptions, htmlAttributes); } private static List GetMenuList(HtmlHelper htmlHelper) { using (var scope = AutofacHelper.Container.BeginLifetimeScope()) { IMenuServices menuServices = scope.Resolve(); var user = htmlHelper.ViewContext.HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal; var menuList = menuServices.GetMenuByUserIDAndRoleID(user.UserID, user.RoleID); 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 { using (var scope = AutofacHelper.Container.BeginLifetimeScope()) { IMenuServices menuServices = scope.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; } } }