feat: 完成账号逻辑处理
This commit is contained in:
@@ -8,6 +8,4 @@ public class LoginOutput
|
||||
/// 后端访问token
|
||||
/// </summary>
|
||||
public string? Token { get; set; }
|
||||
|
||||
public LoginResultEnum Result{ get; set; }
|
||||
}
|
||||
@@ -2,42 +2,6 @@
|
||||
|
||||
public class RegisterInput
|
||||
{
|
||||
|
||||
//电话号码,根据code的表示来获取
|
||||
|
||||
/// <summary>
|
||||
/// 账号
|
||||
/// </summary>
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 唯一标识码
|
||||
/// </summary>
|
||||
public string? Uuid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 验证码
|
||||
/// </summary>
|
||||
public string? Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 电话
|
||||
/// </summary>
|
||||
public long Phone { get; set; }
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
/// </summary>
|
||||
public string? Nick{ get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序code
|
||||
/// </summary>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -28,15 +28,16 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices
|
||||
Task<UserRoleMenuDto?> GetAsync(string? userName,long? phone);
|
||||
|
||||
/// <summary>
|
||||
/// 手机验证码
|
||||
/// 校验电话验证码,需要与电话号码绑定
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<object> PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType,
|
||||
PhoneCaptchaImageDto input);
|
||||
Task ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum validationPhoneType, long phone,
|
||||
string code);
|
||||
|
||||
/// <summary>
|
||||
/// 校验图片登录验证码,无需和账号绑定
|
||||
/// 临时注册
|
||||
/// 不需要验证,为了给第三方使用,例如微信小程序,后续可通过绑定操作,进行账号合并
|
||||
/// </summary>
|
||||
void ValidationImageCaptcha(string? uuid,string? code );
|
||||
/// <param name="input"></param>
|
||||
Task PostTempRegisterAsync(RegisterDto input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[RemoteService(isEnabled:false)]
|
||||
public async Task<object> PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType,
|
||||
private async Task<object> PostCaptchaPhoneAsync(ValidationPhoneTypeEnum validationPhoneType,
|
||||
PhoneCaptchaImageDto input)
|
||||
{
|
||||
//验证uuid 和 验证码
|
||||
@@ -266,7 +266,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// <summary>
|
||||
/// 校验电话验证码,需要与电话号码绑定
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 临时注册
|
||||
/// 不需要验证,为了给第三方使用,例如微信小程序,后续可通过绑定操作,进行账号合并
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
[RemoteService(isEnabled:false)]
|
||||
public async Task PostTempRegisterAsync(RegisterDto input)
|
||||
{
|
||||
//注册领域逻辑
|
||||
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone, input.Nick);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询已登录的账户信息
|
||||
|
||||
@@ -152,13 +152,15 @@ namespace Yi.Framework.Rbac.Application.Services.Authentication
|
||||
{
|
||||
return base.DeleteAsync(id);
|
||||
}
|
||||
|
||||
|
||||
[RemoteService(IsEnabled = false)]
|
||||
public override async Task<AuthOutputDto> 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)
|
||||
|
||||
Reference in New Issue
Block a user