ConvertHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using Newtonsoft.Json;
  3. using System.Globalization;
  4. using WingInterfaceLibrary.Enum;
  5. namespace WingDeviceService.Common
  6. {
  7. public static class ConvertHelper
  8. {
  9. /// <summary>
  10. /// convert object to Json string
  11. /// </summary>
  12. /// <param name="obj">object</param>
  13. /// <param name="formatting">format</param>
  14. /// <param name="settings">settings</param>
  15. /// <returns></returns>
  16. public static string ToJson(this object obj, Formatting formatting = Formatting.None, JsonSerializerSettings settings = null)
  17. {
  18. if (obj == null) return string.Empty;
  19. try
  20. {
  21. return JsonConvert.SerializeObject(obj, formatting, settings);
  22. }
  23. catch { return string.Empty; }
  24. }
  25. /// <summary>
  26. /// convert Json string to object
  27. /// </summary>
  28. /// <typeparam name="T">object type</typeparam>
  29. /// <param name="jsonStr">Json string</param>
  30. /// <param name="settings">settings</param>
  31. /// <returns></returns>
  32. public static T JsonToObj<T>(this string jsonStr, JsonSerializerSettings settings = null)
  33. {
  34. if (string.IsNullOrWhiteSpace(jsonStr)) return default(T);
  35. try
  36. {
  37. return JsonConvert.DeserializeObject<T>(jsonStr, settings);
  38. }
  39. catch(Exception ex)
  40. {
  41. Console.WriteLine(ex.Message);
  42. return default(T);
  43. }
  44. }
  45. /// <summary>
  46. /// convert Json string to object
  47. /// </summary>
  48. /// <param name="jsonStr">Json string</param>
  49. /// <param name="type">object type</param>
  50. /// <param name="settings">settings</param>
  51. /// <returns></returns>
  52. public static object JsonToObj(this string jsonStr, Type type, JsonSerializerSettings settings = null)
  53. {
  54. if (string.IsNullOrWhiteSpace(jsonStr)) return type.IsValueType ? Activator.CreateInstance(type) : null;
  55. try
  56. {
  57. return JsonConvert.DeserializeObject(jsonStr, type, settings);
  58. }
  59. catch { return type.IsValueType ? Activator.CreateInstance(type) : null; }
  60. }
  61. /// <summary>
  62. /// convert sting to int
  63. /// </summary>
  64. /// <param name="str">string</param>
  65. /// <param name="defaultValue">default Value</param>
  66. /// <returns></returns>
  67. public static int ToInt(this string str, int defaultValue = 0)
  68. {
  69. if (string.IsNullOrWhiteSpace(str)) return defaultValue;
  70. int.TryParse(str, out defaultValue);
  71. return defaultValue;
  72. }
  73. public static double ToDouble(this string str, double defaultValue = 0)
  74. {
  75. if (string.IsNullOrWhiteSpace(str)) return defaultValue;
  76. double.TryParse(str, out defaultValue);
  77. return defaultValue;
  78. }
  79. /// <summary>
  80. /// convert sting to float
  81. /// </summary>
  82. /// <param name="str"></param>
  83. /// <param name="defaultValue"></param>
  84. /// <returns></returns>
  85. public static float ToFloat(this string str, float defaultValue = 0)
  86. {
  87. if (string.IsNullOrWhiteSpace(str)) return defaultValue;
  88. float.TryParse(str, out defaultValue);
  89. return defaultValue;
  90. }
  91. /// <summary>
  92. /// convert sting to DateTime
  93. /// </summary>
  94. /// <param name="str"></param>
  95. /// <param name="defaultValue"></param>
  96. /// <returns></returns>
  97. public static DateTime ToDateTime(this string str)
  98. {
  99. DateTime defaultValue;
  100. if (string.IsNullOrWhiteSpace(str)) return DateTime.MinValue;
  101. DateTime.TryParse(str, out defaultValue);
  102. return defaultValue;
  103. }
  104. /// <summary>
  105. /// convert sting to int
  106. /// </summary>
  107. /// <param name="str">string</param>
  108. /// <param name="defaultValue">default Value</param>
  109. /// <returns></returns>
  110. public static long ToLong(this string str, long defaultValue = 0)
  111. {
  112. if (string.IsNullOrWhiteSpace(str)) return defaultValue;
  113. long.TryParse(str, out defaultValue);
  114. return defaultValue;
  115. }
  116. /// <summary>
  117. /// Convert string to other type object
  118. /// </summary>
  119. /// <param name="str">string</param>
  120. /// <param name="type">type</param>
  121. /// <returns></returns>
  122. public static object ToObject(this string str, Type type)
  123. {
  124. try
  125. {
  126. return Convert.ChangeType(str, type, CultureInfo.InvariantCulture);
  127. }
  128. catch (Exception ex)
  129. {
  130. Console.WriteLine(ex.Message);
  131. return type.IsValueType ? Activator.CreateInstance(type) : null;
  132. }
  133. }
  134. /// <summary>
  135. /// convert sting to Boolean
  136. /// </summary>
  137. /// <param name="str">sting</param>
  138. /// <returns></returns>
  139. public static bool ToBool(this string str)
  140. {
  141. if (string.IsNullOrWhiteSpace(str)) return false;
  142. bool.TryParse(str, out bool val);
  143. return val;
  144. }
  145. /// <summary>
  146. /// 验证是否为空
  147. /// </summary>
  148. /// <param name="str">sting</param>
  149. /// <returns>true</returns>
  150. public static bool IsEmpty(this string str)
  151. {
  152. if (string.IsNullOrWhiteSpace(str))
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. /// <summary>
  159. /// 时间转长整形的时间戳
  160. /// </summary>
  161. /// <param name="time">当前时间</param>
  162. /// <param name="isUtc">是否utc格式时间戳</param>
  163. /// <returns>true</returns>
  164. public static long ToTimestamp(this DateTime time, bool isUtc = false)
  165. {
  166. DateTime refTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Utc);
  167. var diffSpan = (isUtc ? time : time.ToUniversalTime()) - refTime;
  168. long unixTime = (long)Math.Round(diffSpan.TotalMilliseconds, MidpointRounding.AwayFromZero);
  169. return unixTime;
  170. }
  171. /// <summary>
  172. /// url转token
  173. /// </summary>
  174. /// <param name="url">url地址</param>
  175. /// <returns>true</returns>
  176. public static string ToUrlToken(this string url)
  177. {
  178. string fileToken = "";
  179. if (string.IsNullOrWhiteSpace(url))
  180. {
  181. return fileToken;
  182. }
  183. var fileName = "";
  184. if (url.Contains("http://") || url.Contains("https://"))
  185. {
  186. fileToken = TransferUrlToToken(url);
  187. return fileToken;
  188. }
  189. else
  190. {
  191. fileName = url;
  192. }
  193. //处理 传的是token 的情况
  194. if (string.IsNullOrEmpty(fileName))
  195. {
  196. return fileToken;
  197. }
  198. if (fileName.Split('.').Length > 1)
  199. {
  200. int lastIndex = fileName.LastIndexOf('.');
  201. var fileExtend = fileName.Substring(lastIndex + 1);
  202. try
  203. {
  204. var fileTypeEnum = (WingInterfaceLibrary.Enum.UploadFileTypeEnum)Enum.Parse(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), fileExtend.ToUpper());
  205. var fileType = Enum.GetName(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), fileTypeEnum.GetHashCode());
  206. if (!string.IsNullOrEmpty(fileType))
  207. {
  208. fileName = fileName.Substring(0, lastIndex);
  209. }
  210. else
  211. {
  212. fileName = "";
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. fileName = "";
  218. }
  219. }
  220. if (fileName.Split('_').Length > 1)
  221. {
  222. fileToken = fileName;
  223. }
  224. return fileToken;
  225. }
  226. /// <summary>
  227. /// url转token
  228. /// </summary>
  229. /// <param name="url">url地址</param>
  230. /// <returns>true</returns>
  231. private static string TransferUrlToToken(string url)
  232. {
  233. //需要做处理
  234. var newUrl = url.Replace("http://", "").Replace("https://", "");
  235. var urlArray = newUrl.Split('/');
  236. var storageServer = WingServerCommon.Config.ConfigurationManager.GetParammeter<WingServerCommon.Config.Parameters.StringParameter>("Storage", "StorageServer").Value;
  237. if (storageServer == "COS") {
  238. if (urlArray.Length >= 2)
  239. {
  240. var storageSettingsParam = WingServerCommon.Config.ConfigurationManager.StorageSettings;
  241. var fileName = storageSettingsParam.CDNServerUrl + "/" + urlArray[1];
  242. return fileName;
  243. }
  244. else {
  245. return url;
  246. }
  247. }
  248. else {
  249. return url;
  250. }
  251. }
  252. }
  253. }