啊鑫
2025-02-18 f91c09e452ce121a66755e8b6f133efeac4edead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.gs.xky.service;
 
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.gs.xky.config.ApiResponse;
import com.gs.xky.config.DingTalkParam;
import com.gs.xky.config.DingTalkResponse;
import com.gs.xky.config.XkyCommonParam;
import okhttp3.*;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
 
@Service
public class ApiService {
    private final OkHttpClient client;
    private final ObjectMapper objectMapper;
 
 
    public ApiService() {
        this.client = new OkHttpClient.Builder().connectTimeout(90, TimeUnit.SECONDS) // Set connection timeout
                .readTimeout(90, TimeUnit.SECONDS)    // Set read timeout
                .build();
        this.objectMapper = new ObjectMapper();
 
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
 
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }
 
    public <T> ApiResponse<T> sendListRequest(XkyCommonParam requestBody, Class<T> responseType, String url) throws IOException {
        // 设置请求体的媒体类型为 application/json
        MediaType mediaType = MediaType.parse("application/json");
 
        // 将 ApiCommonParam 对象转换为 JSON 字符串
        String jsonBody = objectMapper.writeValueAsString(requestBody);
 
        // 创建请求体
        RequestBody body = RequestBody.create(mediaType, jsonBody);
 
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "*/*")
                .addHeader("Connection", "keep-alive")
                .build();
 
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return objectMapper.readValue(response.body().string(), objectMapper.getTypeFactory().constructParametricType(ApiResponse.class, responseType));
            } else {
                return handleErrorResponse(response);
            }
        }
    }
 
    private <T> ApiResponse<T> handleErrorResponse(Response response) throws IOException {
        ApiResponse<T> errorResponse = new ApiResponse<>();
        errorResponse.setResult(response.code());
//        errorResponse.setError(response.message());
        // Optionally set other fields like list or total
        return errorResponse;
    }
 
    public <T> DingTalkResponse<T> sendListRequest(DingTalkParam requestBody, Class<T> responseType, String url) throws IOException {
 
        // 设置请求体的媒体类型为 application/json
        MediaType mediaType = MediaType.parse("application/json");
 
        // 将 ApiCommonParam 对象转换为 JSON 字符串
        String jsonBody = objectMapper.writeValueAsString(requestBody);
 
        // 创建请求体
        RequestBody body = RequestBody.create(mediaType, jsonBody);
 
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "*/*")
                .addHeader("Connection", "keep-alive")
                .build();
 
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return objectMapper.readValue(response.body().string(), objectMapper.getTypeFactory().constructParametricType(DingTalkResponse.class, responseType));
            } else {
                return handleErrorResponse1(response);
            }
        }
    }
 
    private <T> DingTalkResponse<T> handleErrorResponse1(Response response) throws IOException {
        DingTalkResponse<T> errorResponse = new DingTalkResponse<>();
        errorResponse.setIsSuccess(String.valueOf(response.code()));
//        errorResponse.setError(response.message());
        // Optionally set other fields like list or total
        return errorResponse;
    }
}