Enterprise-level component library based on Bootstrap and Blazor

star nuget master download license repo commit

获取 IP 地理位置

多用系统日志跟踪

注入服务显示客户端地理位置信息

Demo

用法介绍

组件中使用注入服务 IIPLocatorProvider 调用 Locate 方法

[Inject]
[NotNull]
private IIPLocatorProvider? IPLocator { get; set; }
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
IP 测试数据
112.224.74.239 山东省济南市 联通
183.160.236.53 安徽省合肥市 电信

扩展自定义地理位置查询接口

1. 实现自定义定位器

private class CustomerLocator : IIPLocator
{
    public Task<string> Locate(IPLocatorOption option)
    {
        throw new NotImplementedException();
    }
}

2. 配置自定义定位器

public void ConfigureServices(IServiceCollection services)
{
    services.AddBootstrapBlazor(locatorAction: option =>
    {
        option.LocatorName = "CustomerLocator";
        option.LocatorFactory = name => new CustomerLocator();
        option.Url = "http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=";
    });
}
通过 AddBootstrapBlazor 方法的回调委托参数进行自定义定位器 CustomerLocator 配置
@page "/locator"

<h3>获取 IP 地理位置</h3>

<h4>多用系统日志跟踪</h4>

<Block Title="普通用法" Introduction="注入服务显示客户端地理位置信息">
    <p><b>用法介绍</b></p>
    <div class="mb-3">
        <p>组件中使用注入服务 <code>IIPLocatorProvider</code> 调用 <code>Locate</code> 方法</p>
        <Pre>[Inject]
[NotNull]
private IIPLocatorProvider? IPLocator { get; set; }
</Pre>
    </div>
    <Tips>
        <div>由于地理位置查询接口返回字符集可能是其他字符集如 <code>gbk</code>,程序会报错;</div>
        <div><b>解决办法:</b></div>
        <div><code>Startup</code> 文件中 <code>ConfigureServices</code> 方法内增加下面这句话即可解决</div>
    </Tips>
    <Pre>Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)</Pre>
    <div class="mb-2">IP 测试数据</div>
    <div><code>112.224.74.239</code> 山东省济南市 联通</div>
    <div class="mb-3"><code>183.160.236.53</code> 安徽省合肥市 电信</div>
    <div class="row g-3 form-inline">
        <div class="col-12 col-sm-6">
            <BootstrapInput @bind-Value="Ip" DisplayText="Ip 地址" ShowLabel="true" />
        </div>
        <div class="col-12 col-sm-6">
            <Display Value="Location" DisplayText="地理位置" ShowLabel="true" />
        </div>
        <div class="col-12">
            <Button Icon="fa fa-location" Text="定位" OnClick="OnClick" />
        </div>
    </div>
    <p class="mt-3"><b>扩展自定义地理位置查询接口</b></p>
    <div>
        <p><b>1. 实现自定义定位器</b></p>
        <Pre>private class CustomerLocator : IIPLocator
{
    public Task&lt;string&gt; Locate(IPLocatorOption option)
    {
        throw new NotImplementedException();
    }
}</Pre>
        <p><b>2. 配置自定义定位器</b></p>
        <Pre>public void ConfigureServices(IServiceCollection services)
{
    services.AddBootstrapBlazor(locatorAction: option =>
    {
        option.LocatorName = "CustomerLocator";
        option.LocatorFactory = name => new CustomerLocator();
        option.Url = "http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=";
    });
}</Pre>
        <div>通过 <code>AddBootstrapBlazor</code> 方法的回调委托参数进行自定义定位器 <code>CustomerLocator</code> 配置</div>
    </div>
</Block>
// 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 BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace BootstrapBlazor.Shared.Pages
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Locator
    {
        [Inject]
        [NotNull]
        private WebClientService? ClientService { get; set; }

        [Inject]
        [NotNull]
        private IIPLocatorProvider? IPLocator { get; set; }

        private string? Ip { get; set; }

        private string? Location { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="firstRender"></param>
        /// <returns></returns>
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);

            if (firstRender)
            {
                await ClientService.RetrieveRemoteInfo();
                Ip = ClientService.Ip;
                StateHasChanged();
            }
        }

        private async Task OnClick()
        {
            if (!string.IsNullOrEmpty(Ip))
            {
                Location = await IPLocator.Locate(Ip);
            }
        }
    }
}

B 站相关视频链接

暂无

交流群

QQ Group:BootstrapAdmin & Blazor 795206915( Full) 675147445 Welcome to join the group discussion
Themes
Bootstrap
Ant Design (完善中)
Bluma (完善中)
LayUI (完善中)
Motronic (已集成)
An error has occurred. This application may no longer respond until reloaded. Reload