From a395512a242a8400311492ab10d563bcb6d1f2a2 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 23 Dec 2021 12:36:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/SMSButton.razor | 9 +++- .../Components/SMSButton.razor.cs | 47 +++++++++++++++---- .../Controllers/AccountController.cs | 28 +++-------- .../Pages/Account/Login.razor | 4 -- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor b/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor index b3a59274..a1e6971d 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor @@ -1,6 +1,11 @@ - + + + + + + - + diff --git a/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor.cs index 5893dfab..b4e045b2 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Components/SMSButton.razor.cs @@ -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) diff --git a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs index e0958c65..4c370374 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs @@ -96,7 +96,11 @@ namespace Bootstrap.Admin.Controllers /// /// [HttpPost()] - public async Task Mobile([FromServices] ISMSProvider provider, [FromServices] ILogin loginService, string phone, string code) + public async Task 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(); } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor b/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor index 8dde3378..f98d659a 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor @@ -8,10 +8,6 @@