ERPHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using JLHHJSvr.BLL;
  2. using JLHHJSvr.Com.Model;
  3. using JLHHJSvr.LJException;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace JLHHJSvr.Helper
  12. {
  13. internal class ERPHelper : HelperBase
  14. {
  15. private JObject BuildLoginRequest()
  16. {
  17. return new JObject
  18. {
  19. { "token", GlobalVar.ERP_TOKEN },
  20. { "account", GlobalVar.ERP_ACCOUNT_NAME },
  21. { "userid", GlobalVar.ERP_ACCOUNT_USERNAME },
  22. { "password", GlobalVar.ERP_ACCOUNT_PASSWORD },
  23. { "clientType", 30 }
  24. };
  25. }
  26. private static readonly Dictionary<string, string> _apiResultMap = new Dictionary<string, string>()
  27. {
  28. {"GetL1Mtrldef", "mtrldefList"},
  29. {"GetSCWorkgroupList", "scworkgroupList"},
  30. {"GetL1Mtrltype", "mtrltypeList"},
  31. {"GetL1ConfigureCode", "resultList"},
  32. {"CommonDynamicSelect", "datatable"},
  33. };
  34. protected void Login()
  35. {
  36. var request = BuildLoginRequest();
  37. try
  38. {
  39. var result = DoExecute("Login", request);
  40. var token = result.GetValue("token").ToObject<string>();
  41. if (!string.IsNullOrEmpty(token))
  42. {
  43. GlobalVar.ERP_TOKEN = token;
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. throw new LJCommonException($"ERP登录失败: {ex.Message}");
  49. }
  50. }
  51. public void CheckLogin()
  52. {
  53. if(string.IsNullOrEmpty(GlobalVar.ERP_TOKEN))
  54. {
  55. Login();
  56. }
  57. }
  58. public List<T> GetERPList<T>(string apiMethod, JObject parameters = null, int tryCnt = 0)
  59. {
  60. CheckLogin();
  61. try
  62. {
  63. var request = BuildRequest(parameters);
  64. var result = DoExecute(apiMethod, request);
  65. if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}");
  66. return result[listKey]?.ToObject<List<T>>() ?? new List<T>();
  67. }
  68. catch (Exception ex) when (IsTokenExpired(ex))
  69. {
  70. if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!");
  71. Login();
  72. return GetERPList<T>(apiMethod, parameters, tryCnt + 1);
  73. }
  74. catch
  75. {
  76. throw;
  77. }
  78. }
  79. public List<T> GetERPList<T>(string apiMethod, ref int total, JObject parameters = null, int tryCnt = 0)
  80. {
  81. CheckLogin();
  82. try
  83. {
  84. var request = BuildRequest(parameters);
  85. var result = DoExecute(apiMethod, request);
  86. if (!_apiResultMap.TryGetValue(apiMethod, out var listKey)) throw new ArgumentException($"Unsupported API Method: {apiMethod}");
  87. total = ((int)result["totalcnt"]);
  88. return result[listKey]?.ToObject<List<T>>() ?? new List<T>();
  89. }
  90. catch (Exception ex) when (IsTokenExpired(ex))
  91. {
  92. if (tryCnt >= 3) throw new LJCommonException("超过最大重连次数,请检查连接!");
  93. Login();
  94. return GetERPList<T>(apiMethod, ref total, parameters, tryCnt + 1);
  95. }
  96. catch
  97. {
  98. throw;
  99. }
  100. }
  101. // 提取请求构建逻辑
  102. private JObject BuildRequest(JObject parameters)
  103. {
  104. var request = new JObject { ["token"] = GlobalVar.ERP_TOKEN };
  105. parameters = parameters ?? new JObject();
  106. foreach (var param in parameters)
  107. {
  108. request.Add(param.Key, param.Value);
  109. }
  110. return request;
  111. }
  112. private bool IsTokenExpired(Exception ex)
  113. {
  114. const string expiredFlag = "已与服务器失联";
  115. return ex.Message.Contains(expiredFlag);
  116. }
  117. }
  118. }