|
@@ -0,0 +1,73 @@
|
|
|
+package org.jeecg.modules.adweb.dmp.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.common.util.FastJsonUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * GTM容器和标签管理
|
|
|
+ *
|
|
|
+ * @author wfansh
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping(TradeSparqController.ROOT_PATH)
|
|
|
+@Slf4j
|
|
|
+public class TradeSparqController {
|
|
|
+ static final String ROOT_PATH = "/tradesparq";
|
|
|
+
|
|
|
+ @Value("${tradesparq.api.host}")
|
|
|
+ private String apiHost;
|
|
|
+
|
|
|
+ @Value("${tradesparq.api.id}")
|
|
|
+ private String apiId;
|
|
|
+
|
|
|
+ @Value("${tradesparq.api.secret}")
|
|
|
+ private String apiSecret;
|
|
|
+
|
|
|
+ private RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ @PostMapping("/**")
|
|
|
+ @ResponseBody
|
|
|
+ public Result<JSONObject> doPost(
|
|
|
+ @RequestBody JSONObject requestBody, HttpServletRequest httpRequest) {
|
|
|
+ // 1. 生成签名
|
|
|
+ String sortedValues =
|
|
|
+ requestBody.keySet().stream()
|
|
|
+ .sorted()
|
|
|
+ .map(key -> FastJsonUtil.toJSONString(requestBody.get(key)))
|
|
|
+ .collect(Collectors.joining());
|
|
|
+ String sign = DigestUtils.md5DigestAsHex((apiSecret + sortedValues).getBytes());
|
|
|
+
|
|
|
+ // 2. 构建并发送POST请求
|
|
|
+ String apiPath = httpRequest.getServletPath().split(ROOT_PATH)[1];
|
|
|
+ RequestEntity<String> requestEntity =
|
|
|
+ RequestEntity.post(apiHost + apiPath)
|
|
|
+ .headers(this.getHttpHeaders(sign))
|
|
|
+ .body(requestBody.toJSONString());
|
|
|
+ ResponseEntity<JSONObject> responseEntity =
|
|
|
+ restTemplate.exchange(requestEntity, JSONObject.class);
|
|
|
+
|
|
|
+ return Result.ok(responseEntity.getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpHeaders getHttpHeaders(String sign) {
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ httpHeaders.set("X-API-UID", apiId);
|
|
|
+ httpHeaders.set("X-API-REQUEST-SIGN", sign);
|
|
|
+
|
|
|
+ return httpHeaders;
|
|
|
+ }
|
|
|
+}
|