/// <summary> /// 手机归属地查询 /// </summary> /// <param name="searcher"><see cref="ISearcher"/></param> /// <param name="mobilePhone">中国手机号码</param> /// <param name="info">手机归属地信息</param> /// <returns>是否匹配成功</returns> public static bool TryGet(this ISearcher searcher, string mobilePhone, out MobilePhone info) { info = null; if (string.IsNullOrWhiteSpace(mobilePhone)) { return(false); } if (mobilePhone.Length < 7) { return(false); } if (IsMobilePhone(mobilePhone)) { if (mobilePhone.Length > 7) { mobilePhone = mobilePhone.Substring(0, 7); } if (int.TryParse(mobilePhone, out int number)) { var result = searcher.Search(number); if (result.Success) { var adCode = ChinaAdCode.Get(result.AdCode); info = new MobilePhone { Phone = number, AdCode = result.AdCode, Isp = result.Isp, CityCode = adCode.CityCode, ZipCode = adCode.ZipCode, Area = adCode.Province + adCode.City }; return(true); } } } return(false); }
private static void RrepareResource() { var lstAdCode = new List <ChinaAdCode>(); var t = typeof(ChinaAdCode); var name = $"{t.Namespace}.{t.Name}.txt"; using (var stream = t.Assembly.GetManifestResourceStream(name)) { using (StreamReader sr = new StreamReader(stream)) { string line; while (!string.IsNullOrEmpty((line = sr.ReadLine()))) { var values = line.Split('|'); ChinaAdCode info = new ChinaAdCode { Id = int.Parse(values[0]), ParentId = int.Parse(values[1]), Level = int.Parse(values[2]), Province = values[3], City = values[4], District = values[5], Lng = double.Parse(values[6]), Lat = double.Parse(values[7]), CityCode = values[8], ZipCode = values[9] }; lstAdCode.Add(info); } } } //建立查询索引 Dict_AreaCode = new Dictionary <int, ChinaAdCode>(lstAdCode.Count); Dict_CityCode = new Dictionary <string, IList <ChinaAdCode> >(); Dict_ZipCode = new Dictionary <string, IList <ChinaAdCode> >(); Dict_Province = new Dictionary <string, IList <ChinaAdCode> >(); foreach (var entity in lstAdCode) { Dict_AreaCode[entity.Id] = entity; if (!string.IsNullOrWhiteSpace(entity.Province)) { if (Dict_Province.TryGetValue(entity.Province, out var list)) { list.Add(entity); } else { Dict_Province.Add(entity.Province, new[] { entity }.ToList()); } } if (!string.IsNullOrWhiteSpace(entity.CityCode)) { if (Dict_CityCode.TryGetValue(entity.CityCode, out var list)) { list.Add(entity); } else { Dict_CityCode.Add(entity.CityCode, new[] { entity }.ToList()); } } if (!string.IsNullOrWhiteSpace(entity.ZipCode)) { if (Dict_ZipCode.TryGetValue(entity.ZipCode, out var list)) { list.Add(entity); } else { Dict_ZipCode.Add(entity.ZipCode, new[] { entity }.ToList()); } } } }