AppExtension.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Entity;
  3. using PTMedicalInsurance.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace PTMedicalInsurance.Common
  12. {
  13. public static class AppExtension
  14. {
  15. /// <summary>
  16. /// 获取交易枚举对于的code信息
  17. /// </summary>
  18. public static string GetCode(this TradeEnum tradeEnum)
  19. {
  20. Type type = tradeEnum.GetType();
  21. FieldInfo fd = type.GetField(tradeEnum.ToString());
  22. return getFieldCode(fd);
  23. }
  24. /// <summary>
  25. /// 获取交易描述信息
  26. /// </summary>
  27. /// <param name="tradeEnum"></param>
  28. /// <returns></returns>
  29. public static string GetName(this TradeEnum tradeEnum)
  30. {
  31. Type type = tradeEnum.GetType();
  32. FieldInfo fd = type.GetField(tradeEnum.ToString());
  33. return getFieldName(fd);
  34. }
  35. public static ModeEnum GetMode(this TradeEnum tradeEnum)
  36. {
  37. Type type = tradeEnum.GetType();
  38. FieldInfo fd = type.GetField(tradeEnum.ToString());
  39. TradeAttribute attribute = getAttribte(fd);
  40. if (attribute != null) return attribute.Mode;
  41. // 默认值
  42. return ModeEnum.REST;
  43. }
  44. public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code)
  45. {
  46. Type type = tradeEnum.GetType();
  47. FieldInfo[] fds = type.GetFields();
  48. foreach (FieldInfo fd in fds)
  49. {
  50. TradeAttribute attribute = getAttribte(fd);
  51. if (code.Equals(attribute?.Code))
  52. {
  53. return (TradeEnum)fd.GetValue(tradeEnum);
  54. }
  55. }
  56. return TradeEnum.DEFAULT;
  57. }
  58. private static TradeAttribute getAttribte(FieldInfo fd)
  59. {
  60. TradeAttribute attribute = null;
  61. if (fd == null)
  62. {
  63. return attribute;
  64. }
  65. var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false);
  66. if (attributes != null && attributes.Length == 1)
  67. {
  68. attribute = attributes.Single() as TradeAttribute;
  69. return attribute;
  70. }
  71. return attribute;
  72. }
  73. public static string Text(this JToken jtoken)
  74. {
  75. if (jtoken == null) return string.Empty;
  76. return jtoken.ToString();
  77. }
  78. /// <summary>
  79. /// 转换为前端需要的对象字符串
  80. /// </summary>
  81. /// <param name="text"></param>
  82. /// <returns></returns>
  83. public static string ParseReturnObject(this string text)
  84. {
  85. if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) {
  86. return JsonHelper.setIrisReturnValue(-1, text, null).ToString();
  87. }
  88. JObject joRtn = JObject.Parse(text);
  89. // irisReturn格式
  90. if (joRtn.ContainsKey("errorCode"))
  91. {
  92. return text;
  93. }
  94. return JsonHelper.setIrisReturnValue(0, "", joRtn).ToString();
  95. }
  96. public static int Int(this JToken jtoken)
  97. {
  98. if (jtoken == null) return 0;
  99. return int.Parse(jtoken.ToString());
  100. }
  101. private static string getFieldCode(FieldInfo fd)
  102. {
  103. TradeAttribute attribute = getAttribte(fd);
  104. return attribute?.Code;
  105. }
  106. private static string getFieldName(FieldInfo fd)
  107. {
  108. TradeAttribute attribute = getAttribte(fd);
  109. return attribute?.Name;
  110. }
  111. public static void Extend(this JObject jObject,string key,object value)
  112. {
  113. if (jObject != null && !jObject.ContainsKey(key))
  114. {
  115. jObject.Add(key, new JValue(value));
  116. }
  117. }
  118. }
  119. }