package com.ydtech.modules.equity.components; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.ydtech.exception.SystemException; import com.ydtech.modules.equity.model.request.*; import com.ydtech.modules.equity.model.response.BaseResponse; import com.ydtech.modules.equity.model.response.PageResultResponse; import com.ydtech.modules.equity.model.response.PointRatioDto; import com.ydtech.modules.equity.utils.SMCheckDataUtils; import com.ydtech.modules.order.utils.InsuranceLog; import com.ydtech.utils.HttpUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; /** * 权益包接口 */ @Slf4j @Component @RequiredArgsConstructor public class EquityRequestApiComponents { private final EquityApiConfigurationProperties properties; public B post(String url, @Nullable T requestBody, Class responseType) { String busReqString = JSON.toJSONString(requestBody, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty); InsuranceLog.infoLog("权益包接口", "\n\t---------> URl: {} \n\t---------> 请求参数:{}", url, busReqString); String privateKeyHex = properties.getPrivateKey(); String publicKeyHex = properties.getPublicKey(); String scret = properties.getSecret(); //加签 String signStr = SMCheckDataUtils.sm2SignStr(busReqString,privateKeyHex,publicKeyHex); //加密 String dataStr = SMCheckDataUtils.sm4EncryptStr(busReqString,scret); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); Map requetBean = new HashMap(); requetBean.put("appKey",properties.getAppkey()); requetBean.put("data",dataStr); requetBean.put("sign",signStr); String content = JSON.toJSONString(requetBean); InsuranceLog.infoLog("权益包接口", "\n\t---------> URl: {} \n\t---------> 请求参数: {}", url, content); String postStr = HttpUtil.sendPost(url, content); // 解密参数 String decryptStr = SMCheckDataUtils.sm4DecryptStr(postStr,scret); B res = JSON.parseObject(decryptStr).toJavaObject(responseType); if(!"200".equals(res.code)){ throw new SystemException(res.message); } System.out.println(res.getResult()); return res; } public B request(String url, String apiType, T requestBody, Class responseType) { String apiUrl = url + apiType; return this.post(apiUrl, requestBody, responseType); } /** * 权益包分页列表 * @param threeRightsInfoPageVo * @return */ public PageResultResponse equityPage(ThreeRightsInfoPageVo threeRightsInfoPageVo) { PageResultResponse response = request(properties.getUrl(), "getPageRightInfoList",threeRightsInfoPageVo ,PageResultResponse.class); PageResultResponse res = JSON.parseObject(response.getResult()).toJavaObject(PageResultResponse.class); return res; } // /** // * 权益包列表 // * @param threeUserOrderListVo // * @return // */ // public PageResultResponse equityList(ThreeUserOrderListVo threeUserOrderListVo) { // return null; // } // /** * 权益包下单 * @param threeUserOrderVo * @return */ public BaseResponse createOrder(ThreeUserOrderVo threeUserOrderVo) { BaseResponse response = request(properties.getUrl(), "addThreeUserOrder" ,threeUserOrderVo ,BaseResponse.class); return response; } /** * 权益包确认下单 * @param threeOrderVo */ public BaseResponse confirmOrder(ThreeOrderVo threeOrderVo) { BaseResponse response = request(properties.getUrl(), "updateThreeOrder" ,threeOrderVo ,BaseResponse.class); return response; } /** * 获取积分兑换比例 * @param threePointVo * @return */ public PointRatioDto getPointRatio(ThreePointVo threePointVo) { PointRatioDto response = request(properties.getUrl(), "getPointRatio" ,threePointVo ,PointRatioDto.class); PointRatioDto res = JSON.parseObject(response.getResult()).toJavaObject(PointRatioDto.class); return res; } }