From 3b759ef71bb48f9bb6f8445770d20e8ea7921788 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 06 一月 2026 13:58:14 +0800
Subject: [PATCH] Merge branch 'wuhuyancao' of http://139.186.142.91:10010/r/productDev/dmvisit into wuhuyancao

---
 server/jiandaoyun_service/src/main/java/com/doumee/core/jiandaoyun/model/http/ApiClient.java |  214 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 214 insertions(+), 0 deletions(-)

diff --git a/server/jiandaoyun_service/src/main/java/com/doumee/core/jiandaoyun/model/http/ApiClient.java b/server/jiandaoyun_service/src/main/java/com/doumee/core/jiandaoyun/model/http/ApiClient.java
new file mode 100644
index 0000000..679cc20
--- /dev/null
+++ b/server/jiandaoyun_service/src/main/java/com/doumee/core/jiandaoyun/model/http/ApiClient.java
@@ -0,0 +1,214 @@
+package com.doumee.core.jiandaoyun.model.http;
+
+import com.alibaba.fastjson.JSONObject;
+import com.doumee.core.jiandaoyun.util.LimitUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.Charsets;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.io.EmptyInputStream;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.ssl.SSLContextBuilder;
+
+import javax.net.ssl.SSLContext;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+public abstract class ApiClient {
+
+    /**
+     * apiKey
+     */
+    private String apiKey;
+
+    /**
+     * 鍦板潃
+     */
+    private String host;
+
+    /**
+     * 榛樿鐗堟湰
+     */
+    private String defaultVersion;
+
+    /**
+     * 鍚堟硶鐗堟湰
+     */
+    private List<String> validVersionList;
+
+    public ApiClient(String apiKey, String host) {
+        this.apiKey = apiKey;
+        this.host = host;
+    }
+
+    public String getApiKey() {
+        return apiKey;
+    }
+
+    public void setApiKey(String apiKey) {
+        this.apiKey = apiKey;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public void setHost(String host) {
+        this.host = host;
+    }
+
+    public String getDefaultVersion() {
+        return defaultVersion;
+    }
+
+    public void setDefaultVersion(String defaultVersion) {
+        this.defaultVersion = defaultVersion;
+    }
+
+    public List<String> getValidVersionList() {
+        return validVersionList;
+    }
+
+    public void setValidVersionList(List<String> validVersionList) {
+        this.validVersionList = validVersionList;
+    }
+
+
+    /**
+     * 鐢熸垚 path
+     *
+     * @param version - 鐗堟湰鍙�
+     * @param path    - 璺緞
+     * @return 鎺ュ彛鐨勮矾寰�
+     */
+    public abstract String generatePath(String version, String path);
+
+    /**
+     * 鑾峰緱鍚堟硶鐨勭増鏈彿
+     *
+     * @param version - 鐗堟湰鍙�
+     * @return 鍚堟硶鐨勭増鏈彿
+     */
+    public String getValidVersion(String version) {
+        if (this.getValidVersionList() != null && this.getValidVersionList().contains(version)) {
+            return version;
+        }
+        return this.getDefaultVersion();
+    }
+
+    /**
+     * 鍙戦�丳OST璇锋眰
+     *
+     * @param param - 璇锋眰鍙傛暟
+     * @return 鎺ュ彛杩斿洖鍙傛暟
+     */
+    public Map<String, Object> sendPostRequest(HttpRequestParam param) throws Exception {
+        if (param == null || StringUtils.isBlank(param.getPath())) {
+            throw new Exception("缂哄け鍙傛暟锛�");
+        }
+        HttpClient client = getSSLHttpClient();
+        Header[] headers = getHttpHeaders(this.getApiKey());
+        String url = this.host + param.getPath();
+        log.error("===绠�閬撲簯鎺ュ彛url锛�"+url);
+        HttpRequestBase request = new HttpPost(url);
+
+        // 璇锋眰鍙傛暟
+        if (param.getData() != null) {
+            ObjectMapper queryMap = new ObjectMapper();
+            HttpEntity entity = new StringEntity(queryMap.writeValueAsString(param.getData()), Charsets.UTF_8);
+            ((HttpPost) request).setEntity(entity);
+        }
+        // 璁剧疆璇锋眰澶�
+        request.setHeaders(headers);
+        // 闄愭祦闃诲
+        LimitUtil.tryBeforeRun();
+        // 鍙戦�佽姹傚苟鑾峰彇杩斿洖缁撴灉
+        HttpResponse response = client.execute(request);
+        // 杩斿洖鐘舵�佺爜
+        int statusCode = response.getStatusLine().getStatusCode();
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> result = new HashMap<>();
+        // 鏈夐儴鍒嗘帴鍙g洿鎺ヨ繑鍥� 娌℃湁鏁版嵁
+        // fix锛氫笉鑳界敤content-length澶т簬0鍒ゆ柇锛宺esponse header涓篻zip缂栫爜鏂瑰紡鐨勬儏鍐典笅涓�-1
+        if (!(response.getEntity().getContent() instanceof EmptyInputStream)) {
+            result = (Map<String, Object>) mapper.readValue(response.getEntity().getContent(), Object.class);
+        }
+        if (statusCode >= 400) {
+            log.error("===绠�閬撲簯鎺ュ彛锛氳姹傞敊璇紝statusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg"));
+            throw new Exception("璇锋眰閿欒锛宻tatusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg"));
+        } else {
+            // 澶勭悊杩斿洖缁撴灉
+            log.error("===绠�閬撲簯鎺ュ彛锛氳姹傛垚鍔焤esult:" + JSONObject.toJSONString(result));
+            return result;
+        }
+    }
+
+    private static HttpClient getSSLHttpClient() throws Exception {
+        //淇′换鎵�鏈�
+        SSLContext sslContext =
+                new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
+        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
+        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
+    }
+
+    /**
+     * 鑾峰彇璇锋眰澶翠俊鎭�
+     *
+     * @return 璇锋眰澶翠俊鎭�
+     */
+    private Header[] getHttpHeaders(String apiKey) {
+        List<Header> headerList = new ArrayList<>();
+        headerList.add(new BasicHeader("Authorization", "Bearer " + apiKey));
+        headerList.add(new BasicHeader("Content-Type", "application/json;charset=utf-8"));
+        return headerList.toArray(new Header[headerList.size()]);
+    }
+
+    public Map<String, Object> httpPostFile(String url, String token, File file) throws Exception {
+        HttpClient client = getSSLHttpClient();
+        HttpPost httpPost = new HttpPost(url);
+        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+        httpPost.addHeader("token", token);
+        builder.addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, file.getName());
+        // 浼犻�� token
+        builder.addTextBody("token", token);
+        StringBody tokenBody = new StringBody(token, ContentType.MULTIPART_FORM_DATA);
+        builder.addPart("token", tokenBody);
+        HttpEntity entity = builder.build();
+        httpPost.setEntity(entity);
+        // 闄愭祦闃诲
+        LimitUtil.tryBeforeRun();
+        // 鍙戦�佽姹傚苟鑾峰彇杩斿洖缁撴灉
+        HttpResponse response = client.execute(httpPost);
+        // 杩斿洖鐘舵�佺爜
+        int statusCode = response.getStatusLine().getStatusCode();
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> result = new HashMap<>();
+        // 鏈夐儴鍒嗘帴鍙g洿鎺ヨ繑鍥� 娌℃湁鏁版嵁
+        // fix锛氫笉鑳界敤content-length澶т簬0鍒ゆ柇锛宺esponse header涓篻zip缂栫爜鏂瑰紡鐨勬儏鍐典笅涓�-1
+        if (!(response.getEntity().getContent() instanceof EmptyInputStream)) {
+            result = (Map<String, Object>) mapper.readValue(response.getEntity().getContent(), Object.class);
+        }
+        if (statusCode >= 400) {
+            throw new RuntimeException("璇锋眰閿欒锛宻tatusCode:" + statusCode + ",Error Code: " + result.get("code") + ", Error Msg: " + result.get("msg"));
+        } else {
+            // 澶勭悊杩斿洖缁撴灉
+            return result;
+        }
+    }
+}

--
Gitblit v1.9.3