ERPHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // 提取请求构建逻辑
  80. private JObject BuildRequest(JObject parameters)
  81. {
  82. var request = new JObject { ["token"] = GlobalVar.ERP_TOKEN };
  83. parameters = parameters ?? new JObject();
  84. foreach (var param in parameters)
  85. {
  86. request.Add(param.Key, param.Value);
  87. }
  88. return request;
  89. }
  90. private bool IsTokenExpired(Exception ex)
  91. {
  92. const string expiredFlag = "已与服务器失联";
  93. return ex.Message.Contains(expiredFlag);
  94. }
  95. }
  96. }