微信小程序短信登录,运动员新增时验证码判断

This commit is contained in:
马梓彭 2026-03-31 16:31:48 +08:00
parent 440e929311
commit 10fb96434c
5 changed files with 68 additions and 8 deletions

View File

@ -4,6 +4,7 @@ using YD_Event.Core.Entities;
using AppUserEntity = YD_Event.Core.Entities.AppUser; using AppUserEntity = YD_Event.Core.Entities.AppUser;
using YD_Event.Core.Enum; using YD_Event.Core.Enum;
using Mapster; using Mapster;
using EasyCaching.Core;
namespace YD_Event.Application.AppUser; namespace YD_Event.Application.AppUser;
@ -15,14 +16,17 @@ public class AppUserService : BaseService<AppUserEntity>, ITransient
private readonly ISqlSugarRepository<AppUserEntity> _appUserRep; private readonly ISqlSugarRepository<AppUserEntity> _appUserRep;
private readonly ISqlSugarClient _db; private readonly ISqlSugarClient _db;
private readonly AuthManager _authManager; private readonly AuthManager _authManager;
private readonly IEasyCachingProvider _cachingProvider;
public AppUserService(ISqlSugarRepository<AppUserEntity> appUserRep, public AppUserService(ISqlSugarRepository<AppUserEntity> appUserRep,
ISqlSugarClient db, ISqlSugarClient db,
AuthManager authManager) : base(appUserRep) AuthManager authManager,
IEasyCachingProvider cachingProvider) : base(appUserRep)
{ {
_appUserRep = appUserRep; _appUserRep = appUserRep;
_db = db; _db = db;
_authManager = authManager; _authManager = authManager;
_cachingProvider = cachingProvider;
} }
/// <summary> /// <summary>
@ -133,6 +137,23 @@ public class AppUserService : BaseService<AppUserEntity>, ITransient
[DisplayName("统一新增/编辑成员")] [DisplayName("统一新增/编辑成员")]
public async Task SaveUnifiedMember(CreateOrUpdateMemberInput input) public async Task SaveUnifiedMember(CreateOrUpdateMemberInput input)
{ {
// 仅在新增领队/教练时验证验证码
if (!input.Id.HasValue && (input.PersonType == PersonType.Leader || input.PersonType == PersonType.Coach))
{
if (string.IsNullOrWhiteSpace(input.VerificationCode))
{
throw Oops.Bah("验证码不能为空");
}
var cacheKey = $"sms:code:{input.Mobile}";
var cachedCodeResult = _cachingProvider.Get<string>(cacheKey);
string? cachedCode = cachedCodeResult.Value;
if (!cachedCodeResult.HasValue || cachedCode != input.VerificationCode)
{
throw Oops.Bah("验证码错误或已过期");
}
}
var (userId, merchantCertificationId) = await GetCurrentUserAndMerchantCertificationId(); var (userId, merchantCertificationId) = await GetCurrentUserAndMerchantCertificationId();
// 校验唯一性 (手机号或身份证号,具体根据角色定) // 校验唯一性 (手机号或身份证号,具体根据角色定)

View File

@ -190,6 +190,11 @@ public class CreateOrUpdateMemberInput
/// 备注 /// 备注
/// </summary> /// </summary>
public string? Remark { get; set; } public string? Remark { get; set; }
/// <summary>
/// 验证码(仅新增领队/教练时需要)
/// </summary>
public string? VerificationCode { get; set; }
} }
/// <summary> /// <summary>

View File

@ -15,9 +15,14 @@ public class MiniProgramLoginInput
public string Mobile { get; set; } public string Mobile { get; set; }
/// <summary> /// <summary>
/// 密码 /// 密码(账号登录时必填)
/// </summary> /// </summary>
public string Password { get; set; } public string Password { get; set; }
/// <summary>
/// 验证码(短信登录时必填)
/// </summary>
public string VerificationCode { get; set; }
} }
/// <summary> /// <summary>

View File

