123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace HaBookCms.Common
- {
- public class Utils
- {
- /// <summary>
- /// 生成随机字母字符串(数字字母混和)
- /// </summary>
- /// <param name="codeCount">待生成的位数</param>
- public static string GetCheckCode(int codeCount)
- {
- string str = string.Empty;
- int rep = 0;
- long num2 = DateTime.Now.Ticks + rep;
- rep++;
- Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
- for (int i = 0; i < codeCount; i++)
- {
- char ch;
- int num = random.Next();
- if ((num % 2) == 0)
- {
- ch = (char)(0x30 + ((ushort)(num % 10)));
- }
- else
- {
- ch = (char)(0x41 + ((ushort)(num % 0x1a)));
- }
- str = str + ch.ToString();
- }
- return str;
- }
- /// <summary>
- /// 将字符串转换为int类型数组
- /// </summary>
- /// <param name="str">如1,2,3,4,5</param>
- /// <returns></returns>
- public static List<int> StrToListInt(string str)
- {
- var list = new List<int>();
- if (!str.Contains(","))
- {
- list.Add(int.Parse(str));
- return list;
- }
- var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var item in slist)
- {
- list.Add(int.Parse(item));
- }
- return list;
- }
- /// <summary>
- /// 将字符串转换为int类型数组
- /// </summary>
- /// <param name="str">如1,2,3,4,5</param>
- /// <returns></returns>
- public static List<string> StrToListString(string str)
- {
- var list = new List<string>();
- if (!str.Contains(","))
- {
- list.Add(str);
- return list;
- }
- var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var item in slist)
- {
- list.Add(item);
- }
- return list;
- }
- /// <summary>
- /// 将字符串转换为数组
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns>字符串数组</returns>
- public static string[] GetStrArray(string str)
- {
- return str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- }
- #region 截取字符串
- public static string GetSubString(string pSrcString, int pLength, string pTailString)
- {
- return GetSubString(pSrcString, 0, pLength, pTailString);
- }
- public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
- {
- string str = pSrcString;
- byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
- foreach (char ch in Encoding.UTF8.GetChars(bytes))
- {
- if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
- {
- if (pStartIndex >= pSrcString.Length)
- {
- return "";
- }
- return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
- }
- }
- if (pLength < 0)
- {
- return str;
- }
- byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
- if (sourceArray.Length <= pStartIndex)
- {
- return str;
- }
- int length = sourceArray.Length;
- if (sourceArray.Length > (pStartIndex + pLength))
- {
- length = pLength + pStartIndex;
- }
- else
- {
- pLength = sourceArray.Length - pStartIndex;
- pTailString = "";
- }
- int num2 = pLength;
- int[] numArray = new int[pLength];
- byte[] destinationArray = null;
- int num3 = 0;
- for (int i = pStartIndex; i < length; i++)
- {
- if (sourceArray[i] > 0x7f)
- {
- num3++;
- if (num3 == 3)
- {
- num3 = 1;
- }
- }
- else
- {
- num3 = 0;
- }
- numArray[i] = num3;
- }
- if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
- {
- num2 = pLength + 1;
- }
- destinationArray = new byte[num2];
- Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
- return (Encoding.Default.GetString(destinationArray) + pTailString);
- }
- #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;
- }
- 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 = htmlstring.Replace("<", "");
- htmlstring = htmlstring.Replace(">", "");
- htmlstring = htmlstring.Replace("\r\n", "");
- //htmlstring = HttpContext.Current.Server.HtmlEncode(htmlstring).Trim();
- return htmlstring;
- }
- #endregion
- #region 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
- /// <summary>
- /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
- /// </summary>
- /// <param name="CnChar">单个汉字</param>
- /// <returns>单个大写字母</returns>
- public static string GetCharSpellCode(string CnChar)
- {
- long iCnChar;
- byte[] ZW = Encoding.Default.GetBytes(CnChar);
- //如果是字母,则直接返回首字母
- if (ZW.Length == 1)
- {
- return CutString(CnChar.ToUpper(), 1);
- }
- else
- {
- int i1 = (short)(ZW[0]);
- int i2 = (short)(ZW[1]);
- iCnChar = i1 * 256 + i2;
- }
- // iCnChar match the constant
- if ((iCnChar >= 45217) && (iCnChar <= 45252))
- {
- return "A";
- }
- else if ((iCnChar >= 45253) && (iCnChar <= 45760))
- {
- return "B";
- }
- else if ((iCnChar >= 45761) && (iCnChar <= 46317))
- {
- return "C";
- }
- else if ((iCnChar >= 46318) && (iCnChar <= 46825))
- {
- return "D";
- }
- else if ((iCnChar >= 46826) && (iCnChar <= 47009))
- {
- return "E";
- }
- else if ((iCnChar >= 47010) && (iCnChar <= 47296))
- {
- return "F";
- }
- else if ((iCnChar >= 47297) && (iCnChar <= 47613))
- {
- return "G";
- }
- else if ((iCnChar >= 47614) && (iCnChar <= 48118))
- {
- return "H";
- }
- else if ((iCnChar >= 48119) && (iCnChar <= 49061))
- {
- return "J";
- }
- else if ((iCnChar >= 49062) && (iCnChar <= 49323))
- {
- return "K";
- }
- else if ((iCnChar >= 49324) && (iCnChar <= 49895))
- {
- return "L";
- }
- else if ((iCnChar >= 49896) && (iCnChar <= 50370))
- {
- return "M";
- }
- else if ((iCnChar >= 50371) && (iCnChar <= 50613))
- {
- return "N";
- }
- else if ((iCnChar >= 50614) && (iCnChar <= 50621))
- {
- return "O";
- }
- else if ((iCnChar >= 50622) && (iCnChar <= 50905))
- {
- return "P";
- }
- else if ((iCnChar >= 50906) && (iCnChar <= 51386))
- {
- return "Q";
- }
- else if ((iCnChar >= 51387) && (iCnChar <= 51445))
- {
- return "R";
- }
- else if ((iCnChar >= 51446) && (iCnChar <= 52217))
- {
- return "S";
- }
- else if ((iCnChar >= 52218) && (iCnChar <= 52697))
- {
- return "T";
- }
- else if ((iCnChar >= 52698) && (iCnChar <= 52979))
- {
- return "W";
- }
- else if ((iCnChar >= 52980) && (iCnChar <= 53640))
- {
- return "X";
- }
- else if ((iCnChar >= 53689) && (iCnChar <= 54480))
- {
- return "Y";
- }
- else if ((iCnChar >= 54481) && (iCnChar <= 55289))
- {
- return "Z";
- }
- else
- return ("?");
- }
- #endregion
- #region 获得IP地址
- /// <summary>
- /// 获得IP地址
- /// </summary>
- /// <returns>字符串数组</returns>
- public static string GetIp()
- {
- HttpContextAccessor _context = new HttpContextAccessor();
- var ip = _context.HttpContext.Request.Headers["X-Forwarded-For"].ToString();
- if (string.IsNullOrEmpty(ip))
- {
- ip = _context.HttpContext.Connection.RemoteIpAddress.ToString();
- }
- return ip;
- }
- #endregion
- #region 获得当前访问的URL地址
- /// <summary>
- /// 获得当前访问的URL地址
- /// </summary>
- /// <returns>字符串数组</returns>
- public static string GetUrl()
- {
- HttpContextAccessor _context = new HttpContextAccessor();
- return _context.HttpContext.Request.Path.ToString();
- }
- #endregion
- #region 分割字符串
- public static string[] SplitString(string strContent, char strSplit)
- {
- if (!string.IsNullOrEmpty(strContent))
- {
- return strContent.Split(new char[] { strSplit }, StringSplitOptions.RemoveEmptyEntries);
- }
- else
- {
- return new string[0] { };
- }
- }
- /// <summary>
- /// 分割字符串
- /// </summary>
- public static string[] SplitString(string strContent, string strSplit)
- {
- if (!string.IsNullOrEmpty(strContent))
- {
- if (strContent.IndexOf(strSplit, StringComparison.Ordinal) < 0)
- return new string[] { strContent };
- return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
- }
- else
- return new string[0] { };
- }
- #endregion
- #region 显示错层方法
- /// <summary>
- /// 显示错层方法
- /// </summary>
- public static string LevelName(string name, decimal? level)
- {
- if (level > 1)
- {
- string nbsp = "";
- for (int i = 0; i < level; i++)
- {
- nbsp += " ";
- }
- name = nbsp + "|--" + name;
- }
- return name;
- }
- #endregion
- #region 生成随机字母或数字
- private static readonly Random Random = new Random();
- /// <summary>
- /// 生成随机数字
- /// </summary>
- /// <param name="length">生成长度</param>
- /// <returns></returns>
- public static string Number(int length)
- {
- return Number(length, false);
- }
- /// <summary>
- /// 生成随机数字
- /// </summary>
- /// <param name="length">生成长度</param>
- /// <param name="sleep">是否要在生成前将当前线程阻止以避免重复</param>
- /// <returns></returns>
- public static string Number(int length, bool sleep)
- {
- if (sleep)
- System.Threading.Thread.Sleep(2);
- string result = "";
- for (int i = 0; i < length; i++)
- {
- result += Random.Next(10).ToString();
- }
- return result;
- }
- /// <summary>
- /// 根据日期和随机码生成订单号
- /// </summary>
- /// <returns></returns>
- public static string GetOrderNumber()
- {
- string num = DateTime.Now.ToString("yyyyMMddHHmmssms"); //yyyyMMddHHmmssms
- return num + Number(2);
- }
- #endregion
- #region 条形码解析
- /// <summary>
- /// 分解条形码,并返回数组
- /// </summary>
- /// <param name="code">条形码</param>
- /// <returns>数组0=品牌 1=季节 2=款式 3=批次 4=尺码</returns>
- public static List<string> GetSkuArray(string code)
- {
- var str = new List<string>();
- //品牌,截取0-3
- str.Add(code.Substring(0, 3));
- //季节截取
- str.Add(code.Substring(3, 1));
- //款式截取
- str.Add(code.Substring(4, 1));
- //批次截取
- str.Add(code.Substring(5, 1));
- //尺码截取
- str.Add(code.Substring(6, 1));
- return str;
- }
- #endregion
- #region 返回采购单入库状态
- /// <summary>
- /// 返回采购单入库状态
- /// </summary>
- /// <param name="status">状态</param>
- /// <returns></returns>
- public static string PurchaseStatus(string status)
- {
- var str = "";
- switch (int.Parse(status))
- {
- case 1: str = "未完成入库"; break;
- case 2: str = "未完成付款"; break;
- case 3: str = "未完成到票"; break;
- case 4: str = "完成"; break;
- }
- return str;
- }
- #endregion
- #region 生成采购单编号
- /// <summary>
- /// 生成采购单编号
- /// </summary>
- /// <returns></returns>
- public static string PurchaseNumber(int lastNumber)
- {
- return "CG-" + DateTime.Now.ToString("yyyyMMdd") + "-" + lastNumber.ToString();
- }
- #endregion
- #region 上传配置
- /// <summary>
- /// 根据文件类型分配路径
- /// </summary>
- /// <param name="fileExt"></param>
- /// <returns></returns>
- public static string AssigendPath(string fileExt, string path)
- {
- if (IsImage(fileExt))
- return path + "/upload/images/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
- if (IsVideos(fileExt))
- return path + "/upload/videos/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
- if (IsDocument(fileExt))
- return "/upload/files/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
- if (IsMusics(fileExt))
- return "/upload/musics/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
- return path + "/upload/others/";
- }
- #endregion
- #region 文件格式
- /// <summary>
- /// 是否为图片
- /// </summary>
- /// <param name="_fileExt">文件扩展名,不含“.”</param>
- /// <returns></returns>
- private static bool IsImage(string _fileExt)
- {
- var images = new List<string> { "bmp", "gif", "jpg", "jpeg", "png" };
- if (images.Contains(_fileExt.ToLower())) return true;
- return false;
- }
- /// <summary>
- /// 是否为视频
- /// </summary>
- /// <param name="_fileExt">文件扩展名,不含“.”</param>
- /// <returns></returns>
- private static bool IsVideos(string _fileExt)
- {
- var videos = new List<string> { "rmvb", "mkv", "ts", "wma", "avi", "rm", "mp4", "flv", "mpeg", "mov", "3gp", "mpg" };
- if (videos.Contains(_fileExt.ToLower())) return true;
- return false;
- }
- /// <summary>
- /// 是否为音频
- /// </summary>
- /// <param name="_fileExt">文件扩展名,不含“.”</param>
- /// <returns></returns>
- private static bool IsMusics(string _fileExt)
- {
- var musics = new List<string> { "mp3", "wav" };
- if (musics.Contains(_fileExt.ToLower())) return true;
- return false;
- }
- /// <summary>
- /// 是否为文档
- /// </summary>
- /// <param name="_fileExt">文件扩展名,不含“.”</param>
- /// <returns></returns>
- private static bool IsDocument(string _fileExt)
- {
- var documents = new List<string> { "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf" };
- if (documents.Contains(_fileExt.ToLower())) return true;
- return false;
- }
- #endregion
- #region 返回活动名称
- /// <summary>
- /// 返回活动名称
- /// </summary>
- /// <param name="method"></param>
- /// <returns></returns>
- public static string GetActivityMethod(int method)
- {
- var str = "";
- switch (method)
- {
- case 1: str = "打折"; break;
- case 2: str = "满减"; break;
- case 3: str = "买一赠一"; break;
- default: str = "无"; break;
- }
- return str;
- }
- /// <summary>
- /// 根据方式返回参加活动类型
- /// </summary>
- /// <param name="method"></param>
- /// <returns></returns>
- public static byte GetActivityTypes(int method)
- {
- byte str = 1;
- switch (method)
- {
- case 1: str = 2; break;
- case 2: str = 3; break;
- case 3: str = 4; break;
- }
- return str;
- }
- #endregion
- #region 得到一周的周一和周日的日期
- /// <summary>
- /// 计算本周的周一日期
- /// </summary>
- /// <returns></returns>
- public static DateTime GetMondayDate()
- {
- return GetMondayDate(DateTime.Now);
- }
- /// <summary>
- /// 计算本周周日的日期
- /// </summary>
- /// <returns></returns>
- public static DateTime GetSundayDate()
- {
- return GetSundayDate(DateTime.Now);
- }
- /// <summary>
- /// 计算某日起始日期(礼拜一的日期)
- /// </summary>
- /// <param name="someDate">该周中任意一天</param>
- /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
- public static DateTime GetMondayDate(DateTime someDate)
- {
- int i = someDate.DayOfWeek - DayOfWeek.Monday;
- if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
- TimeSpan ts = new TimeSpan(i, 0, 0, 0);
- return someDate.Subtract(ts);
- }
- /// <summary>
- /// 计算某日结束日期(礼拜日的日期)
- /// </summary>
- /// <param name="someDate">该周中任意一天</param>
- /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
- public static DateTime GetSundayDate(DateTime someDate)
- {
- int i = someDate.DayOfWeek - DayOfWeek.Sunday;
- if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。
- TimeSpan ts = new TimeSpan(i, 0, 0, 0);
- return someDate.Add(ts);
- }
- /// <summary>
- /// 根据星期几获得数字的星期几
- /// </summary>
- /// <param name="weekName">例如周一:Monday</param>
- /// <returns></returns>
- public static int GetWeekByWeekName(string weekName)
- {
- var week = 1;
- switch (weekName)
- {
- case "Monday":
- week = 1;
- break;
- case "Tuesday":
- week = 2;
- break;
- case "Wednesday":
- week = 3;
- break;
- case "Thursday":
- week = 4;
- break;
- case "Friday":
- week = 5;
- break;
- case "Saturday":
- week = 6;
- break;
- case "Sunday":
- week = 7;
- break;
- }
- return week;
- }
- #endregion
- /// <summary>
- /// 时间戳转换为日期(时间戳单位秒)
- /// </summary>
- /// <param name="TimeStamp"></param>
- /// <returns></returns>
- public static DateTime ConvertToDateTime(long timeStamp)
- {
- var dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- TimeSpan toNow = new TimeSpan(timeStamp);
- return dtStart.Add(toNow);
- //var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- //return start.AddMilliseconds(timeStamp).AddHours(8);
- }
- /// <summary>
- /// 日期转换为时间戳(时间戳单位秒)
- /// </summary>
- /// <param name="TimeStamp"></param>
- /// <returns></returns>
- public static long ConvertToTimeStamp(DateTime time)
- {
- DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- return (long)(time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
- }
- }
- }
|