足力健后端,使用.netcore版本,合并1个项目使用

MenuController.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.IRepositories;
  5. using System.Linq;
  6. using System.Model;
  7. using System.Security.Claims;
  8. using System.Threading.Tasks;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. namespace TVShoppingCallCenter_ZLJ.Controllers.System
  12. {
  13. [Authorize]
  14. [Produces("application/json")]
  15. [Route("api/[controller]")]
  16. public class MenuController : BaseController
  17. {
  18. private readonly ISys_RoleFunctionRepository _sys_role_functionRepository;
  19. private readonly ISys_ModuleInfoRepository _sys_module_infoRepository;
  20. private readonly ISys_ModuleButtonInfoRepository _sys_modulebutton_infoRepository;
  21. public MenuController(ISys_ModuleInfoRepository sys_moduleInfoRepository, ISys_RoleFunctionRepository sys_role_functionRepository, ISys_ModuleButtonInfoRepository sys_modulebuttonInfoRepository
  22. )
  23. {
  24. _sys_module_infoRepository = sys_moduleInfoRepository;
  25. _sys_role_functionRepository = sys_role_functionRepository;
  26. _sys_modulebutton_infoRepository = sys_modulebuttonInfoRepository;
  27. }
  28. /// <summary>
  29. /// 获取菜单及按钮
  30. /// </summary>
  31. /// <returns></returns>
  32. [HttpGet("getmenutree")]
  33. public async Task<IActionResult> GetMenuTreeAsync()
  34. {
  35. var roleId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role).Value;
  36. var data = await _sys_role_functionRepository.GetMenuAsync(int.Parse(roleId));
  37. return Success("成功", data);
  38. }
  39. /// <summary>
  40. /// 获取菜单及按钮
  41. /// </summary>
  42. /// <returns></returns>
  43. [HttpGet("getcurrentmsg")]
  44. public async Task<IActionResult> GetCurrentMsgAsync()
  45. {
  46. var data = new
  47. {
  48. authorizeMenu = await GetMenuAsync(),
  49. authorizeButton = await GetMenuButtonAsync()
  50. };
  51. return Content(data.ToJson());
  52. }
  53. #region 私有方法
  54. /// <summary>
  55. /// 获取菜单
  56. /// </summary>
  57. /// <returns></returns>
  58. private async Task<object> GetMenuAsync()
  59. {
  60. var roleId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role).Value;
  61. var role_function_list = (await _sys_role_functionRepository.GetListALL(x => x.F_RoleId == int.Parse(roleId) && x.F_FunctionType == (int)EnumRoleControlType.Moudle)).Select(x=>x.F_FunctionId).ToList();
  62. var authModuleFunctionList = (await _sys_module_infoRepository.GetListALL(x => x.F_State == (int)EnumDelState.Enabled && role_function_list.Contains(x.F_Id))).OrderBy(x => x.F_Sort).OrderByDescending(x => x.F_LastModifyOn).ToList();
  63. return TreeRecursion(authModuleFunctionList);
  64. }
  65. /// <summary>
  66. /// 获取按钮
  67. /// </summary>
  68. /// <returns></returns>
  69. private async Task<object> GetMenuButtonAsync()
  70. {
  71. var roleId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role).Value;
  72. var functionModuleList = (await _sys_role_functionRepository.GetListALL(x => x.F_RoleId == int.Parse(roleId) && x.F_FunctionType == (int)EnumRoleControlType.Moudle)).Select(x => x.F_FunctionId).ToList();
  73. var functionButtonList = (await _sys_role_functionRepository.GetListALL(x => x.F_RoleId == int.Parse(roleId) && x.F_FunctionType == (int)EnumRoleControlType.Button)).Select(x => x.F_FunctionId).ToList();
  74. var authbuttonFunctionList = (await _sys_modulebutton_infoRepository.GetListALL(x => x.F_State == (int)EnumDelState.Enabled && functionButtonList.Contains(x.F_Id))).OrderBy(x => x.F_Sort).ToList();
  75. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  76. foreach (var item in functionModuleList)
  77. {
  78. var _list = authbuttonFunctionList.Where(x => x.F_MenuId == item).ToList();
  79. dictionary.Add(item.ToString(), _list);
  80. }
  81. return dictionary;
  82. }
  83. /// <summary>
  84. /// 生成菜单数据方法
  85. /// </summary>
  86. /// <param name="data"></param>
  87. /// <param name="parentId"></param>
  88. /// <returns></returns>
  89. private List<object> TreeRecursion(List<T_Sys_ModuleInfo> data, int parentId=0)
  90. {
  91. List<object> newList = new List<object>();
  92. List<T_Sys_ModuleInfo> item = data.FindAll(t => t.F_ParentId == parentId);
  93. if (item.Count > 0)
  94. {
  95. foreach (T_Sys_ModuleInfo entity in item)
  96. {
  97. var obj = new
  98. {
  99. entity.F_Id,
  100. entity.F_MenuName,
  101. entity.F_MenuCode,
  102. entity.F_ParentId,
  103. entity.F_OptUrl,
  104. entity.F_ImgUrl,
  105. entity.F_State,
  106. entity.F_Sort,
  107. entity.F_IsMenu,
  108. entity.F_Target,
  109. entity.F_Remark,
  110. entity.F_CreateBy,
  111. entity.F_CreateOn,
  112. entity.F_LastModifyOn,
  113. entity.F_LastModifyBy,
  114. entity.F_DeleteOn,
  115. entity.F_DeleteBy,
  116. childnodes = TreeRecursion(data, entity.F_Id)
  117. };
  118. newList.Add(obj);
  119. }
  120. }
  121. return newList;
  122. }
  123. #endregion
  124. }
  125. }