AppExtension.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. public static ModeEnum GetMode(this TradeEnum tradeEnum)
  25. {
  26. Type type = tradeEnum.GetType();
  27. FieldInfo fd = type.GetField(tradeEnum.ToString());
  28. TradeAttribute attribute = getAttribte(fd);
  29. if (attribute != null) return attribute.Mode;
  30. // 默认值
  31. return ModeEnum.REST;
  32. }
  33. public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code)
  34. {
  35. Type type = tradeEnum.GetType();
  36. FieldInfo[] fds = type.GetFields();
  37. foreach (FieldInfo fd in fds)
  38. {
  39. TradeAttribute attribute = getAttribte(fd);
  40. if (code.Equals(attribute?.Code))
  41. {
  42. return (TradeEnum)fd.GetValue(tradeEnum);
  43. }
  44. }
  45. return TradeEnum.DEFAULT;
  46. }
  47. private static TradeAttribute getAttribte(FieldInfo fd)
  48. {
  49. TradeAttribute attribute = null;
  50. if (fd == null)
  51. {
  52. return attribute;
  53. }
  54. var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false);
  55. if (attributes != null && attributes.Length == 1)
  56. {
  57. attribute = attributes.Single() as TradeAttribute;
  58. return attribute;
  59. }
  60. return attribute;
  61. }
  62. public static string Text(this JToken jtoken)
  63. {
  64. if (jtoken == null) return string.Empty;
  65. return jtoken.ToString();
  66. }
  67. /// <summary>
  68. /// 将字符串转为JObject
  69. /// </summary>
  70. /// <param name="text"></param>
  71. /// <returns></returns>
  72. public static string ParseReturnObject(this string text)
  73. {
  74. if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) {
  75. return JsonHelper.setIrisReturnValue(-1, text, null).ToString();
  76. }
  77. return JsonHelper.setIrisReturnValue(0, "", JObject.Parse(text)).ToString();
  78. }
  79. public static int Int(this JToken jtoken)
  80. {
  81. if (jtoken == null) return 0;
  82. return int.Parse(jtoken.ToString());
  83. }
  84. private static string getFieldCode(FieldInfo fd)
  85. {
  86. TradeAttribute attribute = getAttribte(fd);
  87. return attribute?.Code;
  88. }
  89. public static void Extend(this JObject jObject,string key,object value)
  90. {
  91. if (jObject != null && !jObject.ContainsKey(key))
  92. {
  93. jObject.Add(key, new JValue(value));
  94. }
  95. }
  96. }
  97. }