fix: 注册接口加分布式锁防止并发重复注册
This commit is contained in:
@@ -226,27 +226,32 @@ public class FuwuhaoService : ApplicationService
|
|||||||
|
|
||||||
var result = await _fileService.Post(files);
|
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("你已注册过意社区账号");
|
if (await _userRepository.IsAnyAsync(x => x.FuwuhaoOpenId == userInfo.OpenId))
|
||||||
}
|
|
||||||
|
|
||||||
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
|
throw new UserFriendlyException("你已注册过意社区账号");
|
||||||
},
|
}
|
||||||
new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(50) });
|
|
||||||
|
|
||||||
|
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 "恭喜你已注册成功意社区账号!";
|
return "恭喜你已注册成功意社区账号!";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user