479 lines
17 KiB
C#
479 lines
17 KiB
C#
|
using System;
|
|||
|
using System.Data;
|
|||
|
using System.Data.SqlClient;
|
|||
|
using System.IO;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Mail;
|
|||
|
using System.Security.Cryptography;
|
|||
|
using System.Text;
|
|||
|
using System.Web;
|
|||
|
using MyCode.Project.Infrastructure.Common;
|
|||
|
using MyCode.Project.Infrastructure.Constant;
|
|||
|
using MyCode.Project.Infrastructure.Extensions;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace MyCode.Project.Infrastructure
|
|||
|
{
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public class MyUtils
|
|||
|
{
|
|||
|
#region GetNewId(取得自递增ID,非雪花算法)
|
|||
|
/// <summary>
|
|||
|
/// 取得自递增ID
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public static long GetNewId()
|
|||
|
{
|
|||
|
System.Threading.Thread.Sleep(1);
|
|||
|
|
|||
|
return Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmmssfff") + WebConfigUtils.GetAppSettingsInfo("IdLastNum"));
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 获取业务编号
|
|||
|
/// <summary>
|
|||
|
/// 获取单据编号
|
|||
|
/// </summary>
|
|||
|
/// <param name="prefix">前缀</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetBillNo(string prefix)
|
|||
|
{
|
|||
|
System.Threading.Thread.Sleep(1);
|
|||
|
var num = (new Random().Next(99) + 1).ToString("00");
|
|||
|
return prefix + DateTime.Now.ToString("yyMMddHHmmssfff") + num;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region GetRandNum(从一个范围得到一个随机的数字)
|
|||
|
public static int GetRandNum(int min,int max)
|
|||
|
{
|
|||
|
return new Random().Next(min, max);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
private static char[] constant = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
|
|||
|
|
|||
|
#region GenerateRandom(生成不重复的随机数)
|
|||
|
/// <summary>
|
|||
|
/// 生成不重复的随机数
|
|||
|
/// </summary>
|
|||
|
/// <param name="length">长度</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GenerateRandom(int length)
|
|||
|
{
|
|||
|
var random = new StringBuilder(62);
|
|||
|
|
|||
|
Random rd = new Random(Guid.NewGuid().GetHashCode());
|
|||
|
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
random.Append(constant[rd.Next(62)]);
|
|||
|
}
|
|||
|
|
|||
|
var returnV = random.ToString();
|
|||
|
|
|||
|
return returnV;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region CreateValidateCode(生成验证码)
|
|||
|
/// <summary>
|
|||
|
/// 生成验证码
|
|||
|
/// </summary>
|
|||
|
/// <param name="length">指定验证码的长度</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string CreateValidateCode(int pMax)
|
|||
|
{
|
|||
|
pMax = (pMax < 2 || pMax > 6) ? 4 : pMax;
|
|||
|
int[] randMembers = new int[pMax];
|
|||
|
int[] validateNums = new int[pMax];
|
|||
|
StringBuilder validateNumberStr = new StringBuilder();
|
|||
|
//生成起始序列值
|
|||
|
int seekSeek = unchecked((int)DateTime.Now.Ticks);
|
|||
|
Random seekRand = new Random(seekSeek);
|
|||
|
int beginSeek = seekRand.Next(0, Int32.MaxValue - pMax * 10000);
|
|||
|
int[] seeks = new int[pMax];
|
|||
|
for (int i = 0; i < pMax; i++)
|
|||
|
{
|
|||
|
beginSeek += 10000;
|
|||
|
seeks[i] = beginSeek;
|
|||
|
}
|
|||
|
//生成随机数字
|
|||
|
for (int i = 0; i < pMax; i++)
|
|||
|
{
|
|||
|
Random rand = new Random(seeks[i]);
|
|||
|
int pownum = 1 * (int)Math.Pow(10, pMax);
|
|||
|
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
|
|||
|
}
|
|||
|
//抽取随机数字
|
|||
|
for (int i = 0; i < pMax; i++)
|
|||
|
{
|
|||
|
string numStr = randMembers[i].ToString();
|
|||
|
int numLength = numStr.Length;
|
|||
|
Random rand = new Random();
|
|||
|
int numPosition = rand.Next(0, numLength - 1);
|
|||
|
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
|
|||
|
}
|
|||
|
//生成验证码
|
|||
|
for (int i = 0; i < pMax; i++)
|
|||
|
{
|
|||
|
validateNumberStr.Append(validateNums[i]);
|
|||
|
}
|
|||
|
return validateNumberStr.ToString();
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region ConvertLongToDateTime(转long为时间)
|
|||
|
// long --> DateTime
|
|||
|
public static DateTime ConvertLongToDateTime(long d)
|
|||
|
{
|
|||
|
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|||
|
long lTime = long.Parse(d + "0000000");
|
|||
|
TimeSpan toNow = new TimeSpan(lTime);
|
|||
|
return dtStart.Add(toNow);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region GetTimeStamp(得到Linux那边的时间戳)
|
|||
|
public static int GetTimeStamp()
|
|||
|
{
|
|||
|
var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
|||
|
|
|||
|
return (int)(DateTime.Now - startTime).TotalSeconds;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region GetDistance(取得两点位置的距离)
|
|||
|
//地球半径,单位米
|
|||
|
private const double EARTH_RADIUS = 6378137;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 计算两点位置的距离,返回两点的距离,单位 米
|
|||
|
/// 该公式为GOOGLE提供,误差小于0.2米
|
|||
|
/// </summary>
|
|||
|
/// <param name="lng1">第一点经度</param>
|
|||
|
/// <param name="lat1">第一点纬度</param>
|
|||
|
/// <param name="lng2">第二点经度</param>
|
|||
|
/// <param name="lat2">第二点纬度</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static double GetDistance( double lng1, double lat1, double lng2 ,double lat2)
|
|||
|
{
|
|||
|
double radLat1 = Rad(lat1);
|
|||
|
double radLng1 = Rad(lng1);
|
|||
|
double radLat2 = Rad(lat2);
|
|||
|
double radLng2 = Rad(lng2);
|
|||
|
double a = radLat1 - radLat2;
|
|||
|
double b = radLng1 - radLng2;
|
|||
|
double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 经纬度转化成弧度
|
|||
|
/// </summary>
|
|||
|
/// <param name="d"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private static double Rad(double d)
|
|||
|
{
|
|||
|
return (double)d * Math.PI / 180d;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region SubStrAddSuffix(截取字符串长度,超出部分使用后缀suffix代替)
|
|||
|
/// <summary>
|
|||
|
/// 截取字符串长度,超出部分使用后缀suffix代替,比如abcdevfddd取前3位,后面使用...代替
|
|||
|
/// </summary>
|
|||
|
/// <param name="orginStr"></param>
|
|||
|
/// <param name="length"></param>
|
|||
|
/// <param name="suffix"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string SubStrAddSuffix(string orginStr, int length, string suffix)
|
|||
|
{
|
|||
|
string ret = orginStr;
|
|||
|
if (orginStr.Length > length)
|
|||
|
{
|
|||
|
ret = orginStr.Substring(0, length) + suffix;
|
|||
|
}
|
|||
|
return ret;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 删除最后结尾的一个逗号
|
|||
|
/// <summary>
|
|||
|
/// 删除最后结尾的一个逗号
|
|||
|
/// </summary>
|
|||
|
public static string DelLastComma(string str)
|
|||
|
{
|
|||
|
if (str.Length < 1)
|
|||
|
{
|
|||
|
return "";
|
|||
|
}
|
|||
|
return str.Substring(0, str.LastIndexOf(","));
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 删除最后结尾的指定字符后的字符
|
|||
|
/// <summary>
|
|||
|
/// 删除最后结尾的指定字符后的字符
|
|||
|
/// </summary>
|
|||
|
public static string DelLastChar(string str, string strchar)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(str))
|
|||
|
return "";
|
|||
|
if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
|
|||
|
{
|
|||
|
return str.Substring(0, str.LastIndexOf(strchar));
|
|||
|
}
|
|||
|
return str;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 生成指定长度的字符串
|
|||
|
/// <summary>
|
|||
|
/// 生成指定长度的字符串,即生成strLong个str字符串
|
|||
|
/// </summary>
|
|||
|
/// <param name="strLong">生成的长度</param>
|
|||
|
/// <param name="str">以str生成字符串</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string StringOfChar(int strLong, string str)
|
|||
|
{
|
|||
|
string ReturnStr = "";
|
|||
|
for (int i = 0; i < strLong; i++)
|
|||
|
{
|
|||
|
ReturnStr += str;
|
|||
|
}
|
|||
|
|
|||
|
return ReturnStr;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 截取字符长度
|
|||
|
/// <summary>
|
|||
|
/// 截取字符长度
|
|||
|
/// </summary>
|
|||
|
/// <param name="inputString">字符</param>
|
|||
|
/// <param name="len">长度</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string CutString(string inputString, int len)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(inputString))
|
|||
|
return "";
|
|||
|
inputString = DropHTML(inputString);
|
|||
|
ASCIIEncoding ascii = new 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 (mybyte.Length > len)
|
|||
|
tempString += "…";
|
|||
|
return tempString;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 清除HTML标记
|
|||
|
public static string DropHTML(string Htmlstring)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(Htmlstring)) return "";
|
|||
|
//删除脚本
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
|
|||
|
//删除HTML
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
|
|||
|
|
|||
|
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
|
|||
|
Htmlstring.Replace("<", "");
|
|||
|
Htmlstring.Replace(">", "");
|
|||
|
Htmlstring.Replace("\r\n", "");
|
|||
|
Htmlstring.Replace(" ", "");
|
|||
|
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
|
|||
|
return Htmlstring;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 清除HTML标记且返回相应的长度
|
|||
|
public static string DropHTML(string Htmlstring, int strLen)
|
|||
|
{
|
|||
|
return CutString(DropHTML(Htmlstring), strLen);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region TXT代码转换成HTML格式
|
|||
|
/// <summary>
|
|||
|
/// 字符串字符处理
|
|||
|
/// </summary>
|
|||
|
/// <param name="chr">等待处理的字符串</param>
|
|||
|
/// <returns>处理后的字符串</returns>
|
|||
|
/// //把TXT代码转换成HTML格式
|
|||
|
public static String ToHtml(string Input)
|
|||
|
{
|
|||
|
StringBuilder sb = new StringBuilder(Input);
|
|||
|
sb.Replace("'", "'");
|
|||
|
sb.Replace("&", "&");
|
|||
|
sb.Replace("<", "<");
|
|||
|
sb.Replace(">", ">");
|
|||
|
sb.Replace("\r\n", "<br />");
|
|||
|
sb.Replace("\n", "<br />");
|
|||
|
sb.Replace("\t", " ");
|
|||
|
//sb.Replace(" ", " ");
|
|||
|
return sb.ToString();
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region HTML代码转换成TXT格式
|
|||
|
/// <summary>
|
|||
|
/// 字符串字符处理
|
|||
|
/// </summary>
|
|||
|
/// <param name="chr">等待处理的字符串</param>
|
|||
|
/// <returns>处理后的字符串</returns>
|
|||
|
/// //把HTML代码转换成TXT格式
|
|||
|
public static String ToTxt(String Input)
|
|||
|
{
|
|||
|
StringBuilder sb = new StringBuilder(Input);
|
|||
|
sb.Replace(" ", " ");
|
|||
|
sb.Replace("<br>", "\r\n");
|
|||
|
sb.Replace("<br>", "\n");
|
|||
|
sb.Replace("<br />", "\n");
|
|||
|
sb.Replace("<br />", "\r\n");
|
|||
|
sb.Replace("<", "<");
|
|||
|
sb.Replace(">", ">");
|
|||
|
sb.Replace("&", "&");
|
|||
|
return sb.ToString();
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 检测是否有Sql危险字符
|
|||
|
/// <summary>
|
|||
|
/// 检测是否有Sql危险字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="str">要判断字符串</param>
|
|||
|
/// <returns>判断结果</returns>
|
|||
|
public static bool IsSafeSqlString(string str)
|
|||
|
{
|
|||
|
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查危险字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="Input"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string Filter(string sInput)
|
|||
|
{
|
|||
|
if (sInput == null || sInput == "")
|
|||
|
return null;
|
|||
|
string sInput1 = sInput.ToLower();
|
|||
|
string output = sInput;
|
|||
|
string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
|
|||
|
if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
|
|||
|
{
|
|||
|
throw new Exception("字符串中含有非法字符!");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
output = output.Replace("'", "''");
|
|||
|
}
|
|||
|
return output;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查过滤设定的危险字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="InText">要过滤的字符串 </param>
|
|||
|
/// <returns>如果参数存在不安全字符,则返回true </returns>
|
|||
|
public static bool SqlFilter(string word, string InText)
|
|||
|
{
|
|||
|
if (InText == null)
|
|||
|
return false;
|
|||
|
foreach (string i in word.Split('|'))
|
|||
|
{
|
|||
|
if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 过滤特殊字符
|
|||
|
/// <summary>
|
|||
|
/// 过滤特殊字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="Input"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string Htmls(string Input)
|
|||
|
{
|
|||
|
if (Input != string.Empty && Input != null)
|
|||
|
{
|
|||
|
string ihtml = Input.ToLower();
|
|||
|
ihtml = ihtml.Replace("<script", "<script");
|
|||
|
ihtml = ihtml.Replace("script>", "script>");
|
|||
|
ihtml = ihtml.Replace("<%", "<%");
|
|||
|
ihtml = ihtml.Replace("%>", "%>");
|
|||
|
ihtml = ihtml.Replace("<$", "<$");
|
|||
|
ihtml = ihtml.Replace("$>", "$>");
|
|||
|
return ihtml;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region FirstCharToUpper(首字母大写)
|
|||
|
/// <summary>
|
|||
|
/// 首字母大写
|
|||
|
/// </summary>
|
|||
|
/// <param name="input"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string FirstCharToUpper(string input)
|
|||
|
{
|
|||
|
if (String.IsNullOrEmpty(input))
|
|||
|
return input;
|
|||
|
string str = input.First().ToString().ToUpper() + input.Substring(1);
|
|||
|
return str;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|