提交rbac

提交设置右键错位的bug
This commit is contained in:
2025-04-08 15:15:02 +08:00
parent ab38ee029a
commit 9f7c6206ca
139 changed files with 27868 additions and 117 deletions

View File

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
public static class DESHelper
{
#region DES加密解密
/// <summary>
/// 默认密钥。
/// </summary>
private const string DESENCRYPT_KEY = "hsdjxlzf";
/// <summary>
/// DES加密使用自定义密钥。
/// </summary>
/// <param name="text">待加密的明文</param>
/// <param name="key">8位字符的密钥字符串</param>
/// <returns></returns>
public static string DESEncrypt(this string text, string key)
{
if (key.Length != 8)
{
key = DESENCRYPT_KEY;
}
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(text);
byte[] a = ASCIIEncoding.ASCII.GetBytes(key);
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);//将第一个参数转换为十六进制数,长度为2,不足前面补0
}
return ret.ToString();
}
/// <summary>
/// DES解密使用自定义密钥。
/// </summary>
/// <param name="cyphertext">待解密的秘文</param>
/// <param name="key">必须是8位字符的密钥字符串(不能有特殊字符)</param>
/// <returns></returns>
public static string DESDecrypt(this string cyphertext, string key)
{
if (key.Length != 8)
{
key = DESENCRYPT_KEY;
}
if (string.IsNullOrEmpty(cyphertext))
return string.Empty;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[cyphertext.Length / 2];
for (int x = 0; x < cyphertext.Length / 2; x++)
{
int i = (Convert.ToInt32(cyphertext.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.GetEncoding("UTF-8").GetString(ms.ToArray());
}
/// <summary>
/// DES加密使用默认密钥。
/// </summary>
/// <param name="text">待加密的明文</param>
/// <returns></returns>
public static string DESEncrypt(this string text)
{
return DESEncrypt(text, DESENCRYPT_KEY);
}
/// <summary>
/// DES解密使用默认密钥。
/// </summary>
/// <param name="cyphertext">待解密的秘文</param>
/// <returns></returns>
public static string DESDecrypt(this string cyphertext)
{
return DESDecrypt(cyphertext, DESENCRYPT_KEY);
}
#endregion
}
}

View File

@ -0,0 +1,40 @@
#if NETFRAMEWORK || WINDOWS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DH.RBAC.Utility.Other
{
public class FormHelper
{
public static Form subForm;
/// <summary>
/// 显示子Form
/// </summary>
/// <param name="form"></param>
public static void ShowSubForm(Form form,UserControl userControl)
{
//关闭之前的
try { if (subForm != null) subForm.Close(); } catch { }
//打开现在的
subForm = form;
form.Show(userControl);
}
public static void ShowSubForm(Form form)
{
//关闭之前的
try { if (subForm != null) subForm.Close(); } catch { }
//打开现在的
subForm = form;
form.Show();
}
}
}
#endif

View File

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
namespace DH.RBAC.Utility.Other
{
public static class JsonHelper
{
/// <summary>
/// 对象序列化成JSON字符串。
/// </summary>
/// <param name="obj">序列化对象</param>
/// <param name="ignoreProperties">设置需要忽略的属性</param>
/// <returns></returns>
public static string ToJson(this object obj)
{
if (obj == null)
return string.Empty;
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
return JsonConvert.SerializeObject(obj, timeConverter);
}
/// <summary>
/// JSON字符串序列化成对象。
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">JSON字符串</param>
/// <returns></returns>
public static T ToObject<T>(this string json)
{
//var setting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
return json == null ? default(T) : JsonConvert.DeserializeObject<T>(json);//, setting);
}
/// <summary>
/// JSON字符串序列化成集合。
/// </summary>
/// <typeparam name="T">集合类型</typeparam>
/// <param name="json">JSON字符串</param>
/// <returns></returns>
public static List<T> ToList<T>(this string json)
{
//var setting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
return json == null ? null : JsonConvert.DeserializeObject<List<T>>(json);//, setting);
}
/// <summary>
/// JSON字符串序列化成DataTable。
/// </summary>
/// <param name="json">JSON字符串</param>
/// <returns></returns>
public static DataTable ToTable(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject<DataTable>(json);
}
/// <summary>
/// 将JSON字符串反序列化成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="baseEntity"></param>
/// <param name="strJson"></param>
/// <returns></returns>
public static T Json2Obj<T>(T baseEntity, string strJson)
{
return JsonConvert.DeserializeAnonymousType(strJson, baseEntity);
}
/// <summary>
/// 将对象转换层JSON字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static string Obj2Json<T>(T data)
{
return JsonConvert.SerializeObject(data);
}
public static List<T> JsonToList<T>(string strJson)
{
T[] list = JsonConvert.DeserializeObject<T[]>(strJson);
return list.ToList();
}
public static T Json2Obj<T>(string strJson)
{
return JsonConvert.DeserializeObject<T>(strJson);
}
public static DataTable ToDataTable(this string json)
{
return json.ToTable();
}
public static string FormatJson(this string json)
{
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(json);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return json;
}
}
}
}

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
public static class MD5Helper
{
/// <summary>
/// 字符串MD5加密。
/// </summary>
/// <param name="strOri">需要加密的字符串</param>
/// <returns></returns>
public static string md5(this string text)
{
return md5(text, Encoding.Default);
}
public static string MD5(this string text)
{
return MD5(text, Encoding.Default);
}
/// <summary>
/// 字符串MD5加密。
/// </summary>
/// <param name="strOri">需要加密的字符串</param>
/// <returns></returns>
public static string md5(this string text, Encoding encoder)
{
// Create a new instance of the MD5CryptoServiceProvider object.
System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(encoder.GetBytes(text));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString().ToLower();
}
public static string MD5(this string text, Encoding encoder)
{
return md5(text, encoder).ToUpper();
}
/// <summary>
/// 文件流MD5加密。
/// </summary>
/// <param name="stream">需要加密的文件流</param>
/// <returns></returns>
public static string md5(this Stream stream)
{
MD5 md5serv = MD5CryptoServiceProvider.Create();
byte[] buffer = md5serv.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
foreach (byte var in buffer)
{
sb.Append(var.ToString("x2"));
}
return sb.ToString().ToLower();
}
public static string MD5(this Stream stream)
{
return md5(stream).ToUpper();
}
#region MD5加密
/// <summary>
/// 字符串MD5加密。
/// </summary>
/// <param name="strOri">需要加密的字符串</param>
/// <returns></returns>
public static string MD5Encrypt(this string text)
{
// Create a new instance of the MD5CryptoServiceProvider object.
System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(text));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
/// <summary>
/// 文件流MD5加密。
/// </summary>
/// <param name="stream">需要加密的文件流</param>
/// <returns></returns>
public static string MD5Encrypt(this Stream stream)
{
MD5 md5serv = MD5CryptoServiceProvider.Create();
byte[] buffer = md5serv.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
foreach (byte var in buffer)
{
sb.Append(var.ToString("x2"));
}
return sb.ToString();
}
#endregion
}
}

