From 3ba118feb585368de68b0a1cf583e192c313a8a6 Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期三, 26 十一月 2025 09:49:14 +0800
Subject: [PATCH] 新增企业微信的接口
---
src/main/java/com/gs/xky/service/WorkWXService.java | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 232 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/gs/xky/service/WorkWXService.java b/src/main/java/com/gs/xky/service/WorkWXService.java
index 430d00f..14753bf 100644
--- a/src/main/java/com/gs/xky/service/WorkWXService.java
+++ b/src/main/java/com/gs/xky/service/WorkWXService.java
@@ -5,14 +5,16 @@
import com.gs.xky.config.DataAcquisitionConfiguration;
import lombok.Data;
import lombok.RequiredArgsConstructor;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
+import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.util.concurrent.TimeUnit;
@Service
@@ -54,8 +56,183 @@
}
log.info("鎴愬姛鑾峰彇浼佷笟寰俊access_token锛屾湁鏁堟湡: {}绉�", tokenResponse.getExpiresIn());
+ log.info("access_token : {}", tokenResponse.getAccessToken());
return tokenResponse.getAccessToken();
}
+ }
+
+ public String getContactAccessToken() throws IOException {
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
+ DataAcquisitionConfiguration.CORPID,
+ DataAcquisitionConfiguration.TXL_CORPSECRET);
+
+ Request request = new Request.Builder()
+ .url(url)
+ .get()
+ .build();
+
+ try (Response response = client.newCall(request).execute()) {
+ if (!response.isSuccessful()) {
+ log.error("鑾峰彇浼佷笟寰俊閫氳褰昦ccess_token澶辫触锛孒TTP鐘舵�佺爜: {}", response.code());
+ throw new IOException("鑾峰彇閫氳褰昦ccess_token澶辫触: " + response.message());
+ }
+
+ String responseBody = response.body().string();
+ WorkWXTokenResponse tokenResponse = objectMapper.readValue(responseBody, WorkWXTokenResponse.class);
+
+ if (tokenResponse.getErrcode() != 0) {
+ log.error("鑾峰彇浼佷笟寰俊閫氳褰昦ccess_token澶辫触锛岄敊璇爜: {}, 閿欒淇℃伅: {}",
+ tokenResponse.getErrcode(), tokenResponse.getErrmsg());
+ throw new IOException("鑾峰彇閫氳褰昦ccess_token澶辫触: " + tokenResponse.getErrmsg());
+ }
+
+ log.info("鎴愬姛鑾峰彇浼佷笟寰俊閫氳褰昦ccess_token锛屾湁鏁堟湡: {}绉�", tokenResponse.getExpiresIn());
+ return tokenResponse.getAccessToken();
+ }
+ }
+
+ public String getUserIdByMobile(String mobile) throws IOException {
+ String accessToken = getContactAccessToken();
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=%s", accessToken);
+
+ Map<String, Object> requestBody = new HashMap<>();
+ requestBody.put("mobile", mobile);
+
+ 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("閫氳繃鎵嬫満鍙疯幏鍙杣serid澶辫触锛孒TTP鐘舵�佺爜: {}", response.code());
+ throw new IOException("鑾峰彇userid澶辫触: " + response.message());
+ }
+
+ String responseBody = response.body().string();
+ log.info("閫氳繃鎵嬫満鍙疯幏鍙杣serid鍝嶅簲: {}", responseBody);
+
+ WorkWXUserIdResponse userIdResponse = objectMapper.readValue(responseBody, WorkWXUserIdResponse.class);
+
+ if (userIdResponse.getErrcode() != 0) {
+ log.error("閫氳繃鎵嬫満鍙疯幏鍙杣serid澶辫触锛岄敊璇爜: {}, 閿欒淇℃伅: {}",
+ userIdResponse.getErrcode(), userIdResponse.getErrmsg());
+ throw new IOException("鑾峰彇userid澶辫触: " + userIdResponse.getErrmsg());
+ }
+
+ log.info("鎴愬姛閫氳繃鎵嬫満鍙� {} 鑾峰彇userid: {}", mobile, userIdResponse.getUserid());
+ return userIdResponse.getUserid();
+ }
+ }
+
+ public List<WorkWXUser> getUserList() throws IOException {
+ String accessToken = getContactAccessToken();
+ String url = String.format(
+ "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=1&fetch_child=1",
+ accessToken);
+
+ Request request = new Request.Builder()
+ .url(url)
+ .get()
+ .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();
+ log.info("鑾峰彇鐢ㄦ埛鍒楄〃鍝嶅簲: {}", responseBody);
+
+ WorkWXUserListResponse userListResponse = objectMapper.readValue(responseBody, WorkWXUserListResponse.class);
+
+ if (userListResponse.getErrcode() != 0) {
+ log.error("鑾峰彇浼佷笟寰俊鐢ㄦ埛鍒楄〃澶辫触锛岄敊璇爜: {}, 閿欒淇℃伅: {}",
+ userListResponse.getErrcode(), userListResponse.getErrmsg());
+ throw new IOException("鑾峰彇鐢ㄦ埛鍒楄〃澶辫触: " + userListResponse.getErrmsg());
+ }
+
+ log.info("鎴愬姛鑾峰彇浼佷笟寰俊鐢ㄦ埛鍒楄〃锛岀敤鎴锋暟閲�: {}",
+ userListResponse.getUserlist() != null ? userListResponse.getUserlist().size() : 0);
+ return userListResponse.getUserlist();
+ }
+ }
+
+ 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
@@ -67,4 +244,56 @@
@JsonProperty("expires_in")
private Integer expiresIn;
}
+
+ @Data
+ private static class WorkWXUserIdResponse {
+ private Integer errcode;
+ private String errmsg;
+ private String userid;
+ }
+
+ @Data
+ private static class WorkWXUserListResponse {
+ private Integer errcode;
+ private String errmsg;
+ private List<WorkWXUser> userlist;
+ }
+
+ @Data
+ public static class WorkWXUser {
+ private String userid;
+ private String name;
+ private List<Integer> department;
+ @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