添加报警窗口
出库流程调试、优化
This commit is contained in:
@ -3,15 +3,18 @@ using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using WCS.BLL.Tool.Api.Models;
|
||||
using WCS.BLL;
|
||||
using WCS.Model;
|
||||
|
||||
namespace WCS.BLL.Tool.Api
|
||||
namespace WCS.BLL.Tool
|
||||
{
|
||||
public static class ApiHelp
|
||||
{
|
||||
@ -19,57 +22,57 @@ namespace WCS.BLL.Tool.Api
|
||||
static ApiHelp()
|
||||
{
|
||||
httpClient = new HttpClient();
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(5);
|
||||
httpClient.DefaultRequestHeaders.Add("User-Agent", "Chrome/95.0.4638.69 Safari/537.36");
|
||||
//ServicePointManager.Expect100Continue = false;
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
||||
httpClient.DefaultRequestHeaders.Add("UserModel-Agent", "Chrome/95.0.4638.69 Safari/537.36");
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
}
|
||||
|
||||
public static ApiResult Get(string uri, object? query = null, object? body = null)
|
||||
public static async Task<ResponseBase> Get(string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send(HttpMethod.Get, uri, query, body);
|
||||
return await Send(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult Get(IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
public static async Task<ResponseBase> Get(IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send(HttpMethod.Get, uri, query, body);
|
||||
return await Send(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Get<T>(string uri, object? query = null, object? body = null)
|
||||
public static async Task<T> Get<T>(string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Get, uri, query, body);
|
||||
return await Send<T>(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Get<T>(IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
public static async Task<T> Get<T>(IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Get, uri, query, body);
|
||||
return await Send<T>(HttpMethod.Get, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult Post(string uri, object? body = null, object? query = null)
|
||||
public static async Task<ResponseBase> Post(string uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send(HttpMethod.Post, uri, query, body);
|
||||
return await Send(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult Post(IEnumerable<object> uri, object? body = null, object? query = null)
|
||||
public static async Task<ResponseBase> Post(IEnumerable<object> uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send(HttpMethod.Post, uri, query, body);
|
||||
return await Send(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Post<T>(string uri, object? body = null, object? query = null)
|
||||
public static async Task<T> Post<T>(string uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Post, uri, query, body);
|
||||
return await Send<T>(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Post<T>(IEnumerable<object> uri, object? body = null, object? query = null)
|
||||
public static async Task<T> Post<T>(IEnumerable<object> uri, object? body = null, object? query = null)
|
||||
{
|
||||
return Send<T>(HttpMethod.Post, uri, query, body);
|
||||
return await Send<T>(HttpMethod.Post, uri, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult Send(HttpMethod method, string uri, object? query = null, object? body = null)
|
||||
public static async Task<ResponseBase> Send(HttpMethod method, string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send(method, new string[] { uri }, query, body);
|
||||
return await Send(method, new string[] { uri }, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult Send(HttpMethod method, IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
public static async Task<ResponseBase> Send(HttpMethod method, IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, "".AppendPathSegments(uri).SetQueryParams(query));
|
||||
|
||||
@ -79,35 +82,44 @@ namespace WCS.BLL.Tool.Api
|
||||
httpRequestMessage.Content = content;
|
||||
}
|
||||
|
||||
var re = httpClient.SendAsync(httpRequestMessage).Result;
|
||||
var re = await httpClient.SendAsync(httpRequestMessage);
|
||||
if (!re.IsSuccessStatusCode)
|
||||
return new ApiResult() { Code = ((int)re.StatusCode).ToString() };
|
||||
return new ResponseBase() { Code = (int)re.StatusCode };
|
||||
|
||||
var con = re.Content.ReadAsStringAsync().Result;
|
||||
return JsonConvert.DeserializeObject<ApiResult>(con) ?? new ApiResult();
|
||||
var con = await re.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<ResponseBase>(con) ?? new ResponseBase();
|
||||
}
|
||||
|
||||
public static ApiResult<T> Send<T>(HttpMethod method, string uri, object? query = null, object? body = null)
|
||||
public static async Task<T> Send<T>(HttpMethod method, string uri, object? query = null, object? body = null)
|
||||
{
|
||||
return Send<T>(method, new string[] { uri }, query, body);
|
||||
return await Send<T>(method, new string[] { uri }, query, body);
|
||||
}
|
||||
|
||||
public static ApiResult<T> Send<T>(HttpMethod method, IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
public static async Task<T> Send<T>(HttpMethod method, IEnumerable<object> uri, object? query = null, object? body = null)
|
||||
{
|
||||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, "".AppendPathSegments(uri).SetQueryParams(query));
|
||||
|
||||
if (body != null)
|
||||
try
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
||||
httpRequestMessage.Content = content;
|
||||
var url = "".AppendPathSegments(uri).SetQueryParams(query);
|
||||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url);
|
||||
|
||||
if (body != null)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
||||
httpRequestMessage.Content = content;
|
||||
}
|
||||
|
||||
var re = await httpClient.SendAsync(httpRequestMessage);
|
||||
if (!re.IsSuccessStatusCode)
|
||||
return default(T);
|
||||
|
||||
var con = await re.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<T>(con) ?? default(T); ;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
var re = httpClient.SendAsync(httpRequestMessage).Result;
|
||||
if (!re.IsSuccessStatusCode)
|
||||
return new ApiResult<T>() { Code = ((int)re.StatusCode).ToString() };
|
||||
|
||||
var con = re.Content.ReadAsStringAsync().Result;
|
||||
return JsonConvert.DeserializeObject<ApiResult<T>>(con) ?? new ApiResult<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -118,17 +130,18 @@ namespace WCS.BLL.Tool.Api
|
||||
/// <returns>地址</returns>
|
||||
public static string AppendPathSegments(this string url, IEnumerable<object> segments)
|
||||
{
|
||||
string urlStr = url;
|
||||
string urlStr = url.Trim();
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
var val = segment?.ToString();
|
||||
var val = segment?.ToString()?.Trim() ?? "";
|
||||
if (string.IsNullOrWhiteSpace(val))
|
||||
continue;
|
||||
|
||||
if (urlStr.EndsWith("/"))
|
||||
urlStr = urlStr.Substring(0, urlStr.Length - 1);
|
||||
|
||||
urlStr += val!.StartsWith("/") ? val : $"/{val}";
|
||||
if (val.Length > 0)
|
||||
urlStr += string.IsNullOrEmpty(urlStr) || val.StartsWith("/") ? val : $"/{val}";
|
||||
}
|
||||
return urlStr;
|
||||
}
|
||||
@ -178,5 +191,126 @@ namespace WCS.BLL.Tool.Api
|
||||
|
||||
return urlStr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post方式下载Excel文件
|
||||
/// </summary>
|
||||
/// <param name="localFilePath">文件保存的路径</param>
|
||||
/// <param name="method"></param>
|
||||
/// <param name="requestUrl"></param>
|
||||
/// <param name="body"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task PostDownloadFileAsync(string localFilePath, HttpMethod method, string requestUrl, object? body = null)
|
||||
{
|
||||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, requestUrl);
|
||||
if (body != null)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
||||
httpRequestMessage.Content = content;
|
||||
}
|
||||
using (var response = await httpClient.SendAsync(httpRequestMessage))
|
||||
{
|
||||
response.EnsureSuccessStatusCode(); // 确保请求成功
|
||||
|
||||
// 获取内容头中的文件名(如果需要的话)
|
||||
//var contentDisposition = ContentDispositionHeaderValue.Parse(response.Content.Headers.ContentDisposition.ToString());
|
||||
//string filename = contentDisposition.FileName.Trim('"'); // 去除引号
|
||||
var filename = string.Empty;
|
||||
// 如果提供的本地文件路径没有文件名,则使用从响应头中获取的文件名
|
||||
if (Path.GetFileName(localFilePath) == string.Empty)
|
||||
{
|
||||
localFilePath = Path.Combine(Path.GetDirectoryName(localFilePath), filename);
|
||||
}
|
||||
// 写入文件
|
||||
using (var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
|
||||
{
|
||||
await response.Content.CopyToAsync(fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post方式导入Excel文件
|
||||
/// </summary>
|
||||
/// <param name="localFilePath">文件保存的路径</param>
|
||||
/// <param name="method"></param>
|
||||
/// <param name="requestUrl"></param>
|
||||
/// <param name="body"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<T> PostImportFileAsync<T>(string localFilePath, HttpMethod method, string requestUrl, string userName, string deviceType)
|
||||
{
|
||||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, requestUrl);
|
||||
|
||||
using (var content = new MultipartFormDataContent())
|
||||
{
|
||||
var fileContent = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
|
||||
fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
|
||||
content.Add(fileContent, "excelFile", Path.GetFileName(localFilePath));
|
||||
|
||||
var userNameContent = new StringContent(userName);
|
||||
userNameContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
|
||||
content.Add(userNameContent, "userName");
|
||||
|
||||
var deviceTypeContent = new StringContent(deviceType);
|
||||
deviceTypeContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
|
||||
content.Add(deviceTypeContent, "deviceType");
|
||||
|
||||
httpRequestMessage.Content = content;
|
||||
using (var response = await httpClient.SendAsync(httpRequestMessage))
|
||||
{
|
||||
response.EnsureSuccessStatusCode(); // 确保请求成功
|
||||
var filename = string.Empty;
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return default(T);
|
||||
|
||||
var con = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<T>(con) ?? default(T); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static T GetDataFromHttp<T>(string url, object dataObj, string httpMethod, bool isSaveLog = false)
|
||||
{
|
||||
Guid guid = Guid.NewGuid();
|
||||
var data = JsonConvert.SerializeObject(dataObj);
|
||||
try
|
||||
{
|
||||
if (isSaveLog)
|
||||
Logs.Write($"【{guid}】开始请求调用接口 url:{url} 请求方式:{httpMethod} 数据:{data}", LogsType.Api);
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = httpMethod;
|
||||
request.ContentType = "application/json";
|
||||
request.Timeout = 10000;
|
||||
|
||||
if (!string.IsNullOrEmpty(data))
|
||||
{
|
||||
string strContent = data; //参数data
|
||||
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
|
||||
{
|
||||
dataStream.Write(strContent);
|
||||
dataStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
string encoding = response.ContentEncoding;
|
||||
if (encoding == null || encoding.Length < 1)
|
||||
{
|
||||
encoding = "UTF-8"; //默认编码
|
||||
}
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
||||
string retString = reader.ReadToEnd();
|
||||
if (isSaveLog)
|
||||
Logs.Write($"【{guid}】请求调用接口结束 返回数据为{retString}", LogsType.Api);
|
||||
return JsonConvert.DeserializeObject<T>(retString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Write($"【{guid}】请求调用遇到异常 异常信息为{ex.Message}");
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user