From ae30d4b2cb4be329cac553c180e10dfbcbdef934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Fri, 26 Apr 2024 19:08:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=80=BC=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=9F=A5=E8=AF=A2=E5=AF=BC=E8=87=B4=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=B7=B2=E5=90=8C=E6=AD=A5sqlsugar=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/System/UserService.cs | 37 ++++++++++----- .../Entities/UserEntity.cs | 4 +- .../Managers/UserManager.cs | 46 ++++++++++--------- Yi.Abp.Net8/version.props | 2 +- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs index fced9661..79d2a473 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs @@ -3,6 +3,7 @@ using SqlSugar; using TencentCloud.Tcr.V20190924.Models; using Volo.Abp; using Volo.Abp.Application.Dtos; +using Volo.Abp.Caching; using Volo.Abp.EventBus.Local; using Volo.Abp.Users; using Yi.Framework.Ddd.Application; @@ -12,6 +13,7 @@ using Yi.Framework.Rbac.Domain.Authorization; using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Managers; using Yi.Framework.Rbac.Domain.Repositories; +using Yi.Framework.Rbac.Domain.Shared.Caches; using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.Rbac.Domain.Shared.Etos; using Yi.Framework.Rbac.Domain.Shared.OperLog; @@ -25,10 +27,11 @@ namespace Yi.Framework.Rbac.Application.Services.System public class UserService : YiCrudAppService,IUserService //IUserService { - public UserService(ISqlSugarRepository repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus) : base(repository) + private IDistributedCache _userCache; + public UserService(ISqlSugarRepository repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus, IDistributedCache userCache) : base(repository) => - (_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus) = - (userManager, userRepository, currentUser, deptService, repository, localEventBus); + (_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus, _userCache) = + (userManager, userRepository, currentUser, deptService, repository, localEventBus, userCache); private UserManager _userManager { get; set; } private ISqlSugarRepository _repository; private IUserRepository _userRepository { get; set; } @@ -77,6 +80,14 @@ namespace Yi.Framework.Rbac.Application.Services.System return result; } + + protected override UserEntity MapToEntity(UserCreateInputVo createInput) + { + var output= base.MapToEntity(createInput); + output.EncryPassword = new Domain.Entities.ValueObjects.EncryPasswordValueObject(createInput.Password); + return output; + } + /// /// 添加用户 /// @@ -99,13 +110,13 @@ namespace Yi.Framework.Rbac.Application.Services.System { throw new UserFriendlyException(UserConst.User_Exist); } - var entities = await MapToEntityAsync(input); - - entities.BuildPassword(); + var entitiy = await MapToEntityAsync(input); + + entitiy.BuildPassword(); //using (var uow = _unitOfWorkManager.CreateContext()) //{ - var returnEntity = await _repository.InsertReturnEntityAsync(entities); + var returnEntity = await _repository.InsertReturnEntityAsync(entitiy); await _userManager.GiveUserSetRoleAsync(new List { returnEntity.Id }, input.RoleIds); await _userManager.GiveUserSetPostAsync(new List { returnEntity.Id }, input.PostIds); //uow.Commit(); @@ -156,13 +167,13 @@ namespace Yi.Framework.Rbac.Application.Services.System entity.BuildPassword(); } await MapToEntityAsync(input, entity); - //using (var uow = _unitOfWorkManager.CreateContext()) - //{ + var res1 = await _repository.UpdateAsync(entity); await _userManager.GiveUserSetRoleAsync(new List { id }, input.RoleIds); await _userManager.GiveUserSetPostAsync(new List { id }, input.PostIds); - // uow.Commit(); - //} + + await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId())); + return await MapToGetOutputDtoAsync(entity); } @@ -179,6 +190,7 @@ namespace Yi.Framework.Rbac.Application.Services.System await _repository.UpdateAsync(entity); var dto = await MapToGetOutputDtoAsync(entity); + await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId())); return dto; } @@ -200,13 +212,16 @@ namespace Yi.Framework.Rbac.Application.Services.System } entity.State = state; await _repository.UpdateAsync(entity); + await _userCache.RefreshAsync(new UserInfoCacheKey(id)); return await MapToGetOutputDtoAsync(entity); } [OperLog("删除用户", OperEnum.Delete)] [Permission("system:user:delete")] public override async Task DeleteAsync(Guid id) { + await base.DeleteAsync(id); + await _userCache.RefreshAsync(new UserInfoCacheKey(id)); } [Permission("system:user:export")] diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/UserEntity.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/UserEntity.cs index 9e83b2b4..12865286 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/UserEntity.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/UserEntity.cs @@ -59,7 +59,7 @@ namespace Yi.Framework.Rbac.Domain.Entities /// 加密密码 /// [SugarColumn(IsOwnsOne = true)] - public EncryPasswordValueObject EncryPassword { get; set; } + public EncryPasswordValueObject EncryPassword { get; set; } = new EncryPasswordValueObject(); ///// ///// 密码 @@ -182,7 +182,7 @@ namespace Yi.Framework.Rbac.Domain.Entities //如果不传值,那就把自己的password当作传进来的password if (password == null) { - if (EncryPassword.Password == null) + if (EncryPassword?.Password == null) { throw new ArgumentNullException(nameof(EncryPassword.Password)); } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs index 90f8b363..dcc906b6 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs @@ -95,42 +95,26 @@ namespace Yi.Framework.Rbac.Domain.Managers /// public async Task GetInfoAsync(Guid userId) { - var user = await _userRepository.GetUserAllInfoAsync(userId); - var output = await GetInfoByCacheAsync(user); + + var output = await GetInfoByCacheAsync(userId); return output; } - - /// - /// 批量查询用户信息 - /// - /// - /// - public async Task> GetInfoListAsync(List userIds) - { - List output = new List(); - var users = await _userRepository.GetListUserAllInfoAsync(userIds); - foreach (var user in users) - { - output.Add(await GetInfoByCacheAsync(user)); - } - return output; - } - - private async Task GetInfoByCacheAsync(UserEntity user) + private async Task GetInfoByCacheAsync(Guid userId) { //此处优先从缓存中获取 UserRoleMenuDto output = null; var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService>().Value.ExpiresMinuteTime; - var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(user.Id), + var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(userId), async () => { + var user = await _userRepository.GetUserAllInfoAsync(userId); var data = EntityMapToDto(user); //系统用户数据被重置,老前端访问重新授权 if (data is null) { throw new AbpAuthorizationException(); } - data.Menus.Clear(); + //data.Menus.Clear(); output = data; return new UserInfoCacheItem(data); }, @@ -143,6 +127,24 @@ namespace Yi.Framework.Rbac.Domain.Managers return output!; } + + /// + /// 批量查询用户信息 + /// + /// + /// + public async Task> GetInfoListAsync(List userIds) + { + List output = new List(); + foreach (var userId in userIds) + { + output.Add(await GetInfoByCacheAsync(userId)); + } + return output; + } + + + private UserRoleMenuDto EntityMapToDto(UserEntity user) { diff --git a/Yi.Abp.Net8/version.props b/Yi.Abp.Net8/version.props index 1cfda791..7a7a259e 100644 --- a/Yi.Abp.Net8/version.props +++ b/Yi.Abp.Net8/version.props @@ -1,6 +1,6 @@ 8.0.5 - 5.1.4.149 + 5.1.4.154-preview01 \ No newline at end of file