tanglong 752e8450bc ss
2025-06-06 15:15:42 +08:00

123 lines
3.6 KiB
C#

using Newtonsoft.Json;
using System;
using System.Runtime.InteropServices;
namespace YD_XinWei.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;
}
public static bool IsNullOrEmpty(this object str)
{
if (str == null)
return true;
return str.ToString() == "";
}
public static bool _windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static string ReplacePath(this string path)
{
if (string.IsNullOrEmpty(path))
return "";
if (_windows)
return path.Replace("/", "\\");
return path.Replace("\\", "/");
}
}
}