新增调用DS请求相关工具类

20230922-ljl-fixBug
wangchaoxu 1 year ago
parent 06a18a94c5
commit e84d3b5dd5

@ -0,0 +1,36 @@
package com.ms.api;
import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.api.BaseResponse;
import com.jinritemai.cloud.base.api.ExtensionService;
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
import com.ms.api.spi.DsRequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
@ExtensionService("dsRequest")
@Slf4j
public class DsRequestService implements ExtensionServiceHandler<ItemDTO, Integer> {
@Autowired
private DsRequestTemplate dsRequestTemplate;
@Override
public BaseResponse<Integer> handle(BaseRequest<ItemDTO> req) {
Map params = new HashMap<>();
params.put("userId", "2332");
params.put("platform", "ddMicroApp");
params.put("platformOrders", "sdsdsdsd");
params.put("operateInfo", "123,123.123.123.123");
params.put("purchaseSource", "dsfsdfsdf");
try {
dsRequestTemplate.execute("/order/batchAddPurchaseConfirmLog", params);
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
}

@ -0,0 +1,78 @@
package com.ms.api.spi;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.lang.reflect.Field;
import java.util.*;
/**
* DS
*/
@Slf4j
@Component
public class DsRequestTemplate {
@Autowired
private CloudRequestTemplate cloudRequestTemplate;
@Value("${jx.ds.host-prod}")
private String dsHostProd;
@Value("${jx.ds.host-test}")
private String dsHostTest;
@Value("${jx.ds.token-prod}")
private String dsTokenProd;
@Value("${jx.ds.token-test}")
private String dsTokenTest;
private String dsHost;
private String dsToken;
public <T> String execute(String url, T body) throws Exception {
if (StringUtils.isEmpty(dsHost)) {
String activeProfile = System.getenv("sys-deploy-env");
if ("Prod".equals(activeProfile)) {
// 当前为生产环境
dsHost = dsHostProd;
dsToken = dsTokenProd;
} else {
dsHost = dsHostTest;
dsToken = dsTokenTest;
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-app-name", "msddMicro");
String jsonStr = JSON.toJSONString(body);
Map<String, Object> data = JSON.parseObject(jsonStr, Map.class);
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[0]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
sb.append(dsToken);
for (int i = 0; i < keyArray.length; i++) {
if (String.valueOf(data.get(keyArray[i])).length() > 0) {
sb.append(keyArray[i]).append(data.get(keyArray[i]));
}
}
sb.append(dsToken);
String sign = SecureUtil.md5(sb.toString());
headers.set("x-dd-micro-app-sign", sign);
return cloudRequestTemplate.executePost(dsHost + url, body, headers);
}
}

@ -11,4 +11,10 @@ cloud.app.secret=4c927c89-d55c-42cf-805c-694269a4b165
spring.redis.database=0
spring.redis.host=r-2zext1mz5bw1hsochrpd.redis.rds.aliyuncs.com
spring.redis.port=6379
spring.redis.password=Jnk2UBjewhjY&Bhj2
spring.redis.password=Jnk2UBjewhjY&Bhj2
jx.ds.host-prod=https://acn-ds.chengji-inc.com/open
jx.ds.host-test=https://acn-ds-test.chengji-inc.com/open
jx.ds.token-prod=dighgfghf^&)l3sedi&(.D@BC
jx.ds.token-test=dighgfghf^&)l3sedi&(.D@BC

Loading…
Cancel
Save