修改ESS查询问卷数据接口

master
xiaochanghai 2 weeks ago
parent fcf53eec8b
commit b327491715
  1. BIN
      Lib/Tiobon.Core.Base.dll
  2. BIN
      Lib/Tiobon.Core.dll
  3. 6
      Lib/Tiobon.Core.xml
  4. 2
      Tiobon.Core.Api/Controllers/CommonController.cs
  5. 33
      Tiobon.Core.Common/Extensions/HttpResponseExceptions.cs
  6. 71
      Tiobon.Core.Common/Https/FluentHttpResponseStream.cs
  7. 365
      Tiobon.Core.Common/Https/HttpPolly/HttpPollyHelper.cs
  8. 18
      Tiobon.Core.Common/Https/HttpPolly/IHttpPollyHelper.cs
  9. 80
      Tiobon.Core.Common/Https/RequestIpUtility.cs
  10. 2
      Tiobon.Core.Extensions/Middlewares/EncryptionResponseMiddleware.cs
  11. 2
      Tiobon.Core.Extensions/Middlewares/FluentResponseBodyMiddleware.cs
  12. 1
      Tiobon.Core.Extensions/Middlewares/RequRespLogMiddleware.cs
  13. 3
      Tiobon.Core.Extensions/ServiceExtensions/HttpPollySetup.cs
  14. 3
      Tiobon.Core.Model/ViewModels/Extend/Ghre_ESSSurvey.cs
  15. 2
      Tiobon.Core.Serilog/Utility/SerilogRequestUtility.cs
  16. 1
      Tiobon.Core.Services/Ghre/Ghre_SurveyServices.cs
  17. 2
      Tiobon.Core.Services/Ghre/Ghre_TeacherServices.cs
  18. 16
      Tiobon.Core.Services/GlobalUsings.cs
  19. 8
      Tiobon.Core.Tests/Repository_Test/OrmTest.cs
  20. 2
      Tiobon.Core/Tiobon.Core.Api.xml

Binary file not shown.

Binary file not shown.

@ -2918,6 +2918,12 @@
<param name="accessor"></param>
<param name="logger"></param>
</member>
<member name="T:Tiobon.Core.Https.FluentHttpResponseStream">
<summary>
扩展 HttpResponseStream <br/>
原始[HttpResponseStream]实际上只是个包装类,内部包装了[HttpResponsePipeWriter]来进行写入响应数据
</summary>
</member>
<member name="T:Tiobon.Core.Hubs.ChatHub">
<summary>

@ -1,6 +1,6 @@
using Newtonsoft.Json.Linq;
using System.Data;
using Tiobon.Core.Common.Https.HttpPolly;
using Tiobon.Core.Https.HttpPolly;
using Tiobon.Core.DB.Dapper;
using Tiobon.Core.DB.Dapper.DBManager;

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Http;
using Tiobon.Core.Common.Https;
namespace Tiobon.Core.Common.Extensions;
public static class HttpResponseExceptions
{
public static string GetResponseBody(this HttpResponse response)
{
if (response is null)
{
return string.Empty;
}
//原始HttpResponseStream 无法读取
//实际上只是个包装类,内部使用了HttpResponsePipeWriter write
switch (response.Body)
{
case FluentHttpResponseStream:
case MemoryStream:
{
response.Body.Position = 0;
using var stream = new StreamReader(response.Body, leaveOpen: true);
var body = stream.ReadToEnd();
response.Body.Position = 0;
return body;
}
default:
// throw new ApplicationException("The response body is not a FluentHttpResponseStream");
return string.Empty;
}
}
}

