|
|
|
@ -19,6 +19,7 @@ namespace EntrustSettle.Services
|
|
|
|
|
{
|
|
|
|
|
public class HYDService : IHYDService
|
|
|
|
|
{
|
|
|
|
|
private const string HYD_LOGIN_ERROR_MSG = "重新登录";
|
|
|
|
|
private readonly ICaching caching;
|
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly IRestClient client;
|
|
|
|
@ -50,31 +51,29 @@ namespace EntrustSettle.Services
|
|
|
|
|
request.AddHeaders(headers);
|
|
|
|
|
request.AddJsonBody(submitDto);
|
|
|
|
|
|
|
|
|
|
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
var resp = await client.PostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
logger.LogRestSharp("海运达下单接口", client, request, resp);
|
|
|
|
|
|
|
|
|
|
if (resp.IsSuccessful)
|
|
|
|
|
var jobj = JObject.Parse(resp.Content);
|
|
|
|
|
var code = jobj.GetValue("code").ObjToInt();
|
|
|
|
|
if (code == 200)
|
|
|
|
|
{
|
|
|
|
|
var jobj = JObject.Parse(resp.Content);
|
|
|
|
|
var code = jobj.GetValue("code").ObjToInt();
|
|
|
|
|
if (code == 200)
|
|
|
|
|
{
|
|
|
|
|
var bsno = jobj.GetValue("data").ObjToLong();
|
|
|
|
|
return bsno;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
var bsno = jobj.GetValue("data").ObjToLong();
|
|
|
|
|
return bsno;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var msg = jobj.GetValue("msg").ToString();
|
|
|
|
|
if ((code > 400 && code < 500) || msg.Contains(HYD_LOGIN_ERROR_MSG))
|
|
|
|
|
{
|
|
|
|
|
if (code > 400 && code < 500)
|
|
|
|
|
{
|
|
|
|
|
await caching.DelCacheKeyAsync(CacheConst.HydAccessToken);
|
|
|
|
|
}
|
|
|
|
|
await caching.RemoveAsync(CacheConst.HydAccessToken);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception($"code:{code}");
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("调用外部下单接口失败");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("下单失败,原因:" + ex.Message);
|
|
|
|
|
throw new Exception("下单失败,原因:调用外部下单接口失败(" + ex.Message + ")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -137,59 +136,57 @@ namespace EntrustSettle.Services
|
|
|
|
|
request.AddHeaders(headers);
|
|
|
|
|
request.AddJsonBody(item);
|
|
|
|
|
|
|
|
|
|
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
var resp = await client.PostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
logger.LogRestSharp("海运达反馈接口", client, request, resp);
|
|
|
|
|
|
|
|
|
|
if (resp.IsSuccessful)
|
|
|
|
|
//var jobj = JObject.Parse("{\"msg\":\"token格式/信息有误,或账号已在其他系统登录,请重新登录\",\"code\":500}");
|
|
|
|
|
var jobj = JObject.Parse(resp.Content);
|
|
|
|
|
var code = jobj.GetValue("code").ObjToInt();
|
|
|
|
|
if (code == 200)
|
|
|
|
|
{
|
|
|
|
|
var jobj = JObject.Parse(resp.Content);
|
|
|
|
|
var code = jobj.GetValue("code").ObjToInt();
|
|
|
|
|
if (code == 200)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var msg = jobj.GetValue("msg").ToString();
|
|
|
|
|
if ((code > 400 && code < 500) || msg.Contains(HYD_LOGIN_ERROR_MSG))
|
|
|
|
|
{
|
|
|
|
|
if (code > 400 && code < 500)
|
|
|
|
|
{
|
|
|
|
|
await caching.DelCacheKeyAsync(CacheConst.HydAccessToken);
|
|
|
|
|
}
|
|
|
|
|
var msg = jobj.GetValue("msg").ToString();
|
|
|
|
|
await caching.RemoveAsync(CacheConst.HydAccessToken);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception($"code:{code}");
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("调用外部反馈接口失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("反馈失败,原因:" + ex.Message);
|
|
|
|
|
throw new Exception("反馈失败,原因:调用外部反馈接口失败(" + ex.Message + ")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> GetToken()
|
|
|
|
|
{
|
|
|
|
|
var token = await caching.GetAsync<string>(CacheConst.HydAccessToken);
|
|
|
|
|
if (!string.IsNullOrEmpty(token))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
var token = await caching.GetAsync<string>(CacheConst.HydAccessToken);
|
|
|
|
|
if (!string.IsNullOrEmpty(token))
|
|
|
|
|
{
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = AppSettings.app("Apis", "Hyd", "LoginUrl");
|
|
|
|
|
var request = new RestRequest(url);
|
|
|
|
|
var body = new
|
|
|
|
|
{
|
|
|
|
|
username = AppSettings.app("Apis", "Hyd", "username"),
|
|
|
|
|
password = AppSettings.app("Apis", "Hyd", "password")
|
|
|
|
|
};
|
|
|
|
|
request.AddJsonBody(body);
|
|
|
|
|
var url = AppSettings.app("Apis", "Hyd", "LoginUrl");
|
|
|
|
|
var request = new RestRequest(url);
|
|
|
|
|
var body = new
|
|
|
|
|
{
|
|
|
|
|
username = AppSettings.app("Apis", "Hyd", "username"),
|
|
|
|
|
password = AppSettings.app("Apis", "Hyd", "password")
|
|
|
|
|
};
|
|
|
|
|
request.AddJsonBody(body);
|
|
|
|
|
|
|
|
|
|
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
logger.LogRestSharp("海运达登录接口", client, request, resp);
|
|
|
|
|
var resp = await client.PostAsync(request); // 以Execute开头的方法不会引发异常
|
|
|
|
|
logger.LogRestSharp("海运达登录接口", client, request, resp);
|
|
|
|
|
|
|
|
|
|
if (resp.IsSuccessful)
|
|
|
|
|
{
|
|
|
|
|
var jobj = JObject.Parse(resp.Content);
|
|
|
|
|
var code = jobj.GetValue("code");
|
|
|
|
|
if (code.ObjToInt() == 200)
|
|
|
|
@ -198,8 +195,15 @@ namespace EntrustSettle.Services
|
|
|
|
|
await caching.SetAsync(CacheConst.HydAccessToken, token, TimeSpan.FromHours(3));
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"code:{code}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"调用外部登录接口失败({ex.InnerException?.Message})");
|
|
|
|
|
}
|
|
|
|
|
throw new Exception($"调用外部登录接口失败({resp.ErrorException?.InnerException?.Message})");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|