|
@@ -68,6 +68,9 @@ using Azure.Messaging.ServiceBus.Administration;
|
|
|
using static TEAMModelOS.SDK.CoreAPIHttpService;
|
|
|
using System.Xml;
|
|
|
using System.Drawing.Printing;
|
|
|
+using TEAMModelOS.SDK.DI.IPIP;
|
|
|
+using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
|
|
|
+using static TEAMModelOS.SDK.Extension.GeoRegion;
|
|
|
|
|
|
namespace TEAMModelBI.Controllers.BITest
|
|
|
{
|
|
@@ -87,7 +90,8 @@ namespace TEAMModelBI.Controllers.BITest
|
|
|
private readonly CoreAPIHttpService _coreAPIHttpService;
|
|
|
private readonly IHttpClientFactory _httpClient;
|
|
|
private IPSearcher _ipSearcher;
|
|
|
- public TestController(IPSearcher ipSearcher, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient)
|
|
|
+ private readonly City _city;
|
|
|
+ public TestController(IPSearcher ipSearcher, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, City city)
|
|
|
{
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_azureStorage = azureStorage;
|
|
@@ -98,6 +102,7 @@ namespace TEAMModelBI.Controllers.BITest
|
|
|
_configuration = configuration;
|
|
|
_coreAPIHttpService = coreAPIHttpService;
|
|
|
_httpClient = httpClient; _ipSearcher = ipSearcher;
|
|
|
+ _city = city;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -2045,6 +2050,132 @@ namespace TEAMModelBI.Controllers.BITest
|
|
|
return Ok(new { state = 200, result });
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// IP轉換地理位置
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("getGeoFromIpTest")]
|
|
|
+ public async Task<IActionResult> getGeoFromIpTest(JsonElement jsonElement)
|
|
|
+ {
|
|
|
+ string ip = (jsonElement.TryGetProperty("ip", out JsonElement _ip)) ? _ip.ToString() : string.Empty;
|
|
|
+ string lang = (jsonElement.TryGetProperty("lang", out JsonElement _lang)) ? _lang.ToString() : "zh-tw";
|
|
|
+ var ipInfo = _city.find(ip, "CN");
|
|
|
+
|
|
|
+ regiondata regionData = GetRegionData(); //取得國省市區地理資訊架構
|
|
|
+ List<string> comeRemoveStr = GeoRegion.comeRemoveStr;
|
|
|
+ ///國字典製作
|
|
|
+ var geoCountryIdNameDic = new Dictionary<string, string>();
|
|
|
+ foreach (KeyValuePair<string, regionbase> item in regionData.country)
|
|
|
+ {
|
|
|
+ geoCountryIdNameDic.Add(item.Value.code, item.Value.name);
|
|
|
+ }
|
|
|
+ ///省字典製作
|
|
|
+ var geoProvinceIdNameDic = new Dictionary<string, string>();
|
|
|
+ foreach (KeyValuePair<string, regionbase> item in regionData.province["CN"])
|
|
|
+ {
|
|
|
+ geoProvinceIdNameDic.Add(item.Value.code, item.Value.name);
|
|
|
+ }
|
|
|
+ ///市字典製作
|
|
|
+ var geoCityIdNameDic = new Dictionary<string, Dictionary<string, string>>();
|
|
|
+ geoCityIdNameDic.Add("TW", new Dictionary<string, string>());
|
|
|
+ geoCityIdNameDic.Add("CN", new Dictionary<string, string>());
|
|
|
+ List<string> municipalityId = GeoRegion.municipalityId; //大陸直轄市的省ID
|
|
|
+ foreach (var itemCountry in regionData.city)
|
|
|
+ {
|
|
|
+ if(itemCountry.Key.Equals("TW"))
|
|
|
+ {
|
|
|
+ foreach (var itemCity in regionData.city["TW"]["tw"])
|
|
|
+ {
|
|
|
+ geoCityIdNameDic["TW"].Add(itemCity.Value.code, itemCity.Value.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(itemCountry.Key.Equals("CN"))
|
|
|
+ {
|
|
|
+ foreach (var itemProvince in regionData.city["CN"])
|
|
|
+ {
|
|
|
+ string provinceIdTmp = itemProvince.Key;
|
|
|
+ foreach (var itemCity in regionData.city["CN"][provinceIdTmp])
|
|
|
+ {
|
|
|
+ geoCityIdNameDic["CN"].Add(itemCity.Value.code, itemCity.Value.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<string> ipToGeoList = ipInfo.ToList();
|
|
|
+ IotTeachingData.Geo geo = new IotTeachingData.Geo();
|
|
|
+ if (ipToGeoList[0].Equals("中国") && ipToGeoList[1].Equals("台湾"))
|
|
|
+ {
|
|
|
+ geo.countryId = "TW";
|
|
|
+ geo.countryName = geoCountryIdNameDic["TW"];
|
|
|
+ comeRemoveStr.ForEach(c => { geo.countryName = geo.countryName.Replace(c, ""); });
|
|
|
+ string cityNameTmp = ipToGeoList[2];
|
|
|
+ if (!string.IsNullOrWhiteSpace(cityNameTmp))
|
|
|
+ {
|
|
|
+ cityNameTmp = ChineseConverter.Convert(cityNameTmp, ChineseConversionDirection.SimplifiedToTraditional);
|
|
|
+ comeRemoveStr.ForEach(c => { cityNameTmp = cityNameTmp.Replace(c, ""); });
|
|
|
+ var cityDicRow = geoCityIdNameDic["TW"].FirstOrDefault(c => c.Value.Contains(cityNameTmp));
|
|
|
+ if (!cityDicRow.Equals(default(KeyValuePair<string, string>)))
|
|
|
+ {
|
|
|
+ geo.cityId = cityDicRow.Key;
|
|
|
+ geo.cityName = cityDicRow.Value;
|
|
|
+ comeRemoveStr.ForEach(c => { geo.cityName = geo.cityName.Replace(c, ""); });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(ipToGeoList[0].Equals("中国"))
|
|
|
+ {
|
|
|
+ geo.countryId = "CN";
|
|
|
+ geo.countryName = geoCountryIdNameDic["CN"];
|
|
|
+ string provinceNameTmp = ipToGeoList[1];
|
|
|
+ comeRemoveStr.ForEach(p => { provinceNameTmp = provinceNameTmp.Replace(p, ""); });
|
|
|
+ var provinceDicRow = geoProvinceIdNameDic.FirstOrDefault(p => p.Value.Contains(provinceNameTmp));
|
|
|
+ if (!provinceDicRow.Equals(default(KeyValuePair<string, string>)))
|
|
|
+ {
|
|
|
+ geo.provinceId = provinceDicRow.Key;
|
|
|
+ geo.provinceName = provinceDicRow.Value;
|
|
|
+ comeRemoveStr.ForEach(c => { geo.provinceName = geo.provinceName.Replace(c, ""); });
|
|
|
+ }
|
|
|
+ string cityNameTmp = ipToGeoList[2];
|
|
|
+ if(!string.IsNullOrWhiteSpace(cityNameTmp))
|
|
|
+ {
|
|
|
+ comeRemoveStr.ForEach(c => { cityNameTmp = cityNameTmp.Replace(c, ""); });
|
|
|
+ if (!string.IsNullOrWhiteSpace(geo.provinceId) && municipalityId.Contains(geo.provinceId)) ///直轄市處理
|
|
|
+ {
|
|
|
+ var cityDicFromRegion = regionData.city["CN"][geo.provinceId].First();
|
|
|
+ string cityId = cityDicFromRegion.Key;
|
|
|
+ geo.cityId = cityId;
|
|
|
+ geo.cityName = geoCityIdNameDic["CN"][cityId];
|
|
|
+ }
|
|
|
+ else ///非直轄市
|
|
|
+ {
|
|
|
+ var cityDicRow = geoCityIdNameDic["CN"].FirstOrDefault(c => c.Value.Contains(cityNameTmp));
|
|
|
+ if (!cityDicRow.Equals(default(KeyValuePair<string, string>)))
|
|
|
+ {
|
|
|
+ geo.cityId = cityDicRow.Key;
|
|
|
+ geo.cityName = cityDicRow.Value;
|
|
|
+ comeRemoveStr.ForEach(c => { geo.cityName = geo.cityName.Replace(c, ""); });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ string countryNameTmp = ipToGeoList[0];
|
|
|
+ countryNameTmp = ChineseConverter.Convert(countryNameTmp, ChineseConversionDirection.SimplifiedToTraditional);
|
|
|
+ var countryDicRow = geoCountryIdNameDic.FirstOrDefault(c => c.Value.Contains(countryNameTmp));
|
|
|
+ if (!countryDicRow.Equals(default(KeyValuePair<string, string>)))
|
|
|
+ {
|
|
|
+ geo.countryId = countryDicRow.Key;
|
|
|
+ geo.countryName = countryDicRow.Value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Ok(new { state = 200, geo, geoFromIp = ipToGeoList });
|
|
|
+ }
|
|
|
+
|
|
|
public class linqTest
|
|
|
{
|
|
|
public string id { get; set; }
|