wfansh 6 months ago
parent
commit
103a8b993e

+ 22 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/dto/APIStatus.java

@@ -0,0 +1,22 @@
+package org.jeecg.modules.adweb.common.dto;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import lombok.AllArgsConstructor;
+
+/**
+ * @author wfansh
+ */
+@AllArgsConstructor
+public enum APIStatus {
+    SUCCESS(200),
+    BAD_REQUEST(400),
+    UNAUTHORIZED(401),
+    INTERNAL_SERVER_ERROR(500);
+
+    @JsonValue
+    public int getCode() {
+        return code;
+    }
+
+    private final int code;
+}

+ 16 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/dto/OpenAPIRequest.java

@@ -0,0 +1,16 @@
+package org.jeecg.modules.adweb.common.dto;
+
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class OpenAPIRequest<T> {
+
+    private String requestServer;
+
+    private long requestTime;
+
+    private T data;
+}

+ 19 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/dto/OpenAPIResponse.java

@@ -0,0 +1,19 @@
+package org.jeecg.modules.adweb.common.dto;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class OpenAPIResponse<T> {
+
+    @Builder.Default
+    private APIStatus status = APIStatus.SUCCESS;
+
+    private String message;
+
+    private T data;
+}

+ 41 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/util/HttpClientUtils.java

@@ -0,0 +1,41 @@
+package org.jeecg.modules.adweb.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import okhttp3.*;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 基于{@link OkHttpClient}的API访问工具
+ *
+ * @author wfansh
+ */
+@Slf4j
+public class HttpClientUtils {
+
+    private static final MediaType MEDIA_TYPE_JSON =
+            MediaType.get("application/json; charset=utf-8");
+
+    private static OkHttpClient OK_HTTP_CLIENT =
+            new OkHttpClient.Builder()
+                    .connectTimeout(5, TimeUnit.MINUTES)
+                    .readTimeout(5, TimeUnit.MINUTES)
+                    .writeTimeout(5, TimeUnit.MINUTES)
+                    .build();
+
+    private static String postForObject(String url, String json) throws IOException {
+        RequestBody requestBody = RequestBody.create(json, MEDIA_TYPE_JSON);
+        Request request = new Request.Builder().url(url).post(requestBody).build();
+
+        try (Response response = OK_HTTP_CLIENT.newCall(request).execute()) {
+            return response.body().string();
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            return StringUtils.EMPTY;
+        }
+    }
+}