StringExtend.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* ==============================================================================
  2. * 功能描述:StringExtend
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/5/29 10:51:45
  5. * ==============================================================================*/
  6. using System;
  7. using Newtonsoft.Json.Linq;
  8. using SAGA.DotNetUtils;
  9. using SAGA.DotNetUtils.Extend;
  10. namespace MBIUtility.Extend
  11. {
  12. /// <summary>
  13. /// StringExtend
  14. /// </summary>
  15. public static class StringExtend
  16. {
  17. /// <summary>
  18. /// 云平台BIMId转化为Revit Id
  19. /// 去掉文件名前缀,找不到返回值为0
  20. /// </summary>
  21. /// <returns></returns>
  22. public static int GetBIMID(this string cbimId)
  23. {
  24. try
  25. {
  26. string bimId = cbimId;
  27. var strs = cbimId.Split(':');
  28. if (strs.Length > 1)
  29. bimId = strs[1];
  30. return bimId.ToInt();
  31. }
  32. catch (Exception e)
  33. {
  34. Console.WriteLine(e);
  35. return 0;
  36. }
  37. }
  38. /// <summary>
  39. /// 仅保留岗位,没有对应模型的bimid
  40. /// 格式为:
  41. /// a,只有楼层 id,没有模型bimid
  42. /// b,""
  43. /// </summary>
  44. /// <returns></returns>
  45. public static bool IsOnlyDutyNoModelBIMID(this string cbimId)
  46. {
  47. var strs = cbimId.Split(':');
  48. return strs.Length != 2;
  49. }
  50. /// <summary>
  51. /// 云平台BIMId转化为Floor Id
  52. /// 去掉文件名后缀
  53. /// </summary>
  54. /// <returns></returns>
  55. public static string GetFloorId(this string cbimId)
  56. {
  57. string bimId = cbimId;
  58. var strs = cbimId.Split(':');
  59. if (strs.Length > 1)
  60. bimId = strs[0];
  61. return bimId;
  62. }
  63. /// <summary>
  64. /// 返回数据是验证-带message
  65. /// </summary>
  66. /// <param name="request"></param>
  67. /// <returns></returns>
  68. public static bool IsSuccessRequest(this string request, ref string msg)
  69. {
  70. bool result = false;
  71. if (request.IsNullOrEmpty()) return result;
  72. try
  73. {
  74. JObject jObject = JObject.Parse(request);
  75. result = jObject["Result"].ToString() == "success";
  76. msg = jObject["ResultMsg"].ToString();
  77. }
  78. catch (Exception e)
  79. {
  80. result = false;
  81. msg = e.StackTrace;
  82. }
  83. return result;
  84. }
  85. /// <summary>
  86. /// 返回数据是验证
  87. /// </summary>
  88. /// <param name="request"></param>
  89. /// <returns></returns>
  90. public static bool IsSuccessRequest(this string request)
  91. {
  92. bool result = false;
  93. if (request.IsNullOrEmpty()) return result;
  94. try
  95. {
  96. JObject jObject = JObject.Parse(request);
  97. result = jObject["Result"].ToString() == "success";
  98. }
  99. catch (Exception e)
  100. {
  101. result = false;
  102. }
  103. return result;
  104. }
  105. /// <summary>
  106. /// 返回数据是否正确
  107. /// 数据格式为result,message
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public static bool IsSuccessRequest2(this string request, ref string msg)
  112. {
  113. bool result = false;
  114. if (request.IsNullOrEmpty()) return result;
  115. try
  116. {
  117. JObject jObject = JObject.Parse(request);
  118. result = jObject["result"].ToString() == "success";
  119. msg = jObject["message"].ToString();
  120. }
  121. catch (Exception e)
  122. {
  123. result = false;
  124. }
  125. return result;
  126. }
  127. /// <summary>
  128. /// 返回的集合不为空集合
  129. /// </summary>
  130. /// <param name="request"></param>
  131. /// <returns></returns>
  132. public static bool IsRequestHasItem(this string request)
  133. {
  134. bool result = false;
  135. if (IsSuccessRequest(request))
  136. {
  137. try
  138. {
  139. JObject jObject = JObject.Parse(request);
  140. if (jObject.IsContainKeyEx("Count"))
  141. {
  142. result = jObject.GetValueEx("Count").ToInt() != 0;
  143. }
  144. else
  145. {
  146. if (jObject.IsContainKeyEx("Content"))
  147. result = ((JArray)jObject["Content"]).Count != 0;
  148. }
  149. }
  150. catch (Exception e)
  151. {
  152. result = false;
  153. }
  154. }
  155. return result;
  156. }
  157. }
  158. }