SysMenuService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // <author></author>
  2. // <date></date>
  3. // <description></description>
  4. using ERP.Core.Constant;
  5. using ERP.Core.Dto;
  6. using ERP.Core.Entities;
  7. using ERP.Core.Interface;
  8. using ERP.Core.Repositorys;
  9. using ERP.Framework.Security;
  10. using ERP.Framework.Utils;
  11. namespace ERP.Core.Services
  12. {
  13. public class SysMenuService : ISysMenuService
  14. {
  15. private readonly SysMenuRepository _sysMenuRepository;
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="sysMenuRepository"></param>
  20. public SysMenuService(SysMenuRepository sysMenuRepository)
  21. {
  22. _sysMenuRepository = sysMenuRepository;
  23. }
  24. /// <summary>
  25. /// 获取
  26. /// </summary>
  27. /// <returns></returns>
  28. public List<SysMenu> SelectMenuTreeByUserId(long userId)
  29. {
  30. List<SysMenu> result;
  31. if (LoginHelper.IsAdmin())
  32. {
  33. result = _sysMenuRepository.List();
  34. }
  35. else
  36. {
  37. result = _sysMenuRepository.SelectMenuListByUserId(userId);
  38. }
  39. return TreeUtil.Build(result);
  40. }
  41. public List<Router> BuildMenuTree(List<SysMenu> list, string parentPath)
  42. {
  43. var routers = new List<Router>();
  44. foreach (var menu in list)
  45. {
  46. var router = new Router()
  47. {
  48. Hidden = menu.Visible,
  49. Name = menu.Path,
  50. Path = GetRoutePath(menu, parentPath),
  51. Component = GetComponent(menu),
  52. Meta = new Meta()
  53. {
  54. Icon = menu.Icon,
  55. Title = menu.MenuName,
  56. //IsCache = menu.ca
  57. }
  58. };
  59. var cMenus = menu.Children;
  60. if (cMenus != null && menu.MenuType == Enums.MenuTypeEnum.Catalog)
  61. {
  62. router.Children = BuildMenuTree(cMenus, router.Path);
  63. }
  64. else if (IsMenuFrame(menu))
  65. {
  66. var childrenList = new List<Router>();
  67. var children = new Router()
  68. {
  69. Path = menu.Path,
  70. Component = menu.Component,
  71. Name = menu.Path,
  72. Meta = new Meta()
  73. {
  74. Icon = menu.Icon,
  75. Title = menu.MenuName
  76. }
  77. };
  78. childrenList.Add(children);
  79. router.Children = childrenList;
  80. }
  81. routers.Add(router);
  82. }
  83. return routers;
  84. }
  85. private string GetRoutePath(SysMenu menu, string parentPath)
  86. {
  87. string routerPath;
  88. if (menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog)
  89. {
  90. routerPath = "/" + menu.Path;
  91. }
  92. else if (IsMenuFrame(menu))
  93. {
  94. routerPath = "/";
  95. }
  96. else
  97. {
  98. routerPath = parentPath + "/" + menu.Path;
  99. }
  100. return routerPath;
  101. }
  102. /// <summary>
  103. /// 是否为一级菜单
  104. /// </summary>
  105. /// <param name="menu"></param>
  106. /// <returns></returns>
  107. private bool IsMenuFrame(SysMenu menu)
  108. {
  109. return menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Menu;
  110. }
  111. /// <summary>
  112. /// 是否为中间组件
  113. /// </summary>
  114. /// <param name="menu"></param>
  115. /// <returns></returns>
  116. private bool IsParentView(SysMenu menu)
  117. {
  118. return menu.ParentId != 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog;
  119. }
  120. /// <summary>
  121. /// 获取组件值
  122. /// </summary>
  123. /// <param name="menu"></param>
  124. /// <returns></returns>
  125. private string GetComponent(SysMenu menu)
  126. {
  127. var component = CoreConstant.LAYOUT;
  128. if (!menu.Component.IsNullOrEmpty() && !IsMenuFrame(menu))
  129. {
  130. component = menu.Component;
  131. }
  132. else if (menu.Component.IsNullOrEmpty() && IsParentView(menu))
  133. {
  134. component = CoreConstant.PARENT_VIEW;
  135. }
  136. return component;
  137. }
  138. }
  139. }