2021-05-27 16:58:40 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Nirvana.Common
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 常用公共类
|
|
|
|
|
|
/// </summary>
|
2025-02-11 13:58:37 +08:00
|
|
|
|
public static class Common
|
2021-05-27 16:58:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
#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;
|
|
|
|
|
|
}
|
|
|
|
|
|
#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>
|
|
|
|
|
|
/// 生成订单号,共21位,订单号规则,V{年月日}{当前登录账户的手机号后四位}{随机八位数}
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="phone">用户手机号</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string OrderNo(string phone)
|
|
|
|
|
|
{
|
|
|
|
|
|
//订单号,共21位,订单号规则,V{年月日}{当前登录账户的手机号后四位}{随机八位数}
|
|
|
|
|
|
string telphone = "0000";
|
|
|
|
|
|
if (!string.IsNullOrEmpty(phone) && phone.Length > 4)
|
|
|
|
|
|
{
|
|
|
|
|
|
telphone = phone.Substring(phone.Length - 4);
|
|
|
|
|
|
}
|
|
|
|
|
|
var orderno = $"{DateTime.Now.ToString("yyyyMMdd")}{telphone}{Common.RndNum(8)}";
|
|
|
|
|
|
return orderno;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 根据GUID获取16位的唯一字符串
|
|
|
|
|
|
public static string Guid16()
|
|
|
|
|
|
{
|
|
|
|
|
|
long i = 1;
|
|
|
|
|
|
foreach (byte b in Guid.NewGuid().ToByteArray())
|
|
|
|
|
|
i *= ((int)b + 1);
|
|
|
|
|
|
return string.Format("{0:x}", i - DateTime.Now.Ticks);
|
|
|
|
|
|
}
|
|
|
|
|
|
#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="starttime">开始时间</param>
|
|
|
|
|
|
/// <param name="endtime">结束时间</param>
|
|
|
|
|
|
/// <param name="t">1-毫秒,2-秒,3-分钟,4-小时,5-天</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static double DateDiff(DateTime starttime, DateTime endtime, int t = 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
TimeSpan ts1 = new TimeSpan(starttime.Ticks);
|
|
|
|
|
|
TimeSpan ts2 = new TimeSpan(endtime.Ticks);
|
|
|
|
|
|
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
|
|
|
|
|
|
if (t == 1) //计算毫秒
|
|
|
|
|
|
{
|
|
|
|
|
|
return ts3.TotalMilliseconds;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (t == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ts3.TotalSeconds;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (t == 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ts3.TotalMinutes;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (t == 4)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ts3.TotalHours;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (t == 5)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ts3.TotalDays;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 获取当前时间戳
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前时间戳
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string GetTimestamp()
|
|
|
|
|
|
{
|
|
|
|
|
|
TimeSpan ts = DateTimeOffset.Now - new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
|
return Convert.ToInt64(ts.TotalSeconds).ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前时间戳,int类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static int GetTimestampInt()
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetTimestamp().ToInt();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 生成随机字符串
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成随机字符串
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string GetNoncestr()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Md5.md5(Guid.NewGuid().ToString(), 32);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 腾讯常用签名生成
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成MD5签名
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="param">参数</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string CreateMd5Sign(Hashtable param, string appkey = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
var str = SortParam(param, appkey);
|
|
|
|
|
|
string Sign = Md5.md5(str, 32).ToUpper();
|
|
|
|
|
|
return Sign;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 组合生成签名的参数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="param"></param>
|
|
|
|
|
|
/// <param name="appkey"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string SortParam(Hashtable param, string appkey = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
ArrayList akeys = new ArrayList(param.Keys);
|
|
|
|
|
|
akeys.Sort(ASCIISort.Create());
|
|
|
|
|
|
foreach (string k in akeys)
|
|
|
|
|
|
{
|
|
|
|
|
|
string v = string.Empty;
|
|
|
|
|
|
if (param[k] != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
v = HttpUtility.UrlEncode(param[k].ToString());
|
|
|
|
|
|
if (v.Contains("%"))
|
|
|
|
|
|
{
|
|
|
|
|
|
v = v.ToUpper();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (null != v && "".CompareTo(v) != 0
|
|
|
|
|
|
&& "sign".CompareTo(k) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sb.Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append(k + "=" + v);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append("&" + k + "=" + v);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrEmpty(appkey))
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append("&app_key=" + appkey);
|
|
|
|
|
|
}
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
// <summary>
|
|
|
|
|
|
/// 根据GUID获取19位的唯一数字序列
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static long GuidToLongID()
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] buffer = Guid.NewGuid().ToByteArray();
|
|
|
|
|
|
return BitConverter.ToInt64(buffer, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-11 13:58:37 +08:00
|
|
|
|
public static T GetListValue<T>(this IList<T> list, int index, T def = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (list.Count > index)
|
|
|
|
|
|
{
|
|
|
|
|
|
return list[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
return def;
|
|
|
|
|
|
}
|
2021-05-27 16:58:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|