using System.Data;
using System.Dynamic;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
namespace Gs.Toolbox;
public static class UtilityHelper
{
///
/// 读取登录用户信息:返回用户Code,用户Guid,组织ID
///
///
///
public static (string?, string?, string?) GetUserGuidAndOrgGuid(
IHttpContextAccessor _http)
{
try
{
string _token = _http.HttpContext.Request.Headers["token"];
var _userGuid = _token;
var _sb = new StringBuilder();
_sb.Append("select ACCOUNT as t1,'' as t3 from SYS_USER where guid='" + _userGuid + "' ");
var dset = new DataSet();
dset = DbHelperSQL.Query(_sb.ToString());
if (dset != null && dset.Tables[0].Rows.Count > 0)
{
DataRow _row = dset.Tables[0].Rows[0];
var _userCode = _row["t1"].ToString();
var _orgFids = _row["t3"].ToString();
return (_userCode, _userGuid, _orgFids);
}
return (null, null, null);
}
catch (Exception ex)
{
LogHelper.Debug("GetUserGuidAndOrgGuid:", ex.Message);
}
return (null, null, null);
}
///
/// 32位MD5加密
///
///
///
public static string MD5Encrypt32(string password)
{
var cl = password;
var pwd = "";
var md5 = MD5.Create();
var s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
for (var i = 0; i < s.Length; i++) pwd = pwd + s[i].ToString("X");
return pwd;
}
///
/// table转DynamicList
///
///
///
public static List TableToDynamicList(this DataTable table)
{
var list = new List();
// 遍历DataTable中的每一行
foreach (DataRow row in table.Rows)
{
dynamic item = new ExpandoObject();
// 遍历每一列,并添加到dynamic对象中
foreach (DataColumn column in table.Columns)
((IDictionary)item)[column.ColumnName] =
row[column];
list.Add(item);
}
return list;
}
///
/// row转DynamicList
///
///
///
public static dynamic RowToDynamic(this DataRow row)
{
dynamic expando = new ExpandoObject();
if (row != null)
foreach (DataColumn column in row.Table.Columns)
{
var value = row[column];
// 如果值为DBNull.Value,则转换为null
if (value == DBNull.Value) value = null;
((IDictionary)expando)[column.ColumnName] =
value;
}
return expando;
}
public static int? ToInt(string strV)
{
if (string.IsNullOrEmpty(strV))
return null;
return int.Parse(strV);
}
///
///
/// 操作用户
/// 关联主键,有时候会为null
/// 表名,表描述
/// 行为
///
public static StringBuilder BuildLog(string _userGuid, string _abtGuid,
string _table, string _logTxt)
{
var sb = new StringBuilder();
sb.Append(" exec prc_log_create '" + _userGuid + "','" + _abtGuid +
"','" + _table + "','" + _logTxt + "' ");
return sb;
}
///
/// 判断是不是guid
///
///
///
public static bool CheckGuid(Guid? guid)
{
if (guid == Guid.Empty)
return false;
if (guid == null) return false;
return true;
}
///
/// 判断是不是guid
///
///
///
public static bool CheckGuid(string? guid)
{
if (guid == Guid.Empty.ToString())
return false;
if (string.IsNullOrEmpty(guid))
return false;
if (guid == null) return false;
return true;
}
}