shop_template_api/ARW.Common/Common.cs
2023-06-02 21:15:33 +08:00

416 lines
13 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions; //引用正则表达式
namespace ARW.Common
{
/// <summary>
/// 常用公共类
/// </summary>
public class Common
{
public static object _lock = new object();
public static int count = 1;
#region Stopwatch计时器
/// <summary>
/// 计时器开始
/// </summary>
/// <returns></returns>
public static Stopwatch TimerStart()
{
Stopwatch watch = new Stopwatch();
watch.Reset();
watch.Start();
return watch;
}
/// <summary>
/// 计时器结束
/// </summary>
/// <param name="watch"></param>
/// <returns></returns>
public static string TimerEnd(Stopwatch watch)
{
watch.Stop();
double costtime = watch.ElapsedMilliseconds;
return costtime.ToString();
}
#endregion
#region
/// <summary>
/// 删除数组中的重复项
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public static string[] RemoveDup(string[] values)
{
List<string> list = new List<string>();
for (int i = 0; i < values.Length; i++)//遍历数组成员
{
if (!list.Contains(values[i]))
{
list.Add(values[i]);
};
}
return list.ToArray();
}
#endregion
#region
/// <summary>
/// 表示全局唯一标识符 (GUID)。
/// </summary>
/// <returns></returns>
public static string GuId()
{
return Guid.NewGuid().ToString();
}
/// <summary>
/// 自动生成编号 201008251145409865
/// </summary>
/// <returns></returns>
public static string CreateNo()
{
Random random = new Random();
string strRandom = random.Next(1000, 10000).ToString(); //生成编号
string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
return code;
}
/// <summary>
/// 生成不重复的订单,净水器
/// </summary>
/// <returns></returns>
public static string CreateNo2()
{
lock (_lock)
{
if (count >= 10000)
{
count = 1;
}
var number = DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000");
count++;
return number;
}
}
/// <summary>
/// 生成不重复的订单,送芯
/// </summary>
/// <returns></returns>
public static string CreateNo3()
{
lock (_lock)
{
if (count >= 10000)
{
count = 1;
}
var number = "C" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000");
count++;
return number;
}
}
/// <summary>
/// 退款订单号生成
/// </summary>
/// <returns></returns>
public static string CreateNo_Recharge()
{
lock (_lock)
{
if (count >= 10000)
{
count = 1;
}
var number = "R" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000");
count++;
return number;
}
}
/// <summary>
/// 生成订单号
/// </summary>
/// <returns></returns>
public static string CreateNoQuery()
{
lock (_lock)
{
if (count >= 10000)
{
count = 1;
}
var number = "Q" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000");
count++;
return number;
}
}
#endregion
#region 0-9
/// <summary>
/// 生成0-9随机数
/// </summary>
/// <param name="codeNum">生成长度</param>
/// <returns></returns>
public static string RndNum(int codeNum)
{
StringBuilder sb = new StringBuilder(codeNum);
Random rand = new Random();
for (int i = 1; i < codeNum + 1; i++)
{
int t = rand.Next(9);
sb.AppendFormat("{0}", t);
}
return sb.ToString();
}
#endregion
#region
/// <summary>
/// 生成随机数
/// </summary>
/// <param name="length">数据长度</param>
/// <param name="CreateType">生成类型,0纯数字,1:纯字母, 2:数据字+字母</param>
/// <returns></returns>
public static string GetRandomNo(int length, int CreateType)
{
char[] charlist = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'h', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
string result = "";
Random rd = new Random();
switch (CreateType)
{
case 0: //纯数字
for (int i = 0; i < length; i++)
{
result = result + charlist[rd.Next(0, 10)];
}
break;
case 1://纯字母
for (int i = 0; i < length; i++)
{
result = result + charlist[rd.Next(10, 36)];
}
break;
case 2://数据字+字母
for (int i = 0; i < length; i++)
{
result = result + charlist[rd.Next(0, 36)];
}
break;
}
return result;
}
#endregion
#region
/// <summary>
/// 删除最后结尾的一个逗号
/// </summary>
public static string DelLastComma(string str)
{
return str.Substring(0, str.LastIndexOf(","));
}
/// <summary>
/// 删除最后结尾的指定字符后的字符
/// </summary>
public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
/// <summary>
/// 删除最后结尾的长度
/// </summary>
/// <param name="str"></param>
/// <param name="Length"></param>
/// <returns></returns>
public static string DelLastLength(string str, int Length)
{
if (string.IsNullOrEmpty(str))
return "";
str = str.Substring(0, str.Length - Length);
return str;
}
#endregion
#region
/// <summary>
/// 转换人民币大小金额
/// </summary>
/// <param name="num">金额</param>
/// <returns>返回大写形式</returns>
public static string CmycurD(decimal num)
{
string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
string str3 = ""; //从原num值中取出的值
string str4 = ""; //数字的字符串形式
string str5 = ""; //人民币大写金额形式
int i; //循环变量
int j; //num的值乘以100的字符串长度
string ch1 = ""; //数字的汉语读法
string ch2 = ""; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原num值中取出的值
num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15) { return "溢出"; }
str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for (i = 0; i < j; i++)
{
str3 = str4.Substring(i, 1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0")
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 != "0" && nzero == 0)
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 == "0" && nzero >= 3)
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (j >= 11)
{
ch1 = "";
nzero = nzero + 1;
}
else
{
ch1 = "";
ch2 = str2.Substring(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j - 11) || i == (j - 3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j - 1 && str3 == "0")
{
//最后一位为0时加上“整”
str5 = str5 + '整';
}
}
if (num == 0)
{
str5 = "零元整";
}
return str5;
}
#endregion
#region html所有Img标签src地址
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
// 定义正则表达式用来匹配 img 标签
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(sHtmlText);
int i = 0;
string[] sUrlList = new string[matches.Count];
// 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value;
return sUrlList;
}
#endregion
#region html标签
public static string ReplaceHtmlTag(string html, int length = 0)
{
string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
if (length > 0 && strText.Length > length)
return strText.Substring(0, length);
return strText;
}
#endregion
}
}