feat: 增加地理位置定位功能

This commit is contained in:
Argo-Tianyi 2022-01-19 15:41:36 +08:00
parent 3a0525f11f
commit 84e4c68413
6 changed files with 80 additions and 3 deletions

View File

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

View File

@ -483,4 +483,25 @@ class DictService : IDict
return true; return true;
} }
/// <summary>
///
/// </summary>
/// <returns></returns>
public string? GetIpLocatorName()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "IP地理位置接口" && s.Define == EnumDictDefine.System)?.Code;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string? GetIpLocatorUrl(string? name)
{
var dicts = GetAll();
return string.IsNullOrWhiteSpace(name) ? null : dicts.FirstOrDefault(s => s.Category == "地理位置" && s.Name == name && s.Define == EnumDictDefine.System)?.Code;
}
} }

View File

@ -13,6 +13,19 @@ public interface IDict
/// <returns></returns> /// <returns></returns>
List<Dict> GetAll(); List<Dict> GetAll();
/// <summary>
///
/// </summary>
/// <returns></returns>
string? GetIpLocatorName();
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string? GetIpLocatorUrl(string? name);
/// <summary> /// <summary>
/// 获得 配置所有的 App 集合 /// 获得 配置所有的 App 集合
/// </summary> /// </summary>

View File

@ -56,6 +56,13 @@ public partial class AdminLogin : IDisposable
[NotNull] [NotNull]
private WebClientService? WebClientService { get; set; } private WebClientService? WebClientService { get; set; }
/// <summary>
///
/// </summary>
[Inject]
[NotNull]
private IIPLocatorProvider? IPLocatorProvider { get; set; }
private string? ClassString => CssBuilder.Default("login-wrap") private string? ClassString => CssBuilder.Default("login-wrap")
.AddClass("is-mobile", UseMobileLogin) .AddClass("is-mobile", UseMobileLogin)
.Build(); .Build();
@ -129,7 +136,12 @@ public partial class AdminLogin : IDisposable
public async Task Log(string userName, bool result) public async Task Log(string userName, bool result)
{ {
var clientInfo = await WebClientService.GetClientInfo(); var clientInfo = await WebClientService.GetClientInfo();
LoginService.Log(userName, clientInfo.Ip, clientInfo.OS, clientInfo.Browser, clientInfo.City, clientInfo.UserAgent, result); var city = "XX XX";
if (!string.IsNullOrEmpty(clientInfo.Ip))
{
city = await IPLocatorProvider.Locate(clientInfo.Ip);
}
LoginService.Log(userName, clientInfo.Ip, clientInfo.OS, clientInfo.Browser, city, clientInfo.UserAgent, result);
} }
private void Dispose(bool disposing) private void Dispose(bool disposing)

View File

@ -62,6 +62,10 @@ namespace BootstrapAdmin.Web.Shared
[NotNull] [NotNull]
private BootstrapAppContext? AppContext { get; set; } private BootstrapAppContext? AppContext { get; set; }
[Inject]
[NotNull]
private IIPLocatorProvider? IPLocatorProvider { get; set; }
private string? Title { get; set; } private string? Title { get; set; }
private string? Footer { get; set; } private string? Footer { get; set; }
@ -93,10 +97,15 @@ namespace BootstrapAdmin.Web.Shared
{ {
// TODO: 可考虑加入队列中,通过任务管理定时插入提高效率 // TODO: 可考虑加入队列中,通过任务管理定时插入提高效率
var clientInfo = await WebClientService.GetClientInfo(); var clientInfo = await WebClientService.GetClientInfo();
var city = "XX XX";
if (!string.IsNullOrEmpty(clientInfo.Ip))
{
city = await IPLocatorProvider.Locate(clientInfo.Ip);
}
TraceService.Log(new Trace TraceService.Log(new Trace
{ {
Browser = clientInfo.Browser, Browser = clientInfo.Browser,
City = clientInfo.City, City = city,
Ip = clientInfo.Ip, Ip = clientInfo.Ip,
LogTime = DateTime.Now, LogTime = DateTime.Now,
OS = clientInfo.OS, OS = clientInfo.OS,

View File

@ -0,0 +1,22 @@
using BootstrapAdmin.Web.Core;
namespace BootstrapAdmin.Web.Utils;
static class LocatorHelper
{
public static IIPLocator CreateLocator(IServiceProvider provider)
{
var dictService = provider.GetRequiredService<IDict>();
var providerName = dictService.GetIpLocatorName();
var providerUrl = dictService.GetIpLocatorUrl(providerName ?? string.Empty);
// TODO: 稍后完善 其余地理位置定位服务
return providerName switch
{
"BaiDuIPSvr" => new BaiDuIPLocator(),
"JuheIPSvr" => new BaiDuIPLocator(),
"BaiDuIP138Svr" => new BaiDuIPLocator() { Url = providerUrl },
_ => new DefaultIPLocator()
};
}
}