CommonHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 
  2. using System;
  3. namespace WingCloudServer.GeneralDocTools.Helper
  4. {
  5. /// <summary>
  6. /// 通用帮助类
  7. /// </summary>
  8. public class CommonHelper
  9. {
  10. public static string EnumApiString = "枚举代码";
  11. public static string NotificationApiString = "通知服务";
  12. /// <summary>
  13. /// 根据输入的字符串判断类型
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string GetJsonDataTypeByString(string typeStr)
  17. {
  18. var enumStr = "";
  19. string result;
  20. //if (typeStr.Contains("AccountString") || typeStr.Contains("BaseParamsString") || typeStr.Contains("BaseString") || typeStr.Contains("EmailString")
  21. // || typeStr.Contains("PasswordString") || typeStr.Contains("PhoneString") || typeStr.Contains("UrlString") || typeStr.Contains("VerificationCode"))
  22. //{
  23. // typeStr = "System.String";
  24. //} else
  25. if (typeStr.Contains("WingInterfaceLibrary") && typeStr.Contains("`1"))
  26. {
  27. typeStr = "Object";
  28. }
  29. else if (typeStr.Contains("`1"))
  30. {
  31. typeStr = "`1";
  32. }
  33. else if (typeStr.Contains("[]") && typeStr != "Byte[]")
  34. {
  35. typeStr = "[]";
  36. }
  37. else if (typeStr.Contains("Enum"))
  38. {
  39. enumStr = typeStr;
  40. typeStr = "Enum";
  41. }
  42. else if (typeStr == "LiveConsultationMemberStatus")
  43. {
  44. enumStr = typeStr;
  45. typeStr = "Enum";
  46. }
  47. switch (typeStr)
  48. {
  49. case "Enum":
  50. result = enumStr;
  51. break;
  52. case "Int32":
  53. result = "Integer";
  54. break;
  55. case "System.Int32":
  56. result = "Integer";
  57. break;
  58. case "Dictionary`2":
  59. result = "Object";
  60. break;
  61. case "Stream":
  62. case "Byte[]":
  63. result = "File";
  64. break;
  65. case "`1":
  66. case "[]":
  67. result = "Array";
  68. break;
  69. case "String":
  70. case "System.String":
  71. result = "String";
  72. break;
  73. case "Decimal":
  74. case "Long":
  75. case "Int16":
  76. case "Int64":
  77. case "System.Decimal":
  78. case "System.Int16":
  79. case "System.Long":
  80. case "System.Int64":
  81. result = "Number";
  82. break;
  83. case "Float":
  84. case "System.Float":
  85. result = "Float";
  86. break;
  87. case "Double":
  88. case "System.Double":
  89. result = "Double";
  90. break;
  91. case "DateTime":
  92. case "System.DateTime":
  93. result = "DateTime";
  94. break;
  95. case "Boolean":
  96. case "System.Boolean":
  97. result = "Boolean";
  98. break;
  99. default:
  100. result = "Object";
  101. break;
  102. }
  103. return result;
  104. }
  105. /// <summary>
  106. /// 计算字符串中子串出现的次数
  107. /// </summary>
  108. /// <param name="str">字符串</param>
  109. /// <param name="substring">子串</param>
  110. /// <returns>出现的次数</returns>
  111. public static int SubstringCount(string str, string substring)
  112. {
  113. if (str.Contains(substring))
  114. {
  115. string strReplaced = str.Replace(substring, "");
  116. return (str.Length - strReplaced.Length) / substring.Length;
  117. }
  118. return 0;
  119. }
  120. /// <summary>
  121. /// 当前时间转long时间戳
  122. /// </summary>
  123. /// <returns></returns>
  124. public static long DateTimeToUnixTime()
  125. {
  126. return (long)(DateTime.Now - TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local)).TotalMilliseconds;
  127. }
  128. /// <summary>
  129. /// 获取特殊的目标值
  130. /// </summary>
  131. /// <returns></returns>
  132. public static long GetSpecicalTargetIndex()
  133. {
  134. System.Threading.Thread.Sleep(1);
  135. long value = 1;
  136. for (int i = 0; i < 5; i++)
  137. {
  138. Random r = new Random();
  139. value = value * r.Next(10, 20);
  140. }
  141. long val = DateTimeToUnixTime() + value;
  142. return val;
  143. }
  144. }
  145. }