123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using Dm;
- using OASystem.Domain.ViewModels.JuHeExchangeRate;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text.Json;
- using Ubiety.Dns.Core;
- namespace OASystem.API.OAMethodLib.JuHeAPI
- {
- /// <summary>
- /// 聚合Api 服务
- /// </summary>
- public class JuHeApiService: IJuHeApiService
- {
- private readonly HttpClient _httpClient;
- private readonly string _appKey = "0f5429e9fbb8637c0ff3f14bbb42c732"; //配置您申请的appkey
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="clientFactory"></param>
- public JuHeApiService(IHttpClientFactory clientFactory)
- {
- _httpClient = clientFactory.CreateClient("PublicJuHeApi"); ;
- }
- /// <summary>
- /// 汇率转换
- /// </summary>
- /// <returns></returns>
- /// <exception cref="NotImplementedException"></exception>
- public async Task<JuHeAPIResult> GetExchangeRateAsync()
- {
- var result = new JuHeAPIResult() { Resultcode = "10000",Reason="未知错误" };
- string url = string.Format("/finance/exchange/rmbquot");
- try
- {
- var exchangeReq = await _httpClient.PostAsync(url,
- new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
- {
- new KeyValuePair<string, string>("key",_appKey),//你申请的key
- new KeyValuePair<string, string>("type","0"), //两种格式(0或者1,默认为0)
- new KeyValuePair<string, string>("bank","3") //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,
- //4:交通银行,5:农业银行,默认为:0)
- }));
- if (exchangeReq.IsSuccessStatusCode)
- {
- var stringResponse = await exchangeReq.Content.ReadAsStringAsync();
- result = System.Text.Json.JsonSerializer.Deserialize<JuHeAPIResult>(stringResponse,
- new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
- }
- else
- {
- result.Reason = "汇率接口请求失败!";
- }
- }
- catch (Exception ex)
- {
- result.Reason = ex.Message;
- }
-
- return result;
- }
- }
- }
|