Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/BootstrapBlazor.Server/Components/Samples/Locators.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
</Tips>
<Pre>Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)</Pre>
<p><b>@Localizer["LocatorsNormalExtendDescription"]</b></p>

<p><b>@Localizer["LocatorsNormalExtend1"]</b></p>
<Pre>private class CustomerLocatorProvider : DefaultIpLocatorProvider
{
Expand All @@ -25,35 +26,20 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
throw new NotImplementedException();
}
}</Pre>

<p><b>@Localizer["LocatorsNormalExtend2"]</b></p>
<Pre>services.AddSingleton&lt;IIpLocatorProvider, CustomerLocatorProvider&gt;();</Pre>
<p>@((MarkupString)Localizer["LocatorsNormalCustomerLocator"].Value)</p>

<p><b>@Localizer["LocatorsNormalExtend3"]</b></p>
<Pre>var provider = IpLocatorFactory.Create(ProviderName);
Location = await provider.Locate(Ip);</Pre>

<p>@Localizer["LocatorsNormalIpTitle"]</p>
<p><code>112.224.74.239</code> @Localizer["LocatorsNormalTips3"]</p>
<p><code>183.160.236.53</code> @Localizer["LocatorsNormalTips4"]</p>

<DemoBlock Title="@Localizer["LocatorsNormalTitle"]" Introduction="@Localizer["LocatorsNormalIntro"]" Name="Normal">
<section ignore>
@((MarkupString)Localizer["LocatorsProviderDesc"].Value)
<Tips>
<p>@((MarkupString)Localizer["LocatorsProviderOptions"].Value)</p>
</Tips>
<Pre>{
"BootstrapBlazorOptions": {
"WebClientOptions": {
"EnableIpLocator": true
}
}
}</Pre>
<Pre>services.AddBootstrapBlazor(op =>
{
op.WebClientOptions.EnableIpLocator = true;
});</Pre>
<Pre>services.Configure&lt;BootstrapBlazorOptions&gt;(op =>
{
op.WebClientOptions.EnableIpLocator = true;
});</Pre>
</section>
<div class="row g-3 form-inline">
<div class="col-12 col-sm-6">
<Select Items="_providers" Value="@ProviderName" OnValueChanged="OnProviderNameChanged" ShowLabel="true" DisplayText="LocatorProvider">
Expand Down
1 change: 1 addition & 0 deletions src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -4095,6 +4095,7 @@
"LocatorsNormalExtendDescription": "Extend the custom geo-location query interface",
"LocatorsNormalExtend1": "1. Implement a custom locator",
"LocatorsNormalExtend2": "2. Configure a custom locator",
"LocatorsNormalExtend3": "3. Use locator",
"LocatorsNormalCustomerLocator": "Add <code>CustomerLocatorProvider</code> to the service container using the <code>AddSingleton</code> method",
"LocatorsNormalIpTitle": "IP test data",
"LocatorsNormalTips3": "Shandong, China Unicom",
Expand Down
1 change: 1 addition & 0 deletions src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -4095,6 +4095,7 @@
"LocatorsNormalExtendDescription": "扩展自定义地理位置查询接口",
"LocatorsNormalExtend1": "1. 实现自定义定位器",
"LocatorsNormalExtend2": "2. 配置自定义定位器",
"LocatorsNormalExtend3": "3. 通过定位器定位",
"LocatorsNormalCustomerLocator": "通过 <code>AddSingleton</code> 方法将自定义定位器 <code>CustomerLocatorProvider</code> 添加到服务容器中",
"LocatorsNormalIpTitle": "IP 测试数据",
"LocatorsNormalTips3": "山东省 中国联通",
Expand Down
40 changes: 16 additions & 24 deletions src/BootstrapBlazor/Services/IPLocator/DefaultIpLocatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,31 @@ namespace BootstrapBlazor.Components;
/// <summary>
/// IIPLocatorFactory 接口实现类
/// </summary>
class DefaultIpLocatorFactory : IIpLocatorFactory
class DefaultIpLocatorFactory(IServiceProvider provider, IOptionsMonitor<BootstrapBlazorOptions> options) : IIpLocatorFactory
{
private readonly Dictionary<string, IIpLocatorProvider> _providers = [];

private readonly IServiceProvider _serviceProvider;

private readonly IOptionsMonitor<BootstrapBlazorOptions> _options;

public DefaultIpLocatorFactory(IServiceProvider provider, IOptionsMonitor<BootstrapBlazorOptions> options)
{
_serviceProvider = provider;
_options = options;

foreach (var p in provider.GetServices<IIpLocatorProvider>())
{
if (p.Key != null)
{
_providers[p.Key] = p;
}
}
}
private Dictionary<string, IIpLocatorProvider>? _providers = null;

/// <summary>
/// 创建 <see cref="IIpLocatorProvider"/> 实例方法
/// </summary>
/// <param name="key"></param>
public IIpLocatorProvider Create(string? key = null)
{
var providerKey = key;
if (string.IsNullOrEmpty(key))
_providers ??= GenerateProviders();
var providerKey = key ?? options.CurrentValue.IpLocatorOptions.ProviderName;
return string.IsNullOrEmpty(providerKey) ? _providers.Values.Last() : _providers[providerKey];
}

private Dictionary<string, IIpLocatorProvider> GenerateProviders()
{
var providers = new Dictionary<string, IIpLocatorProvider>();
foreach (var p in provider.GetServices<IIpLocatorProvider>())
{
providerKey = _options.CurrentValue.IpLocatorOptions.ProviderName;
if (p.Key != null)
{
providers[p.Key] = p;
}
}
return string.IsNullOrEmpty(providerKey) ? _providers.Values.Last() : _providers[providerKey];
return providers;
}
}
Loading