接口调用调整

master
zhangxiaofeng 6 months ago
parent 745a78b8d5
commit 5c9af1330a

@ -1,4 +1,5 @@
using EntrustSettle.Common;
using EntrustSettle.Common.Caches;
using EntrustSettle.Controllers;
using EntrustSettle.IServices;
using EntrustSettle.Model;
@ -280,5 +281,21 @@ namespace EntrustSettle.Api.Controllers
{
throw new Exception("测试异常");
}
//[HttpGet]
//public async Task<MessageModel> TestSetRedis(string key, string value)
//{
// var caching = App.GetService<ICaching>();
// await caching.SetAsync(key, value, TimeSpan.FromHours(3));
// return SuccessMsg();
//}
//[HttpGet]
//public async Task<MessageModel<string>> TestGetRedis(string key)
//{
// var caching = App.GetService<ICaching>();
// var value = await caching.GetStringAsync(key);
// return Success(value);
//}
}
}

@ -566,6 +566,10 @@ namespace EntrustSettle.Api.Controllers
{
throw new Exception("该订单已完结,无法进行反馈");
}
if (string.IsNullOrWhiteSpace(bindDto.Remark) && (bindDto.AnnexIdList?.Any() ?? false) == false)
{
throw new Exception("无可反馈内容");
}
List<HydFeedbackDto> hydFeedbackDtoList = new List<HydFeedbackDto>();
@ -609,7 +613,6 @@ namespace EntrustSettle.Api.Controllers
}
unitOfWorkManage.BeginTran();
// 如果操作类型为账单或发票,则先删除原有的绑定信息
if (bindDto.OperType == FileTypeEnum.)
{
if (bindDto.AnnexIdList != null && bindDto.AnnexIdList.Count > 0)
@ -621,8 +624,28 @@ namespace EntrustSettle.Api.Controllers
OrderId = bindDto.OrderId
}).ToList();
await orderAnnexService.Add(orderAnnexModelList);
// 将备注更新到附件表中
await annexService.AsUpdateable()
.SetColumns(x => x.Remark == bindDto.Remark)
.Where(x => bindDto.AnnexIdList.Contains(x.Id))
.ExecuteCommandAsync();
}
else
{
var id = await annexService.Add(new Annex()
{
Remark = bindDto.Remark,
Type = 2
});
await orderAnnexService.Add(new OrderAnnex()
{
AnnexId = id,
OrderId = bindDto.OrderId
});
}
}
// 如果操作类型为账单或发票,则先删除原有的绑定信息
else if (bindDto.OperType == FileTypeEnum. || bindDto.OperType == FileTypeEnum.)
{
var oldAnnexIdList = await orderAnnexService.AsQueryable()
@ -735,6 +758,7 @@ namespace EntrustSettle.Api.Controllers
var annexList = await orderAnnexService.Db.Queryable<OrderAnnex, Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id)
.Where((o, a) => a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum.)
.OrderByDescending((o, a) => a.CreateTime)
.Select((o, a) => new AnnexDto()
{
Id = a.Id,

@ -178,7 +178,8 @@
"ConnId": "PingTai",
"DBType": 1,
"Enabled": true,
"Connection": "Data Source =60.209.125.238,32009; Initial Catalog=TestDsPingTai; Persist Security Info=True; User ID =sa; Password=Djy@Sql2022.test;pooling=true"
"Connection": "Data Source =60.209.125.238,32009; Initial Catalog=TestDsPingTai; Persist Security Info=True; User ID =sa; Password=Djy@Sql2022.test;pooling=true",
//"Connection": "Data Source =47.104.73.97,6761; Initial Catalog=DsPingTai; Persist Security Info=True; User ID =sa; Password=QDdjy#2020*;pooling=true"
}
],
"Mongo": {

@ -83,7 +83,10 @@ public class Caching : ICaching
await _cache.SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys));
}
public void DelCacheKey(string cacheKey)
/// <summary>
/// 删除缓存(从缓存中删除Key而不是直接删除Key)如果要删除Key使用<see cref="RemoveAsync(string)"/>
/// </summary>
public void DelCacheKey(string cacheKey)
{
var res = _cache.GetString(CacheConst.KeyAll);
var allkeys = string.IsNullOrWhiteSpace(res) ? new List<string>() : JsonConvert.DeserializeObject<List<string>>(res);
@ -94,12 +97,10 @@ public class Caching : ICaching
}
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public async Task DelCacheKeyAsync(string cacheKey)
/// <summary>
/// 删除缓存(从缓存中删除Key而不是直接删除Key)如果要删除Key使用<see cref="RemoveAsync(string)"/>
/// </summary>
public async Task DelCacheKeyAsync(string cacheKey)
{
var res = await _cache.GetStringAsync(CacheConst.KeyAll);
var allkeys = string.IsNullOrWhiteSpace(res) ? new List<string>() : JsonConvert.DeserializeObject<List<string>>(res);
@ -188,17 +189,18 @@ public class Caching : ICaching
return await _cache.GetStringAsync(cacheKey);
}
public void Remove(string key)
/// <summary>
/// 删除Key
/// </summary>
public void Remove(string key)
{
_cache.Remove(key);
DelCacheKey(key);
}
/// <summary>
/// 删除缓存
/// 删除Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task RemoveAsync(string key)
{
await _cache.RemoveAsync(key);

@ -9,41 +9,51 @@ namespace EntrustSettle.Extensions.ServiceExtensions;
public static class CacheSetup
{
/// <summary>
/// 统一注册缓存
/// </summary>
/// <param name="services"></param>
public static void AddCacheSetup(this IServiceCollection services)
{
var cacheOptions = App.GetOptions<RedisOptions>();
if (cacheOptions.Enabled)
{
// 配置启动Redis服务虽然可能影响项目启动速度但是不能在运行的时候报错所以是合理的
services.AddSingleton<IConnectionMultiplexer>(sp =>
{
//获取连接字符串
var configuration = ConfigurationOptions.Parse(cacheOptions.ConnectionString, true);
configuration.ResolveDns = true;
return ConnectionMultiplexer.Connect(configuration);
});
services.AddSingleton<ConnectionMultiplexer>(p => p.GetService<IConnectionMultiplexer>() as ConnectionMultiplexer);
//使用Redis
services.AddStackExchangeRedisCache(options =>
{
options.ConnectionMultiplexerFactory = () => Task.FromResult(App.GetService<IConnectionMultiplexer>(false));
if (!cacheOptions.InstanceName.IsNullOrEmpty()) options.InstanceName = cacheOptions.InstanceName;
});
/// <summary>
/// 统一注册缓存
/// </summary>
/// <param name="services"></param>
public static void AddCacheSetup(this IServiceCollection services)
{
//string path = "D:\\EntrustSettle\\委托结算临时日志.txt";s
var cacheOptions = App.GetOptions<RedisOptions>();
services.AddTransient<IRedisBasketRepository, RedisBasketRepository>();
}
else
{
cacheOptions.Enabled = AppSettings.app("Redis", "Enabled").ObjToBool();
cacheOptions.ConnectionString = AppSettings.app("Redis", "ConnectionString");
cacheOptions.InstanceName = AppSettings.app("Redis", "InstanceName");
//File.AppendAllText(path, cacheOptions.ToJson() + Environment.NewLine);
if (cacheOptions.Enabled)
{
//File.AppendAllText(path, "添加Redis缓存" + Environment.NewLine);
// 配置启动Redis服务虽然可能影响项目启动速度但是不能在运行的时候报错所以是合理的
services.AddSingleton<IConnectionMultiplexer>(sp =>
{
//获取连接字符串
var configuration = ConfigurationOptions.Parse(cacheOptions.ConnectionString, true);
configuration.ResolveDns = true;
return ConnectionMultiplexer.Connect(configuration);
});
services.AddSingleton<ConnectionMultiplexer>(p => p.GetService<IConnectionMultiplexer>() as ConnectionMultiplexer);
//使用Redis
services.AddStackExchangeRedisCache(options =>
{
options.ConnectionMultiplexerFactory = () => Task.FromResult(App.GetService<IConnectionMultiplexer>(false));
if (!cacheOptions.InstanceName.IsNullOrEmpty()) options.InstanceName = cacheOptions.InstanceName;
});
services.AddTransient<IRedisBasketRepository, RedisBasketRepository>();
}
else
{
//File.AppendAllText(path, "添加内存缓存" + Environment.NewLine);
// 使用内存缓存
services.AddMemoryCache();
// 分布式内存缓存
services.AddDistributedMemoryCache();
}
}
services.AddSingleton<ICaching, Caching>();
}
services.AddSingleton<ICaching, Caching>();
//File.AppendAllText(path, "添加缓存结束" + Environment.NewLine);
}
}

@ -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})");
}
}

Loading…
Cancel
Save