From 47a6450e3908c6b873097dbb6e355e69f5e365e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Sun, 12 Apr 2026 16:54:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=83=E9=99=90=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Role/Dtos/AddSysRoleInput.cs | 23 ++++++++++- YD_Event.Application/Role/SysRoleService.cs | 41 ++++++++++++++++++- YD_Event.Application/User/SysUserService.cs | 30 +++++++++++++- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/YD_Event.Application/Role/Dtos/AddSysRoleInput.cs b/YD_Event.Application/Role/Dtos/AddSysRoleInput.cs index 80fb6a1..78b3ed9 100644 --- a/YD_Event.Application/Role/Dtos/AddSysRoleInput.cs +++ b/YD_Event.Application/Role/Dtos/AddSysRoleInput.cs @@ -1,4 +1,4 @@ -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; using YD_Event.Core.Enum; namespace YD_Event.Application.Role.Dtos; @@ -6,7 +6,7 @@ namespace YD_Event.Application.Role.Dtos; /// /// 添加角色输入 /// -public class AddSysRoleInput +public class AddSysRoleInput : IValidatableObject { /// /// 角色名称 @@ -30,4 +30,23 @@ public class AddSysRoleInput /// 状态 /// public AvailabilityStatus Status { get; set; } = AvailabilityStatus.Enable; + + /// + /// 菜单ID集合 + /// + public List MenuIds { get; set; } + + /// + /// 菜单ID集合(前端使用) + /// + public List Menus { get; set; } + + public IEnumerable Validate(ValidationContext validationContext) + { + var menuIds = MenuIds ?? Menus; + if (menuIds == null || menuIds.Count == 0) + { + yield return new ValidationResult("请选择权限", new[] { nameof(MenuIds), nameof(Menus) }); + } + } } diff --git a/YD_Event.Application/Role/SysRoleService.cs b/YD_Event.Application/Role/SysRoleService.cs index 1784241..24f1a08 100644 --- a/YD_Event.Application/Role/SysRoleService.cs +++ b/YD_Event.Application/Role/SysRoleService.cs @@ -1,4 +1,4 @@ -using YD_Event.Application.Role.Dtos; +using YD_Event.Application.Role.Dtos; using YD_Event.Core.Shared; namespace YD_Event.Application.Role; @@ -70,6 +70,19 @@ public class SysRoleService : BaseService, ITransient } await _sysRoleRepository.InsertAsync(role); + + // 保存角色权限 + var menuIds = dto.MenuIds ?? dto.Menus; + if (menuIds != null && menuIds.Any()) + { + var roleMenus = menuIds.Select(x => new SysRoleMenu() + { + MenuId = x, + RoleId = role.Id + }).ToList(); + + await _sysRoleMenuRepository.InsertRangeAsync(roleMenus); + } } /// @@ -95,6 +108,21 @@ public class SysRoleService : BaseService, ITransient dto.Adapt(sysRole); await _sysRoleRepository.UpdateAsync(sysRole); + + // 更新角色权限 + await _sysRoleMenuRepository.DeleteAsync(x => x.RoleId == dto.Id); + var menuIds = dto.MenuIds ?? dto.Menus; + if (menuIds != null && menuIds.Any()) + { + var roleMenus = menuIds.Select(x => new SysRoleMenu() + { + MenuId = x, + RoleId = dto.Id + }).ToList(); + + await _sysRoleMenuRepository.InsertRangeAsync(roleMenus); + } + await _easyCachingProvider.RemoveByPrefixAsync(CacheConst.PermissionKey); } /// @@ -186,4 +214,15 @@ public class SysRoleService : BaseService, ITransient Label = x.Name }).ToListAsync(); } + + /// + /// 角色下拉选项(兼容接口) + /// + /// + [DisplayName("角色下拉选项(兼容接口)")] + [HttpGet("roleselect")] + public async Task> RoleSelectCompat() + { + return await RoleSelect(); + } } diff --git a/YD_Event.Application/User/SysUserService.cs b/YD_Event.Application/User/SysUserService.cs index 29c2924..d251316 100644 --- a/YD_Event.Application/User/SysUserService.cs +++ b/YD_Event.Application/User/SysUserService.cs @@ -99,7 +99,7 @@ public class SysUserService : BaseService, ITransient /// /// /// - [UnitOfWork, HttpPost] + [UnitOfWork, HttpPost("user")] [DisplayName("添加系统用户")] public async Task AddUser(AddSysUserInput dto) { @@ -107,6 +107,19 @@ public class SysUserService : BaseService, ITransient if (await _repository.IsAnyAsync(x => x.Account == dto.Account)) throw new InvalidOperationException($"账号 {dto.Account} 已存在"); + // 验证角色是否存在 + if (dto.Roles != null && dto.Roles.Any()) + { + var validRoleCount = await _roleRepository.AsQueryable() + .Where(x => dto.Roles.Contains(x.Id) && x.Status == AvailabilityStatus.Enable) + .CountAsync(); + + if (validRoleCount != dto.Roles.Count) + { + throw new InvalidOperationException("部分或全部角色不存在或已被禁用"); + } + } + var user = dto.Adapt(); user.Id = _idGenerator.NextId(); string encode = _idGenerator.Encode(user.Id); @@ -137,7 +150,7 @@ public class SysUserService : BaseService, ITransient /// /// [DisplayName("更新系统用户信息")] - [UnitOfWork, HttpPut] + [UnitOfWork, HttpPut("user")] public async Task UpdateUser(UpdateSysUserInput dto) { var user = await _repository.GetByIdAsync(dto.Id); @@ -147,6 +160,19 @@ public class SysUserService : BaseService, ITransient if (await _repository.IsAnyAsync(x => x.Account == dto.Account && x.Id != dto.Id)) throw new InvalidOperationException($"账号 {dto.Account} 已被使用"); + // 验证角色是否存在 + if (dto.Roles != null && dto.Roles.Any()) + { + var validRoleCount = await _roleRepository.AsQueryable() + .Where(x => dto.Roles.Contains(x.Id) && x.Status == AvailabilityStatus.Enable) + .CountAsync(); + + if (validRoleCount != dto.Roles.Count) + { + throw new InvalidOperationException("部分或全部角色不存在或已被禁用"); + } + } + // 先更新除密码外的其他字段 user.Name = dto.Name; user.Account = dto.Account;