feat: 重构登录日志

This commit is contained in:
Argo-Tianyi 2022-01-15 13:18:07 +08:00
parent 4d98083434
commit 95bec59886
3 changed files with 55 additions and 6 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="6.2.7-beta05" />
<PackageReference Include="BootstrapBlazor" Version="6.2.7-beta08" />
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.1" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" />

View File

@ -1,4 +1,5 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Services;
using Microsoft.JSInterop;
namespace BootstrapAdmin.Web.Components;
@ -6,7 +7,7 @@ namespace BootstrapAdmin.Web.Components;
/// <summary>
///
/// </summary>
public partial class AdminLogin
public partial class AdminLogin : IDisposable
{
private string? Title { get; set; }
@ -22,6 +23,8 @@ public partial class AdminLogin
private string? PostUrl { get; set; }
private JSInterop<AdminLogin>? Interop { get; set; }
/// <summary>
///
/// </summary>
@ -38,10 +41,21 @@ public partial class AdminLogin
[NotNull]
private IDict? DictsService { get; set; }
[Inject]
[NotNull]
private ILogin? LoginService { get; set; }
[Inject]
[NotNull]
private IJSRuntime? JSRuntime { get; set; }
/// <summary>
///
/// </summary>
[Inject]
[NotNull]
private WebClientService? WebClientService { get; set; }
private string? ClassString => CssBuilder.Default("login-wrap")
.AddClass("is-mobile", UseMobileLogin)
.Build();
@ -85,7 +99,8 @@ public partial class AdminLogin
if (firstRender)
{
// register javascript
await JSRuntime.InvokeVoidAsync("$.login", LoginForm, "api/Login");
Interop = new JSInterop<AdminLogin>(JSRuntime);
await Interop.InvokeVoidAsync(this, LoginForm, "login", "api/Login", nameof(Log));
}
}
@ -104,4 +119,37 @@ public partial class AdminLogin
{
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="result"></param>
[JSInvokable]
public async Task Log(string userName, bool result)
{
var clientInfo = await WebClientService.GetClientInfo();
LoginService.Log(userName, clientInfo.Ip, clientInfo.OS, clientInfo.Browser, clientInfo.City, clientInfo.UserAgent, result);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (Interop != null)
{
Interop.Dispose();
Interop = null;
}
}
}
/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

View File

@ -6,7 +6,7 @@
var base = $('head > base').attr('href');
return base + url;
},
login: function (form, url) {
login: function (form, obj, url, method) {
var $form = $(form);
var $login = $('.login-wrap');
@ -70,7 +70,7 @@
}
}
var authenticate = function (postData, mobile) {
var authenticate = function (userName, postData, mobile) {
var postUrl = url + '?mobile=' + mobile;
$.ajax({
url: $.formatUrl(postUrl),
@ -80,6 +80,7 @@
dataType: 'json',
crossDomain: false,
success: function (result) {
obj.invokeMethodAsync(method, userName, result.authenticated);
if (result.authenticated) {
$form.submit();
}
@ -104,7 +105,7 @@
var postData = JSON.stringify({ userName, password });
// call webapi authenticate
authenticate(postData, mobile);
authenticate(userName, postData, mobile);
});
}
});