using Masuit.Tools.Core.Config;
using Masuit.Tools.Models;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Masuit.Tools.Core.Net
{
///
/// Web操作扩展
///
public static class WebExtension
{
#region 获取客户端IP地址信息
///
/// 根据IP地址获取详细地理信息
///
///
///
public static async Task>> GetIPAddressInfo(this string ip)
{
ip.MatchInetAddress(out var isIpAddress);
if (isIpAddress)
{
var address = await GetPhysicsAddressInfo(ip);
if (address.Status == 0)
{
string detail = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance ?? "0"}米";
List pois = address.AddressResult.Pois.Select(p => $"{p.AddressDetail}{p.Name} {p.Direction}{p.Distance ?? "0"}米").ToList();
return new Tuple>(detail, pois);
}
return new Tuple>("IP地址不正确", new List());
}
return new Tuple>($"{ip}不是一个合法的IP地址", new List());
}
///
/// 根据IP地址获取详细地理信息对象
///
///
///
public static async Task GetPhysicsAddressInfo(this string ip)
{
ip.MatchInetAddress(out var isIpAddress);
if (isIpAddress)
{
string ak = CoreConfig.Configuration["BaiduAK"];
if (string.IsNullOrEmpty(ak))
{
throw new Exception("未配置BaiduAK,请先在您的应用程序appsettings.json中下添加BaiduAK配置节(注意大小写)");
}
using (HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") })
{
client.DefaultRequestHeaders.Referrer = new Uri("http://lbsyun.baidu.com/jsdemo.htm");
var task = client.GetAsync($"/location/ip?ak={ak}&ip={ip}&coor=bd09ll").ContinueWith(async t =>
{
if (t.IsFaulted || t.IsCanceled)
{
return null;
}
var res = await t;
if (res.IsSuccessStatusCode)
{
var ipAddress = JsonConvert.DeserializeObject(await res.Content.ReadAsStringAsync());
if (ipAddress.Status == 0)
{
LatiLongitude point = ipAddress.AddressInfo.LatiLongitude;
string result = client.GetStringAsync($"/geocoder/v2/?location={point.Y},{point.X}&output=json&pois=1&radius=1000&latest_admin=1&coordtype=bd09ll&ak={ak}").Result;
PhysicsAddress address = JsonConvert.DeserializeObject(result);
if (address.Status == 0)
{
return address;
}
}
else
{
using (var client2 = new HttpClient { BaseAddress = new Uri("http://ip.taobao.com") })
{
return await await client2.GetAsync($"/service/getIpInfo.php?ip={ip}").ContinueWith(async tt =>
{
if (tt.IsFaulted || tt.IsCanceled)
{
return null;
}
var result = await tt;
if (result.IsSuccessStatusCode)
{
TaobaoIP taobaoIp = JsonConvert.DeserializeObject(await result.Content.ReadAsStringAsync());
if (taobaoIp.Code == 0)
{
return new PhysicsAddress()
{
Status = 0,
AddressResult = new AddressResult()
{
FormattedAddress = taobaoIp.IpData.Country + taobaoIp.IpData.Region + taobaoIp.IpData.City,
AddressComponent = new AddressComponent()
{
Province = taobaoIp.IpData.Region
},
Pois = new List()
}
};
}
}
return null;
});
}
}
}
return null;
});
return await await task;
}
}
return null;
}
///
/// 根据IP地址获取ISP
///
///
///
public static string GetISP(this string ip)
{
if (ip.MatchInetAddress())
{
using (var client = new HttpClient { BaseAddress = new Uri("http://ip.taobao.com") })
{
var task = client.GetAsync($"/service/getIpInfo.php?ip={ip}").ContinueWith(async t =>
{
if (t.IsFaulted)
{
return $"未能找到{ip}的ISP信息";
}
var result = await t;
if (result.IsSuccessStatusCode)
{
TaobaoIP taobaoIp = JsonConvert.DeserializeObject(await result.Content.ReadAsStringAsync());
if (taobaoIp.Code == 0)
{
return taobaoIp.IpData.Isp;
}
}
return $"未能找到{ip}的ISP信息";
});
return task.Result.Result;
}
}
return $"{ip}不是一个合法的IP";
}
#endregion
///
/// 写Session
///
///
/// 键
/// 值
public static void Set(this ISession session, string key, object value)
{
session.SetString(key, value.ToJsonString());
}
///
/// 获取Session
///
/// 对象
///
/// 键
/// 对象
public static T Get(this ISession session, string key)
{
string value = session.GetString(key);
if (string.IsNullOrEmpty(value))
{
return default;
}
return JsonConvert.DeserializeObject(value);
}
}
}