EquityRequestApiComponents.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.ydtech.modules.equity.components;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import com.ydtech.exception.SystemException;
  5. import com.ydtech.modules.equity.model.request.*;
  6. import com.ydtech.modules.equity.model.response.BaseResponse;
  7. import com.ydtech.modules.equity.model.response.PageResultResponse;
  8. import com.ydtech.modules.equity.model.response.PointRatioDto;
  9. import com.ydtech.modules.equity.utils.SMCheckDataUtils;
  10. import com.ydtech.modules.order.utils.InsuranceLog;
  11. import com.ydtech.utils.HttpUtil;
  12. import lombok.RequiredArgsConstructor;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.lang.Nullable;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.web.client.RestTemplate;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * 权益包接口
  23. */
  24. @Slf4j
  25. @Component
  26. @RequiredArgsConstructor
  27. public class EquityRequestApiComponents {
  28. private final EquityApiConfigurationProperties properties;
  29. public <T extends BaseRequest, B extends BaseResponse> B post(String url, @Nullable T requestBody, Class<B> responseType) {
  30. String busReqString = JSON.toJSONString(requestBody, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
  31. InsuranceLog.infoLog("权益包接口", "\n\t---------> URl: {} \n\t---------> 请求参数:{}", url, busReqString);
  32. String privateKeyHex = properties.getPrivateKey();
  33. String publicKeyHex = properties.getPublicKey();
  34. String scret = properties.getSecret();
  35. //加签
  36. String signStr = SMCheckDataUtils.sm2SignStr(busReqString,privateKeyHex,publicKeyHex);
  37. //加密
  38. String dataStr = SMCheckDataUtils.sm4EncryptStr(busReqString,scret);
  39. HttpHeaders httpHeaders = new HttpHeaders();
  40. httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  41. Map requetBean = new HashMap();
  42. requetBean.put("appKey",properties.getAppkey());
  43. requetBean.put("data",dataStr);
  44. requetBean.put("sign",signStr);
  45. String content = JSON.toJSONString(requetBean);
  46. InsuranceLog.infoLog("权益包接口", "\n\t---------> URl: {} \n\t---------> 请求参数: {}", url, content);
  47. String postStr = HttpUtil.sendPost(url, content);
  48. // 解密参数
  49. String decryptStr = SMCheckDataUtils.sm4DecryptStr(postStr,scret);
  50. B res = JSON.parseObject(decryptStr).toJavaObject(responseType);
  51. if(!"200".equals(res.code)){
  52. throw new SystemException(res.message);
  53. }
  54. System.out.println(res.getResult());
  55. return res;
  56. }
  57. public <T extends BaseRequest, B extends BaseResponse> B request(String url, String apiType, T requestBody, Class<B> responseType) {
  58. String apiUrl = url + apiType;
  59. return this.post(apiUrl, requestBody, responseType);
  60. }
  61. /**
  62. * 权益包分页列表
  63. * @param threeRightsInfoPageVo
  64. * @return
  65. */
  66. public PageResultResponse equityPage(ThreeRightsInfoPageVo threeRightsInfoPageVo) {
  67. PageResultResponse response = request(properties.getUrl(), "getPageRightInfoList",threeRightsInfoPageVo ,PageResultResponse.class);
  68. PageResultResponse res = JSON.parseObject(response.getResult()).toJavaObject(PageResultResponse.class);
  69. return res;
  70. }
  71. // /**
  72. // * 权益包列表
  73. // * @param threeUserOrderListVo
  74. // * @return
  75. // */
  76. // public PageResultResponse equityList(ThreeUserOrderListVo threeUserOrderListVo) {
  77. // return null;
  78. // }
  79. //
  80. /**
  81. * 权益包下单
  82. * @param threeUserOrderVo
  83. * @return
  84. */
  85. public BaseResponse createOrder(ThreeUserOrderVo threeUserOrderVo) {
  86. BaseResponse response = request(properties.getUrl(), "addThreeUserOrder"
  87. ,threeUserOrderVo ,BaseResponse.class);
  88. return response;
  89. }
  90. /**
  91. * 权益包确认下单
  92. * @param threeOrderVo
  93. */
  94. public BaseResponse confirmOrder(ThreeOrderVo threeOrderVo) {
  95. BaseResponse response = request(properties.getUrl(), "updateThreeOrder"
  96. ,threeOrderVo ,BaseResponse.class);
  97. return response;
  98. }
  99. /**
  100. * 获取积分兑换比例
  101. * @param threePointVo
  102. * @return
  103. */
  104. public PointRatioDto getPointRatio(ThreePointVo threePointVo) {
  105. PointRatioDto response = request(properties.getUrl(), "getPointRatio"
  106. ,threePointVo ,PointRatioDto.class);
  107. PointRatioDto res = JSON.parseObject(response.getResult()).toJavaObject(PointRatioDto.class);
  108. return res;
  109. }
  110. }