View File

@ -0,0 +1,45 @@
#if NETFRAMEWORK || WINDOWS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DH.RBAC.Utility.Other
{
public class PanelScrollHelper
{
/// <summary>
/// 初始化panel
/// </summary>
/// <param name="panel"></param>
public static void InitializePanelScroll(Panel panel)
{
panel.Click += (obj, arg) => { panel.Select(); };
InitializePanelScroll(panel, panel);
}
/// <summary>
/// 递归初始化panel的内部个容器和控件
/// </summary>
/// <param name="panel1"></param>
/// <param name="panel2"></param>
private static void InitializePanelScroll(Control container, Control panelRoot)
{
foreach (Control control in container.Controls)
{
if (control is Panel || control is GroupBox || control is SplitContainer || control is TabControl || control is UserControl)
{
control.Click += (obj, arg) => { panelRoot.Select(); };
InitializePanelScroll(control, panelRoot);
}
else if (control is Label)
{
control.Click += (obj, arg) => { panelRoot.Select(); };
}
}
}
}
}
#endif

View File

@ -0,0 +1,113 @@
#if NETFRAMEWORK || WINDOWS
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DH.RBAC.Utility.Other
{
public class ScreenUtils
{
#region Dll引用
[DllImport("User32.dll", EntryPoint = "GetDC")]
private extern static IntPtr GetDC(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "ReleaseDC")]
private extern static int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("User32.dll")]
public static extern int GetSystemMetrics(int hWnd);
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In] System.Drawing.Point pt, [In] uint dwFlags);
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
#endregion
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
const int SM_CXSCREEN = 0;
const int SM_CYSCREEN = 1;
/// <summary>
/// 获取DPI缩放比例
/// </summary>
/// <param name="dpiscalex"></param>
/// <param name="dpiscaley"></param>
public static void GetDPIScale(ref float dpiscalex, ref float dpiscaley)
{
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
IntPtr hdc = GetDC(IntPtr.Zero);
int w = GetDeviceCaps(hdc, DESKTOPHORZRES);
int h = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
dpiscalex = (float)w / x;
dpiscaley = (float)h / y;
}
/// <summary>
/// 获取分辨率
/// </summary>
/// <param name="width">宽</param>
/// <param name="height">高</param>
private static void GetResolving(ref int width, ref int height)
{
IntPtr hdc = GetDC(IntPtr.Zero);
width = GetDeviceCaps(hdc, DESKTOPHORZRES);
height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
}
public static int ScreenCount
{
get
{
return Screen.AllScreens.Length;
}
}
public static ScreenSize GetScreenSize(int index)
{
if (index > (ScreenCount - 1))
throw new Exception("索引异常");
Screen screen = Screen.AllScreens[index];
uint width = 0;
uint height = 0;
GetDpi(screen, DpiType.Effective, out width, out height);
double scale = width / 96.0;
return new ScreenSize { Left = (int)(screen.Bounds.X / scale), Top = screen.Bounds.Y, Width = (int)(screen.Bounds.Width / scale), Height = (int)(screen.Bounds.Height / scale), Scale = scale };
}
public static void GetDpi(Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
{
var pnt = new System.Drawing.Point(screen.Bounds.Left + 800, screen.Bounds.Top + 600);
var mon = MonitorFromPoint(pnt, 2);
GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
}
}
public enum DpiType
{
Effective = 0,
Angular = 1,
Raw = 2,
}
public class ScreenSize
{
public int Left { get; set; }
public int Top { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public double Scale { get; set; }
}
}
#endif

