|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Text.Json.Nodes;
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
namespace OMS.NET.Common
|
|
|
{
|
|
|
public class Point
|
|
|
{
|
|
|
public double X { get; set; }
|
|
|
public double Y { get; set; }
|
|
|
}
|
|
|
|
|
|
public class Util
|
|
|
{
|
|
|
public static readonly JsonSerializerOptions options = new()
|
|
|
{
|
|
|
ReadCommentHandling = JsonCommentHandling.Skip, //允许注释
|
|
|
AllowTrailingCommas = true,//允许尾随逗号
|
|
|
//PropertyNamingPolicy = new InstructNamingPolicy(), // 属性名为定制转换
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值
|
|
|
WriteIndented = true, // 美化输出
|
|
|
PropertyNameCaseInsensitive = true,//属性名忽略大小写
|
|
|
};
|
|
|
|
|
|
public static string ObjToBase64(object obj)
|
|
|
{
|
|
|
string utf8 = JsonSerializer.Serialize(obj, options);
|
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(utf8));
|
|
|
}
|
|
|
|
|
|
public static string JsonToBase64(string json)
|
|
|
{
|
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
|
|
|
}
|
|
|
|
|
|
public static string Base64ToJson(string base64)
|
|
|
{
|
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(base64));
|
|
|
}
|
|
|
|
|
|
// public static JsonNode? JsonNodeFromJson(string json)
|
|
|
// {
|
|
|
// return JsonNode.Parse(json);
|
|
|
// }
|
|
|
|
|
|
//===========================================================
|
|
|
|
|
|
public static List<Point> PointsFromBase64(string base64)
|
|
|
{
|
|
|
string utf8 = Base64ToJson(base64);
|
|
|
return PointsFromJson(utf8);
|
|
|
}
|
|
|
|
|
|
public static List<Point> PointsFromJson(string json)
|
|
|
{
|
|
|
return JsonSerializer.Deserialize<List<Point>>(json, options) ?? new List<Point>();
|
|
|
}
|
|
|
|
|
|
public static Point PointFromBase64(string base64)
|
|
|
{
|
|
|
string utf8 = Base64ToJson(base64);
|
|
|
return PointFromJson(utf8);
|
|
|
}
|
|
|
|
|
|
public static Point PointFromJson(string json)
|
|
|
{
|
|
|
return JsonSerializer.Deserialize<Point>(json, options) ?? throw new Exception("转化point类型失败:" + json);
|
|
|
}
|
|
|
|
|
|
//==================================================================================
|
|
|
/// <summary>
|
|
|
/// 麻烦的键值操作
|
|
|
/// </summary>
|
|
|
public static void DetailsTransform(JsonArray details, JsonArray detailsRule)
|
|
|
{
|
|
|
var originalKeys = details.Select(d => d!["key"]?.GetValue<string>())
|
|
|
.Where(k => k != null).ToList();
|
|
|
//好像可以直接拷贝一个新的detail模板,然后继承原有属性即可,顺序可直接继承模板,不用再调整
|
|
|
JsonArray newDetails = new();
|
|
|
foreach (var rule in detailsRule)
|
|
|
{
|
|
|
var key = rule!["name"]?.GetValue<string>()!;
|
|
|
var value = rule["default"];
|
|
|
JsonValueKind newType = rule["default"]!.GetValueKind();
|
|
|
if (originalKeys.Contains(key))
|
|
|
{
|
|
|
int index = originalKeys.IndexOf(key);
|
|
|
JsonValueKind oldType = details.ElementAt(index)!["value"]!.GetValueKind();
|
|
|
//如果模板类型会有变化,原设计中对应的value需要调整下格式
|
|
|
//这里直接放弃旧值,转化来转化去,太麻烦且没必要
|
|
|
if (oldType == newType)
|
|
|
{
|
|
|
value = details.ElementAt(index)!["value"];
|
|
|
}
|
|
|
|
|
|
}
|
|
|
var newDetail = new JsonObject
|
|
|
{
|
|
|
["key"] = key,
|
|
|
["value"] = value
|
|
|
};
|
|
|
newDetails.Add(newDetail);
|
|
|
}
|
|
|
details = newDetails;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成 100000 到 999999 之间的随机整数
|
|
|
/// </summary>
|
|
|
public static int GetRandomNumber6()
|
|
|
{
|
|
|
Random random = new();
|
|
|
int minValue = 100000;
|
|
|
int maxValue = 999999;
|
|
|
return random.Next(minValue, maxValue + 1);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 创建模板id
|
|
|
/// </summary>
|
|
|
public static string CreateTemplateId()
|
|
|
{
|
|
|
// 定义有效字符集
|
|
|
const string validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
Random random = new();
|
|
|
int length = random.Next(8, 15);
|
|
|
string result = "";
|
|
|
for (int i = 0; i < length; i++)
|
|
|
{
|
|
|
int byteValue = random.Next(0, 256);
|
|
|
result += validChars[byteValue % validChars.Length];
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
} |