/// <summary> /// 构造函数 /// </summary> /// <param name="provider"></param> /// <param name="factory"></param> /// <param name="logger"></param> /// <param name="option"></param> public DefaultIPLocatorProvider(IServiceProvider provider, IHttpClientFactory factory, ILogger <DefaultIPLocatorProvider> logger, IOptionsMonitor <IPLocatorOption> option) { _provider = provider; _option = option.CurrentValue; _option.HttpClient = factory.CreateClient(); _option.Logger = logger; }
/// <summary> /// 配置 IP 地理位置查询配置项 注入方法调用此方法 /// </summary> /// <param name="op"></param> public static void ConfigIPLocator(IPLocatorOption op) { var name = RetrieveLocaleIPSvr(); if (!string.IsNullOrEmpty(name) && !name.Equals("None", StringComparison.OrdinalIgnoreCase)) { var url = RetrieveLocaleIPSvrUrl(name); op.LocatorName = name; op.Url = string.IsNullOrEmpty(url) ? string.Empty : $"{url}{op.IP}"; op.Period = RetrieveLocaleIPSvrCachePeriod() * 60 * 1000; } }
public void ConfigIPLocator_Ok() { var op = new IPLocatorOption() { IP = "182.148.123.196" }; var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "IP地理位置接口" && d.Define == 0); Assert.NotNull(dict); dict.Code = "JuheIPSvr"; DictHelper.Save(dict); DictHelper.ConfigIPLocator(op); Assert.NotNull(op.Url); }
/// <summary> /// 配置 IP 地理位置查询配置项 注入方法调用此方法 /// </summary> /// <param name="op"></param> public static void ConfigIPLocator(IPLocatorOption op) { var name = RetrieveLocaleIPSvr(); if (!string.IsNullOrEmpty(name) && !name.Equals("None", StringComparison.OrdinalIgnoreCase)) { var url = RetrieveLocaleIPSvrUrl(name); op.Locator = string.IsNullOrEmpty(url) ? null : DefaultIPLocatorProvider.CreateLocator(name); op.Url = string.IsNullOrEmpty(url) ? string.Empty : $"{url}{op.IP}"; if (int.TryParse(RetrieveLocaleIPSvrCachePeriod(), out var period) && period > 0) { op.Period = period * 60 * 1000; } } }
public void ConfigIPLocator_Ok() { var op = new IPLocatorOption() { IP = "182.148.123.196" }; var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "網站設置" && d.Name == "IP地理位置接口" && d.Define == 0); Assert.NotNull(dict); dict.Code = "JuheIPSvr"; // 演示模式下不能保存 Assert.True(DictHelper.Save(dict)); Assert.Equal("None", DictHelper.RetrieveLocaleIPSvr()); }
public void ConfigLocaleIP_Ok() { var op = new IPLocatorOption() { IP = "182.148.123.196" }; var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "IP地理位置接口" && d.Define == 0); Assert.NotNull(dict); dict.Code = "BaiDuIP138Svr"; // 演示模式下不能保存 Assert.True(DictHelper.Save(dict)); DictHelper.ConfigIPLocator(op); Assert.NotEqual("", op.Url); // 恢复设置 dict.Code = "None"; DictHelper.Save(dict); }
/// <summary> /// /// </summary> /// <param name="option"></param> /// <returns></returns> protected virtual async Task <string?> Locate <T>(IPLocatorOption option) where T : class { string?ret = null; try { if (!string.IsNullOrEmpty(Url) && !string.IsNullOrEmpty(option.IP) && option.HttpClient != null) { var url = string.Format(Url, option.IP); using var token = new CancellationTokenSource(option.RequestTimeout); var result = await option.HttpClient.GetFromJsonAsync <T>(url, token.Token); if (result != null) { ret = result.ToString(); } } } catch (Exception ex) { option.Logger?.LogError(ex, Url, option.IP); } return(ret); }
/// <summary> /// /// </summary> /// <param name="option"></param> /// <returns></returns> public virtual Task <string?> Locate(IPLocatorOption option) => Task.FromResult <string?>(null);
/// <summary> /// 定位方法 /// </summary> /// <param name="option"></param> /// <returns></returns> public override Task <string?> Locate(IPLocatorOption option) => Locate <BaiDuIPLocator>(option);