BaseQdbApi.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.qdb.sdk.api;
  2. import com.alibaba.fastjson2.TypeReference;
  3. import com.qdb.sdk.QdbClient;
  4. import com.qdb.sdk.QdbException;
  5. import com.qdb.sdk.model.response.QdbResponse;
  6. import com.qdb.sdk.util.HttpUtil;
  7. import com.qdb.sdk.util.JsonUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. /**
  10. * 企得宝ERP API 基类
  11. * <p>
  12. * 提供通用的API调用方法,所有业务API类需继承此类。
  13. * 封装了请求执行、响应解析、异常处理等通用逻辑。
  14. * </p>
  15. *
  16. * @author qdb-sdk
  17. * @version 1.0.0
  18. */
  19. @Slf4j
  20. public abstract class BaseQdbApi {
  21. protected final QdbClient client;
  22. protected final HttpUtil httpUtil;
  23. protected BaseQdbApi(QdbClient client) {
  24. this.client = client;
  25. this.httpUtil = client.getHttpUtil();
  26. }
  27. /**
  28. * 执行API调用(返回泛型响应)
  29. *
  30. * @param method API 方法名
  31. * @param request 请求参数对象
  32. * @param <T> data 字段类型
  33. * @return 解析后的响应对象
  34. * @throws QdbException 调用失败时抛出
  35. */
  36. protected <T> QdbResponse<T> execute(String method, Object request) throws QdbException {
  37. return execute(method, request, (Class<T>) null);
  38. }
  39. /**
  40. * 执行API调用(指定data类型)
  41. *
  42. * @param method API 方法名
  43. * @param request 请求参数对象
  44. * @param dataClass data 字段的具体类型,为 null 时不解析 data
  45. * @param <T> data 字段类型
  46. * @return 解析后的响应对象
  47. * @throws QdbException 调用失败时抛出
  48. */
  49. @SuppressWarnings("unchecked")
  50. protected <T> QdbResponse<T> execute(String method, Object request, Class<T> dataClass) throws QdbException {
  51. try {
  52. // 序列化请求参数为 JSON
  53. String businessJson = request != null ? JsonUtil.toJsonString(request) : "{}";
  54. // 执行 HTTP 请求
  55. String responseJson = httpUtil.post(client.getConfig(), method, businessJson);
  56. log.debug("QDB API 原始响应: {}", responseJson);
  57. // 解析通用响应
  58. QdbResponse<T> qdbResponse = JsonUtil.parseObject(responseJson,
  59. new TypeReference<QdbResponse<T>>() {});
  60. if (qdbResponse == null) {
  61. throw new QdbException("API 响应解析失败,响应为空");
  62. }
  63. // 检查业务是否成功
  64. if (!qdbResponse.isSuccess()) {
  65. String errorMsg = qdbResponse.getMsg() != null ? qdbResponse.getMsg() : "未知错误";
  66. String errorCode = qdbResponse.getErrorCode();
  67. throw new QdbException(errorCode, "API 调用失败 [" + method + "]: " + errorMsg);
  68. }
  69. // 如果指定了 dataClass,尝试转换 data 字段
  70. if (dataClass != null && qdbResponse.getData() != null) {
  71. String dataJson = JsonUtil.toJsonString(qdbResponse.getData());
  72. T typedData = JsonUtil.parseObject(dataJson, dataClass);
  73. qdbResponse.setData(typedData);
  74. }
  75. return qdbResponse;
  76. } catch (QdbException e) {
  77. throw e;
  78. } catch (Exception e) {
  79. log.error("API 调用异常 [{}]", method, e);
  80. throw new QdbException("API 调用异常 [" + method + "]: " + e.getMessage(), e);
  81. }
  82. }
  83. /**
  84. * 执行API调用(指定data的TypeReference类型,支持泛型List等)
  85. *
  86. * @param method API 方法名
  87. * @param request 请求参数对象
  88. * @param typeRef data 字段的 TypeReference,用于泛型类型(如 List&lt;SysGoodsVO&gt;)
  89. * @param <T> data 字段类型
  90. * @return 解析后的响应对象
  91. * @throws QdbException 调用失败时抛出
  92. */
  93. @SuppressWarnings("unchecked")
  94. protected <T> QdbResponse<T> execute(String method, Object request, TypeReference<T> typeRef) throws QdbException {
  95. try {
  96. String businessJson = request != null ? JsonUtil.toJsonString(request) : "{}";
  97. String responseJson = httpUtil.post(client.getConfig(), method, businessJson);
  98. log.debug("QDB API 原始响应: {}", responseJson);
  99. QdbResponse<T> qdbResponse = JsonUtil.parseObject(responseJson,
  100. new TypeReference<QdbResponse<T>>() {});
  101. if (qdbResponse == null) {
  102. throw new QdbException("API 响应解析失败,响应为空");
  103. }
  104. if (!qdbResponse.isSuccess()) {
  105. String errorMsg = qdbResponse.getMsg() != null ? qdbResponse.getMsg() : "未知错误";
  106. String errorCode = qdbResponse.getErrorCode();
  107. throw new QdbException(errorCode, "API 调用失败 [" + method + "]: " + errorMsg);
  108. }
  109. if (typeRef != null && qdbResponse.getData() != null) {
  110. String dataJson = JsonUtil.toJsonString(qdbResponse.getData());
  111. T typedData = JsonUtil.parseObject(dataJson, typeRef);
  112. qdbResponse.setData(typedData);
  113. }
  114. return qdbResponse;
  115. } catch (QdbException e) {
  116. throw e;
  117. } catch (Exception e) {
  118. log.error("API 调用异常 [{}]", method, e);
  119. throw new QdbException("API 调用异常 [" + method + "]: " + e.getMessage(), e);
  120. }
  121. }
  122. }