|
@@ -0,0 +1,268 @@
|
|
|
+using System;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System.Globalization;
|
|
|
+using WingInterfaceLibrary.Enum;
|
|
|
+
|
|
|
+namespace WingAIDiagnosisService.Common
|
|
|
+{
|
|
|
+ public static class ConvertHelper
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// convert object to Json string
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj">object</param>
|
|
|
+ /// <param name="formatting">format</param>
|
|
|
+ /// <param name="settings">settings</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string ToJson(this object obj, Formatting formatting = Formatting.None, JsonSerializerSettings settings = null)
|
|
|
+ {
|
|
|
+ if (obj == null) return string.Empty;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return JsonConvert.SerializeObject(obj, formatting, settings);
|
|
|
+ }
|
|
|
+ catch { return string.Empty; }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert Json string to object
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">object type</typeparam>
|
|
|
+ /// <param name="jsonStr">Json string</param>
|
|
|
+ /// <param name="settings">settings</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T JsonToObj<T>(this string jsonStr, JsonSerializerSettings settings = null)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(jsonStr)) return default(T);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return JsonConvert.DeserializeObject<T>(jsonStr, settings);
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Console.WriteLine(ex.Message);
|
|
|
+ return default(T);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert Json string to object
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="jsonStr">Json string</param>
|
|
|
+ /// <param name="type">object type</param>
|
|
|
+ /// <param name="settings">settings</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static object JsonToObj(this string jsonStr, Type type, JsonSerializerSettings settings = null)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(jsonStr)) return type.IsValueType ? Activator.CreateInstance(type) : null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return JsonConvert.DeserializeObject(jsonStr, type, settings);
|
|
|
+ }
|
|
|
+ catch { return type.IsValueType ? Activator.CreateInstance(type) : null; }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// convert sting to int
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str">string</param>
|
|
|
+ /// <param name="defaultValue">default Value</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static int ToInt(this string str, int defaultValue = 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return defaultValue;
|
|
|
+ int.TryParse(str, out defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double ToDouble(this string str, double defaultValue = 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return defaultValue;
|
|
|
+ double.TryParse(str, out defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert sting to float
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str"></param>
|
|
|
+ /// <param name="defaultValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static float ToFloat(this string str, float defaultValue = 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return defaultValue;
|
|
|
+ float.TryParse(str, out defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert sting to DateTime
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str"></param>
|
|
|
+ /// <param name="defaultValue"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static DateTime ToDateTime(this string str)
|
|
|
+ {
|
|
|
+ DateTime defaultValue;
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return DateTime.MinValue;
|
|
|
+ DateTime.TryParse(str, out defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert sting to int
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str">string</param>
|
|
|
+ /// <param name="defaultValue">default Value</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static long ToLong(this string str, long defaultValue = 0)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return defaultValue;
|
|
|
+ long.TryParse(str, out defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Convert string to other type object
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str">string</param>
|
|
|
+ /// <param name="type">type</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static object ToObject(this string str, Type type)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return Convert.ChangeType(str, type, CultureInfo.InvariantCulture);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Console.WriteLine(ex.Message);
|
|
|
+ return type.IsValueType ? Activator.CreateInstance(type) : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// convert sting to Boolean
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str">sting</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static bool ToBool(this string str)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str)) return false;
|
|
|
+ bool.TryParse(str, out bool val);
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 验证是否为空
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="str">sting</param>
|
|
|
+ /// <returns>true</returns>
|
|
|
+ public static bool IsEmpty(this string str)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(str))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 时间转长整形的时间戳
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="time">当前时间</param>
|
|
|
+ /// <param name="isUtc">是否utc格式时间戳</param>
|
|
|
+ /// <returns>true</returns>
|
|
|
+ public static long ToTimestamp(this DateTime time, bool isUtc = false)
|
|
|
+ {
|
|
|
+ DateTime refTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Utc);
|
|
|
+ var diffSpan = (isUtc ? time : time.ToUniversalTime()) - refTime;
|
|
|
+ long unixTime = (long)Math.Round(diffSpan.TotalMilliseconds, MidpointRounding.AwayFromZero);
|
|
|
+ return unixTime;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// url转token
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="url">url地址</param>
|
|
|
+ /// <returns>true</returns>
|
|
|
+ public static string ToUrlToken(this string url)
|
|
|
+ {
|
|
|
+ string fileToken = "";
|
|
|
+ if (string.IsNullOrWhiteSpace(url))
|
|
|
+ {
|
|
|
+ return fileToken;
|
|
|
+ }
|
|
|
+ var fileName = "";
|
|
|
+ if (url.Contains("http://") || url.Contains("https://"))
|
|
|
+ {
|
|
|
+ fileToken = TransferUrlToToken(url);
|
|
|
+ return fileToken;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ fileName = url;
|
|
|
+ }
|
|
|
+ //处理 传的是token 的情况
|
|
|
+ if (string.IsNullOrEmpty(fileName))
|
|
|
+ {
|
|
|
+ return fileToken;
|
|
|
+ }
|
|
|
+ if (fileName.Split('.').Length > 1)
|
|
|
+ {
|
|
|
+ int lastIndex = fileName.LastIndexOf('.');
|
|
|
+ var fileExtend = fileName.Substring(lastIndex + 1);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var fileTypeEnum = (WingInterfaceLibrary.Enum.UploadFileTypeEnum)Enum.Parse(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), fileExtend.ToUpper());
|
|
|
+ var fileType = Enum.GetName(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), fileTypeEnum.GetHashCode());
|
|
|
+ if (!string.IsNullOrEmpty(fileType))
|
|
|
+ {
|
|
|
+ fileName = fileName.Substring(0, lastIndex);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ fileName = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ fileName = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fileName.Split('_').Length > 1)
|
|
|
+ {
|
|
|
+ fileToken = fileName;
|
|
|
+ }
|
|
|
+ return fileToken;
|
|
|
+ }
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+ /// url转token
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="url">url地址</param>
|
|
|
+ /// <returns>true</returns>
|
|
|
+ private static string TransferUrlToToken(string url)
|
|
|
+ {
|
|
|
+ //需要做处理
|
|
|
+ var newUrl = url.Replace("http://", "").Replace("https://", "");
|
|
|
+ var urlArray = newUrl.Split('/');
|
|
|
+ var storageServer = WingServerCommon.Config.ConfigurationManager.GetParammeter<WingServerCommon.Config.Parameters.StringParameter>("Storage", "StorageServer").Value;
|
|
|
+ if (storageServer == "COS") {
|
|
|
+ if (urlArray.Length >= 2)
|
|
|
+ {
|
|
|
+ var storageSettingsParam = WingServerCommon.Config.ConfigurationManager.StorageSettings;
|
|
|
+ var fileName = storageSettingsParam.CDNServerUrl + "/" + urlArray[1];
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|