IUSPUAddressService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using RestSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XYY.Model.Standard.OrderAddressCheck;
  8. namespace XYY.Service.Standard.AddressCheck
  9. {
  10. //如果好用,请收藏地址,帮忙分享。
  11. public class ZipCode
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public string ID { get; set; }
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public string Zip5 { get; set; }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public string City { get; set; }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public string State { get; set; }
  29. }
  30. public class CityStateLookupResponse
  31. {
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public ZipCode ZipCode { get; set; }
  36. }
  37. public class LockupRoot
  38. {
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. public CityStateLookupResponse CityStateLookupResponse { get; set; }
  43. }
  44. public class UspsCityDto
  45. {
  46. public string resultStatus { get; set; }
  47. public string zip5 { get; set; }
  48. public string defaultCity { get; set; }
  49. public string defaultState { get; set; }
  50. public string defaultRecordType { get; set; }
  51. public List<string> citiesList { get; set; }
  52. public List<string> nonAcceptList { get; set; }
  53. }
  54. public class USPUAddressService : IAdderssService
  55. {
  56. public int ChannelId => throw new NotImplementedException();
  57. public string CountryCode => "US";
  58. public async Task<RecommendedAddress> GetRecommendedAddresses(OrderAddress address)
  59. {
  60. var RecommendedAddress = new RecommendedAddress
  61. {
  62. CityRecommendedAddress = new List<RecommendedAddressItem>(),
  63. ZipCodeRecommendedAddress = new List<RecommendedAddressItem>()
  64. };
  65. if (string.IsNullOrEmpty(address.ZipCode))
  66. return null;
  67. string url = $"http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRequest USERID=\"151XINGY7933\"><ZipCode ID= \"0\"><Zip5>{address.ZipCode}</Zip5></ZipCode></CityStateLookupRequest>";
  68. RestClient client = new RestClient(url);
  69. RestRequest req = new RestRequest(Method.POST);
  70. req.AddParameter("zip", address.ZipCode);
  71. client.Execute(req);
  72. var resp = await client.ExecuteAsync(req);
  73. if (resp.StatusCode == System.Net.HttpStatusCode.OK)
  74. {
  75. string content = resp.Content;
  76. System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(CityStateLookupResponse));
  77. var result = (CityStateLookupResponse)xmlSerializer.Deserialize(new System.IO.StringReader(content));
  78. RecommendedAddress.ZipCodeRecommendedAddress = new List<RecommendedAddressItem>
  79. {
  80. new RecommendedAddressItem
  81. {
  82. ZipCode = address.ZipCode,
  83. City = result.ZipCode.City,
  84. State = result.ZipCode.State
  85. }
  86. };
  87. return RecommendedAddress;
  88. }
  89. else
  90. return RecommendedAddress;
  91. }
  92. private RecommendedAddressItem ConvertAddress(FRSLAreas area)
  93. {
  94. return new RecommendedAddressItem { City = area.AreaName, ZipCode = area.ZipCode };
  95. }
  96. public Task<AddressGroup> Verify(OrderAddress address)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. }
  101. }