View File

@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
public class SnowFlakeHelper
{
private static readonly object _obj = new object();
private static SnowFlakeHelper Instance;
private SnowFlakeHelper()
{
}
/// <summary>
/// 获取雪花帮助类对象
/// </summary>
/// <param name="datacenterId">数据中心ID</param>
/// <param name="workerId">工作机器Id</param>
/// <returns></returns>
public static SnowFlakeHelper GetSnowInstance(long datacenterId = 1, long workerId = 1)
{
//双if 加锁
if (Instance == null)
{
lock (_obj)
{
if (Instance == null)
{
Instance = new SnowFlakeHelper(datacenterId, workerId);
}
}
}
return Instance;
}
// 开始时间截((new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc)-Jan1st1970).TotalMilliseconds)
private const long twepoch = 1577836800000L;
// 机器id所占的位数
private const int workerIdBits = 5;
// 数据标识id所占的位数
private const int datacenterIdBits = 5;
// 支持的最大机器id结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
private const long maxWorkerId = -1L ^ (-1L << workerIdBits);
// 支持的最大数据标识id结果是31
private const long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
// 序列在id中占的位数
private const int sequenceBits = 12;
// 数据标识id向左移17位(12+5)
private const int datacenterIdShift = sequenceBits + workerIdBits;
// 机器ID向左移12位
private const int workerIdShift = sequenceBits;
// 时间截向左移22位(5+5+12)
private const int timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
// 生成序列的掩码这里为4095 (0b111111111111=0xfff=4095)
private const long sequenceMask = -1L ^ (-1L << sequenceBits);
// 数据中心ID(0~31)
public long datacenterId { get; private set; }
// 工作机器ID(0~31)
public long workerId { get; private set; }
// 毫秒内序列(0~4095)
public long sequence { get; private set; }
// 上次生成ID的时间截
public long lastTimestamp { get; private set; }
/// <summary>
/// 雪花ID
/// </summary>
/// <param name="datacenterId">数据中心ID</param>
/// <param name="workerId">工作机器ID</param>
private SnowFlakeHelper(long datacenterId, long workerId)
{
if (datacenterId > maxDatacenterId || datacenterId < 0)
{
throw new Exception(string.Format("datacenter Id can't be greater than {0} or less than 0", maxDatacenterId));
}
if (workerId > maxWorkerId || workerId < 0)
{
throw new Exception(string.Format("worker Id can't be greater than {0} or less than 0", maxWorkerId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
this.sequence = 0L;
this.lastTimestamp = -1L;
}
/// <summary>
/// 获得下一个ID
/// </summary>
/// <returns></returns>
public long NextId()
{
lock (this)
{
long timestamp = GetCurrentTimestamp();
if (timestamp > lastTimestamp) //时间戳改变,毫秒内序列重置
{
sequence = 0L;
}
else if (timestamp == lastTimestamp) //如果是同一时间生成的,则进行毫秒内序列
{
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) //毫秒内序列溢出
{
timestamp = GetNextTimestamp(lastTimestamp); //阻塞到下一个毫秒,获得新的时间戳
}
}
else //当前时间小于上一次ID生成的时间戳证明系统时钟被回拨此时需要做回拨处理
{
sequence = (sequence + 1) & sequenceMask;
if (sequence > 0)
{
timestamp = lastTimestamp; //停留在最后一次时间戳上,等待系统时间追上后即完全度过了时钟回拨问题。
}
else //毫秒内序列溢出
{
timestamp = lastTimestamp + 1; //直接进位到下一个毫秒
}
//throw new Exception(string.Format("Clock moved backwards. Refusing to generate id for {0} milliseconds", lastTimestamp - timestamp));
}
lastTimestamp = timestamp; //上次生成ID的时间截
//移位并通过或运算拼到一起组成64位的ID
var id = ((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift)
| sequence;
return id;
}
}
/// <summary>
/// 解析雪花ID
/// </summary>
/// <returns></returns>
public static string AnalyzeId(long Id)
{
StringBuilder sb = new StringBuilder();
var timestamp = (Id >> timestampLeftShift);
var time = Jan1st1970.AddMilliseconds(timestamp + twepoch);
sb.Append(time.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss:fff"));
var datacenterId = (Id ^ (timestamp << timestampLeftShift)) >> datacenterIdShift;
sb.Append("_" + datacenterId);
var workerId = (Id ^ ((timestamp << timestampLeftShift) | (datacenterId << datacenterIdShift))) >> workerIdShift;
sb.Append("_" + workerId);
var sequence = Id & sequenceMask;
sb.Append("_" + sequence);
return sb.ToString();
}
/// <summary>
/// 阻塞到下一个毫秒,直到获得新的时间戳
/// </summary>
/// <param name="lastTimestamp">上次生成ID的时间截</param>
/// <returns>当前时间戳</returns>
private static long GetNextTimestamp(long lastTimestamp)
{
long timestamp = GetCurrentTimestamp();
while (timestamp <= lastTimestamp)
{
timestamp = GetCurrentTimestamp();
}
return timestamp;
}
/// <summary>
/// 获取当前时间戳
/// </summary>
/// <returns></returns>
private static long GetCurrentTimestamp()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
}

View File

@ -0,0 +1,316 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
/// <summary>
/// 字符串操作类
/// </summary>
public static class StringHelper
{
/// <summary>
/// 把字符串按照分隔符转换成 List
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="speater">分隔符</param>
/// <param name="toLower">是否转换为小写</param>
/// <returns></returns>
public static List<string> SplitToList(this string str, char speater = ',', bool toLower = false)
{
List<string> list = new List<string>();
string[] ss = str.Split(speater);
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s) && s != speater.ToString())
{
string strVal = s;
if (toLower)
{
strVal = s.ToLower();
}
list.Add(strVal);
}
}
return list;
}
/// <summary>
/// 把 List<string> 按照分隔符组装成 string
/// </summary>
/// <param name="list"></param>
/// <param name="speater"></param>
/// <returns></returns>
public static string GetStrArray(this List<string> list, string speater = ",")
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i]);
}
else
{
sb.Append(list[i]);
sb.Append(speater);
}
}
return sb.ToString();
}
/// <summary>
/// 删除最后结尾的指定字符后的字符
/// </summary>
public static string DelLastChar(this string str, string strChar = ",")
{
return str.Substring(0, str.LastIndexOf(strChar));
}
/// <summary>
/// 转全角的函数(SBC case)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
}
/// <summary>
/// 转半角的函数(SBC case)
/// </summary>
/// <param name="input">输入</param>
/// <returns></returns>
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
/// <summary>
/// 获取正确的Id如果不是正整数返回0
/// </summary>
/// <param name="value"></param>
/// <returns>返回正确的整数ID失败返回0</returns>
public static int ToInt32(this string value)
{
if (IsNumberId(value))
return int.Parse(value);
else
return 0;
}
/// <summary>
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。(0除外)
/// </summary>
/// <param name="_value">需验证的字符串。。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumberId(string _value)
{
return QuickValidate("^[1-9]*[0-9]*$", _value);
}
/// <summary>
/// 快速验证一个字符串是否符合指定的正则表达式。
/// </summary>
/// <param name="_express">正则表达式的内容。</param>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool QuickValidate(string _express, string _value)
{
if (_value == null) return false;
Regex myRegex = new Regex(_express);
if (_value.Length == 0)
{
return false;
}
return myRegex.IsMatch(_value);
}
/// <summary>
/// 得到字符串长度一个汉字长度为2
/// </summary>
/// <param name="inputString">参数字符串</param>
/// <returns></returns>
public static int StrLength(this string inputString)
{
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
}
return tempLen;
}
/// <summary>
/// 截取指定长度字符串
/// </summary>
/// <param name="inputString">要处理的字符串</param>
/// <param name="len">指定长度</param>
/// <returns>返回处理后的字符串</returns>
public static string splitString(this string inputString, int len)
{
bool isShowFix = false;
if (len % 2 == 1)
{
isShowFix = true;
len--;
}
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
}
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (isShowFix && mybyte.Length > len)
tempString += "…";
return tempString;
}
/// <summary>
/// HTML转行成TEXT
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(this string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", "");
return strOutput;
}
/// <summary>
/// 判断对象是否为空为空返回true
/// </summary>
/// <typeparam name="T">要验证的对象的类型</typeparam>
/// <param name="data">要验证的对象</param>
public static bool IsNullOrEmpty(this object data)
{
//如果为null
if (data == null)
{
return true;
}
if (string.IsNullOrEmpty(data.ToString().Trim()))
return true;
return false;
}
/// <summary>
/// 判断对象是否为空为空返回true
/// </summary>
/// <typeparam name="T">要验证的对象的类型</typeparam>
/// <param name="data">要验证的对象</param>
public static bool IsNullOrEmpty<T>(this List<T> collection)
{
if (collection == null)
return true;
if (collection.Count() == 0)
return true;
return false;
}
public static bool IsNullOrEmpty(this DataSet ds)
{
if (ds == null)
return true;
if (ds.Tables.Count == 0)
return true;
return ds.Tables[0].IsNullOrEmpty();
}
public static bool IsNullOrEmpty(this DataTable dt)
{
if (dt == null)
return true;
if (dt.Rows.Count == 0)
return true;
return false;
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Newtonsoft.Json;
namespace DH.RBAC.Utility.Other
{
/// <summary>
/// Select2树形下拉列表模型。
/// </summary>
public class TreeSelect
{
public string id { get; set; }
public string text { get; set; }
public string parentId { get; set; }
public object data { get; set; }
}
public static class TreeSelectHelper
{
public static string ToTreeSelectJson(this List<TreeSelect> data)
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
sb.Append(ToTreeSelectJson(data, "0", ""));
sb.Append("]");
return sb.ToString();
}
private static string ToTreeSelectJson(List<TreeSelect> data, string parentId, string blank)
{
StringBuilder sb = new StringBuilder();
var childList = data.FindAll(t => t.parentId == parentId);
var tabline = "";
if (parentId != "0")
{
tabline = "  ";
}
if (childList.Count > 0)
{
tabline = tabline + blank;
}
foreach (TreeSelect entity in childList)
{
entity.text = tabline + entity.text;
string strJson = JsonConvert.SerializeObject(entity);
sb.Append(strJson);
sb.Append(ToTreeSelectJson(data, entity.id, tabline));
}
return sb.ToString().Replace("}{", "},{");
}
}
}

