From 7eab4dd5b185d47f12a4ce4ac413481b7a9ed90e Mon Sep 17 00:00:00 2001 From: chenchun Date: Tue, 22 Oct 2024 12:20:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/Account/LoginOutput.cs | 2 - .../Dtos/Account/RegisterInput.cs | 36 ------------- .../WeChatMiniProgramAccountService.cs | 51 +++++++++++-------- .../IServices/IAccountService.cs | 13 ++--- .../Services/AccountService.cs | 20 +++++++- .../Services/Authentication/AuthService.cs | 6 ++- 6 files changed, 60 insertions(+), 68 deletions(-) diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/LoginOutput.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/LoginOutput.cs index 0fa342e2..b99624a6 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/LoginOutput.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/LoginOutput.cs @@ -8,6 +8,4 @@ public class LoginOutput /// 后端访问token /// public string? Token { get; set; } - - public LoginResultEnum Result{ get; set; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/RegisterInput.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/RegisterInput.cs index 2f8609d8..88be3592 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/RegisterInput.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Account/RegisterInput.cs @@ -2,42 +2,6 @@ public class RegisterInput { - - //电话号码,根据code的表示来获取 - - /// - /// 账号 - /// - public string UserName { get; set; } - - /// - /// 密码 - /// - public string Password { get; set; } - - /// - /// 唯一标识码 - /// - public string? Uuid { get; set; } - - /// - /// 验证码 - /// - public string? Code { get; set; } - - /// - /// 电话 - /// - public long Phone { get; set; } - - - - /// - /// 昵称 - /// - public string? Nick{ get; set; } - - /// /// 微信小程序code /// diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs index 42658ede..7523553a 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs @@ -40,17 +40,17 @@ public class WeChatMiniProgramAccountService : ApplicationService //判断wxid中是否有对应的userid关系 //果然有,直接根据userid返回该用户token //如果没有,返回结果即可 - var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(intput.JsCode))).openid; + var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(intput.JsCode))) + .openid; var authInfo = await _authService.TryGetByOpenIdAsync(openId, AuthTypeConst.WeChatMiniProgram); if (authInfo is null) { - output.Result = LoginResultEnum.Error; + throw new UserFriendlyException("该小程序没有绑定任何账号", "1000", "Auth未找到对应关系"); } //根据用户id获取到用户信息 var result = await _accountService.PostLoginAsync(authInfo.UserId); - output.Result = LoginResultEnum.Success; output.Token = result.Token; return output; @@ -65,7 +65,8 @@ public class WeChatMiniProgramAccountService : ApplicationService [HttpPost("wechat/mini-program/account/bind")] public async Task PostBindAsync(BindInput input) { - _accountService.ValidationImageCaptcha(input.Uuid,input.Code); + //验证手机号 + await _accountService.ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.Bind, input.Phone, input.Code); //校验手机号与验证码 //根据手机号查询用户信息 //根据code去获取wxid @@ -77,17 +78,12 @@ public class WeChatMiniProgramAccountService : ApplicationService } //验证手机号的验证码 - await _accountService.PostCaptchaPhoneAsync(ValidationPhoneTypeEnum.Register, new PhoneCaptchaImageDto - { - Phone = input.Phone.ToString() - }); - var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid; - + await PostBindToAuthAsync(userInfo.User.Id, openId, userInfo.User.UserName); } - + private async Task PostBindToAuthAsync(Guid userId, string openId, string? name = null) { await _authService.CreateAsync(new AuthCreateOrUpdateInputDto @@ -106,19 +102,34 @@ public class WeChatMiniProgramAccountService : ApplicationService [HttpPost("wechat/mini-program/account/register")] public async Task PostRegisterAsync(RegisterInput input) { + //先校验code,openId + var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid; + //走普通注册流程 //同时再加一个小程序绑定即可 - await _accountService.PostRegisterAsync(new RegisterDto + var userName = GenerateRandomString(6); + await _accountService.PostTempRegisterAsync(new RegisterDto { - UserName = input.UserName, - Password = input.Password, - Uuid = input.Uuid, - Phone = input.Phone, - Code = input.Code, - Nick = input.Nick + UserName =$"ls-{userName}", + Password = GenerateRandomString(20), + Nick = $"临时账号-{userName}" }); - var userInfo = await _accountService.GetAsync(input.UserName, null); - var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid; + var userInfo = await _accountService.GetAsync(userName, null); + await PostBindToAuthAsync(userInfo.User.Id, openId, userInfo.User.UserName); } + + private string GenerateRandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + Random random = new Random(); + char[] stringChars = new char[length]; + + for (int i = 0; i < length; i++) + { + stringChars[i] = chars[random.Next(chars.Length)]; + } + + return new string(stringChars); + } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs index 6ce1b936..e06151a8 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs @@ -28,15 +28,16 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices Task GetAsync(string? userName,long? phone); /// - /// 手机验证码 + /// 校验电话验证码,需要与电话号码绑定 /// - /// - Task PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType, - PhoneCaptchaImageDto input); + Task ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum validationPhoneType, long phone, + string code); /// - /// 校验图片登录验证码,无需和账号绑定 + /// 临时注册 + /// 不需要验证,为了给第三方使用,例如微信小程序,后续可通过绑定操作,进行账号合并 /// - void ValidationImageCaptcha(string? uuid,string? code ); + /// + Task PostTempRegisterAsync(RegisterDto input); } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs index af1f7dfe..e20527e4 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs @@ -225,7 +225,7 @@ namespace Yi.Framework.Rbac.Application.Services /// /// [RemoteService(isEnabled:false)] - public async Task PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType, + private async Task PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType, PhoneCaptchaImageDto input) { //验证uuid 和 验证码 @@ -266,7 +266,7 @@ namespace Yi.Framework.Rbac.Application.Services /// /// 校验电话验证码,需要与电话号码绑定 /// - private async Task ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum validationPhoneType, long phone, + public async Task ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum validationPhoneType, long phone, string code) { var item = await _phoneCache.GetAsync(new CaptchaPhoneCacheKey(validationPhoneType, phone.ToString())); @@ -323,10 +323,26 @@ namespace Yi.Framework.Rbac.Application.Services await ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.Register, input.Phone, input.Code); } + //临时账号 + if (input.UserName.StartsWith("ls-")) + { + throw new UserFriendlyException("注册账号不能以ls-字符开头"); + } //注册领域逻辑 await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick); } + /// + /// 临时注册 + /// 不需要验证,为了给第三方使用,例如微信小程序,后续可通过绑定操作,进行账号合并 + /// + /// + [RemoteService(isEnabled:false)] + public async Task PostTempRegisterAsync(RegisterDto input) + { + //注册领域逻辑 + await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick); + } /// /// 查询已登录的账户信息 diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs index 3ff2d894..d7607840 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs @@ -152,13 +152,15 @@ namespace Yi.Framework.Rbac.Application.Services.Authentication { return base.DeleteAsync(id); } - + [RemoteService(IsEnabled = false)] public override async Task CreateAsync(AuthCreateOrUpdateInputDto input) { var entity = await MapToEntityAsync(input); + //还需要一步,如果当前openid已经存在被人绑定,移除 + await _repository.DeleteAsync(x => x.AuthType == input.AuthType && x.OpenId == input.OpenId); await _repository.InsertAsync(entity); - return MapToGetOutputDto(entity); + return await MapToGetOutputDtoAsync(entity); } protected override async Task CheckCreateInputDtoAsync(AuthCreateOrUpdateInputDto input)