feat: 更新手机登录逻辑

This commit is contained in:
Argo-Tianyi 2021-12-23 12:36:48 +08:00
parent ae746cd352
commit a395512a24
4 changed files with 51 additions and 37 deletions

View File

@ -1,6 +1,11 @@
<BootstrapInputGroup class="mt-3">
<BootstrapInputGroup>
<BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
<BootstrapInput @bind-Value="@PhoneNumber" type="tel" name="phone" maxlength="11" PlaceHolder="手机号码" />
</BootstrapInputGroup>
<BootstrapInputGroup class="mt-3">
<BootstrapInputGroupIcon Icon="fa fa-fw fa-lock" />
<BootstrapInput TValue="string" typeof="number" name="code" class="digits" maxlength="4" IsDisabled="IsSendCode" PlaceHolder="验证码" />
<BootstrapInput TValue="string" typeof="number" name="code" maxlength="4" IsDisabled="IsSendCode" @bind-Value="@Code" PlaceHolder="验证码" />
<Button class="btn-sms" Text="@SendCodeText" OnClick="OnSendCode" LoadingIcon="" IsAsync="true">
<Tooltip Title="点击发送验证码" Placement="Placement.Top" />
</Button>

View File

@ -1,4 +1,6 @@
namespace BootstrapAdmin.Web.Components;
using BootstrapAdmin.Web.Services.SMS;
namespace BootstrapAdmin.Web.Components;
public partial class SMSButton : IDisposable
{
@ -8,18 +10,45 @@ public partial class SMSButton : IDisposable
private CancellationTokenSource? CancelToken { get; set; }
[NotNull]
private string? PhoneNumber { get; set; }
private string? Code { get; set; }
[Inject]
[NotNull]
private ISMSProvider? SMSProvider { get; set; }
async Task OnSendCode()
{
IsSendCode = false;
var count = 60;
CancelToken ??= new CancellationTokenSource();
while (!CancelToken.IsCancellationRequested && count > 0)
if (!string.IsNullOrEmpty(PhoneNumber))
{
SendCodeText = $"发送验证码 ({count--})";
StateHasChanged();
await Task.Delay(1000, CancelToken.Token);
var result = await SMSProvider.SendCodeAsync(PhoneNumber);
if (result.Result)
{
#if DEBUG
Code = result.Data;
#endif
IsSendCode = false;
var count = 60;
CancelToken ??= new CancellationTokenSource();
while (CancelToken != null && !CancelToken.IsCancellationRequested && count > 0)
{
SendCodeText = $"发送验证码 ({count--})";
StateHasChanged();
await Task.Delay(1000, CancelToken.Token);
}
SendCodeText = "发送验证码";
}
else
{
// 短信发送失败
}
}
else
{
// 手机号不可为空
}
SendCodeText = "发送验证码";
}
private void Dispose(bool disposing)

View File

@ -96,7 +96,11 @@ namespace Bootstrap.Admin.Controllers
/// <param name="code"></param>
/// <returns></returns>
[HttpPost()]
public async Task<IActionResult> Mobile([FromServices] ISMSProvider provider, [FromServices] ILogin loginService, string phone, string code)
public async Task<IActionResult> Mobile(string phone, string code,
[FromServices] ISMSProvider provider,
[FromServices] ILogin loginService,
[FromServices] IUser userService,
[FromServices] BootstrapAppContext context)
{
if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin();
@ -104,27 +108,7 @@ namespace Bootstrap.Admin.Controllers
await loginService.Log(phone, auth);
if (auth)
{
//var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName == phone);
//if (user == null)
//{
// user = new User()
// {
// ApprovedBy = "Mobile",
// ApprovedTime = DateTime.Now,
// DisplayName = "手机用户",
// UserName = phone,
// Password = code,
// Icon = "default.jpg",
// Description = "手机用户",
// App = provider.Options.App
// };
// if (UserHelper.Save(user) && !string.IsNullOrEmpty(user.Id))
// {
// // 根据配置文件设置默认角色
// var roles = RoleHelper.Retrieves().Where(r => provider.Options.Roles.Any(rl => rl.Equals(r.RoleName, StringComparison.OrdinalIgnoreCase))).Select(r => r.Id!);
// RoleHelper.SaveByUserId(user.Id, roles);
// }
//}
userService.TryCreateUserByPhone(phone, context.AppId, provider.Options.Roles);
}
return auth ? await SignInAsync(phone, true, MobileSchema) : RedirectLogin();
}

View File

@ -8,10 +8,6 @@
<div class="login-wrap">
@if (UseMobileLogin)
{
<BootstrapInputGroup>
<BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
<BootstrapInput TValue="string" type="tel" name="phone" class="digits" maxlength="11" PlaceHolder="手机号码" />
</BootstrapInputGroup>
<SMSButton />
}
else