第三方接口请求记录
This commit is contained in:
80
BasicData/Tnb.BasicData/ThirdApiRecordService.cs
Normal file
80
BasicData/Tnb.BasicData/ThirdApiRecordService.cs
Normal 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 ? "成功" : "失败";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user