CommonFun.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. namespace OASystem.Infrastructure.Tools;
  2. /// <summary>
  3. /// 工具类
  4. /// </summary>
  5. public static class CommonFun
  6. {
  7. public static string GUID => Guid.NewGuid().ToString("N");
  8. public static bool IsNull(this string s)
  9. {
  10. return string.IsNullOrWhiteSpace(s);
  11. }
  12. public static bool NotNull(this string s)
  13. {
  14. return !string.IsNullOrWhiteSpace(s);
  15. }
  16. public static int GetRandom(int minNum, int maxNum)
  17. {
  18. var seed = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
  19. return new Random(seed).Next(minNum, maxNum);
  20. }
  21. public static string GetSerialNumber(string prefix = "")
  22. {
  23. return prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + GetRandom(1000, 9999).ToString();
  24. }
  25. public static string ToJson(this object obj)
  26. {
  27. return JsonSerializer.Serialize(obj);
  28. }
  29. public static T ToObject<T>(this string json)
  30. {
  31. return JsonSerializer.Deserialize<T>(json);
  32. }
  33. public static object GetDefaultVal(string typename)
  34. {
  35. return typename switch
  36. {
  37. "Boolean" => false,
  38. "DateTime" => default(DateTime),
  39. "Date" => default(DateTime),
  40. "Double" => 0.0,
  41. "Single" => 0f,
  42. "Int32" => 0,
  43. "String" => string.Empty,
  44. "Decimal" => 0m,
  45. _ => null,
  46. };
  47. }
  48. public static void CoverNull<T>(T model) where T : class
  49. {
  50. if (model == null)
  51. {
  52. return;
  53. }
  54. var typeFromHandle = typeof(T);
  55. var properties = typeFromHandle.GetProperties();
  56. var array = properties;
  57. for (var i = 0; i < array.Length; i++)
  58. {
  59. var propertyInfo = array[i];
  60. if (propertyInfo.GetValue(model, null) == null)
  61. {
  62. propertyInfo.SetValue(model, GetDefaultVal(propertyInfo.PropertyType.Name), null);
  63. }
  64. }
  65. }
  66. public static void CoverNull<T>(List<T> models) where T : class
  67. {
  68. if (models.Count == 0)
  69. {
  70. return;
  71. }
  72. foreach (var model in models)
  73. {
  74. CoverNull(model);
  75. }
  76. }
  77. public static bool ToBool(this object thisValue, bool errorvalue = false)
  78. {
  79. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool reval))
  80. {
  81. return reval;
  82. }
  83. return errorvalue;
  84. }
  85. #region 文件操作
  86. public static FileInfo[] GetFiles(string directoryPath)
  87. {
  88. if (!IsExistDirectory(directoryPath))
  89. {
  90. throw new DirectoryNotFoundException();
  91. }
  92. var root = new DirectoryInfo(directoryPath);
  93. return root.GetFiles();
  94. }
  95. public static bool IsExistDirectory(string directoryPath)
  96. {
  97. return Directory.Exists(directoryPath);
  98. }
  99. public static string ReadFile(string Path)
  100. {
  101. string s;
  102. if (!File.Exists(Path))
  103. s = "不存在相应的目录";
  104. else
  105. {
  106. var f2 = new StreamReader(Path, Encoding.Default);
  107. s = f2.ReadToEnd();
  108. f2.Close();
  109. f2.Dispose();
  110. }
  111. return s;
  112. }
  113. public static void FileMove(string OrignFile, string NewFile)
  114. {
  115. File.Move(OrignFile, NewFile);
  116. }
  117. public static void CreateDir(string dir)
  118. {
  119. if (dir.Length == 0) return;
  120. if (!Directory.Exists(dir))
  121. Directory.CreateDirectory(dir);
  122. }
  123. #endregion
  124. #region IP
  125. /// <summary>
  126. /// 是否为ip
  127. /// </summary>
  128. /// <param name="ip"></param>
  129. /// <returns></returns>
  130. public static bool IsIP(string ip)
  131. {
  132. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  133. }
  134. public static string GetIP(HttpRequest request)
  135. {
  136. if (request == null)
  137. {
  138. return "";
  139. }
  140. var ip = request.Headers["X-Real-IP"].FirstOrDefault();
  141. if (ip.IsNull())
  142. {
  143. ip = request.Headers["X-Forwarded-For"].FirstOrDefault();
  144. }
  145. if (ip.IsNull())
  146. {
  147. ip = request.HttpContext?.Connection?.RemoteIpAddress?.ToString();
  148. }
  149. if (ip.IsNull() || !IsIP(ip))
  150. {
  151. ip = "127.0.0.1";
  152. }
  153. return ip;
  154. }
  155. #endregion
  156. #region 随机数
  157. /// <summary>
  158. /// 根据自定义随机包含的字符获取指定长度的随机字符
  159. /// </summary>
  160. /// <param name="length">随机字符长度</param>
  161. /// <returns>随机字符</returns>
  162. public static string GetRandomStr(int length)
  163. {
  164. string a = "ABCDEFGHJKLMNPQRSTUVWXYZ012356789";
  165. StringBuilder sb = new StringBuilder();
  166. for (int i = 0; i < length; i++)
  167. {
  168. sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
  169. }
  170. return sb.ToString();
  171. }
  172. /// <summary>
  173. /// 根据自定义随机包含的字符获取指定长度的随机字符
  174. /// </summary>
  175. /// <param name="length">随机字符长度</param>
  176. /// <returns>随机字符</returns>
  177. public static string GetRandomAllStr(int length)
  178. {
  179. string a = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz012356789";
  180. StringBuilder sb = new StringBuilder();
  181. for (int i = 0; i < length; i++)
  182. {
  183. sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
  184. }
  185. return sb.ToString();
  186. }
  187. #endregion
  188. }