public virtual async Task <IPGecodeLocation> IPGeocodeAsync(string ipAddress)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "ip", ipAddress },
                { "ak", Options.AccessKey },
                { "coor", Options.CoordType }
            };
            var baiduMapUrl  = "http://api.map.baidu.com";
            var baiduMapPath = "/location/ip";

            if (Options.CaculateAKSN)
            {
                // TODO: 百度的文档不明不白,sn的算法在遇到特殊字符会验证失败,有待完善
                var sn = BaiduAKSNCaculater.CaculateAKSN(Options.AccessSecret, baiduMapPath, requestParamters);
                requestParamters.Add("sn", sn);
            }
            var requestUrl      = BuildRequestUrl(baiduMapUrl, baiduMapPath, requestParamters);
            var responseContent = await MakeRequestAndGetResultAsync(requestUrl);

            var baiduLocationResponse = JsonSerializer.Deserialize <BaiduIpGeocodeResponse>(responseContent);

            if (!baiduLocationResponse.IsSuccess())
            {
                var localizerFactory          = ServiceProvider.GetRequiredService <IStringLocalizerFactory>();
                var localizerErrorMessage     = baiduLocationResponse.GetErrorMessage(Options.VisableErrorToClient).Localize(localizerFactory);
                var localizerErrorDescription = baiduLocationResponse.GetErrorMessage(Options.VisableErrorToClient).Localize(localizerFactory);
                var localizer = ServiceProvider.GetRequiredService <IStringLocalizer <BaiduLocationResource> >();
                localizerErrorMessage = localizer["ResolveLocationFailed", localizerErrorMessage, localizerErrorDescription];
                if (Options.VisableErrorToClient)
                {
                    throw new UserFriendlyException(localizerErrorMessage);
                }
                throw new AbpException($"Resolution address failed:{localizerErrorMessage}!");
            }
            var location = new IPGecodeLocation
            {
                Province = baiduLocationResponse.Content.AddressDetail?.Province,
                City     = baiduLocationResponse.Content.AddressDetail?.City,
                AdCode   = baiduLocationResponse.Content.AddressDetail?.CityCode.ToString(),
            };
            var point = baiduLocationResponse.Content.Point.ToPoint();

            location.Location.Latitude  = point.Y;
            location.Location.Longitude = point.X;

            location.AddAdditional("Address", baiduLocationResponse.Address);
            location.AddAdditional("Content", baiduLocationResponse.Content);

            return(location);
        }
コード例 #2
0
        public virtual async Task <IPGecodeLocation> IPGeocodeAsync(string ipAddress)
        {
            var requestParamters = new Dictionary <string, string>
            {
                { "callback", Options.Callback },
                { "ip", ipAddress },
                { "key", Options.AccessKey },
                { "output", Options.Output }
            };
            var tencentMapUrl  = "https://apis.map.qq.com";
            var tencentMapPath = "/ws/location/v1/ip";

            if (!Options.SecretKey.IsNullOrWhiteSpace())
            {
                var sig = TencentSecretKeyCaculater.CalcSecretKey(tencentMapPath, Options.SecretKey, requestParamters);
                requestParamters.Add("sig", sig);
            }
            var tencentLocationResponse = await GetTencentMapResponseAsync <TencentIPGeocodeResponse>(tencentMapUrl, tencentMapPath, requestParamters);

            var location = new IPGecodeLocation
            {
                IpAddress = tencentLocationResponse.Result.IpAddress,
                AdCode    = tencentLocationResponse.Result.AddressInfo.AdCode,
                City      = tencentLocationResponse.Result.AddressInfo.City,
                Country   = tencentLocationResponse.Result.AddressInfo.Nation,
                District  = tencentLocationResponse.Result.AddressInfo.District,
                Location  = new Location
                {
                    Latitude  = tencentLocationResponse.Result.Location.Lat,
                    Longitude = tencentLocationResponse.Result.Location.Lng
                },
                Province = tencentLocationResponse.Result.AddressInfo.Province
            };

            location.AddAdditional("TencentLocation", tencentLocationResponse.Result);

            return(location);
        }