123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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<string, string> htmlAttributes = null)
- {
- List<AccordionTreeDataItem> dataItemList = new List<AccordionTreeDataItem>();
- 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<Sys_Menu> mRoot = menuList.Where(p => p.ParentMenuNo == rootMenuNo).ToList();
- foreach (Sys_Menu root in mRoot)
- {
- html += "\r\n<div class=\"index_left_menu\" onclick=\"onMenu('" + ProcessUrl(root.Url) + "');\" >";
- html += "\r\n\t<a href=\"javascript:void();\" onclick=\"onMenu('" + ProcessUrl(root.Url) + "',this);\">" + root.MenuName + "</a>";
- html += createChildMenu(menuList, root.MenuNo);
- html += "\r\n</div>";
- }
- return new MvcHtmlString(html);
- //暂不实现
- //return AccordionTreeExtensions.AccordionTree(htmlHelper, dataItemList, accordiontreeOptions, htmlAttributes);
- }
- private static string createChildMenu(List<Sys_Menu> list,string parentMenuNo)
- {
- string rtn = "";
- List<Sys_Menu> mChild = list.Where(p => p.ParentMenuNo == parentMenuNo).ToList();
- foreach (Sys_Menu m in mChild)
- {
- rtn += "\r\n\t\t<li><a href=\"javascript:void();\" onclick=\"onMenu('" + ProcessUrl(m.Url) + "',this);\">" + m.MenuName + "</a></li>";
- }
- if (rtn.Length > 0)
- {
- rtn = "\r\n\t<div class=\"index_left_menu_child\"><ul>" + rtn + "</ul></div>";
- }
- 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<Sys_Menu> GetMenuList(HtmlHelper htmlHelper)
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IMenuServices menuServices = scope.Resolve<IMenuServices>();
- var user = htmlHelper.ViewContext.HttpContext.User as EMISOnline.Utility.FormValidate.CustomPrincipal;
- var menuList = menuServices.GetMenuByUserID(user.UserID);
- return menuList.ToList();
- }
- }
- /// <summary>
- /// 导航栏,根据菜单ID分析出用户的菜单路径
- /// </summary>
- /// <param name="htmlHelper"></param>
- /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
- /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
- /// }
- /// </param>
- /// <returns></returns>
- public static MvcHtmlString Position(this HtmlHelper htmlHelper, IDictionary<string, string> htmlAttributes = null)
- {
- string menuNo = HttpContext.Current.Request["MNU"];
- string path = "";
- string html = "";
- if (string.IsNullOrEmpty(menuNo))
- {
- path = "系统首页";
- }
- else
- {
- IMenuServices menuServices = AutofacHelper.Container.Resolve<IMenuServices>();
- path = menuServices.GetFullMenuPath(menuNo);
- }
- if (htmlAttributes == null)
- {
- htmlAttributes = new Dictionary<string, string>();
- }
- html = "<div class=\"position_title\">\n";
- html += "<div class=\"position_content\" " + string.Join(" ", htmlAttributes.Select(x => x.Key + "=\"" + x.Value + "\"")) + ">";
- html += "当前位置:" + path + "</div>\n";
- html += "</div>";
- 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;
- }
- }
- }
|