From 1fbd521d1ab58509216cb3de51e8aa0494bc4fb1 Mon Sep 17 00:00:00 2001 From: chenchun Date: Fri, 29 Aug 2025 14:33:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B3=A8=E5=86=8C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8A=A0=E5=88=86=E5=B8=83=E5=BC=8F=E9=94=81=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=87=8D=E5=A4=8D=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FuwuhaoService.cs | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/FuwuhaoService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/FuwuhaoService.cs index c5d36ad3..9cc4f7c6 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/FuwuhaoService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/FuwuhaoService.cs @@ -226,27 +226,32 @@ public class FuwuhaoService : ApplicationService var result = await _fileService.Post(files); - if (await _userRepository.IsAnyAsync(x=>x.FuwuhaoOpenId==userInfo.OpenId)) + //由于存在查询/编辑在同一个事务操作,上锁防止并发 + await using (await DistributedLock.AcquireLockAsync("fuwuhao:RegisterLock", TimeSpan.FromMinutes(1))) { - throw new UserFriendlyException("你已注册过意社区账号"); - } - - var userId = await _accountService.PostSystemRegisterAsync(new RegisterDto - { - UserName = $"wx{Random.Shared.Next(100000, 999999)}", - Password = Guid.NewGuid().ToString("N"), - Phone = null, - Email = null, - Nick = userInfo.Nickname, - Icon = result.FirstOrDefault()?.Id.ToString() - }); - - await _userRepository.InsertAsync(new AiUserExtraInfoEntity(userId, userInfo.OpenId)); - await _sceneCache.SetAsync($"{FuwuhaoConst.SceneCacheKey}:{scene}", new SceneCacheDto + if (await _userRepository.IsAnyAsync(x => x.FuwuhaoOpenId == userInfo.OpenId)) { - SceneResult = SceneResultEnum.Register - }, - new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(50) }); + throw new UserFriendlyException("你已注册过意社区账号"); + } + + + var userId = await _accountService.PostSystemRegisterAsync(new RegisterDto + { + UserName = $"wx{Random.Shared.Next(100000, 999999)}", + Password = Guid.NewGuid().ToString("N"), + Phone = null, + Email = null, + Nick = userInfo.Nickname, + Icon = result.FirstOrDefault()?.Id.ToString() + }); + + await _userRepository.InsertAsync(new AiUserExtraInfoEntity(userId, userInfo.OpenId)); + await _sceneCache.SetAsync($"{FuwuhaoConst.SceneCacheKey}:{scene}", new SceneCacheDto + { + SceneResult = SceneResultEnum.Register + }, + new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(50) }); + } return "恭喜你已注册成功意社区账号!"; }