View File

@ -0,0 +1,277 @@
#if NETFRAMEWORK || WINDOWS
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DH.RBAC.Utility.Other
{
/// <summary>
/// UI控件多线程赋值专用
/// </summary>
public static class UIUtils
{
public delegate void InvokeBackColorDelegate(Control control, Color color);
public delegate void InvokeTextDelegate(Control control, string text);
public delegate void InvokeVisibleDelegate(Control control, bool enable);
public delegate void InvokeDataDelegate(Control control, DataTable dt);
public delegate void InvokeLineDelegate(Control control, string[] line);
public delegate void InvokeAppendLineDelegate(Control control, string line);
public delegate void InvokeAppendLineMaxDelegate(Control control, string line);
public delegate void InvokeTabIndex(UITabControl control, int index);
public delegate void InvokMESStateDelegate(UILight light, UILightState state);
public delegate void InvokeProgressDelegate(UIProcessBar control, int value);
public static void InvokeProgressValue(this UIProcessBar control, int value)
{
try
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeProgressDelegate(InvokeProgressValue), new object[] { control, value });
}
else
{
control.Value = value;
}
}
catch (Exception ex)
{
throw ex;
}
}
public static void InvokMESState(this UILight light, UILightState state)
{
if (light.InvokeRequired)
{
light.BeginInvoke(new InvokMESStateDelegate(InvokMESState), new object[] { light, state });
}
else
{
light.State = state;
}
}
public static void InvokeBackColor(this Control control, Color color)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeBackColorDelegate(InvokeBackColor), new object[] { control, color });
}
else
{
control.BackColor = color;
}
}
public static void InvokeVisible(this Control control, bool enable)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeVisibleDelegate(InvokeVisible), new object[] { control, enable });
}
else
{
control.Visible = enable;
}
}
public static void InvokeText(this Control control, string text)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeTextDelegate(InvokeText), new object[] { control, text });
}
else
{
if (control is UITextBox)
{
((UITextBox)control).Text = text;
}
else if (control is TextBox)
{
((TextBox)control).Text = text;
}
else if (control is UILabel)
{
((UILabel)control).Text = text;
}
else if (control is Label)
{
((Label)control).Text = text;
}
else if (control is UIButton)
{
((UIButton)control).Text = text;
}
else if (control is Button)
{
((Button)control).Text = text;
}
else if (control is UIGroupBox)
{
((UIGroupBox)control).Text = text;
}
}
}
public static void InvokeData(this Control control, DataTable dt)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeDataDelegate(InvokeData), new object[] { control, dt });
}
else
{
if (control is UIDataGridView)
{
UIDataGridView dgv = ((UIDataGridView)control);
dgv.DataSource = dt;
dgv.ClearSelection();
dgv.Refresh();
}
else if (control is DataGridView)
{
DataGridView dgv = ((DataGridView)control);
dgv.DataSource = dt;
dgv.ClearSelection();
dgv.Refresh();
}
}
}
public static void InvokeLine(this Control control, string[] line)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeLineDelegate(InvokeLine), new object[] { control, line });
}
else
{
if (control is UITextBox)
{
UITextBox tb = ((UITextBox)control);
tb.Lines = line;
}
else if (control is TextBox)
{
TextBox tb = ((TextBox)control);
tb.Lines = line;
}
}
}
public static void InvokeLine(this Control control, List<string> line)
{
InvokeLine(control, line.ToArray());
}
public static void InvokeAppendLine(this Control control, string value)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeAppendLineDelegate(InvokeAppendLine), new object[] { control, value });
}
else
{
if (control is UITextBox)
{
UITextBox component = (UITextBox)control;
List<string> lines = component.Lines.ToList();
lines.Add(value);
component.Lines = lines.ToArray();
component.Focus();//获取焦点
component.Select(component.TextLength, 0);//光标定位到文本最后
component.ScrollToCaret();//滚动到光标处
}
else if (control is TextBox)
{
TextBox component = (TextBox)control;
List<string> lines = component.Lines.ToList();
lines.Add(value);
component.Lines = lines.ToArray();
component.Focus();//获取焦点
component.Select(component.TextLength, 0);//光标定位到文本最后
component.ScrollToCaret();//滚动到光标处
}
}
}
public static void InvokeAppendLine(this Control control, string value, int maxLineNum)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeAppendLineMaxDelegate(InvokeAppendLine), new object[] { control, value, maxLineNum });
}
else
{
if (control is UITextBox)
{
UITextBox component = (UITextBox)control;
List<string> lines = component.Lines.ToList();
lines.Add(value);
if (lines.Count > maxLineNum)
{
int cha = lines.Count - maxLineNum;
//lines.Reverse();
//差多少,删多少
for (int i = 0; i < cha; i++)
{
lines.RemoveAt(0);
}
//lines.Reverse();
}
component.Lines = lines.ToArray();
component.Focus();//获取焦点
component.Select(component.TextLength, 0);//光标定位到文本最后
component.ScrollToCaret();//滚动到光标处
}
else if (control is TextBox)
{
TextBox component = (TextBox)control;
List<string> lines = component.Lines.ToList();
lines.Add(value);
if (lines.Count > maxLineNum)
{
int cha = lines.Count - maxLineNum;
//lines.Reverse();
//差多少,删多少
for (int i = 0; i < cha; i++)
{
lines.RemoveAt(0);
}
//lines.Reverse();
}
component.Lines = lines.ToArray();
component.Focus();//获取焦点
component.Select(component.TextLength, 0);//光标定位到文本最后
component.ScrollToCaret();//滚动到光标处
}
}
}
public static void InvokeIndex(this UITabControl control, int index)
{
if (control.InvokeRequired)
{
control.BeginInvoke(new InvokeTabIndex(InvokeIndex), new object[] { control, index });
}
else
{
control.SelectedIndex = index;
}
}
}
}
#endif

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DH.RBAC.Utility.Other
{
public class UUID
{
public static long SnowId
{
get
{
return SnowFlakeHelper.GetSnowInstance().NextId();
}
}
public static string StrSnowId
{
get
{
return Convert.ToString(SnowId);
}
}
public static string NewTimeUUID
{
get
{
return DateTime.Today.ToString("yyyyMMdd") + Guid.NewGuid().ToString().Replace("-", "").ToUpper().Substring(0, 10);
}
}
public static string NewTimeUUID2
{
get
{
return DateTime.Today.ToString("yyyyMMdd") + Guid.NewGuid().ToString().Replace("-", "").ToUpper();
}
}
public static string NewUUID
{
get
{
return Guid.NewGuid().ToString().Replace("-", "").ToUpper();
}
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DH.RBAC.Utility.Other
{
/// <summary>
/// zTree单层节点数据模型。
/// </summary>
public class ZTreeNode
{
/// <summary>
/// 节点ID。
/// </summary>
public string id { get; set; }
/// <summary>
/// 父节点ID。
/// </summary>
public string pId { get; set; }
/// <summary>
/// 节点名称。
/// </summary>
public string name { get; set; }
/// <summary>
/// 是否展开。
/// </summary>
public bool open { get; set; }
/// <summary>
/// 是否选中。
/// </summary>
public bool @checked { get; set; }
}
}