/// <summary> /// 获取IP对应区域 /// </summary> /// <param name="ip">ip</param> /// <returns></returns> public static ha_regions GetRegionByIP(string ip) { ha_regions regionInfo = null; HttpCookie cookie = HttpContext.Current.Request.Cookies["loc"]; if (cookie != null) { if (cookie["ip"] == ip) { regionInfo = GetRegionById(TypeHelper.StringToInt(cookie["regionid"])); } else { cookie.Values["ip"] = ip; regionInfo = IPSearch.SearchRegion(ip); if (regionInfo != null) { cookie.Values["regionid"] = regionInfo.regionid.ToString(); } else { cookie.Values["regionid"] = "-1"; } cookie.Expires = DateTime.Now.AddYears(1); HttpContext.Current.Response.AppendCookie(cookie); } } else { cookie = new HttpCookie("loc"); cookie.Values["ip"] = ip; regionInfo = IPSearch.SearchRegion(ip); if (regionInfo != null) { cookie.Values["regionid"] = regionInfo.regionid.ToString(); } else { cookie.Values["regionid"] = "-1"; } cookie.Expires = DateTime.Now.AddYears(1); HttpContext.Current.Response.AppendCookie(cookie); } if (regionInfo != null) { return(regionInfo); } else { return new ha_regions() { regionid = -1, name = "未知区域" } }; } }
/// <summary> /// 获得区域 /// </summary> /// <param name="name">名称</param> /// <param name="layer">级别</param> /// <returns></returns> public static ha_regions GetRegionByNameAndLayer(string name, int layer) { ha_regions regionInfo = Halfox.Core.HaCache.Get(string.Format(CacheKeys.MALL_REGION_INFOBYNAMEANDLAYER, name, layer)) as ha_regions; if (regionInfo == null) { regionInfo = db.ha_regions.Where(p => p.name == name && p.layer == layer).FirstOrDefault(); Halfox.Core.HaCache.Insert(string.Format(CacheKeys.MALL_REGION_INFOBYNAMEANDLAYER, name, layer), regionInfo); } return(regionInfo); }
/// <summary> /// 获得区域 /// </summary> /// <param name="regionId">区域id</param> /// <returns></returns> public static ha_regions GetRegionById(int regionId) { if (regionId > 0) { ha_regions regionInfo = Halfox.Core.HaCache.Get(CacheKeys.MALL_REGION_INFOBYID + regionId) as ha_regions; if (regionInfo == null) { regionInfo = db.ha_regions.Find(regionId); Halfox.Core.HaCache.Insert(CacheKeys.MALL_REGION_INFOBYID + regionId, regionInfo); } return(regionInfo); } return(null); }
/// <summary> /// 获得ip对应区域 /// </summary> /// <param name="ip">ip</param> /// <returns></returns> public static ha_regions SearchRegion(string ip) { IPLocation ipLocation = SearchLocation(ip); if (ipLocation == null || ipLocation.Country.Length == 0) { return(null); } string country = ipLocation.Country; int startIndex = country.IndexOf("省"); int endIndex = country.IndexOf("市"); if (startIndex > 0 && endIndex > 0 && endIndex > startIndex) { ha_regions regionInfo = Regions.GetRegionByNameAndLayer(country.Substring(startIndex + 1, endIndex - startIndex), 2); if (regionInfo != null) { return(regionInfo); } } if (country.StartsWith("北京")) { return(Regions.GetRegionById(35)); } else if (country.StartsWith("天津")) { return(Regions.GetRegionById(36)); } else if (country.StartsWith("上海")) { return(Regions.GetRegionById(107)); } else if (country.StartsWith("重庆")) { return(Regions.GetRegionById(269)); } else if (country.StartsWith("四川省")) { if (country.Length == 3) { return(Regions.GetRegionById(23)); } else if (country.StartsWith("四川省阿坝州")) { return(Regions.GetRegionById(288)); } else if (country.StartsWith("四川省甘孜州")) { return(Regions.GetRegionById(289)); } else if (country.StartsWith("四川省凉山州")) { return(Regions.GetRegionById(290)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("湖南省")) { if (country.Length == 3) { return(Regions.GetRegionById(18)); } else if (country.StartsWith("湖南省湘西州")) { return(Regions.GetRegionById(230)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("湖北省")) { if (country.Length == 3) { return(Regions.GetRegionById(17)); } else if (country.StartsWith("湖北省神农架林区")) { return(Regions.GetRegionById(1997)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("广西")) { if (country.Length == 2) { return(Regions.GetRegionById(20)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(2, country.IndexOf('市') - 2), 2)); } } else if (country.StartsWith("云南省")) { if (country.Length == 3) { return(Regions.GetRegionById(25)); } else if (country.StartsWith("云南省楚雄州")) { return(Regions.GetRegionById(308)); } else if (country.StartsWith("云南省红河州")) { return(Regions.GetRegionById(309)); } else if (country.StartsWith("云南省文山州")) { return(Regions.GetRegionById(310)); } else if (country.StartsWith("云南省西双版纳州")) { return(Regions.GetRegionById(311)); } else if (country.StartsWith("云南省大理州")) { return(Regions.GetRegionById(312)); } else if (country.StartsWith("云南省德宏州")) { return(Regions.GetRegionById(313)); } else if (country.StartsWith("云南省怒江州")) { return(Regions.GetRegionById(314)); } else if (country.StartsWith("云南省迪庆州")) { return(Regions.GetRegionById(315)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("黑龙江省")) { if (country.Length == 4) { return(Regions.GetRegionById(8)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(4, country.IndexOf('市') - 4), 2)); } else if (country.Contains("地区")) { return(Regions.GetRegionByNameAndLayer(country.Substring(4, country.IndexOf("地区") - 3), 2)); } } else if (country.StartsWith("吉林省")) { if (country.Length == 3) { return(Regions.GetRegionById(7)); } else if (country.StartsWith("吉林省延边州")) { return(Regions.GetRegionById(93)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("内蒙古")) { if (country.Length == 3) { return(Regions.GetRegionById(5)); } else if (country.Contains("盟")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('盟') - 3), 2)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("贵州省")) { if (country.Length == 3) { return(Regions.GetRegionById(24)); } else if (country.StartsWith("贵州省黔西南")) { return(Regions.GetRegionById(296)); } else if (country.StartsWith("贵州省黔东南")) { return(Regions.GetRegionById(298)); } else if (country.StartsWith("贵州省黔南州")) { return(Regions.GetRegionById(299)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("甘肃省")) { if (country.Length == 3) { return(Regions.GetRegionById(28)); } else if (country.StartsWith("甘肃省临夏州")) { return(Regions.GetRegionById(345)); } else if (country.StartsWith("甘肃省甘南")) { return(Regions.GetRegionById(346)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("新疆")) { if (country.Length == 2) { return(Regions.GetRegionById(31)); } else if (country.StartsWith("新疆阿勒泰")) { return(Regions.GetRegionById(373)); } else if (country.StartsWith("新疆昌吉州")) { return(Regions.GetRegionById(364)); } else if (country.StartsWith("新疆博尔塔拉州")) { return(Regions.GetRegionById(365)); } else if (country.StartsWith("新疆巴音郭楞州")) { return(Regions.GetRegionById(366)); } else if (country.StartsWith("新疆克孜勒苏柯尔州")) { return(Regions.GetRegionById(368)); } else if (country.StartsWith("新疆伊犁州")) { return(Regions.GetRegionById(371)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(2, country.IndexOf('市') - 2), 2)); } else if (country.Contains("地区")) { return(Regions.GetRegionByNameAndLayer(country.Substring(2, country.IndexOf("地区") - 1), 2)); } } else if (country.StartsWith("青海省")) { if (country.Length == 3) { return(Regions.GetRegionById(29)); } else if (country.StartsWith("青海省海北州")) { return(Regions.GetRegionById(349)); } else if (country.StartsWith("青海省黄南州")) { return(Regions.GetRegionById(350)); } else if (country.StartsWith("青海省海南州")) { return(Regions.GetRegionById(351)); } else if (country.StartsWith("青海省果洛州")) { return(Regions.GetRegionById(352)); } else if (country.StartsWith("青海省玉树州")) { return(Regions.GetRegionById(353)); } else if (country.StartsWith("青海省海西州")) { return(Regions.GetRegionById(354)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(3, country.IndexOf('市') - 3), 2)); } } else if (country.StartsWith("西藏")) { if (country.Length == 2) { return(Regions.GetRegionById(26)); } else if (country.Contains("市")) { return(Regions.GetRegionByNameAndLayer(country.Substring(2, country.IndexOf('市') - 2), 2)); } else if (country.Contains("地区")) { return(Regions.GetRegionByNameAndLayer(country.Substring(2, country.IndexOf("地区") - 1), 2)); } } else if (country.StartsWith("香港")) { return(Regions.GetRegionById(3241)); } else if (country.StartsWith("澳门")) { return(Regions.GetRegionById(3242)); } else if (country.StartsWith("台湾")) { return(Regions.GetRegionById(3240)); } return(null); }