Example #1
0
        public static string GetCity()
        {
            try
            {
                string myHost = Dns.GetHostName();
                string myIP   = Dns.GetHostByName(myHost).AddressList[0].ToString();



                string strIpLocation = string.Empty;

                string url = "https://ipapi.co/json/";

                var client  = new RestClient(url);
                var request = new RestRequest()
                {
                    Method = Method.GET
                };
                var responce = client.Execute(request);
                strIpLocation = responce.Content;

                CityResponce cr = JsonConvert.DeserializeObject <CityResponce>(strIpLocation);
                return(cr.City);
            }
            catch { return(""); }
        }
Example #2
0
        /// <summary>
        /// 同步京东地址库
        /// </summary>
        /// <param name="appkey"></param>
        public void SysJDRegions(string appkey)
        {
            string        errorMsg  = "同步失败";
            List <Region> jdregions = new List <Region>();

            try
            {
                var apiInstance  = new DefaultApi("https://way.jd.com");
                var provinceJson = apiInstance.GetProvince(appkey);
                if (!string.IsNullOrEmpty(provinceJson))
                {
                    var provinces = JsonConvert.DeserializeObject <ProvinceResponce>(provinceJson);
                    if (provinces != null && provinces.code.Equals("10000") && provinces.result.jingdong_area_province_get_responce.province_areas.Any())
                    {
                        //省份循环
                        foreach (var province in provinces.result.jingdong_area_province_get_responce.province_areas)
                        {
                            Region proRegion = new Region();
                            proRegion.Id        = int.Parse(province.id);
                            proRegion.Name      = province.name;
                            proRegion.ShortName = GetShortAddressName(province.name);
                            proRegion.Status    = Region.RegionStatus.Normal;
                            List <Region> CitySubs = new List <Region>();
                            var           cityJson = apiInstance.GetCity(province.id, appkey);
                            if (!string.IsNullOrEmpty(cityJson))
                            {
                                //市循环
                                var citys = new CityResponce();
                                try
                                {
                                    citys = JsonConvert.DeserializeObject <CityResponce>(cityJson);
                                    if (citys != null && citys.code.Equals("10000") && citys.result.jingdong_areas_city_get_responce.baseAreaServiceResponse.data.Any())
                                    {
                                        foreach (var city in citys.result.jingdong_areas_city_get_responce.baseAreaServiceResponse.data)
                                        {
                                            Region cityRegion = new Region();
                                            cityRegion.Id        = city.areaId;
                                            cityRegion.Name      = city.areaName;
                                            cityRegion.ShortName = GetShortAddressName(city.areaName);
                                            cityRegion.Status    = Region.RegionStatus.Normal;
                                            List <Region> countrySubs = new List <Region>();

                                            //区循环
                                            var countryJson = apiInstance.GetCountry(city.areaId.ToString(), appkey);
                                            if (!string.IsNullOrEmpty(countryJson))
                                            {
                                                var countrys = new CountryResponce();
                                                try
                                                {
                                                    countrys = JsonConvert.DeserializeObject <CountryResponce>(countryJson);
                                                    if (countrys != null && countrys.code.Equals("10000") && countrys.result.jingdong_areas_county_get_responce.baseAreaServiceResponse.data.Any())
                                                    {
                                                        foreach (var country in countrys.result.jingdong_areas_county_get_responce.baseAreaServiceResponse.data)
                                                        {
                                                            Region countryRegion = new Region();
                                                            countryRegion.Id        = country.areaId;
                                                            countryRegion.Name      = country.areaName;
                                                            countryRegion.ShortName = GetShortAddressName(country.areaName);
                                                            countryRegion.Status    = Region.RegionStatus.Normal;
                                                            List <Region> townSubs = new List <Region>();

                                                            //街道循环
                                                            var townJson = apiInstance.GetTown(country.areaId.ToString(), appkey);
                                                            if (!string.IsNullOrEmpty(townJson))
                                                            {
                                                                var towns = new TownResponce();
                                                                try
                                                                {
                                                                    towns = JsonConvert.DeserializeObject <TownResponce>(townJson);
                                                                }
                                                                catch (Exception)
                                                                {
                                                                    towns = null;
                                                                }
                                                                if (towns != null && towns.code.Equals("10000") && towns.result.jingdong_areas_town_get_responce.baseAreaServiceResponse.data.Any())
                                                                {
                                                                    foreach (var town in towns.result.jingdong_areas_town_get_responce.baseAreaServiceResponse.data)
                                                                    {
                                                                        Region townRegion = new Region();
                                                                        townRegion.Id        = town.areaId;
                                                                        townRegion.Name      = town.areaName;
                                                                        townRegion.ShortName = GetShortAddressName(town.areaName);
                                                                        townRegion.Status    = Region.RegionStatus.Normal;
                                                                        townRegion.Sub       = new List <Region>();

                                                                        townSubs.Add(townRegion);
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    errorMsg = towns.msg;
                                                                }
                                                            }

                                                            countryRegion.Sub = townSubs;
                                                            countrySubs.Add(countryRegion);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        errorMsg = countrys.msg;
                                                    }
                                                }
                                                catch (Exception)
                                                {
                                                    countrys = null;
                                                }
                                            }

                                            cityRegion.Sub = countrySubs;
                                            CitySubs.Add(cityRegion);
                                        }
                                    }
                                    else
                                    {
                                        errorMsg = citys.msg;
                                    }
                                }
                                catch (Exception)
                                {
                                    citys = null;
                                }
                            }
                            proRegion.Sub = CitySubs;
                            jdregions.Add(proRegion);
                        }
                    }
                    else
                    {
                        errorMsg = provinces.msg;
                    }

                    if (jdregions.Any())
                    {
                        var json = JsonConvert.SerializeObject(jdregions);
                        GetFilePath(out REGION_FILE_PATH, out REGION_BAK_PATH);
                        Core.HimallIO.CreateFile(REGION_FILE_PATH, json, FileCreateType.Create);
                        Cache.Remove(CacheKeyCollection.Region);
                    }
                    else
                    {
                        throw new HimallException("同步失败," + errorMsg);
                    }
                }
                else
                {
                    throw new HimallException("同步失败,请检查appkey");
                }
            }
            catch (Exception ex)
            {
                if (jdregions.Any())
                {
                    var json = JsonConvert.SerializeObject(jdregions);
                    GetFilePath(out REGION_FILE_PATH, out REGION_BAK_PATH);
                    Core.HimallIO.CreateFile(REGION_FILE_PATH, json, FileCreateType.Create);
                    Cache.Remove(CacheKeyCollection.Region);
                }
                throw new HimallException(ex.Message);
            }
        }