140 lines
4.2 KiB
C#
140 lines
4.2 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.ComponentModel;
|
||
using System.Reflection;
|
||
|
||
namespace YD_WeChatApplet.Commons.Utils
|
||
{
|
||
/// <summary>
|
||
/// 工具类
|
||
/// </summary>
|
||
public static class Util
|
||
{
|
||
/// <summary>
|
||
/// 获取短信Code
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string GetSmsCode()
|
||
{
|
||
Random random = new Random();
|
||
return random.Next(1000, 9999).ToString();
|
||
}
|
||
public static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
|
||
{
|
||
try
|
||
{
|
||
int lenth = value.Length - startLen - endLen;
|
||
|
||
string replaceStr = value.Substring(startLen, lenth);
|
||
|
||
string specialStr = string.Empty;
|
||
|
||
for (int i = 0; i < replaceStr.Length; i++)
|
||
{
|
||
specialStr += specialChar;
|
||
}
|
||
|
||
value = value.Replace(replaceStr, specialStr);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw;
|
||
}
|
||
|
||
return value;
|
||
}
|
||
|
||
public static string GetColorValue(string color)
|
||
{
|
||
switch (color)
|
||
{
|
||
case "黄色":
|
||
return "#FFFF00";
|
||
case "绿色":
|
||
return "#008000";
|
||
case "红色":
|
||
return "#FF0000";
|
||
case "蓝色":
|
||
return "#0000FF";
|
||
default:
|
||
return "#0000FF";
|
||
}
|
||
}
|
||
public static string Serialize(this object obj, JsonSerializerSettings formatDate = null)
|
||
{
|
||
if (obj == null) return null;
|
||
formatDate = formatDate ?? new JsonSerializerSettings
|
||
{
|
||
DateFormatString = "yyyy-MM-dd HH:mm:ss"
|
||
};
|
||
return JsonConvert.SerializeObject(obj, formatDate);
|
||
}
|
||
public static int GetInt(this object obj)
|
||
{
|
||
if (obj == null)
|
||
return 0;
|
||
int.TryParse(obj.ToString(), out int _number);
|
||
return _number;
|
||
}
|
||
|
||
private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
|
||
|
||
private static long longTime = 621355968000000000;
|
||
|
||
private static int samllTime = 10000000;
|
||
/// <summary>
|
||
/// 获取时间戳
|
||
/// </summary>
|
||
/// <param name="dateTime"></param>
|
||
/// <returns></returns>
|
||
public static long GetTimeStamp(this DateTime dateTime)
|
||
{
|
||
return (dateTime.ToUniversalTime().Ticks - longTime) / samllTime;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 时间戳转换成日期
|
||
/// </summary>
|
||
/// <param name="timeStamp"></param>
|
||
/// <returns></returns>
|
||
public static DateTime GetTimeSpmpToDate(this object timeStamp)
|
||
{
|
||
if (timeStamp == null) return dateStart;
|
||
DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
|
||
return dateTime;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将 Unix 时间戳(秒)转换为 DateTime
|
||
/// </summary>
|
||
public static DateTime ToDateTime(this long unixTimestamp)
|
||
{
|
||
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将 Unix 时间戳(秒)转换为格式化字符串(默认格式:yyyy-MM-dd HH:mm:ss)
|
||
/// </summary>
|
||
public static string ToDateTimeString(this long unixTimestamp, string format = "yyyy-MM-dd HH:mm:ss")
|
||
{
|
||
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).ToString(format);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将 DateTime 转换为 Unix 时间戳(秒)
|
||
/// </summary>
|
||
public static long ToUnixTimestamp(this DateTime date)
|
||
{
|
||
return new DateTimeOffset(date).ToUnixTimeSeconds();
|
||
}
|
||
|
||
public static bool IsNullOrEmpty(this object str)
|
||
{
|
||
if (str == null)
|
||
return true;
|
||
return str.ToString() == "";
|
||
}
|
||
|
||
}
|
||
}
|