From 9f7368ceb60b8f5c635cf455914f435d8d782a90 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期六, 21 六月 2025 23:58:03 +0800
Subject: [PATCH] 添加钉钉推送消息功能
---
src/main/java/com/gs/xky/service/ApiService.java | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 49 insertions(+), 1 deletions(-)
diff --git a/src/main/java/com/gs/xky/service/ApiService.java b/src/main/java/com/gs/xky/service/ApiService.java
index f4222d9..fcf42c1 100644
--- a/src/main/java/com/gs/xky/service/ApiService.java
+++ b/src/main/java/com/gs/xky/service/ApiService.java
@@ -1,12 +1,17 @@
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
@@ -14,14 +19,21 @@
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 {
+ public <T, B> ApiResponse<T> sendListRequest(XkyCommonParam<B> requestBody, Class<T> responseType, String url) throws IOException {
// 璁剧疆璇锋眰浣撶殑濯掍綋绫诲瀷涓� application/json
MediaType mediaType = MediaType.parse("application/json");
@@ -55,4 +67,40 @@
// 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;
+ }
}
--
Gitblit v1.9.3