@ -30,7 +30,7 @@ public class OAuthController : IDynamicApiController
} }
/// <summary> /// <summary>
/// 登录接口 /// 登录接口(支持账号密码登录和短信验证码登录两种方式)
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
@ -38,9 +38,17 @@ public class OAuthController : IDynamicApiController
[AllowAnonymous] [AllowAnonymous]
public async Task<MiniProgramLoginOutput> Login([FromBody] MiniProgramLoginInput input) public async Task<MiniProgramLoginOutput> Login([FromBody] MiniProgramLoginInput input)
{ {
if (string.IsNullOrEmpty(input.Password) ) var isPasswordLogin = !string.IsNullOrEmpty(input.Password);
var isSmsLogin = !string.IsNullOrEmpty(input.VerificationCode);
if (!isPasswordLogin && !isSmsLogin)
{ {
throw Oops.Bah("密码必填"); throw Oops.Bah("请选择密码登录或短信验证码登录");
}
if (isPasswordLogin && isSmsLogin)
{
throw Oops.Bah("请选择一种登录方式");
} }
var appUser = await _appUserRepository.GetFirstAsync(x => x.Mobile == input.Mobile); var appUser = await _appUserRepository.GetFirstAsync(x => x.Mobile == input.Mobile);
@ -49,7 +57,7 @@ public class OAuthController : IDynamicApiController
throw Oops.Bah("用户不存在,请先注册"); throw Oops.Bah("用户不存在,请先注册");
} }
if (!string.IsNullOrEmpty(input.Password)) if (isPasswordLogin)
{ {
if (string.IsNullOrEmpty(appUser.Password)) if (string.IsNullOrEmpty(appUser.Password))
{ {
@ -61,6 +69,17 @@ public class OAuthController : IDynamicApiController
throw Oops.Bah("密码错误"); throw Oops.Bah("密码错误");
} }
} }
else
{
var cacheKey = $"{SmsCodeCachePrefix}{input.Mobile}";
var cachedCodeResult = _cachingProvider.Get<string>(cacheKey);
string cachedCode = cachedCodeResult.Value;
if (!cachedCodeResult.HasValue || cachedCode != input.VerificationCode)
{
throw Oops.Bah("验证码错误或已过期");
}
_cachingProvider.Remove(cacheKey);
}
(string accessToken, string refreshToken) = GenerateTokens(appUser); (string accessToken, string refreshToken) = GenerateTokens(appUser);

View File

@ -282,6 +282,11 @@
备注 备注
</summary> </summary>
</member> </member>
<member name="P:YD_Event.Application.AppUser.Dtos.CreateOrUpdateMemberInput.VerificationCode">
<summary>
验证码(仅新增领队/教练时需要)
</summary>
</member>
<member name="T:YD_Event.Application.AppUser.Dtos.ChangeMemberStatusInput"> <member name="T:YD_Event.Application.AppUser.Dtos.ChangeMemberStatusInput">
<summary> <summary>
成员状态变更输入 成员状态变更输入
@ -2092,7 +2097,12 @@
</member> </member>
<member name="P:YD_Event.Application.Client.Dtos.MiniProgramLoginInput.Password"> <member name="P:YD_Event.Application.Client.Dtos.MiniProgramLoginInput.Password">
<summary> <summary>
密码 密码(账号登录时必填)
</summary>
</member>
<member name="P:YD_Event.Application.Client.Dtos.MiniProgramLoginInput.VerificationCode">
<summary>
验证码(短信登录时必填)
</summary> </summary>
</member> </member>
<member name="T:YD_Event.Application.Client.Dtos.MiniProgramLoginOutput"> <member name="T:YD_Event.Application.Client.Dtos.MiniProgramLoginOutput">
@ -2601,7 +2611,7 @@
</member> </member>
<member name="M:YD_Event.Application.Client.OAuthController.Login(YD_Event.Application.Client.Dtos.MiniProgramLoginInput)"> <member name="M:YD_Event.Application.Client.OAuthController.Login(YD_Event.Application.Client.Dtos.MiniProgramLoginInput)">
<summary> <summary>
登录接口 登录接口(支持账号密码登录和短信验证码登录两种方式)
</summary> </summary>
<param name="input"></param> <param name="input"></param>
<returns></returns> <returns></returns>