You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Util/JsonUtil.cs

347 lines
12 KiB
C#

2 years ago
using System;
using System.Collections.Generic;
2 years ago
using System.Linq;
2 years ago
using System.Reflection;
2 years ago
using System.Text.RegularExpressions;
2 years ago
using Furion.JsonSerialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Myshipping.Core;
2 years ago
/// <summary>
/// Json序列化工具类
/// </summary>
public static class JsonUtil
{
/// <summary>
/// JSON 字符串转 Object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T ToObject<T>(this string json)
{
json = json.Replace("&nbsp;", "");
return json == null ? default(T) : JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// JSON 字符串转 Object
/// </summary>
/// <param name="Json"></param>
/// <returns></returns>
public static object ToObject(this string Json)
{
return string.IsNullOrEmpty(Json) ? null : JsonConvert.DeserializeObject(Json);
}
/// <summary>
/// Object 转 JSON字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJsonString(this object obj)
{
return obj == null ? string.Empty : JsonConvert.SerializeObject(obj);
}
/// <summary>
/// JSON 字符串转 JObject
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static JObject ToJObject(this string json)
{
return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace("&nbsp;", ""));
}
/// <summary>
/// Dictionary 字符串转 Object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dictionary"></param>
/// <returns></returns>
public static T ToObject<T>(this IDictionary<string, object> dictionary)
{
return dictionary.ToJsonString().ToObject<T>();
}
/// <summary>
/// 把数组转为逗号连接的字符串
/// </summary>
/// <param name="data"></param>
/// <param name="Str"></param>
/// <returns></returns>
public static string ArrayToString(dynamic data, string Str)
{
string resStr = Str;
foreach (var item in data)
{
if (resStr != "")
{
resStr += ",";
}
if (item is string)
{
resStr += item;
}
else
{
resStr += item.ToString();
}
}
return resStr;
}
/// <summary>
/// 判断是否有交集
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public static bool IsArrayIntersection<T>(List<T> list1, List<T> list2)
{
List<T> t = list1.Distinct().ToList();
var exceptArr = t.Except(list2).ToList();
if (exceptArr.Count < t.Count)
{
return true;
}
else
{
return false;
}
}
2 years ago
/// <summary>
/// 处理输入的数据,变为大写,全角改为半角
/// </summary>
/// <param name="model">数据对象</param>
/// <param name="excepProp">不需要转大写的字段</param>
public static void PropToUpper(object model, params string[] excepProp)
{
var props = model.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
var propName = prop.Name;
if (excepProp != null && excepProp.Length > 0)
{
if (!excepProp.Contains(propName))
{
if (prop.PropertyType == typeof(string))
{
object sourceVal = prop.GetValue(model);
if (sourceVal != null)
{
prop.SetValue(model, ToDBC(sourceVal.ToString().ToUpper()));
}
2 years ago
2 years ago
}
}
}
}
}
2 years ago
/// <summary>
/// 全角改为半角
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2 years ago
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
/// <summary>
2 years ago
/// 长TAB键的识别替换空格
2 years ago
/// </summary>
2 years ago
public static string TrimFields(object model)
2 years ago
{
var props = model.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
var propName = prop.Name;
if (prop.PropertyType == typeof(string))
{
object sourceVal = prop.GetValue(model);
if (sourceVal != null)
{
2 years ago
prop.SetValue(model, sourceVal.ToString().Replace(" ", " "));
2 years ago
prop.SetValue(model, sourceVal.ToString().Replace(" ", " "));
2 years ago
prop.SetValue(model, sourceVal.ToString().Replace("", ","));
prop.SetValue(model, sourceVal.ToString().Replace("。", "."));
prop.SetValue(model, sourceVal.ToString().Replace("", "?"));
prop.SetValue(model, sourceVal.ToString().Replace("", "!"));
prop.SetValue(model, sourceVal.ToString().Replace("《", "<<"));
prop.SetValue(model, sourceVal.ToString().Replace("》", ">>"));
prop.SetValue(model, sourceVal.ToString().Replace("》", ">>"));
prop.SetValue(model, sourceVal.ToString().Replace("", ";"));
prop.SetValue(model, sourceVal.ToString().Replace("", "'"));
prop.SetValue(model, sourceVal.ToString().Replace("", "'"));
prop.SetValue(model, sourceVal.ToString().Replace("、", ","));
2 years ago
prop.SetValue(model, sourceVal.ToString().Replace(" ", " "));
1 year ago
if (propName.ToUpper() == "MBLNO"&&! string.IsNullOrEmpty(sourceVal.ToString())) {
if (!Regex.IsMatch(sourceVal.ToString(), @"^[a-zA-Z0-9]+$"))
{
return "提单号存在中文字符或特殊字符";
}
2 years ago
}
if (propName.ToUpper() == "TMBLNO" && !string.IsNullOrEmpty(sourceVal.ToString()))
{
if (!Regex.IsMatch(sourceVal.ToString(), @"^[a-zA-Z0-9]+$"))
{
return "真提单号存在中文字符或特殊字符";
}
}
2 years ago
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "HBLNO")
{
return "分提单号存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VESSEL")
{
return "船名存在中文字符";
2 years ago
}
1 year ago
2 years ago
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VOYNO")
{
return "海关航次存在中文字符";
2 years ago
2 years ago
}
2 years ago
2 years ago
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VOYNOINNER")
{
return "内部航次存在中文字符";
2 years ago
2 years ago
}
1 year ago
2 years ago
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PLACERECEIPT")
{
return "收货地存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PORTLOAD")
{
return "起运港存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PORTDISCHARGE")
{
return "卸货港存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PLACEDELIVERY")
{
return "交货地存在中文字符";
2 years ago
2 years ago
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "DESTINATION")
{
return "目的地存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "MARKS")
{
return "唛头存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "HSCODE")
{
return "HS代码存在中文字符";
}
5 months ago
//if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "DESCRIPTION")
//{
// return "货描存在中文字符";
2 years ago
5 months ago
//}
1 year ago
//if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "CUSTNO")
//{
// return "订舱编号存在中文字符";
2 years ago
1 year ago
//}
2 years ago
//if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PONO")
//{
// return "PONO存在中文字符";
2 years ago
2 years ago
//}
2 years ago
if (sourceVal.ToString().Length>9 && propName.ToUpper() == "VESSELID") {
return "船舶呼号超长";
}
if (sourceVal.ToString().Length > 5&& propName.ToUpper() == "PLACERECEIPTID")
{
return "收货地代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "PLACERECEIPTID")
{
return "收货地代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "PORTLOADID")
{
return "起运港代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "PORTDISCHARGEID")
{
return "卸货港代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "PLACEDELIVERYID")
{
return "交货地代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "DESTINATIONID")
{
return "目的地代码超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "NOBILL")
{
return "提单份数超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "ISSUEPLACEID")
{
return "签单地点代码超长";
}
if (sourceVal.ToString().Length > 1 && propName.ToUpper() == "CARGOID")
{
return "货物标识超长";
}
if (sourceVal.ToString().Length > 5 && propName.ToUpper() == "TRANSPORTID")
{
return "中转港代码超长";
}
2 years ago
2 years ago
}
}
}
2 years ago
return "";
2 years ago
}
2 years ago
}