一厂MES,含注塑,喷涂,冲孔
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
10 KiB

2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using MESClassLibrary.BLL.Log;
namespace MESClassLibrary
{
public class CommonTools
{
public static string MD5encryption(string password)
{
try
{
if (!string.IsNullOrWhiteSpace(password))
{
byte[] result = Encoding.Default.GetBytes(password); //tbPass为输入密码的文本框
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
return BitConverter.ToString(output).Replace("-", "");
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 算法偏移量
/// </summary>
const string m_IV = "12345678";
/// <summary>
/// 功能描述:根据输入的密钥生成8位密钥
/// 作  者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:25:26
/// </summary>
/// <param name="strkey">strkey</param>
/// <returns>8位密钥</returns>
private static string GetKey(string strkey)
{
if (string.IsNullOrEmpty(strkey))
{
strkey = "InfoColl";
}
if (strkey.Length % 8 == 0)
{
return strkey;
}
else
{
return GetKey(strkey + "0");
}
}
/// <summary>
/// 功能描述:加密字符串
/// 作  者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:18:31
/// 任务编号:
/// </summary>
/// <param name="strSourceString">原字符串</param>
/// <param name="strKey">密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string strSourceString, string strKey)
{
strKey = GetKey(strKey);
byte[] btKey = Encoding.UTF8.GetBytes(strKey);
byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
try
{
byte[] inData = Encoding.UTF8.GetBytes(strSourceString);
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
catch
{
return strSourceString;
}
}
}
/// <summary>
/// 功能描述:解密字符串
/// 作  者: 爱给模板网 2gei.cn
/// 创建日期:2015-07-20 17:18:49
/// 任务编号:
/// </summary>
/// <param name="strEncryptedString">原字符串</param>
/// <param name="strKey">密钥</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string strEncryptedString, string strKey)
{
strKey = GetKey(strKey);
byte[] btKey = Encoding.UTF8.GetBytes(strKey);
byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
try
{
byte[] inData = Convert.FromBase64String(strEncryptedString);
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Encoding.UTF8.GetString(ms.ToArray());
}
catch
{
return strEncryptedString;
}
}
}
/// <summary>
/// DataTable转List
/// lx 2017-06-27
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static IList<T> ConvertTo<T>(DataTable table)
{
if (table == null)
{
return null;
}
List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
{
rows.Add(row);
}
return ConvertTo<T>(rows);
}
public static IList<T> ConvertTo<T>(IList<DataRow> rows)
{
IList<T> list = null;
if (rows != null)
{
list = new List<T>();
foreach (DataRow row in rows)
{
T item = CreateItem<T>(row);
list.Add(item);
}
}
return list;
}
public static T CreateItem<T>(DataRow row)
{
T obj = default(T);
if (row != null)
{
obj = Activator.CreateInstance<T>();
foreach (DataColumn column in row.Table.Columns)
{
PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
try
{
object value = row[column.ColumnName];
prop.SetValue(obj, value, null);
}
catch
{ //You can log something here
//throw;
}
}
}
return obj;
}
public static D Mapper<D, S>(S s)
{
D d = Activator.CreateInstance<D>();
try
{
var Types = s.GetType();//获得类型
var Typed = typeof(D);
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
foreach (PropertyInfo dp in Typed.GetProperties())
{
if (dp.Name == sp.Name)//判断属性名是否相同
{
dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return d;
}
public static DateTime StrConvertToDT(string str)
{
DateTime res = DateTime.MinValue;
try
{
res = Convert.ToDateTime(str);
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
}
return res;
}
public static int StrToInt(string str)
{
int res = -1;
try
{
res = Convert.ToInt32(str);
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
}
return res;
}
public static decimal StrToDecimal(string str)
{
decimal res = -1;
try
{
res = Convert.ToDecimal(str);
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
}
return res;
}
public static TimeSpan strToTime(string str)
{
TimeSpan res = TimeSpan.MaxValue;
try
{
TimeSpan.TryParse(str, out res);
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
//LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
}
return res;
}
public static DateTime strToDateTime(string str)
{
DateTime res = DateTime.MaxValue;
try
{
DateTime.TryParse(str, out res);
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
//LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
}
return res;
}
public static DataTable ListToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
//获取类型
Type colType = pi.PropertyType;
//当类型为Nullable<>时
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
result.Columns.Add(pi.Name, colType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
}
}