MenuExtensions.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 EMISOnline.Entities;
  8. using EMISOnline.Utility;
  9. using EMISOnline.CommonLogic.SystemServices;
  10. using Bowin.Common;
  11. using Bowin.Web.Controls.Mvc;
  12. using EMISOnline.CommonLogic.SystemServices;
  13. namespace EMISOnline.Web.Controls
  14. {
  15. public static class MenuExtensions
  16. {
  17. public static MvcHtmlString MenuTree(this HtmlHelper htmlHelper,
  18. AccordionTreeOptions accordiontreeOptions = null,
  19. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  20. {
  21. List<AccordionTreeDataItem> dataItemList = new List<AccordionTreeDataItem>();
  22. BaseExtensions.GetContextUser();
  23. var menuList = GetMenuList(htmlHelper);
  24. /*
  25. foreach (var menu in menuList.Where(x => x.IsVisible == true))
  26. {
  27. if (!dataItemList.Exists(x => x.MnuId == menu.MenuNo))
  28. {
  29. dataItemList.Add(new AccordionTreeDataItem
  30. {
  31. MnuId = menu.MenuNo,
  32. MnuName = menu.MenuName,
  33. MnuOrder = menu.OrderNo,
  34. MnuPic = menu.Icon,
  35. MnuProgram = ((menu.Url ?? "") != "") ?
  36. ("/" + menu.Url + (menu.Url.Contains("?") ? "&" : "?") + "MNU=" + menu.MenuNo)
  37. : null,
  38. ParentMnuId = menu.ParentMenuNo,
  39. state = "close"//(menuList.Exists(x => x.ParentMenuNo == menu.MenuNo)) ? "open" : "close"
  40. });
  41. }
  42. }*/
  43. //暂时在此生成菜单
  44. var user = htmlHelper.ViewContext.HttpContext.User as EMISOnline.Utility.FormValidate.CustomPrincipal;
  45. string rootMenuNo = user.IsStudent ? "MnuStu" : "Mnu";
  46. string html = "";
  47. List<Sys_Menu> mRoot = menuList.Where(p => p.ParentMenuNo == rootMenuNo).ToList();
  48. foreach (Sys_Menu root in mRoot)
  49. {
  50. html += "\r\n<div class=\"index_left_menu\" onclick=\"onMenu('" + ProcessUrl(root.Url) + "');\" >";
  51. html += "\r\n\t<a href=\"javascript:void();\" onclick=\"onMenu('" + ProcessUrl(root.Url) + "',this);\">" + root.MenuName + "</a>";
  52. html += createChildMenu(menuList, root.MenuNo);
  53. html += "\r\n</div>";
  54. }
  55. return new MvcHtmlString(html);
  56. //暂不实现
  57. //return AccordionTreeExtensions.AccordionTree(htmlHelper, dataItemList, accordiontreeOptions, htmlAttributes);
  58. }
  59. private static string createChildMenu(List<Sys_Menu> list,string parentMenuNo)
  60. {
  61. string rtn = "";
  62. List<Sys_Menu> mChild = list.Where(p => p.ParentMenuNo == parentMenuNo).ToList();
  63. foreach (Sys_Menu m in mChild)
  64. {
  65. rtn += "\r\n\t\t<li><a href=\"javascript:void();\" onclick=\"onMenu('" + ProcessUrl(m.Url) + "',this);\">" + m.MenuName + "</a></li>";
  66. }
  67. if (rtn.Length > 0)
  68. {
  69. rtn = "\r\n\t<div class=\"index_left_menu_child\"><ul>" + rtn + "</ul></div>";
  70. }
  71. return rtn;
  72. }
  73. private static string ProcessUrl(string url)
  74. {
  75. if (url == null) return null;
  76. string result = url;
  77. if (url.StartsWith("/"))
  78. {
  79. UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
  80. result = urlHelper.Content("~" + url);
  81. }
  82. return result;
  83. }
  84. private static List<Sys_Menu> GetMenuList(HtmlHelper htmlHelper)
  85. {
  86. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  87. {
  88. IMenuServices menuServices = scope.Resolve<IMenuServices>();
  89. var user = htmlHelper.ViewContext.HttpContext.User as EMISOnline.Utility.FormValidate.CustomPrincipal;
  90. var menuList = menuServices.GetMenuByUserID(user.UserID);
  91. return menuList.ToList();
  92. }
  93. }
  94. /// <summary>
  95. /// 导航栏,根据菜单ID分析出用户的菜单路径
  96. /// </summary>
  97. /// <param name="htmlHelper"></param>
  98. /// <param name="htmlAttributes">自定义Html属性扩展,用Dictionary的方式定义,例如:new Dictionary<string, string> {
  99. /// { "style", "width: 100%;" }, { "name", "ddlUsers" }
  100. /// }
  101. /// </param>
  102. /// <returns></returns>
  103. public static MvcHtmlString Position(this HtmlHelper htmlHelper, IDictionary<string, string> htmlAttributes = null)
  104. {
  105. string menuNo = HttpContext.Current.Request["MNU"];
  106. string path = "";
  107. string html = "";
  108. if (string.IsNullOrEmpty(menuNo))
  109. {
  110. path = "系统首页";
  111. }
  112. else
  113. {
  114. IMenuServices menuServices = AutofacHelper.Container.Resolve<IMenuServices>();
  115. path = menuServices.GetFullMenuPath(menuNo);
  116. }
  117. if (htmlAttributes == null)
  118. {
  119. htmlAttributes = new Dictionary<string, string>();
  120. }
  121. html = "<div class=\"position_title\">\n";
  122. html += "<div class=\"position_content\" " + string.Join(" ", htmlAttributes.Select(x => x.Key + "=\"" + x.Value + "\"")) + ">";
  123. html += "当前位置:" + path + "</div>\n";
  124. html += "</div>";
  125. return MvcHtmlString.Create(html);
  126. }
  127. public static string AddMenuParameter(this string url)
  128. {
  129. return AddMenuParamenter(url, HttpContext.Current.Request["MNU"]);
  130. }
  131. private static string AddMenuParamenter(string url, string menuNo)
  132. {
  133. string result;
  134. bool hasQuestionMark = url.IndexOf('?') > 0;
  135. if (hasQuestionMark)
  136. {
  137. result = url + "&";
  138. }
  139. else
  140. {
  141. result = url + "?";
  142. }
  143. result += "MNU=" + menuNo;
  144. return result;
  145. }
  146. }
  147. }