常用常量
/// <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>
/// 默认图片
/// </summary>
public static BitmapImage NoPicture { get; set; } = new BitmapImage(new Uri("pack://application:,,,/Image/nopicture.jpg"));
/// <summary>
/// 画刷-白色
/// </summary>
public static SolidColorBrush BackgroundColorWhite = new SolidColorBrush(Colors.White);
/// <summary>
/// 画刷-红色
/// </summary>
public static SolidColorBrush BackgroundColorRed = new SolidColorBrush(Colors.Red);
/// <summary>
/// 画刷-绿色
/// </summary>
public static SolidColorBrush BackgroundColorGreen = new SolidColorBrush(Colors.Green);
/// <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;
}
HTTP常用
/// <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}";
}
字节校验
using System.Linq;
/// <summary>
/// 数据累积和计算
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte GetSum(byte[] data)
{
return data.Aggregate((l, r) => (byte)(l + r));
}
/// <summary>
/// 数据异或计算
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte GetXOR(byte[] data)
{
return data.Aggregate((l, r) => (byte)(l ^ r));
}
IP操作
/// <summary>
/// ip加1
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static string IpAddOne(string ip)
{
if (!string.IsNullOrEmpty(ip) && IPAddress.TryParse(ip, out _))
{
var data = IpToBytes(ip);
if (data[3] <= 254)
{
data[3] += 1;
string newIp = string.Join(".", data);
if (IPAddress.TryParse(newIp, out _))
{
return newIp;
}
}
}
return ip;
}
/// <summary>
/// IP转换成数组形式
/// </summary>
/// <param name="ip">X.X.X.X</param>
/// <returns></returns>
public static byte[] IpToBytes(string ip)
{
var arr = ip.Split('.');
var rslt = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
if (byte.TryParse(arr[i], out byte b))
{
rslt[i] = b;
}
}
return rslt;
}
常用数组
合并数组 (不改变原数组)
/// <summary>
/// 合并T[]数组 (不改变原数组)
/// </summary>
/// <param name="b1"></param>
/// <param name="b2"></param>
/// <returns>合并后的数组</returns>
public static T[] MergeRange<T>(this T[] b1, T[] b2)
{
T[] result = new T[b1.Length + b2.Length];
Array.Copy(b1, 0, result, 0, b1.Length);
Array.Copy(b2, 0, result, b1.Length, b2.Length);
return result;
}
截取数组 (不改变原数组)
/// <summary>
/// 截取数组
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="source">原数组</param>
/// <param name="offset">开始处索引</param>
/// <param name="length">截取长度</param>
/// <returns></returns>
public static T[] CloneRange<T>(this T[] source, int offset, int length)
{
T[] array = new T[length];
Array.Copy(source, offset, array, 0, length);
return array;
}