diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/BootstrapBlazor.JuHeIpLocatorProvider.csproj b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/BootstrapBlazor.JuHeIpLocatorProvider.csproj index 90bf0d02..d5a6d7f4 100644 --- a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/BootstrapBlazor.JuHeIpLocatorProvider.csproj +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/BootstrapBlazor.JuHeIpLocatorProvider.csproj @@ -1,12 +1,16 @@ + + 10.1.0 + + Bootstrap Blazor WebAssembly wasm UI Components JuHe Ip Locator Bootstrap UI components extensions of JuHeIpLocator - + diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/Extensions/ServiceCollectionExtensions.cs b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/Extensions/ServiceCollectionExtensions.cs index 90a8f529..1dcdb5f4 100644 --- a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/Extensions/ServiceCollectionExtensions.cs +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ @@ -7,12 +7,14 @@ namespace Microsoft.Extensions.DependencyInjection; /// -/// BootstrapBlazor 服务扩展类 +/// BootstrapBlazor 服务扩展类 +/// BootstrapBlazor service extensions /// -public static class BootstrapBlazoJuHeIpLocatorExtensions +public static class BootstrapBlazorJuHeIpLocatorExtensions { /// - /// 添加 AzureOpenAIService 服务 + /// 添加聚合搜索引擎 IP 定位器服务 + /// Adds JuHe IP locator service /// /// public static IServiceCollection AddBootstrapBlazorJuHeIpLocatorService(this IServiceCollection services) diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorOptions.cs b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorOptions.cs index ce58ec2c..444dbcc4 100644 --- a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorOptions.cs +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorOptions.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone @@ -6,22 +6,26 @@ namespace BootstrapBlazor.Components; /// -/// 聚合搜索引擎 IP 定位器配置类 +/// 聚合搜索引擎 IP 定位器配置类 +/// JuHe IP locator options /// class JuHeIpLocatorOptions { /// - /// 聚合搜索引擎 IP 定位器 AppKey + /// 获得/设置 聚合搜索引擎 IP 定位器 AppKey + /// Gets or sets the JuHe IP locator AppKey /// - public string Key { get; set; } = ""; + public string? Key { get; set; } /// - /// 聚合搜索引擎 IP 定位器请求地址 + /// 获得/设置 聚合搜索引擎 IP 定位器请求地址 + /// Gets or sets the JuHe IP locator request URL /// - public string Url { get; set; } = "http://apis.juhe.cn/ip/ipNew"; + public string? Url { get; set; } /// - /// 聚合搜索引擎 IP 定位器请求超时时间 默认 5 秒 + /// 获得/设置 聚合搜索引擎 IP 定位器请求超时时间 默认 5 秒 + /// Gets or sets the JuHe IP locator request timeout, default is 5 seconds /// public TimeSpan Timeout { get; set; } } diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorProvider.cs b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorProvider.cs index 9aad0e95..abdec664 100644 --- a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorProvider.cs +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorProvider.cs @@ -1,17 +1,16 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Net.Http.Json; -using System.Text.Json.Serialization; namespace BootstrapBlazor.Components; /// -/// 聚合搜索引擎 IP 定位器 +/// 聚合搜索引擎 IP 定位器 +/// JuHe IP locator provider /// /// /// @@ -26,6 +25,8 @@ class JuHeIpLocatorProvider(IHttpClientFactory httpClientFactory, private JuHeIpLocatorOptions? _options; + private const string Url = "http://apis.juhe.cn/ip/ipNewV3"; + /// /// /// @@ -52,164 +53,42 @@ private JuHeIpLocatorOptions GetOptions() { var options = juHeIpLocatorOptions.Value; - try + if (string.IsNullOrEmpty(options.Key)) { - if (string.IsNullOrEmpty(options.Key)) - { - throw new InvalidOperationException($"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key"); - } - if (string.IsNullOrEmpty(options.Url)) - { - options.Url = "http://apis.juhe.cn/ip/ipNewV3"; - } + LastError = $"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key"; + Log(LastError); + } - catch (Exception ex) + if (string.IsNullOrEmpty(options.Url)) { - logger.LogError(ex, "{GetOptions} failed", nameof(GetOptions)); + options.Url = Url; } return options; } /// - /// 请求获得地理位置接口方法 + /// 请求获得地理位置接口方法 + /// Fetches the geolocation data /// /// /// /// - /// protected virtual async Task Fetch(string url, HttpClient client, CancellationToken token) { - var result = await client.GetFromJsonAsync(url, token); + var result = await client.GetFromJsonAsync(url, token); if (result != null && result.ErrorCode != 0) { - logger.LogError("ErrorCode: {ErrorCode} Reason: {Reason}", result.ErrorCode, result.Reason); + LastError = $"ErrorCode: {result.ErrorCode} Reason: {result.Reason}"; + Log(LastError); } return result?.ToString(); } - /// - /// LocationResult 结构体 - /// - class LocationResult + private void Log(string? message) { - /// - /// 获得/设置 结果状态返回码 为 查询成功 时通讯正常 - /// - public string? Reason { get; set; } - - /// - /// 获得/设置 错误码 - /// - [JsonPropertyName("error_code")] - public int ErrorCode { get; set; } - - /// - /// 获得/设置 定位信息 - /// - public LocationData? Result { get; set; } - - /// - /// - /// - /// - public override string? ToString() + if (logger.IsEnabled(LogLevel.Error)) { - string? ret = null; - if (ErrorCode == 0) - { - ret = Result?.Country == "中国" - ? $"{Result?.Prov}{Result?.City}{Result?.District} {Result?.Isp}" - : $"{Result?.Continent} {Result?.Country} {Result?.City}"; - } - return ret; + logger.LogError("{message}", message); } } - - class LocationData - { - /// - /// 获得/设置 州 - /// - public string? Continent { get; set; } - - /// - /// 获得/设置 国家 - /// - public string? Country { get; set; } - - /// - /// 获得/设置 邮编 - /// - public string? ZipCode { get; set; } - - /// - /// 获得/设置 时区 - /// - public string? TimeZone { get; set; } - - /// - /// 获得/设置 精度 - /// - public string? Accuracy { get; set; } - - /// - /// 获得/设置 所属 - /// - public string? Owner { get; set; } - - /// - /// 获得/设置 运营商 - /// - public string? Isp { get; set; } - - /// - /// 获得/设置 来源 - /// - public string? Source { get; set; } - - /// - /// 获得/设置 区号 - /// - public string? AreaCode { get; set; } - - /// - /// 获得/设置 行政区划代码 - /// - public string? AdCode { get; set; } - - /// - /// 获得/设置 国家代码 - /// - public string? AsNumber { get; set; } - - /// - /// 获得/设置 经度 - /// - public string? Lat { get; set; } - - /// - /// 获得/设置 纬度 - /// - public string? Lng { get; set; } - - /// - /// 获得/设置 半径 - /// - public string? Radius { get; set; } - - /// - /// 获得/设置 省份 - /// - public string? Prov { get; set; } - - /// - /// 获得/设置 城市 - /// - public string? City { get; set; } - - /// - /// 获得/设置 区县 - /// - public string? District { get; set; } - } } diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationData.cs b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationData.cs new file mode 100644 index 00000000..2bb5f281 --- /dev/null +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationData.cs @@ -0,0 +1,114 @@ +// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone + +namespace BootstrapBlazor.Components; + +/// +/// 聚合定位数据类 +/// JuHe location data class +/// +class JuHeLocationData +{ + /// + /// 获得/设置 州 + /// Gets or sets the continent + /// + public string? Continent { get; set; } + + /// + /// 获得/设置 国家 + /// Gets or sets the country + /// + public string? Country { get; set; } + + /// + /// 获得/设置 邮编 + /// Gets or sets the zip code + /// + public string? ZipCode { get; set; } + + /// + /// 获得/设置 时区 + /// Gets or sets the time zone + /// + public string? TimeZone { get; set; } + + /// + /// 获得/设置 精度 + /// Gets or sets the accuracy + /// + public string? Accuracy { get; set; } + + /// + /// 获得/设置 所属 + /// Gets or sets the owner + /// + public string? Owner { get; set; } + + /// + /// 获得/设置 运营商 + /// Gets or sets the ISP + /// + public string? Isp { get; set; } + + /// + /// 获得/设置 来源 + /// Gets or sets the source + /// + public string? Source { get; set; } + + /// + /// 获得/设置 区号 + /// Gets or sets the area code + /// + public string? AreaCode { get; set; } + + /// + /// 获得/设置 行政区划代码 + /// Gets or sets the administrative division code + /// + public string? AdCode { get; set; } + + /// + /// 获得/设置 国家代码 + /// Gets or sets the AS number + /// + public string? AsNumber { get; set; } + + /// + /// 获得/设置 经度 + /// Gets or sets the latitude + /// + public string? Lat { get; set; } + + /// + /// 获得/设置 纬度 + /// Gets or sets the longitude + /// + public string? Lng { get; set; } + + /// + /// 获得/设置 半径 + /// Gets or sets the radius + /// + public string? Radius { get; set; } + + /// + /// 获得/设置 省份 + /// Gets or sets the province + /// + public string? Prov { get; set; } + + /// + /// 获得/设置 城市 + /// Gets or sets the city + /// + public string? City { get; set; } + + /// + /// 获得/设置 区县 + /// Gets or sets the district + /// + public string? District { get; set; } +} diff --git a/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationResult.cs b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationResult.cs new file mode 100644 index 00000000..42179614 --- /dev/null +++ b/src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeLocationResult.cs @@ -0,0 +1,48 @@ +// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone + +using System.Text.Json.Serialization; + +namespace BootstrapBlazor.Components; + +/// +/// 聚合定位结果类 +/// JuHe location result class +/// +class JuHeLocationResult +{ + /// + /// 获得/设置 结果状态返回码 + /// Gets or sets the result status reason + /// + public string? Reason { get; set; } + + /// + /// 获得/设置 错误码 + /// Gets or sets the error code + /// + [JsonPropertyName("error_code")] + public int ErrorCode { get; set; } + + /// + /// 获得/设置 定位信息 + /// Gets or sets the location data + /// + public JuHeLocationData? Result { get; set; } + + /// + /// + /// + public override string? ToString() + { + string? ret = null; + if (ErrorCode == 0) + { + ret = Result?.Country == "中国" + ? $"{Result?.Prov}{Result?.City}{Result?.District} {Result?.Isp}" + : $"{Result?.Continent} {Result?.Country} {Result?.City}"; + } + return ret; + } +}