You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
14 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Shentun.Peis.ItemTypes;
  5. using Shentun.Peis.Models;
  6. using Shentun.Peis.OrganizationUnits;
  7. using Shentun.Peis.RoleMenuInfos;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Volo.Abp;
  14. using Volo.Abp.Application.Services;
  15. using Volo.Abp.Domain.Repositories;
  16. using Volo.Abp.Identity;
  17. using Volo.Abp.Users;
  18. namespace Shentun.Peis.MenuInfos
  19. {
  20. /// <summary>
  21. /// 后台菜单
  22. /// </summary>
  23. [ApiExplorerSettings(GroupName = "Work")]
  24. [Authorize]
  25. public class MenuInfoAppService : ApplicationService
  26. {
  27. private readonly IRepository<MenuInfo, Guid> _menuInfoRepository;
  28. private readonly IRepository<RoleMenuInfo> _roleMenuInfoRepository;
  29. private readonly ICurrentUser _currentUser;
  30. private readonly IIdentityUserRepository _userRepository;
  31. private readonly IRepository<IdentityUser, Guid> _identityUserRepository;
  32. private readonly IConfiguration _configuration;
  33. private readonly MenuInfoManager _menuInfoManager;
  34. public MenuInfoAppService(
  35. IRepository<MenuInfo, Guid> menuInfoRepository,
  36. IRepository<RoleMenuInfo> roleMenuInfoRepository,
  37. ICurrentUser currentUser,
  38. MenuInfoManager menuInfoManager,
  39. IIdentityUserRepository userRepository,
  40. IRepository<IdentityUser, Guid> identityUserRepository,
  41. IConfiguration configuration)
  42. {
  43. this._menuInfoRepository = menuInfoRepository;
  44. this._menuInfoManager = menuInfoManager;
  45. this._roleMenuInfoRepository = roleMenuInfoRepository;
  46. this._currentUser = currentUser;
  47. this._userRepository = userRepository;
  48. this._identityUserRepository = identityUserRepository;
  49. this._configuration = configuration;
  50. }
  51. /// <summary>
  52. /// 查询菜单数据 根据ID
  53. /// </summary>
  54. /// <param name="MenuInfoId"></param>
  55. /// <returns></returns>
  56. [HttpGet("api/app/menuinfo/getmenuinfoasync")]
  57. public async Task<MenuInfoDto> GetMenuInfoAsync(Guid MenuInfoId)
  58. {
  59. var entity = await _menuInfoRepository.GetAsync(MenuInfoId);
  60. var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
  61. var userList = await _userRepository.GetListAsync();
  62. dto.CreatorName = EntityHelper.GetSurnameNoSql(userList, dto.CreatorId);
  63. dto.LastModifierName = EntityHelper.GetSurnameNoSql(userList, dto.LastModifierId);
  64. return dto;
  65. }
  66. /// <summary>
  67. /// 创建后台菜单
  68. /// </summary>
  69. /// <param name="input"></param>
  70. /// <returns></returns>
  71. [HttpPost("api/app/menuinfo/createtmenuinfo")]
  72. public async Task<MenuInfoDto> CreatetMenuInfoAsync(CreateMenuInfoDto input)
  73. {
  74. var createEntity = ObjectMapper.Map<CreateMenuInfoDto, MenuInfo>(input);
  75. var entity = await _menuInfoManager.CreatetMenuInfoAsync(createEntity);
  76. entity = await _menuInfoRepository.InsertAsync(entity);
  77. var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
  78. return dto;
  79. }
  80. /// <summary>
  81. /// 修改后台菜单
  82. /// </summary>
  83. /// <param name="id"></param>
  84. /// <param name="input"></param>
  85. /// <returns></returns>
  86. [HttpPost("api/app/menuinfo/updatemenuinfo")]
  87. public async Task<MenuInfoDto> UpdateMenuInfoAsync(Guid id, UpdateMenuInfoDto input)
  88. {
  89. var entity = await _menuInfoRepository.GetAsync(id);
  90. var sourceEntity = ObjectMapper.Map<UpdateMenuInfoDto, MenuInfo>(input);
  91. _menuInfoManager.UpdateMenuInfoAsync(sourceEntity, entity);
  92. entity = await _menuInfoRepository.UpdateAsync(entity);
  93. var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
  94. return dto;
  95. }
  96. /// <summary>
  97. /// 删除
  98. /// </summary>
  99. /// <param name="id"></param>
  100. /// <returns></returns>
  101. [HttpPost("api/app/menuinfo/deletemenuinfo")]
  102. public async Task DeleteMenuInfoAsync(Guid id)
  103. {
  104. //判断是否有下级节点
  105. if ((await _menuInfoRepository.GetQueryableAsync()).Where(m => m.ParentId == id).Count() > 0)
  106. throw new UserFriendlyException("当前菜单有下级节点,不能删除");
  107. //删除角色选择的对应的菜单数据
  108. await _roleMenuInfoRepository.DeleteAsync(d => d.MenuInfoId == id);
  109. //删除菜单
  110. await _menuInfoRepository.DeleteAsync(id);
  111. }
  112. /// <summary>
  113. /// 获取当前用户的树型菜单结果
  114. /// </summary>
  115. /// <returns></returns>
  116. [HttpGet("api/app/menuinfo/getmymenuinfotreelist")]
  117. public async Task<List<MenuInfoTreeDto>> GetMyMenuInfoTreeListAsync()
  118. {
  119. var dataList = await _menuInfoRepository.GetQueryableAsync();
  120. string AdminId = _configuration.GetValue<string>("AdminId");
  121. if (_currentUser.Id.Value != Guid.Parse(AdminId))
  122. {
  123. var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
  124. if (!userRoles.Any())
  125. return new List<MenuInfoTreeDto>();
  126. var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
  127. if (!userMenuInfoList.Any())
  128. return new List<MenuInfoTreeDto>();
  129. dataList = dataList.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.Id));
  130. }
  131. var items = from p in dataList.OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName)
  132. select new MenuInfoTreeDto()
  133. {
  134. Id = p.Id,
  135. ParentId = p.ParentId,
  136. DisplayOrder = p.DisplayOrder,
  137. IconName = p.IconName,
  138. IsActive = p.IsActive,
  139. RouteUrl = p.RouteUrl,
  140. DisplayName = p.DisplayName,
  141. SimpleCode = p.SimpleCode,
  142. MenuType = p.MenuType
  143. };
  144. return GetMenuInfoTree(items.ToList(), null);
  145. }
  146. /// <summary>
  147. /// 获取树型菜单结果
  148. /// </summary>
  149. /// <returns></returns>
  150. [HttpGet("api/app/menuinfo/getmenuinfotreelist")]
  151. public async Task<List<MenuInfoTreeDto>> GetMenuInfoTreeListAsync()
  152. {
  153. var dataList = await _menuInfoRepository.GetQueryableAsync();
  154. var items = from p in dataList.OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName)
  155. select new MenuInfoTreeDto()
  156. {
  157. Id = p.Id,
  158. ParentId = p.ParentId,
  159. DisplayOrder = p.DisplayOrder,
  160. IconName = p.IconName,
  161. IsActive = p.IsActive,
  162. RouteUrl = p.RouteUrl,
  163. DisplayName = p.DisplayName,
  164. SimpleCode = p.SimpleCode,
  165. MenuType = p.MenuType
  166. };
  167. return GetMenuInfoTree(items.ToList(), null);
  168. }
  169. /// <summary>
  170. /// 获取当前用户的菜单列表
  171. /// </summary>
  172. /// <returns></returns>
  173. [HttpGet("api/app/menuinfo/getmymenuinfolist")]
  174. public async Task<List<GetMyMenuInfoListDto>> GetMyMenuInfoListAsync()
  175. {
  176. var query = await _menuInfoRepository.GetQueryableAsync();
  177. query = query.Where(m => m.IsActive == 'Y');
  178. string AdminId = _configuration.GetValue<string>("AdminId");
  179. if (_currentUser.Id.Value != Guid.Parse(AdminId))
  180. {
  181. var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
  182. if (!userRoles.Any())
  183. return new List<GetMyMenuInfoListDto>();
  184. var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
  185. if (!userMenuInfoList.Any())
  186. return new List<GetMyMenuInfoListDto>();
  187. query = query.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.Id));
  188. }
  189. var entlistdto = query.Select(s => new GetMyMenuInfoListDto
  190. {
  191. DisplayName = s.DisplayName,
  192. DisplayOrder = s.DisplayOrder,
  193. IconName = s.IconName,
  194. Id = s.Id,
  195. MenuType = s.MenuType,
  196. ParentId = s.ParentId,
  197. RouteUrl = s.RouteUrl,
  198. }).OrderBy(o => o.ParentId).ThenBy(o => o.DisplayOrder).ToList();
  199. return entlistdto;
  200. }
  201. /// <summary>
  202. /// 获取指定用户的菜单列表
  203. /// </summary>
  204. /// <returns></returns>
  205. [HttpGet("api/app/menuinfo/getmymenuinfolistinuser")]
  206. public async Task<List<MenuInfoDto>> GetMyMenuInfoListInUserAsync(Guid UserId)
  207. {
  208. var query = from a in await _menuInfoRepository.GetQueryableAsync()
  209. join b in await _identityUserRepository.GetQueryableAsync() on a.CreatorId equals b.Id into bb
  210. from ab in bb.DefaultIfEmpty()
  211. join c in await _identityUserRepository.GetQueryableAsync() on a.LastModifierId equals c.Id into cc
  212. from ac in cc.DefaultIfEmpty()
  213. select new
  214. {
  215. a,
  216. LastModifierName = ac != null ? ac.Surname : "",
  217. CreatorName = ab != null ? ab.Surname : ""
  218. };
  219. var userRoles = await _userRepository.GetRolesAsync(UserId); //获取当前用户的角色
  220. if (!userRoles.Any())
  221. return new List<MenuInfoDto>();
  222. var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
  223. if (!userMenuInfoList.Any())
  224. return new List<MenuInfoDto>();
  225. query = query.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.a.Id));
  226. var entlistdto = query.Select(s => new MenuInfoDto
  227. {
  228. CreationTime = s.a.CreationTime,
  229. CreatorId = s.a.CreatorId,
  230. DisplayName = s.a.DisplayName,
  231. DisplayOrder = s.a.DisplayOrder,
  232. IconName = s.a.IconName,
  233. Id = s.a.Id,
  234. IsActive = s.a.IsActive,
  235. LastModifierId = s.a.LastModifierId,
  236. LastModificationTime = s.a.LastModificationTime,
  237. MenuType = s.a.MenuType,
  238. ParentId = s.a.ParentId,
  239. RouteUrl = s.a.RouteUrl,
  240. SimpleCode = s.a.SimpleCode,
  241. CreatorName = s.CreatorName,
  242. LastModifierName = s.LastModifierName
  243. }).OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName).ToList();
  244. return entlistdto;
  245. }
  246. /// <summary>
  247. /// 获取菜单列表
  248. /// </summary>
  249. /// <returns></returns>
  250. [HttpGet("api/app/menuinfo/getmenuinfolist")]
  251. public async Task<List<MenuInfoDto>> GetMenuInfoListAsync()
  252. {
  253. var query = from a in await _menuInfoRepository.GetQueryableAsync()
  254. join b in await _identityUserRepository.GetQueryableAsync() on a.CreatorId equals b.Id into bb
  255. from ab in bb.DefaultIfEmpty()
  256. join c in await _identityUserRepository.GetQueryableAsync() on a.LastModifierId equals c.Id into cc
  257. from ac in cc.DefaultIfEmpty()
  258. select new
  259. {
  260. a,
  261. LastModifierName = ac != null ? ac.Surname : "",
  262. CreatorName = ab != null ? ab.Surname : ""
  263. };
  264. var entlistdto = query.Select(s => new MenuInfoDto
  265. {
  266. CreationTime = s.a.CreationTime,
  267. CreatorId = s.a.CreatorId,
  268. DisplayName = s.a.DisplayName,
  269. DisplayOrder = s.a.DisplayOrder,
  270. IconName = s.a.IconName,
  271. Id = s.a.Id,
  272. IsActive = s.a.IsActive,
  273. LastModifierId = s.a.LastModifierId,
  274. LastModificationTime = s.a.LastModificationTime,
  275. MenuType = s.a.MenuType,
  276. ParentId = s.a.ParentId,
  277. RouteUrl = s.a.RouteUrl,
  278. SimpleCode = s.a.SimpleCode,
  279. CreatorName = s.CreatorName,
  280. LastModifierName = s.LastModifierName
  281. }).OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName).ToList();
  282. return entlistdto;
  283. }
  284. /// <summary>
  285. ///
  286. /// </summary>
  287. /// <param name="items"></param>
  288. /// <param name="ParentId"></param>
  289. /// <returns></returns>
  290. private List<MenuInfoTreeDto> GetMenuInfoTree(List<MenuInfoTreeDto> items, Guid? ParentId)
  291. {
  292. return (from p in items
  293. where p.ParentId == ParentId
  294. let subs = GetMenuInfoTree(items, p.Id)
  295. select new MenuInfoTreeDto()
  296. {
  297. Id = p.Id,
  298. ParentId = p.ParentId,
  299. DisplayOrder = p.DisplayOrder,
  300. IconName = p.IconName,
  301. IsActive = p.IsActive,
  302. RouteUrl = p.RouteUrl,
  303. DisplayName = p.DisplayName,
  304. SimpleCode = p.SimpleCode,
  305. MenuType = p.MenuType,
  306. TreeChildren = subs.ToList()
  307. }
  308. ).ToList();
  309. }
  310. }
  311. }