123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using System;
- using Newtonsoft.Json;
- using System.Globalization;
- using WingInterfaceLibrary.Enum;
- namespace WingDeviceService.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;
- }
- }
- }
- }
|