first commit

This commit is contained in:
2026-09-02 16:31:50 +08:00
commit a461727193
911 changed files with 692450 additions and 0 deletions
@@ -0,0 +1,387 @@
using JSMachine.DCS.Infrastructure;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace JSMachine.WMS.Infrastructure.Helper
{
public static class HttpRequestHelper
{
/// <summary>
/// 字典方式请求,支持表单数据
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="dic"></param>
/// <param name="postFileParam"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public async static Task<string> RequestByDic(string url,
Method method,
Dictionary<string, object> dic = null,
List<PostFileParam> postFileParam = null,
int timeOut = 5000)
{
RestClient client = new(url)
{
Timeout = timeOut
};
RestRequest request = new(method);
dic?.Select(p => p.Key)
.ToList()
.ForEach(key => request.AddParameter(key, dic[key]));
postFileParam?.ForEach(p =>
{
request.AddFile(p.RemoteParamName, p.FileBytes, p.FileName);
});
IRestResponse response = await client.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK && response.ErrorException == null)
return response.Content;
if (response.ErrorException != null)
{
LogHelper.Error($"HttpRequstHandling.PostFile--{response.ErrorException.Message}");
LogHelper.Error("当前请求url:" + url);
}
return null;
}
/// <summary>
/// JSON参数请求
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="json"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public async static Task<string> RequestByJson(string url, Method method, string json, int timeOut = 5000)
{
RestClient client = new(url)
{
Timeout = timeOut
};
RestRequest request = new(method);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK && response.ErrorException == null)
return response.Content;
if (response.ErrorException != null)
{
LogHelper.Error($"HttpRequstHandling.PostFile--{response.ErrorException.Message}");
LogHelper.Error("当前请求url:" + url);
}
return null;
}
/// <summary>
/// JSON参数请求
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="json"></param>
/// <param name="token"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public async static Task<string> RequestByJson(string url, Method method, string json, string token, int timeOut = 5000)
{
RestClient client = new(url)
{
Timeout = timeOut
};
RestRequest request = new(method);
request.AddHeader("Authorization", $"Bearer {token}");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = await client.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK && response.ErrorException == null)
return response.Content;
if (response.ErrorException != null)
{
LogHelper.Error($"HttpRequstHandling.PostFile--{response.ErrorException.Message}");
LogHelper.Error("当前请求url:" + url);
}
return null;
}
/// <summary>
/// 无参请求返回二进制(用于下载文件)
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public async static Task<byte[]> RequestRturnRawAsync(string url, Method method, int timeOut = 5000)
{
if (string.IsNullOrEmpty(url))
return null;
RestClient client = new(url)
{
Timeout = timeOut
};
RestRequest request = new(method);
request.AlwaysMultipartFormData = true;
IRestResponse response = await client.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK && response.ErrorException == null)
return response.RawBytes;
if (response.ErrorException != null)
{
LogHelper.Error($"HttpRequstHandling.PostFile--{response.ErrorException.Message}");
LogHelper.Error("当前请求url:" + url);
}
return null;
}
/// <summary>
/// 无参请求返回二进制(用于下载文件)
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static byte[] RequestRturnRaw(string url, Method method, int timeOut = 5000)
{
if (string.IsNullOrEmpty(url))
return null;
RestClient client = new(url)
{
Timeout = timeOut
};
RestRequest request = new(method);
request.AlwaysMultipartFormData = true;
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK && response.ErrorException == null)
return response.RawBytes;
if (response.ErrorException != null)
{
LogHelper.Error($"HttpRequstHandling.PostFile--{response.ErrorException.Message}");
LogHelper.Error("当前请求url:" + url);
}
return null;
}
/// <summary>
/// 获取请求的数据
/// </summary>
private static string GetResponseString(WebResponse webResponse)
{
using (var stream = webResponse.GetResponseStream())
{
if (stream == null) return string.Empty;
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// 以GET方式请求HTTP地址并获取返回
/// </summary>
public async static Task<string> Get(string url, Dictionary<string, string> headers = null,
CookieCollection cookies = null)
{
HttpWebRequest request = null;
WebResponse response = null;
try
{
ServicePointManager.DefaultConnectionLimit = 200;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
//对服务端证书进行有效性校验
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
response = request.GetResponse();
return GetResponseString(response);
}
catch (System.Threading.ThreadAbortException e)
{
System.Threading.Thread.ResetAbort();
throw new Exception(e.Message);
}
finally
{
request?.Abort();
response?.Dispose();
}
}
/// <summary>
/// 以POST方式请求HTTP地址并获取返回
/// </summary>
/// <param name="url">HTTP地址</param>
/// <param name="parameters">POST的键值对参数</param>
/// <param name="headers"></param>
/// <param name="cookies"></param>
/// <returns></returns>
public async static Task<string> Post(string url, Dictionary<string, string> parameters,
Dictionary<string, string> headers = null, CookieCollection cookies = null)
{
return await Post(url, GetPostData(parameters), null, headers, cookies);
}
/// <summary>
/// 以POST方式请求HTTP地址并获取返回
/// </summary>
/// <param name="url">HTTP地址</param>
/// <param name="content">POST的内容</param>
/// <param name="contentType"></param>
/// <param name="headers"></param>
/// <param name="cookies"></param>
/// <returns></returns>
public async static Task<string> Post(string url, string content, string contentType = null,
Dictionary<string, string> headers = null, CookieCollection cookies = null)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
ServicePointManager.DefaultConnectionLimit = 200;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
//对服务端证书进行有效性校验
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Timeout = 30000;
request.ContentType = contentType ?? "application/x-www-form-urlencoded";
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
//发送POST数据
var data = Encoding.UTF8.GetBytes(content ?? string.Empty);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
response = (HttpWebResponse)request.GetResponse();
return GetResponseString(response);
}
catch (System.Threading.ThreadAbortException e)
{
System.Threading.Thread.ResetAbort();
throw new Exception(e.Message);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
request?.Abort();
response?.Dispose();
}
}
/// <summary>
/// 字典转化为post数据
/// </summary>
/// <param name="dictionary"></param>
/// <returns></returns>
private static string GetPostData(Dictionary<string, string> dictionary)
{
if (dictionary == null || dictionary.Count == 0) return string.Empty;
var sb = new StringBuilder();
var keys = dictionary.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
{
if (i > 0)
{
sb.Append("&");
}
sb.AppendFormat("{0}={1}", keys[i], HttpUtility.UrlEncode(dictionary[keys[i]]));
}
return sb.ToString();
}
/// <summary>
/// 验证证书
/// </summary>
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors errors)
{
return true;
}
}
public class PostFileParam
{
public string RemoteParamName { get; set; }
public string FileName { get; set; }
public byte[] FileBytes { get; set; }
}
}
@@ -0,0 +1,121 @@
using NLog;
using NLog.Web;
using System;
namespace JSMachine.WMS.Infrastructure.Helper
{
public static class LogHelper
{
private static ILogger logger = GetLogger();
private static ILogger GetLogger()
{
ILogger logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
return logger;
}
/// <summary>
/// 调试
/// </summary>
/// <param name="debug"></param>
public static void Debug(string debug)
{
logger.Debug(debug);
}
public static void Debug(Exception ex)
{
logger.Debug(ex);
}
public static void Debug(string debug,Exception ex)
{
logger.Debug(ex,debug);
}
/// <summary>
/// 信息
/// </summary>
/// <param name="info"></param>
public static void Info(string info)
{
logger.Info(info);
}
public static void Info(Exception ex)
{
logger.Info(ex);
}
public static void Info(string info,Exception ex)
{
logger.Info(ex,info);
}
/// <summary>
/// 警告
/// </summary>
/// <param name="warn"></param>
public static void Warn(string warn)
{
logger.Warn(warn);
}
public static void Warn(Exception ex)
{
logger.Warn(ex);
}
public static void Warn(string warn,Exception ex)
{
logger.Warn(ex,warn);
}
/// <summary>
/// 错误
/// </summary>
/// <param name="error"></param>
public static void Error(string error)
{
logger.Error(error);
}
public static void Error(Exception error)
{
logger.Error(error);
}
public static void Error(string error, Exception ex)
{
logger.Error(ex, error);
}
/// <summary>
/// 严重错误
/// </summary>
/// <param name="fatale"></param>
public static void Fatal(string fatal)
{
logger.Fatal(fatal);
}
public static void Fatal(Exception ex)
{
logger.Fatal(ex);
}
public static void Fatal(string fatal,Exception ex)
{
logger.Fatal(ex,fatal);
}
/// <summary>
/// 跟踪
/// </summary>
/// <param name="trace"></param>
public static void Trace(string trace)
{
logger.Trace(trace);
}
public static void Trace(Exception ex)
{
logger.Trace(ex);
}
public static void Trace(string trace,Exception ex)
{
logger.Trace(ex,trace);
}
}
}
@@ -0,0 +1,61 @@
using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
/// <summary>
/// Md5加密辅助类
/// </summary>
public static class Md5EncryptionHelper
{
/// <summary>
/// 16位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt16(string str)
{
MD5 md5 = MD5.Create();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="password"></param>
/// <param name="lower">是否小写输出</param>
/// <returns></returns>
public static string MD5Encrypt32(string str, bool lower)
{
string cl = str;
string pwd = string.Empty;
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = lower ? pwd + s[i].ToString("x").PadLeft(2, '0') : pwd + s[i].ToString("X").PadLeft(2, '0');
}
return pwd;
}
public static string MD5Encrypt64(string str)
{
string cl = str;
//string pwd = "";
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
return Convert.ToBase64String(s);
}
}
}
@@ -0,0 +1,30 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
/// <summary>
/// 对象拷贝帮助类
/// </summary>
public static class ObjectCloneHelper
{
/// <summary>
/// 深拷贝
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T DeepClone<T>(T t) where T : class
{
if (t == null)
return null;
string json = JsonConvert.SerializeObject(t);
return JsonConvert.DeserializeObject<T>(json);
}
}
}
@@ -0,0 +1,73 @@
using JSMachine.DCS.Infrastructure;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
public static class ProcessHelper
{
//调用浏览器打开指定的网址
public static bool OpenBrowserUrl(string url)
{
try
{
//优先用IE,不行再尝试用谷歌
if (OpenIe(url))
return true;
// 64位注册表路径
string openKey = @"SOFTWARE\Wow6432Node\Google\Chrome";
if (IntPtr.Size == 4)
{
// 32位注册表路径
openKey = @"SOFTWARE\Google\Chrome";
}
RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey);
// 谷歌浏览器就用谷歌打开,没找到就用系统默认的浏览器
// 谷歌卸载了,注册表还没有清空,程序会返回一个"系统找不到指定的文件。"的bug
if (appPath != null)
{
Process result = Process.Start("chrome.exe", url);
return result != null;
}
else
{
Process result = Process.Start("chrome.exe", url);
return result != null;
}
}
catch (Exception ex)
{
LogHelper.Error(ex.Message);
}
return false;
}
/// <summary>
/// 用IE打开浏览器
/// </summary>
/// <param name="url"></param>
public static bool OpenIe(string url)
{
Process process = new();
process.StartInfo.FileName = "iexplore.exe"; //IE浏览器,可以更换
process.StartInfo.Arguments = url;
try
{
process.Start();
return true;
}
catch (Exception ex)
{
LogHelper.Error($"使用IE打开网址错误:{ex.Message}");
}
return false;
}
}
}
@@ -0,0 +1,20 @@
using BeetleX.BNR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
/// <summary>
/// 序列号生成帮助类
/// </summary>
public static class SerialNumHelper
{
public async static Task<string> GenerateSerialNum()
{
return await BNRFactory.Default.Create("[CN:WH][D:yyyyMMdd][N:[D:yyyyMMdd]/000000]");
}
}
}
@@ -0,0 +1,104 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace SuperDogTest
{
/// <summary>
/// 超级狗辅助类
/// </summary>
public static class SuperDogHelper
{
/// <summary>
/// 加载dll
/// </summary>
/// <param name="DllName"></param>
/// <returns></returns>
[DllImport("kernel32.dll")]
public extern static IntPtr LoadLibrary(string DllName);
/// <summary>
/// 释放dll
/// </summary>
/// <param name="hModule"></param>
/// <returns></returns>
[DllImport("kernel32")]
public extern static bool FreeLibrary(IntPtr hModule);
/// <summary>
/// 获取dll中的方法句柄
/// </summary>
/// <param name="hModule"></param>
/// <param name="ProcName"></param>
/// <returns></returns>
[DllImport("kernel32.dll")]
public extern static IntPtr GetProcAddress(IntPtr hModule, string ProcName);
/// <summary>
/// 查找加密狗委托
/// </summary>
/// <param name="Count"></param>
/// <returns></returns>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int VikeyFindType(ref int Count);
/// <summary>
/// 读取加密狗数据委托
/// </summary>
/// <param name="Index"></param>
/// <param name="Addr"></param>
/// <param name="Length"></param>
/// <param name="buffer"></param>
/// <returns></returns>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int VikeyReadDataType(int Index, int Addr, int Length, StringBuilder buffer);
/// <summary>
/// 管理员登录加密狗委托
/// </summary>
/// <param name="Index"></param>
/// <param name="AdminPassword"></param>
/// <returns></returns>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int VikeyAdminLoginType(int Index, string AdminPassword);
/// <summary>
/// 加密狗句柄
/// </summary>
private static IntPtr VikeyHandle;
/// <summary>
/// 查找加密狗
/// </summary>
public static VikeyFindType VikeyFind { get; private set; }
/// <summary>
/// 加密狗管理员登录
/// </summary>
public static VikeyAdminLoginType VikeyAdminLogin { get; private set; }
/// <summary>
/// 加密狗读取数据
/// </summary>
public static VikeyReadDataType VikeyReadData { get; private set; }
static SuperDogHelper()
{
if (IntPtr.Size == 4)
{
VikeyHandle = LoadLibrary("ViKey32.dll");
}
else if (IntPtr.Size == 8)
{
VikeyHandle = LoadLibrary("ViKey64.dll");
}
else
return;
VikeyFind = Marshal.GetDelegateForFunctionPointer<VikeyFindType>(GetProcAddress(VikeyHandle, "VikeyFind"));
VikeyAdminLogin = Marshal.GetDelegateForFunctionPointer<VikeyAdminLoginType>(GetProcAddress(VikeyHandle, "VikeyAdminLogin"));
VikeyReadData = Marshal.GetDelegateForFunctionPointer<VikeyReadDataType>(GetProcAddress(VikeyHandle, "VikeyReadData"));
}
}
}
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
/// <summary>
/// 值转换服务类
/// </summary>
public static class ValueConvertHelper
{
/// <summary>
/// 将一个int32类型转换为一个ushort数组,高位在前面
/// </summary>
/// <returns></returns>
public static ushort[] IntToUshorts(int value)
{
string str = Convert.ToString(value, 2).PadLeft(32, '0');
string high = str.Substring(0, 16);
string low = str.Substring(16);
return new ushort[2] { Convert.ToUInt16(high, 2), Convert.ToUInt16(low, 2) };
}
public static short[] IntToShorts(int value)
{
string str = Convert.ToString(value, 2).PadLeft(32, '0');
string high = str.Substring(0, 16);
string low = str.Substring(16);
return new short[2] { Convert.ToInt16(high, 2), Convert.ToInt16(low, 2) };
}
/// <summary>
/// 将两个ushort转换为一个int,高位在前
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public static int UshortsToInt(ushort[] values)
{
return values.First() << 16 | values.Last();
}
/// <summary>
/// 将两个short转换为一个int,高位在前
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public static int ShortsToInt(short[] values)
{
return values.First() << 16 | values.Last();
}
/// <summary>
/// 调整值的后一位强制转换成无符号
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public static int adjShortsToInt(short[] values)
{
ushort value = (ushort)(values.Last());
return values.First() << 16 | value;
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace JSMachine.WMS.Infrastructure.Helper
{
public static class XmlHelper
{
public static T XmlToModel<T>(string xml) where T : class, new()
{
try
{
xml = Regex.Replace(xml, @"<\?xml*.*?>", "", RegexOptions.IgnoreCase);
XmlSerializer xmlSer = new(typeof(T));
using (StringReader xmlReader = new(xml))
{
return (T)xmlSer.Deserialize(xmlReader);
}
}
catch (Exception ex)
{
return null;
//throw new Exception("将XML字符串转换为实体异常", ex); ;
}
}
public static string ModelToXml<T>(T obj) where T : class, new()
{
try
{
MemoryStream stream = new();
XmlSerializer xmlSer = new(typeof(T));
xmlSer.Serialize(stream, obj);
stream.Position = 0;
StreamReader sr = new(stream);
return sr.ReadToEnd();
}
catch (Exception ex)
{
return null;
//throw new Exception("将实体对象转换成XML异常", ex);
}
}
}
}