MenuExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Autofac;
  7. using EMIS.Entities;
  8. using EMIS.Utility;
  9. using EMIS.CommonLogic.SystemServices;
  10. using Bowin.Common;
  11. using Bowin.Web.Controls.Mvc;
  12. namespace EMIS.Web.Controls
  13. {
  14. public static class MenuExtensions
  15. {
  16. public static MvcHtmlString MenuTree(this HtmlHelper htmlHelper,
  17. AccordionTreeOptions accordiontreeOptions = null,
  18. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  19. {
  20. List<AccordionTreeDataItem> dataItemList = new List<AccordionTreeDataItem>();
  21. BaseExtensions.GetContextUser();
  22. var menuList = GetMenuList(htmlHelper);
  23. foreach (var menu in menuList.Where(x => x.IsVisible == true))
  24. {
  25. if (!dataItemList.Exists(x => x.MnuId == menu.MenuNo))
  26. {
  27. dataItemList.Add(new AccordionTreeDataItem
  28. {
  29. MnuId = menu.MenuNo,
  30. MnuName = menu.MenuName,
  31. MnuOrder = menu.OrderNo,
  32. MnuPic = menu.Icon,
  33. MnuProgram = ((menu.Url ?? "") != "") ?
  34. ((menu.Url.StartsWith("http://") ? "" : "/") + menu.Url + (menu.Url.Contains("?") ? "&" : "?") + "MNU=" + menu.MenuNo)
  35. : null,
  36. ParentMnuId = menu.ParentMenuNo,
  37. state = "close"//(menuList.Exists(x => x.ParentMenuNo == menu.MenuNo)) ? "open" : "close"
  38. });
  39. }
  40. }
  41. return AccordionTreeExtensions.AccordionTree(htmlHelper, dataItemList, accordiontreeOptions, htmlAttributes);
  42. }
  43. private static List<Sys_Menu> GetMenuList(HtmlHelper htmlHelper)
  44. {
  45. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  46. {
  47. IMenuServices menuServices = scope.Resolve<IMenuServices>();
  48. var user = htmlHelper.ViewContext.HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
  49. var menuList = menuServices.GetMenuByUserIDAndRoleID(user.UserID, user.RoleID);
  50. return menuList.ToList();
  51. }
  52. }
  53. /// <summary>
  54. /// 导航栏,根据菜单ID分析出用户的菜单路径
  55. /// </summary>
  56. /// <param name="htmlHelper"></param>
  57. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  58. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  59. /// }
  60. /// </param>
  61. /// <returns></returns>
  62. public static MvcHtmlString Position(this HtmlHelper htmlHelper, IDictionary<string, string> htmlAttributes = null)
  63. {
  64. string menuNo = HttpContext.Current.Request["MNU"];
  65. string path = "";
  66. string html = "";
  67. if (string.IsNullOrEmpty(menuNo))
  68. {
  69. path = "系统首页";
  70. }
  71. else
  72. {
  73. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  74. {
  75. IMenuServices menuServices = scope.Resolve<IMenuServices>();
  76. path = menuServices.GetFullMenuPath(menuNo);
  77. }
  78. }
  79. if (htmlAttributes == null)
  80. {
  81. htmlAttributes = new Dictionary<string, string>();
  82. }
  83. html = "<div class=\"position_title\">\n";
  84. html += "<div class=\"position_content\" " + string.Join(" ", htmlAttributes.Select(x => x.Key + "=\"" + x.Value + "\"")) + ">";
  85. html += "当前位置:" + path + "</div>\n";
  86. html += "</div>";
  87. return MvcHtmlString.Create(html);
  88. }
  89. public static string AddMenuParameter(this string url)
  90. {
  91. return AddMenuParamenter(url, HttpContext.Current.Request["MNU"]);
  92. }
  93. private static string AddMenuParamenter(string url, string menuNo)
  94. {
  95. string result;
  96. bool hasQuestionMark = url.IndexOf('?') > 0;
  97. if (hasQuestionMark)
  98. {
  99. result = url + "&";
  100. }
  101. else
  102. {
  103. result = url + "?";
  104. }
  105. result += "MNU=" + menuNo;
  106. return result;
  107. }
  108. }
  109. }