187 lines
5.6 KiB
C#
187 lines
5.6 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Newtonsoft.Json;
|
|
using XSystem.Security.Cryptography;
|
|
|
|
namespace Common
|
|
{
|
|
public static class Tools
|
|
{
|
|
|
|
private static string GetTimeString()
|
|
{
|
|
return DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
|
}
|
|
public static string GetTimeStringRandom(string str = "")
|
|
{
|
|
return GetTimeString() + str;
|
|
}
|
|
|
|
public static string LockKey(object param)
|
|
{
|
|
var source = JsonConvert.SerializeObject(param);
|
|
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
|
|
byte[] bytes = Encoding.UTF8.GetBytes(source);
|
|
string result = BitConverter.ToString(md5.ComputeHash(bytes));
|
|
return "HSCW:RedisLock:" + result.Replace("-", "").ToLower();
|
|
}
|
|
|
|
public static string GetSerialNumber()
|
|
{
|
|
return DateTime.Now.ToString("yyyyMMddHHmmssfff") + Random.Shared.Next(100000, 999999);
|
|
}
|
|
|
|
public static string ReplaceWithSpecialChar(string value, int startLen = 1, int endLen = 1, char specialChar = '*')
|
|
{
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
if (value.Length < startLen + endLen)
|
|
{
|
|
return value;
|
|
}
|
|
|
|
string startStr = value.Substring(0, startLen);
|
|
var len = value.Length - startLen - endLen;
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
startStr += specialChar;
|
|
}
|
|
startStr += value.Substring(startLen + len, endLen);
|
|
return startStr;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
public static string SubStringText(string content, int length)
|
|
{
|
|
if (string.IsNullOrEmpty(content))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
if (content.Length <= length)
|
|
{
|
|
return content;
|
|
}
|
|
|
|
return content.Substring(0, length);
|
|
}
|
|
public static string SubStringTextLast(string content, int length)
|
|
{
|
|
if (string.IsNullOrEmpty(content))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
if (content.Length <= length)
|
|
{
|
|
return content;
|
|
}
|
|
|
|
return content.Substring(content.Length - length);
|
|
}
|
|
|
|
|
|
public static List<string> PickupImgUrl(string html)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
return null;
|
|
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
|
|
MatchCollection matches = regImg.Matches(html);
|
|
List<string> lstImg = new List<string>();
|
|
foreach (Match match in matches)
|
|
{
|
|
lstImg.Add(match.Groups["imgUrl"].Value);
|
|
}
|
|
|
|
return lstImg;
|
|
}
|
|
public static string NoHTML(string html)
|
|
{
|
|
string str = "";
|
|
for (int i = 0; i < html.Length; i++)
|
|
{
|
|
if (Convert.ToInt32(Convert.ToChar(html.Substring(i, 1))) > Convert.ToInt32(Convert.ToChar(128)))
|
|
{
|
|
str += html.Substring(i, 1).ToString();
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
|
|
public static string RegexSubstring(string content, string startStr, string endStr)
|
|
{
|
|
Regex rex = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
|
|
return rex.Match(content).Value;
|
|
}
|
|
public static bool CheckIsUrl(string strValue)
|
|
{
|
|
return CheckIsFormat(@"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&$%\$#\=~])*$", strValue);
|
|
}
|
|
|
|
private static bool CheckIsFormat(string strRegex, string strValue)
|
|
{
|
|
if (strValue != null && strValue.Trim() != "")
|
|
{
|
|
Regex re = new Regex(strRegex);
|
|
if (re.IsMatch(strValue))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public static bool IsIdCard(string idCardNumber)
|
|
{
|
|
if (string.IsNullOrEmpty(idCardNumber))
|
|
return false;
|
|
idCardNumber = idCardNumber.ToUpper();
|
|
|
|
if (!Regex.IsMatch(idCardNumber, @"^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"))
|
|
return false;
|
|
|
|
|
|
string birthDateString = idCardNumber.Substring(6, 8);
|
|
char genderCheckDigitChar = idCardNumber[16];
|
|
char checkDigit = idCardNumber[17];
|
|
|
|
|
|
|
|
if (!DateTime.TryParseExact(birthDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthDate))
|
|
{
|
|
return false;
|
|
}
|
|
DateTime today = DateTime.Now.AddYears(18);
|
|
if (birthDate > today)
|
|
{
|
|
return false;
|
|
}
|
|
if (today.Year - birthDate.Year > 80)
|
|
{
|
|
return false;
|
|
}
|
|
if (!char.IsDigit(genderCheckDigitChar))
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|