JuheMobileAddressUtils.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Newtonsoft.Json;
  2. using NLog;
  3. using System;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using Wicture.DbRESTFul;
  7. namespace JiaZhiQuan.Common.JuheAPI.PhoneNumber
  8. {
  9. /// <summary>
  10. /// 手机号归属地查询
  11. /// </summary>
  12. /// <param name="db"></param>
  13. /// <param name="phone"></param>
  14. /// <returns>Item1: 是不是要保存到ES,Item2: 地址,Item3: 省,Item4: 市</returns>
  15. public class JuheMobileAddressUtils
  16. {
  17. const string JUHE_PHONE_KEY = "641cf7b85574a965e6eb3d4864e18da3";
  18. public static async Task<(bool, string, string, string)> GetMobileAddressAsync(string mobile, HttpClient client)
  19. {
  20. try
  21. {
  22. var res = await client.GetStringAsync($"http://apis.juhe.cn/mobile/get?phone={mobile}&key={JUHE_PHONE_KEY}");
  23. var obj = JsonConvert.DeserializeObject<JuheResponseModel<JuheMobileAddressResultDetails>>(res);
  24. if (obj.ResultCode == "200" && obj.ErrorCode == 0)
  25. {
  26. string company = string.IsNullOrEmpty(obj.Result.Company) ? "未知" : obj.Result.Company,
  27. province = string.IsNullOrEmpty(obj.Result.Province) ? "未知" : obj.Result.Province,
  28. city = string.IsNullOrEmpty(obj.Result.City) ? "未知" : obj.Result.City;
  29. string addr = $"【{company}】-【{province}-{city}】";
  30. return (true, addr, province, city);
  31. }
  32. // 查询无结果
  33. else if (obj.ResultCode == "200" && obj.ErrorCode == 201103)
  34. {
  35. return (true, "未知", "未知", "未知");
  36. }
  37. else
  38. {
  39. LoggerManager.Logger.Error($"调用聚合手机号归属地API失败,请求结果:" + res);
  40. return (false, "未知", "未知", "未知");
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. LoggerManager.Logger.Error(ex, $"调用聚合手机号归属地API报错:" + ex.Message);
  46. return (false, "未知", "未知", "未知");
  47. }
  48. }
  49. }
  50. public class JuheMobileAddressResultDetails
  51. {
  52. public string Province { get; set; }
  53. public string City { get; set; }
  54. public string AreaCode { get; set; }
  55. public string Zip { get; set; }
  56. public string Company { get; set; }
  57. public string Card { get; set; }
  58. }
  59. }