/// <summary>
/// 常用功能
/// </summary>
public static partial class WSCommFunc
{
/// <summary>
/// "全部"
/// </summary>
public static readonly string AllText = "全部";
/// <summary>
/// 单字符-逗号
/// </summary>
public static readonly char Char_Comma = ',';
/// <summary>
/// 单字符-顿号
/// </summary>
public static readonly char Char_CaesuraSign = '、';
/// <summary>
/// 单字符-竖线
/// </summary>
public static readonly char Char_VerticalBar = '|';
/// <summary>
/// 单字符-连字符
/// </summary>
public static readonly char Char_Hyphen = '-';
/// <summary>
/// 单字符-与和符
/// </summary>
public static readonly char Char_Ampersand = '&';
/// <summary>
/// 单字符-星号
/// </summary>
public static readonly char Char_Asterisk = '*';
/// <summary>
/// 单字符-点号
/// </summary>
public static readonly char Char_Asterisk = '.';
/// <summary>
/// 当月第一天0时0分0秒
/// </summary>
public static readonly DateTime CurrentMonthFirstDay = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
/// <summary>
/// 当月第一天23时59分59秒
/// </summary>
public static readonly DateTime CurrentMonthFirstDayLast = CurrentMonthFirstDay.AddDays(1).AddSeconds(-1);
/// <summary>
/// 当月最后一天23时59分59秒
/// </summary>
public static readonly DateTime CurrentMonthLastSecond = CurrentMonthFirstDay.AddMonths(1).AddSeconds(-1);
/// <summary>
/// 默认最早起始时间 2000年1月1日
/// </summary>
public static readonly DateTime DefualtEarliestDay = new DateTime(2000, 1, 1);
/// <summary>
/// 当前线程信息
/// </summary>
/// <param name="msg"></param>
public static void PrintThreadId(string msg)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss,fff} [{Thread.CurrentThread.ManagedThreadId}]: {msg}");
#endif
}
/// <summary>
/// 日期格式的字符串显示成:yyyy-MM-dd
/// </summary>
/// <param name="dateTimeStr">日期格式的字符串</param>
/// <param name="dateTimeFormatStr">对应的字符串形式,默认yyyyMMddHHmmss</param>
/// <returns></returns>
public static string Str2yyyyMMdd(string dateTimeStr, string dateTimeFormatStr = "yyyyMMddHHmmss")
{
if (DateTime.TryParseExact(dateTimeStr, dateTimeFormatStr, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out DateTime dt))
{
return dt.ToString("yyyy-MM-dd");
}
return string.Empty;
}
/// <summary>
/// 日期格式的字符串显示成:HH:mm:ss
/// </summary>
/// <param name="dateTimeStr">日期格式的字符串</param>
/// <param name="dateTimeFormatStr">对应的字符串形式,默认HHmmss</param>
/// <returns></returns>
public static string Str2HHmmss(string dateTimeStr, string dateTimeFormatStr = "HHmmss")
{
if (DateTime.TryParseExact(dateTimeStr, dateTimeFormatStr, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out DateTime dt))
{
return dt.ToString("HH:mm:ss");
}
return string.Empty;
}
/// <summary>
/// 日期格式的字符串转成标准日期
/// </summary>
/// <param name="dateTimeStr">日期格式的字符串</param>
/// <param name="dateTimeFormatStr">对应的字符串形式,默认yyyyMMddHHmmss</param>
/// <returns></returns>
public static DateTime Str2DateTime(string dateTimeStr, string dateTimeFormatStr = "yyyyMMddHHmmss")
{
if (DateTime.TryParseExact(dateTimeStr, dateTimeFormatStr, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out DateTime dt))
{
return dt;
}
return DateTime.MinValue;
}
/// <summary>
/// DateTime字符串形式(yyyyMMddHHmmss)
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string Date2yyyyMMddHHmmss(DateTime dt)
{
return dt.ToString("yyyyMMddHHmmss");
}
/// <summary>
/// DateTime字符串形式(HHmmss)
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string Date2HHmmss(DateTime dt)
{
return dt.ToString("HHmmss");
}
/// <summary>
/// 生成时间戳
/// </summary>
/// <returns>long</returns>
public static long GenerateTimestamp()
{
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
}
/// <summary>
/// 接口签名
/// </summary>
/// <param name="parameters">业务参数列表</param>
/// <param name="inner">app_id+version</param>
/// <param name="acsConfig"></param>
/// <returns>{签名, 接口参数}</returns>
public static KeyValuePair<string, object> GenerateParam(Dictionary<string, object> parameters, Dictionary<string, object> inner, DtoConfig acsConfig)
{
var src = new Dictionary<string, object>(parameters);
foreach (var item in inner)
{
src.Add(item.Key, item.Value);
}
// 所有待签名参数排序后,使用URL键值对的格式,拼接成字符串。
var order = src.OrderBy(o => o.Key).Select(s => $"{s.Key}={s.Value}");
// 后面追加secret
var value = string.Join("&", order) + acsConfig.Sercret;
// 进行SHA1签名
var sign = ShaHelper.Sha1Encrypt(value);
// 转换为16进制大写字符串
sign = sign.Replace("-", "").ToUpper();
order = parameters.Select(s => $"{s.Key}={s.Value}");
value = string.Join("&", order);
return new KeyValuePair<string, object>(sign, value);
}
/// <summary>
/// 拼接接口参数
/// </summary>
/// <param name="url">接口</param>
/// <param name="param">业务参数</param>
/// <param name="sign">签名参数</param>
/// <returns></returns>
public static string ConcatUrl(string url, object param, string sign)
{
return $"{url}?{param}&sign={sign}";
}
}