From 2e7cf0157390b2b89a4657a6a78c7147afc09ea8 Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期二, 25 十一月 2025 15:38:49 +0800
Subject: [PATCH] 新增企业微信的接口

---
 src/main/java/com/gs/xky/service/WorkWXService.java |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/gs/xky/service/WorkWXService.java b/src/main/java/com/gs/xky/service/WorkWXService.java
index 6aa3594..44996b2 100644
--- a/src/main/java/com/gs/xky/service/WorkWXService.java
+++ b/src/main/java/com/gs/xky/service/WorkWXService.java
@@ -11,6 +11,7 @@
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -133,6 +134,77 @@
         }
     }
 
+    public List<CheckinData> getCheckinData(long startTime, long endTime, List<String> useridList) throws IOException {
+        String accessToken = getAccessToken();
+        String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=%s", accessToken);
+
+        List<CheckinData> allCheckinData = new ArrayList<>();
+
+        int batchSize = 100;
+        int totalUsers = useridList.size();
+        int batchCount = (totalUsers + batchSize - 1) / batchSize;
+
+        log.info("寮�濮嬭幏鍙栨墦鍗℃暟鎹紝鎬荤敤鎴锋暟: {}, 鍒嗘壒鏁�: {}, 鏃堕棿鑼冨洿: {} - {}", totalUsers, batchCount, startTime, endTime);
+
+        for (int i = 0; i < batchCount; i++) {
+            int fromIndex = i * batchSize;
+            int toIndex = Math.min((i + 1) * batchSize, totalUsers);
+
+            List<String> batchUserList = useridList.subList(fromIndex, toIndex);
+            log.info("姝e湪鑾峰彇绗� {}/{} 鎵规墦鍗℃暟鎹紝鐢ㄦ埛鏁�: {}", i + 1, batchCount, batchUserList.size());
+
+            Map<String, Object> requestBody = new HashMap<>();
+            requestBody.put("opencheckindatatype", 3);
+            requestBody.put("starttime", startTime);
+            requestBody.put("endtime", endTime);
+            requestBody.put("useridlist", batchUserList);
+
+            MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
+            String jsonBody = objectMapper.writeValueAsString(requestBody);
+            RequestBody body = RequestBody.create(mediaType, jsonBody);
+
+            Request request = new Request.Builder()
+                    .url(url)
+                    .post(body)
+                    .addHeader("Content-Type", "application/json; charset=UTF-8")
+                    .build();
+
+            try (Response response = client.newCall(request).execute()) {
+                if (!response.isSuccessful()) {
+                    log.error("鑾峰彇鎵撳崱鏁版嵁澶辫触锛孒TTP鐘舵�佺爜: {}", response.code());
+                    throw new IOException("鑾峰彇鎵撳崱鏁版嵁澶辫触: " + response.message());
+                }
+
+                String responseBody = response.body().string();
+
+                WorkWXCheckinResponse checkinResponse = objectMapper.readValue(responseBody, WorkWXCheckinResponse.class);
+
+                if (checkinResponse.getErrcode() != 0) {
+                    log.error("鑾峰彇鎵撳崱鏁版嵁澶辫触锛岄敊璇爜: {}, 閿欒淇℃伅: {}",
+                            checkinResponse.getErrcode(), checkinResponse.getErrmsg());
+                    throw new IOException("鑾峰彇鎵撳崱鏁版嵁澶辫触: " + checkinResponse.getErrmsg());
+                }
+
+                if (checkinResponse.getCheckindata() != null) {
+                    allCheckinData.addAll(checkinResponse.getCheckindata());
+                    log.info("绗� {}/{} 鎵硅幏鍙栧埌鎵撳崱璁板綍鏁�: {}", i + 1, batchCount, checkinResponse.getCheckindata().size());
+                }
+            }
+
+            if (i < batchCount - 1) {
+                try {
+                    Thread.sleep(500);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    log.warn("鎵规闂寸瓑寰呰涓柇");
+                }
+            }
+        }
+
+        log.info("鎵撳崱鏁版嵁鑾峰彇瀹屾垚锛屾�昏褰曟暟: {}", allCheckinData.size());
+        return allCheckinData;
+    }
+
     @Data
     private static class WorkWXTokenResponse {
         private Integer errcode;
@@ -165,4 +237,33 @@
         @JsonProperty("open_userid")
         private String openUserid;
     }
+
+    @Data
+    private static class WorkWXCheckinResponse {
+        private Integer errcode;
+        private String errmsg;
+        private List<CheckinData> checkindata;
+    }
+
+    @Data
+    public static class CheckinData {
+        private String userid;
+        private String groupname;
+        @JsonProperty("checkin_type")
+        private String checkinType;
+        @JsonProperty("exception_type")
+        private String exceptionType;
+        @JsonProperty("checkin_time")
+        private Long checkinTime;
+        @JsonProperty("location_title")
+        private String locationTitle;
+        @JsonProperty("location_detail")
+        private String locationDetail;
+        private String wifiname;
+        private String notes;
+        private String wifimac;
+        private String mediaids;
+        private Double lat;
+        private Double lng;
+    }
 }

--
Gitblit v1.9.3