@ -1,71 +0,0 @@
using Microsoft.AspNetCore.Http.Features;
namespace Tiobon.Core.Common.Https;
/// <summary>
/// 扩展 HttpResponseStream <br/>
/// 原始[HttpResponseStream]实际上只是个包装类,内部包装了[HttpResponsePipeWriter]来进行写入响应数据
/// </summary>
public class FluentHttpResponseStream : Stream
{
private readonly IHttpBodyControlFeature _bodyControl;
private readonly IHttpResponseBodyFeature _pipeWriter;
private readonly MemoryStream _stream = new();
public FluentHttpResponseStream(IHttpResponseBodyFeature pipeWriter, IHttpBodyControlFeature bodyControl)
{
_pipeWriter = pipeWriter;
_bodyControl = bodyControl;
}
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;
public override long Position { get => _stream.Position; set => _stream.Position = value; }
public override void Flush()
{
if (!_bodyControl.AllowSynchronousIO)
{
throw new InvalidOperationException("SynchronousWritesDisallowed ");
}
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
WriteAsync(buffer, offset, count, default).GetAwaiter().GetResult();
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
_stream.Write(buffer, offset, count);
return _pipeWriter.Writer.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
protected override void Dispose(bool disposing)
{
_stream.Dispose();
base.Dispose(disposing);
}
}

@ -1,365 +0,0 @@
using Newtonsoft.Json;
using System.Text;
using Tiobon.Core.Common.Helper;
using Tiobon.Core.Helper;
using Tiobon.Core.Model;
namespace Tiobon.Core.Common.Https.HttpPolly;
public class HttpPollyHelper : IHttpPollyHelper
{
private readonly IHttpClientFactory _clientFactory;
public HttpPollyHelper(IHttpClientFactory httpClientFactory)
{
_clientFactory = httpClientFactory;
}
public async Task<T> PostAsync<T, R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<T> PostAsync<T>(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(request, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<string> PostAsync<R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<string> PostAsync(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(request, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<T> GetAsync<T>(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var response = await client.GetAsync(url);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<string> GetAsync(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var response = await client.GetAsync(url);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync(); ;
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<T> PutAsync<T, R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<T> PutAsync<T>(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var stringContent = new StringContent(request, Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, stringContent);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task<T> DeleteAsync<T>(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null)
{
try
{
var client = _clientFactory.CreateClient(httpEnum.ToString());
if (headers != null)
{
foreach (var header in headers)
{
if (!client.DefaultRequestHeaders.Contains(header.Key))
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
var response = await client.DeleteAsync(url);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception($"Http Error StatusCode:{response.StatusCode}");
}
}
catch (Exception)
{
throw;
}
}
public async Task DownLoad(string fileUrl, string destinationPath)
{
var client = _clientFactory.CreateClient();
destinationPath = FileHelper.GetPhysicsPath() + destinationPath;
HttpResponseMessage response = await client.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode(); // 确保请求成功
using (Stream contentStream = await response.Content.ReadAsStreamAsync(),
fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
{
await contentStream.CopyToAsync(fileStream);
}
}
}

@ -1,18 +0,0 @@
using Tiobon.Core.Model;
namespace Tiobon.Core.Common.Https.HttpPolly;
public interface IHttpPollyHelper
{
Task<T> PostAsync<T, R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null);
Task<T> PostAsync<T>(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null);
Task<string> PostAsync(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null);
Task<string> PostAsync<R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null);
Task<T> GetAsync<T>(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null);
Task<string> GetAsync(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null);
Task<T> PutAsync<T, R>(HttpEnum httpEnum, string url, R request, Dictionary<string, string> headers = null);
Task<T> PutAsync<T>(HttpEnum httpEnum, string url, string request, Dictionary<string, string> headers = null);
Task<T> DeleteAsync<T>(HttpEnum httpEnum, string url, Dictionary<string, string> headers = null);
Task DownLoad(string fileUrl, string destinationPath);
}

@ -1,80 +0,0 @@
using Microsoft.AspNetCore.Http;
using System.Net;
namespace Tiobon.Core.Common.Https;
public static class RequestIpUtility
{
public static string GetRequestIp(this HttpContext context)
{
string ip = SplitCsv(GetHeaderValueAs<string>(context, "X-Forwarded-For")).FirstOrDefault();
if (string.IsNullOrWhiteSpace(ip))
ip = SplitCsv(GetHeaderValueAs<string>(context, "X-Real-IP")).FirstOrDefault();
if (string.IsNullOrWhiteSpace(ip) && context.Connection?.RemoteIpAddress != null)
ip = context.Connection.RemoteIpAddress.ToString();
if (string.IsNullOrWhiteSpace(ip))
ip = GetHeaderValueAs<string>(context, "REMOTE_ADDR");
return ip;
}
public static bool IsLocal(this HttpContext context)
{
return GetRequestIp(context) is "127.0.0.1" or "::1" || context.Request?.IsLocal() == true;
}
public static bool IsLocal(this HttpRequest req)
{
var connection = req.HttpContext.Connection;
if (connection.RemoteIpAddress != null)
{
if (connection.LocalIpAddress != null)
{
return connection.RemoteIpAddress.Equals(connection.LocalIpAddress);
}
else
{
return IPAddress.IsLoopback(connection.RemoteIpAddress);
}
}
// for in memory TestServer or when dealing with default connection info
if (connection.RemoteIpAddress == null && connection.LocalIpAddress == null)
{
return true;
}
return false;
}
private static T GetHeaderValueAs<T>(HttpContext context, string headerName)
{
if (context.Request?.Headers?.TryGetValue(headerName, out var values) ?? false)
{
string rawValues = values.ToString();
if (!string.IsNullOrWhiteSpace(rawValues))
return (T) Convert.ChangeType(values.ToString(), typeof(T));
}
return default;
}
private static List<string> SplitCsv(string csvList)
{
if (string.IsNullOrWhiteSpace(csvList))
return new List<string>();
return csvList
.TrimEnd(',')
.Split(',')
.AsEnumerable<string>()
.Select(s => s.Trim())
.ToList();
}
}

@ -2,8 +2,6 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Text;
using Tiobon.Core.Common;
using Tiobon.Core.Common.Extensions;
namespace Tiobon.Core.Extensions;

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
using Tiobon.Core.Common.Https;
using Tiobon.Core.Https;
namespace Tiobon.Core.Extensions.Middlewares;

@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using Tiobon.Core.Common.Extensions;
using Tiobon.Core.LogHelper;
namespace Tiobon.Core.Extensions.Middlewares;

@ -2,8 +2,7 @@
using Polly;
using Polly.Extensions.Http;
using Polly.Timeout;
using Tiobon.Core.Common.Https.HttpPolly;
using Tiobon.Core.Model;
using Tiobon.Core.Https.HttpPolly;
namespace Tiobon.Core.Extensions;

@ -14,5 +14,8 @@ public class Ghre_ESSSurvey
public string BeginEndTime { get; set; }
public string IsAnonymous1 { get; set; }
public bool? IsAnonymous { get; set; }
public bool? IsSubmit { get; set; }
}

@ -1,5 +1,5 @@
using Tiobon.Core.Extensions;
using Tiobon.Core.Common.Https;
using Tiobon.Core.Https;
using Microsoft.AspNetCore.Http;
using Serilog;
using Serilog.Events;

@ -863,6 +863,7 @@ WHERE A.IsEnable = 1 AND A.Status ! = 'Temporary'
{
x.IsAnonymous1 = x.IsAnonymous == true ? "是" : "否";
x.Status = x.SubmitDate != null ? "已填" : "未填";
x.IsSubmit = x.SubmitDate != null ? true : false;
x.SurveyClass = await GetParaLabel("TrainSurveyClass", x.SurveyClass);
if (x.BeginTime != null && x.EndTime != null)
x.BeginEndTime = $"{DateTimeHelper.ConvertToMiniuteString(x.BeginTime)}~{DateTimeHelper.ConvertToMiniuteString(x.EndTime)}";

@ -1,5 +1,5 @@
using Serilog;
using Tiobon.Core.Common.Https.HttpPolly;
using Tiobon.Core.Https.HttpPolly;
using static Tiobon.Core.Model.Consts;
namespace Tiobon.Core.Services;

@ -1,5 +1,6 @@
global using AgileObjects.AgileMapper;
global using Microsoft.AspNetCore.Http;
global using Newtonsoft.Json;
global using Newtonsoft.Json.Linq;
global using SqlSugar;
global using System.Collections;
@ -7,23 +8,20 @@ global using System.Data;
global using System.Dynamic;
global using System.Linq.Expressions;
global using System.Reflection;
global using Tiobon.Core.Common;
global using Tiobon.Core.Caches;
global using Tiobon.Core.DB.Dapper;
global using Tiobon.Core.DB.Dapper.Extensions;
global using Tiobon.Core.Common.Enums;
global using Tiobon.Core.Common.Extensions;
global using Tiobon.Core.Helper;
global using Tiobon.Core.Common.Helper;
global using Tiobon.Core.UserManager;
global using Tiobon.Core.DataAccess;
global using Tiobon.Core.DB.Dapper;
global using Tiobon.Core.DB.Dapper.Extensions;
global using Tiobon.Core.Helper;
global using Tiobon.Core.IRepository.Base;
global using Tiobon.Core.IServices;
global using Tiobon.Core.Model;
global using Tiobon.Core.Model.Entity;
global using Tiobon.Core.Model.Models;
global using Tiobon.Core.Model.ViewModels;
global using Tiobon.Core.Model.ViewModels.Extend;
global using Tiobon.Core.Services.BASE;
global using Tiobon.Core.Model.ViewModels;
global using Newtonsoft.Json;
global using Tiobon.Core.Model.Entity;
global using Tiobon.Core.UserManager;

@ -1,10 +1,8 @@
using System;
using Autofac;
using Tiobon.Core.Common.Extensions;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.Model.Models;
using Autofac;
//using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using SqlSugar;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.Model.Models;
using Xunit;
using Xunit.Abstractions;

@ -168,7 +168,7 @@
公共服务
</summary>
</member>
<member name="M:Tiobon.Core.Controllers.CommonController.#ctor(Tiobon.Core.IServices.ICommonServices,Tiobon.Core.Common.Https.HttpPolly.IHttpPollyHelper)">
<member name="M:Tiobon.Core.Controllers.CommonController.#ctor(Tiobon.Core.IServices.ICommonServices,Tiobon.Core.Https.HttpPolly.IHttpPollyHelper)">
<summary>
构造函数
</summary>

Loading…
Cancel
Save