From ecad98aa74a1284a036572fa364bcc352480149c Mon Sep 17 00:00:00 2001
From: yl <ykxkd@outlook.com>
Date: 星期一, 11 五月 2026 08:01:35 +0800
Subject: [PATCH] 1.车间综合看板接口调整 2.报工记录子表和不良记录表写入报工人员,班组编码 3.修改调整定时任务写入到数据库,并实现消息推送
---
VueWebCoreApi/Quartz/HttpManager.cs | 189 ++++++++++++++++++++++++++++++++++++++--------
1 files changed, 155 insertions(+), 34 deletions(-)
diff --git a/VueWebCoreApi/Quartz/HttpManager.cs b/VueWebCoreApi/Quartz/HttpManager.cs
index f1f9c4a..af2f55d 100644
--- a/VueWebCoreApi/Quartz/HttpManager.cs
+++ b/VueWebCoreApi/Quartz/HttpManager.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
+using System.Text;
using System.Threading.Tasks;
namespace VueWebCoreApi.Quartz
@@ -12,62 +13,182 @@
{
public static string GetUserIP(IHttpContextAccessor httpContextAccessor)
{
- var Request = httpContextAccessor.HttpContext.Request;
- string realIP = null;
- string forwarded = null;
- string remoteIpAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
- if (Request.Headers.ContainsKey("X-Real-IP"))
+ if (httpContextAccessor?.HttpContext == null)
+ return string.Empty;
+
+ var request = httpContextAccessor.HttpContext.Request;
+ string remoteIpAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString() ?? string.Empty;
+
+ if (request.Headers.ContainsKey("X-Real-IP"))
{
- realIP = Request.Headers["X-Real-IP"].ToString();
- if (realIP != remoteIpAddress)
+ var realIp = request.Headers["X-Real-IP"].ToString();
+ if (!string.IsNullOrEmpty(realIp) && realIp != remoteIpAddress)
{
- remoteIpAddress = realIP;
+ remoteIpAddress = realIp;
}
}
- if (Request.Headers.ContainsKey("X-Forwarded-For"))
+
+ if (request.Headers.ContainsKey("X-Forwarded-For"))
{
- forwarded = Request.Headers["X-Forwarded-For"].ToString();
- if (forwarded != remoteIpAddress)
+ var forwarded = request.Headers["X-Forwarded-For"].ToString();
+ if (!string.IsNullOrEmpty(forwarded) && forwarded != remoteIpAddress)
{
remoteIpAddress = forwarded;
}
}
+
return remoteIpAddress;
}
-
-
- public static async Task<string> HttpSendAsync(this IHttpClientFactory httpClientFactory, HttpMethod method, string url, Dictionary<string, string> headers = null)
+ public static async Task<string> HttpSendAsyncString(this IHttpClientFactory httpClientFactory, HttpMethod method, string url, string parameters = null, Dictionary<string, string> headers = null)
{
+ if (httpClientFactory == null)
+ throw new ArgumentNullException(nameof(httpClientFactory));
+ if (method == null)
+ throw new ArgumentNullException(nameof(method));
+ if (string.IsNullOrEmpty(url))
+ throw new ArgumentNullException(nameof(url));
var client = httpClientFactory.CreateClient();
- var content = new StringContent("");
- //content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
- //content.Headers.ContentType.CharSet = "utf-8";
- var request = new HttpRequestMessage(method, url)
- {
- Content = content
- };
- if (headers != null)
- {
- foreach (var header in headers)
- {
- request.Headers.Add(header.Key, header.Value);
- }
- }
+ string finalUrl = url;
+ HttpContent content = new StringContent("");
+
try
{
- HttpResponseMessage httpResponseMessage = await client.SendAsync(request);
+ // ====================== GET 璇锋眰锛氭嫾鎺ュ弬鏁板埌 URL ======================
+ if (method == HttpMethod.Get && !string.IsNullOrWhiteSpace(parameters))
+ {
+ finalUrl = url.Contains("?")
+ ? $"{url}&{parameters}"
+ : $"{url}?{parameters}";
+ }
- var result = await httpResponseMessage.Content
- .ReadAsStringAsync();
- return result;
+ // ====================== POST 璇锋眰锛氬弬鏁版斁鍏� Body ======================
+ if (method == HttpMethod.Post && !string.IsNullOrWhiteSpace(parameters))
+ {
+ content = new StringContent(
+ parameters,
+ System.Text.Encoding.UTF8,
+ "application/x-www-form-urlencoded");
+ }
+
+ var request = new HttpRequestMessage(method, finalUrl) { Content = content };
+
+ // 杩藉姞璇锋眰澶�
+ if (headers != null)
+ {
+ foreach (var header in headers)
+ {
+ if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value))
+ {
+ content.Headers.TryAddWithoutValidation(header.Key, header.Value);
+ }
+ }
+ }
+
+ using (var response = await client.SendAsync(request))
+ {
+ response.EnsureSuccessStatusCode();
+ return await response.Content.ReadAsStringAsync();
+ }
}
catch (Exception ex)
{
- Console.WriteLine(ex.Message);
- return ex.Message;
+ return $"璇锋眰澶辫触锛歿ex.Message}";
}
}
+
+ public static async Task<HttpResult> HttpSendAsync(this IHttpClientFactory httpClientFactory,HttpMethod method,string url,string parameters = null,Dictionary<string, string> headers = null)
+ {
+ if (httpClientFactory == null)
+ throw new ArgumentNullException(nameof(httpClientFactory));
+ if (method == null)
+ throw new ArgumentNullException(nameof(method));
+ if (string.IsNullOrEmpty(url))
+ throw new ArgumentNullException(nameof(url));
+
+ var client = httpClientFactory.CreateClient();
+ string finalUrl = url;
+ HttpContent content = new StringContent("");
+
+ try
+ {
+ // GET 鎷兼帴鍙傛暟
+ if (method == HttpMethod.Get && !string.IsNullOrWhiteSpace(parameters))
+ {
+ finalUrl = url.Contains("?") ? $"{url}&{parameters}" : $"{url}?{parameters}";
+ }
+
+ // POST 澶勭悊鍙傛暟
+ if (method == HttpMethod.Post && !string.IsNullOrWhiteSpace(parameters))
+ {
+ content = new StringContent(parameters, Encoding.UTF8, "application/x-www-form-urlencoded");
+ }
+
+ var request = new HttpRequestMessage(method, finalUrl) { Content = content };
+
+ // 璇锋眰澶�
+ if (headers != null)
+ {
+ foreach (var header in headers)
+ {
+ if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value))
+ {
+ content.Headers.TryAddWithoutValidation(header.Key, header.Value);
+ }
+ }
+ }
+
+ using (var response = await client.SendAsync(request))
+ {
+ // 璇诲彇杩斿洖鍐呭
+ string responseContent = await response.Content.ReadAsStringAsync();
+
+ return new HttpResult
+ {
+ IsSuccess = response.IsSuccessStatusCode, // 鏄惁鎴愬姛
+ StatusCode = (int)response.StatusCode, // 鐘舵�佺爜 200/404/500
+ Content = responseContent, // 杩斿洖鍐呭
+ ErrorMsg = response.IsSuccessStatusCode ? "" : $"HTTP璇锋眰澶辫触锛岀姸鎬佺爜锛歿(int)response.StatusCode}"
+ };
+ }
+ }
+ catch (Exception ex)
+ {
+ // 缃戠粶寮傚父銆佽秴鏃剁瓑
+ return new HttpResult
+ {
+ IsSuccess = false,
+ StatusCode = 0, // 缃戠粶寮傚父鐘舵�佺爜璁句负0
+ Content = "",
+ ErrorMsg = ex.Message
+ };
+ }
+ }
+ /// <summary>
+ /// HTTP 璇锋眰缁熶竴杩斿洖妯″瀷
+ /// </summary>
+ public class HttpResult
+ {
+ /// <summary>
+ /// 鏄惁璇锋眰鎴愬姛锛�200-299锛�
+ /// </summary>
+ public bool IsSuccess { get; set; }
+
+ /// <summary>
+ /// HTTP 鐘舵�佺爜锛堟暟瀛楋細200,404,500锛�
+ /// </summary>
+ public int StatusCode { get; set; }
+
+ /// <summary>
+ /// 杩斿洖鍐呭
+ /// </summary>
+ public string Content { get; set; }
+
+ /// <summary>
+ /// 閿欒淇℃伅
+ /// </summary>
+ public string ErrorMsg { get; set; }
+ }
}
}
--
Gitblit v1.9.3