From e84d3b5dd5691bc6c674053c7444cebe49aa559a Mon Sep 17 00:00:00 2001 From: wangchaoxu Date: Thu, 24 Aug 2023 16:42:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B0=83=E7=94=A8DS=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9B=B8=E5=85=B3=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ms/api/DsRequestService.java | 36 +++++++++ .../com/ms/api/spi/DsRequestTemplate.java | 78 +++++++++++++++++++ .../src/main/resources/application.properties | 8 +- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 ms-biz/src/main/java/com/ms/api/DsRequestService.java create mode 100644 ms-biz/src/main/java/com/ms/api/spi/DsRequestTemplate.java diff --git a/ms-biz/src/main/java/com/ms/api/DsRequestService.java b/ms-biz/src/main/java/com/ms/api/DsRequestService.java new file mode 100644 index 00000000..2b5a0f0a --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/DsRequestService.java @@ -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 { + + @Autowired + private DsRequestTemplate dsRequestTemplate; + + @Override + public BaseResponse handle(BaseRequest 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; + } +} diff --git a/ms-biz/src/main/java/com/ms/api/spi/DsRequestTemplate.java b/ms-biz/src/main/java/com/ms/api/spi/DsRequestTemplate.java new file mode 100644 index 00000000..54fcf162 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/spi/DsRequestTemplate.java @@ -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 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 data = JSON.parseObject(jsonStr, Map.class); + Set 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); + } + +} diff --git a/ms-dal/src/main/resources/application.properties b/ms-dal/src/main/resources/application.properties index 5ab064eb..06a8e50f 100644 --- a/ms-dal/src/main/resources/application.properties +++ b/ms-dal/src/main/resources/application.properties @@ -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 \ No newline at end of file +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