2022-05-18 20:15:25 +08:00
|
|
|
using System;
|
2024-04-16 06:29:21 +08:00
|
|
|
using System.Linq;
|
2022-05-18 20:15:25 +08:00
|
|
|
using System.Security.Cryptography;
|
2019-12-23 16:32:06 +08:00
|
|
|
using System.Text;
|
2022-05-16 11:26:42 +08:00
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Text.Json;
|
2023-10-23 17:39:27 +08:00
|
|
|
using System.Text.Json.Serialization;
|
2019-12-23 16:32:06 +08:00
|
|
|
using System.Text.RegularExpressions;
|
2022-05-16 11:26:42 +08:00
|
|
|
using System.Text.Unicode;
|
2019-12-23 16:32:06 +08:00
|
|
|
|
2024-03-23 07:29:43 +08:00
|
|
|
namespace SimApi.Helpers;
|
|
|
|
|
|
|
|
|
|
public static class SimApiUtil
|
2019-12-23 16:32:06 +08:00
|
|
|
{
|
2024-03-23 07:29:43 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 当前CST时间
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static DateTime CstNow => DateTime.UtcNow.AddHours(8);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// JSON序列化常规选项
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static JsonSerializerOptions JsonOption => new()
|
2019-12-23 16:32:06 +08:00
|
|
|
{
|
2024-03-23 07:29:43 +08:00
|
|
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
|
|
|
};
|
2023-10-23 17:39:27 +08:00
|
|
|
|
2024-03-23 07:29:43 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 当前秒级时间戳
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static double TimestampNow => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检测手机号是否正确
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cell">手机号码</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool CheckCell(string cell)
|
|
|
|
|
{
|
|
|
|
|
var regex = new Regex("^1[3456789]\\d{9}$");
|
|
|
|
|
return regex.IsMatch(cell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// MD5加密字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">源字符串</param>
|
|
|
|
|
/// <param name="mode">加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string Md5(string source, string mode = "x2")
|
|
|
|
|
{
|
2024-04-23 12:27:36 +08:00
|
|
|
var sourceBytes = Encoding.UTF8.GetBytes(source);
|
|
|
|
|
var result = MD5.HashData(sourceBytes);
|
|
|
|
|
var stringBuilder = new StringBuilder(40);
|
2024-03-23 07:29:43 +08:00
|
|
|
foreach (var t in result)
|
2022-05-18 20:37:41 +08:00
|
|
|
{
|
2024-04-23 12:27:36 +08:00
|
|
|
stringBuilder.Append(t.ToString(mode));
|
2019-12-23 16:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-23 12:27:36 +08:00
|
|
|
return stringBuilder.ToString();
|
2024-03-23 07:29:43 +08:00
|
|
|
}
|
2019-12-23 16:32:06 +08:00
|
|
|
|
2024-03-23 07:29:43 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 将对象序列化成JSON (控制台输出中文不会被编码)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string Json(object obj)
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.Serialize(obj, JsonOption);
|
2019-12-23 16:32:06 +08:00
|
|
|
}
|
2024-04-16 06:29:21 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <param name="page">页码</param>
|
|
|
|
|
/// <param name="count">每页数量</param>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int count)
|
|
|
|
|
{
|
|
|
|
|
if (page < 1)
|
|
|
|
|
page = 1;
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
count = 10;
|
|
|
|
|
return query.Skip((page - 1) * count).Take(count);
|
|
|
|
|
}
|
2019-12-23 16:32:06 +08:00
|
|
|
}
|