feat: 完成微信小程序账户应用服务

This commit is contained in:
橙子
2024-10-19 16:17:26 +08:00
parent 736995c35b
commit 0f687a7e34
16 changed files with 363 additions and 73 deletions

View File

@@ -0,0 +1,8 @@
namespace Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Account;
public class BindInput
{
public string JsCode { get; set; }
public long Phone { get; set; }
}

View File

@@ -3,7 +3,7 @@
public class LoginInput
{
/// <summary>
/// 微信小程序code
/// 微信小程序jscode
/// </summary>
public string Code { get; set; }
public string JsCode { get; set; }
}

View File

@@ -0,0 +1,43 @@
namespace Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Account;
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 long Phone { get; set; }
/// <summary>
/// 验证码
/// </summary>
public string? Code { get; set; }
/// <summary>
/// 昵称
/// </summary>
public string? Nick{ get; set; }
/// <summary>
/// 微信小程序code
/// </summary>
public string JsCode { get; set; }
}

View File

@@ -1,41 +0,0 @@
using Volo.Abp.Application.Services;
namespace Yi.Framework.DigitalCollectibles.Application.Services.Account;
public class CollectiblesAccountService: ApplicationService
{
/// <summary>
/// 小程序登录
/// </summary>
/// <returns></returns>
public Task PostLoginAsync()
{
throw new NotImplementedException();
//根据code去获取wxid
//判断wxid中是否有对应的userid关系
//果然有直接根据userid返回该用户token
//如果没有,返回结果即可
}
/// <summary>
/// 小程序绑定账号
/// </summary>
/// <returns></returns>
public Task PostBindAsync()
{
throw new NotImplementedException();
//根据code去获取wxid
//校验手机号
//根据手机号查询用户信息
//将wxid和用户user绑定
}
//小程序注册
public Task PostRegisterAsync()
{
throw new NotImplementedException();
//走普通注册流程
//同时再加一个小程序绑定即可
}
}

View File

@@ -0,0 +1,123 @@
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Account;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Enums;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.WeChat.MiniProgram;
using Yi.Framework.WeChat.MiniProgram.HttpModels;
namespace Yi.Framework.DigitalCollectibles.Application.Services.Account;
/// <summary>
/// 微信小程序账户应用服务
/// </summary>
public class WeChatMiniProgramAccountService : ApplicationService
{
private readonly IWeChatMiniProgramManager _weChatMiniProgramManager;
private readonly IAuthService _authService;
private readonly IAccountService _accountService;
public WeChatMiniProgramAccountService(IWeChatMiniProgramManager weChatMiniProgramManager, IAuthService authService,
IAccountService accountService)
{
_weChatMiniProgramManager = weChatMiniProgramManager;
_authService = authService;
_accountService = accountService;
}
/// <summary>
/// 使用小程序jsCode登录意社区账号
/// </summary>
/// <returns></returns>
[HttpPost("wechat/mini-program/account/login")]
public async Task<LoginOutput> PostLoginAsync(LoginInput intput)
{
var output = new LoginOutput();
//根据code去获取wxid
//判断wxid中是否有对应的userid关系
//果然有直接根据userid返回该用户token
//如果没有,返回结果即可
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;
}
//根据用户id获取到用户信息
var result = await _accountService.PostLoginAsync(authInfo.UserId);
output.Result = LoginResultEnum.Success;
output.Token = result.Token;
return output;
}
/// <summary>
/// 将小程序第三方授权绑定给意社区账号
/// </summary>
/// <param name="input"></param>
/// <exception cref="UserFriendlyException"></exception>
[HttpPost("wechat/mini-program/account/bind")]
public async Task PostBindAsync(BindInput input)
{
//校验手机号与验证码
//根据手机号查询用户信息
//根据code去获取wxid
//将wxid和用户user绑定
var userInfo = await _accountService.GetAsync(null, input.Phone);
if (userInfo is null)
{
throw new UserFriendlyException("该手机号未被注册,无法绑定微信小程序");
}
//验证手机号的验证码
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
{
UserId = userId,
OpenId = openId,
Name = name ?? "未知",
AuthType = AuthTypeConst.WeChatMiniProgram
});
}
/// <summary>
/// 使用小程序去注册意社区账号
/// </summary>
/// <param name="input"></param>
[HttpPost("wechat/mini-program/account/register")]
public async Task PostRegisterAsync(RegisterInput input)
{
//走普通注册流程
//同时再加一个小程序绑定即可
await _accountService.PostRegisterAsync(new RegisterDto
{
UserName = input.UserName,
Password = input.Password,
Uuid = input.Uuid,
Phone = input.Phone,
Code = input.Code,
Nick = input.Nick
});
var userInfo = await _accountService.GetAsync(input.UserName, null);
var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid;
await PostBindToAuthAsync(userInfo.User.Id, openId, userInfo.User.UserName);
}
}

View File

@@ -0,0 +1,6 @@
namespace Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
public class AuthTypeConst
{
public const string WeChatMiniProgram = "WeChatMiniProgram";
}

View File

@@ -10,10 +10,12 @@
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.Mapster\Yi.Framework.Mapster.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.WeChat.MiniProgram\Yi.Framework.WeChat.MiniProgram.csproj" />
<ProjectReference Include="..\..\setting-management\Yi.Framework.SettingManagement.Domain\Yi.Framework.SettingManagement.Domain.csproj" />
<ProjectReference Include="..\Yi.Framework.DigitalCollectibles.Domain.Shared\Yi.Framework.DigitalCollectibles.Domain.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="EventHandlers\" />
<Folder Include="Repositories\" />

View File

@@ -4,12 +4,14 @@ using Volo.Abp.Modularity;
using Yi.Framework.DigitalCollectibles.Domain.Shared;
using Yi.Framework.Mapster;
using Yi.Framework.SettingManagement.Domain;
using Yi.Framework.WeChat.MiniProgram;
namespace Yi.Framework.DigitalCollectibles.Domain
{
[DependsOn(
typeof(YiFrameworkDigitalCollectiblesDomainSharedModule),
typeof(YiFrameworkWeChatMiniProgramModule),
typeof(YiFrameworkSettingManagementDomainModule),
typeof(YiFrameworkMapsterModule),
typeof(AbpDddDomainModule),