JuHeApiService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Dm;
  2. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  3. using System.Net.Http;
  4. using System.Net.Http.Json;
  5. using System.Text.Json;
  6. using Ubiety.Dns.Core;
  7. namespace OASystem.API.OAMethodLib.JuHeAPI
  8. {
  9. /// <summary>
  10. /// 聚合Api 服务
  11. /// </summary>
  12. public class JuHeApiService: IJuHeApiService
  13. {
  14. private readonly HttpClient _httpClient;
  15. private readonly string _appKey = "0f5429e9fbb8637c0ff3f14bbb42c732"; //配置您申请的appkey
  16. /// <summary>
  17. /// 初始化
  18. /// </summary>
  19. /// <param name="clientFactory"></param>
  20. public JuHeApiService(IHttpClientFactory clientFactory)
  21. {
  22. _httpClient = clientFactory.CreateClient("PublicJuHeApi"); ;
  23. }
  24. /// <summary>
  25. /// 汇率转换
  26. /// </summary>
  27. /// <returns></returns>
  28. /// <exception cref="NotImplementedException"></exception>
  29. public async Task<JuHeAPIResult> GetExchangeRateAsync()
  30. {
  31. var result = new JuHeAPIResult() { Resultcode = "10000",Reason="未知错误" };
  32. string url = string.Format("/finance/exchange/rmbquot");
  33. try
  34. {
  35. var exchangeReq = await _httpClient.PostAsync(url,
  36. new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
  37. {
  38. new KeyValuePair<string, string>("key",_appKey),//你申请的key
  39. new KeyValuePair<string, string>("type","0"), //两种格式(0或者1,默认为0)
  40. new KeyValuePair<string, string>("bank","3") //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,
  41. //4:交通银行,5:农业银行,默认为:0)
  42. }));
  43. if (exchangeReq.IsSuccessStatusCode)
  44. {
  45. var stringResponse = await exchangeReq.Content.ReadAsStringAsync();
  46. result = System.Text.Json.JsonSerializer.Deserialize<JuHeAPIResult>(stringResponse,
  47. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  48. }
  49. else
  50. {
  51. result.Reason = "汇率接口请求失败!";
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. result.Reason = ex.Message;
  57. }
  58. return result;
  59. }
  60. }
  61. }