123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- 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<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.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<Sys_Menu> GetMenuList(HtmlHelper htmlHelper)
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IMenuServices menuServices = scope.Resolve<IMenuServices>();
- var user = htmlHelper.ViewContext.HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
- var menuList = menuServices.GetMenuByUserIDAndRoleID(user.UserID, user.RoleID);
- 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
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IMenuServices menuServices = scope.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;
- }
- }
- }
|