第三方接口请求记录

This commit is contained in:
2024-07-11 15:20:45 +08:00
parent 614681284d
commit 22f255f85e
5 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
namespace Tnb.BasicData.Entities
{
public class ThirdResult
{
public int Code { get; set; }
public string msgResult { get; set; }
}
// public enum ThirdResultCode
// {
// OK = 200
// }
}

View File

@@ -0,0 +1,78 @@
using JNPF.Common.Contracts;
using SqlSugar;
namespace Tnb.BasicData.Entities
{
/// <summary>
/// 工位资料
/// </summary>
[SugarTable("third_webapi_record")]
public class ThirdWebapiRecord : BaseEntity<string>
{
/// <summary>
/// 第三方名
/// </summary>
public string third_name { get; set; }
/// <summary>
/// 名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 请求方式
/// </summary>
public string method { get; set; }
/// <summary>
/// 地址
/// </summary>
public string url { get; set; }
/// <summary>
/// 请求数据
/// </summary>
public string request_data { get; set; }
/// <summary>
/// 接收数据
/// </summary>
public string response_data { get; set; }
/// <summary>
/// 返回码
/// </summary>
public int response_code { get; set; }
/// <summary>
/// 响应时长
/// </summary>
public long response_time { get; set; }
/// <summary>
/// 是否发送 0 否 1是
/// </summary>
public int is_send { get; set; }
/// <summary>
/// 状态 0 失败 1 成功
/// </summary>
public int status { get; set; }
/// <summary>
/// 发送成功时类型 自动 手动
/// </summary>
public string send_type { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime create_time { get; set; }
/// <summary>
/// 最后发送时间
/// </summary>
public DateTime? last_send_time { get; set; }
}
}

View File

@@ -0,0 +1,80 @@
using System.Diagnostics;
using JNPF.Common.Core.Manager;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.CollectiveOAuth.Utils;
using JNPF.Systems.Interfaces.Permission;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData.Entities;
namespace Tnb.BasicData
{
/// <summary>
/// 第三方接口发送记录
/// </summary>
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
[Route("api/[area]/[controller]/[action]")]
public class ThirdApiRecordService : IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<ThirdWebapiRecord> _repository;
private readonly IUserManager _userManager;
public ThirdApiRecordService(
ISqlSugarRepository<ThirdWebapiRecord> repository,
IUserManager userManager
)
{
_repository = repository;
_userManager = userManager;
}
[HttpGet]
public async Task<string> Send(string id)
{
var db = _repository.AsSugarClient();
string response = "";
ThirdWebapiRecord record = await _repository.GetSingleAsync(x=>x.id==id);
DateTime now = DateTime.Now;
Stopwatch stopwatch = Stopwatch.StartNew();
switch (record.method.ToUpper())
{
case "GET":
response = HttpUtils.RequestGet(record.url);
break;
case "POST":
response = HttpUtils.RequestPost(record.url, record.request_data);
break;
}
stopwatch.Stop();
var elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
ThirdResult thirdResult = JsonConvert.DeserializeObject<ThirdResult>(response);
if (thirdResult.Code == 200)
{
await db.Updateable<ThirdWebapiRecord>()
.SetColumns(x => x.response_data == response)
.SetColumns(x => x.response_code == thirdResult.Code)
.SetColumns(x => x.last_send_time == now)
.SetColumns(x => x.response_time == elapsedMilliseconds)
.SetColumns(x => x.send_type == "手动")
.SetColumns(x => x.status == 1)
.Where(x=>x.id==record.id)
.ExecuteCommandAsync();
}
else
{
await db.Updateable<ThirdWebapiRecord>()
.SetColumns(x => x.response_data == response)
.SetColumns(x => x.response_code == thirdResult.Code)
.SetColumns(x => x.last_send_time == now)
.SetColumns(x => x.response_time == elapsedMilliseconds)
.Where(x=>x.id==record.id)
.ExecuteCommandAsync();
}
return thirdResult.Code == 200 ? "成功" : "失败";
}
}
}