From 8ce90f1bf72106489840da64a2de6a0120edc32d Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Fri, 5 Jun 2026 14:23:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=94=AF=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wecom/WecomCallbackController.java | 7 +++++ .../integration/wecom/WecomClient.java | 29 +++++++++++++++++++ .../wecom/WecomUserInfoResponse.java | 19 ++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/main/java/com/company/agentfoundation/integration/wecom/WecomUserInfoResponse.java diff --git a/src/main/java/com/company/agentfoundation/integration/wecom/WecomCallbackController.java b/src/main/java/com/company/agentfoundation/integration/wecom/WecomCallbackController.java index 4ad246b..3aca6af 100644 --- a/src/main/java/com/company/agentfoundation/integration/wecom/WecomCallbackController.java +++ b/src/main/java/com/company/agentfoundation/integration/wecom/WecomCallbackController.java @@ -73,6 +73,13 @@ public class WecomCallbackController { msg == null ? null : msg.getMsgType(), preview(msg == null ? null : msg.getContent())); log.info("wecom callback current user FromUserName={}", msg == null ? null : msg.getFromUserName()); + if (msg != null && msg.getFromUserName() != null && !msg.getFromUserName().isBlank()) { + WecomUserInfoResponse userInfo = wecomClient.getUserInfo(msg.getFromUserName()); + if (userInfo != null && userInfo.success()) { + log.info("wecom callback resolved member userid={} name={} department={}", + userInfo.userId(), userInfo.name(), userInfo.department()); + } + } if (msg == null || msg.getMsgType() == null || !"text".equalsIgnoreCase(msg.getMsgType())) { log.info("wecom callback ignored non-text message"); return "success"; diff --git a/src/main/java/com/company/agentfoundation/integration/wecom/WecomClient.java b/src/main/java/com/company/agentfoundation/integration/wecom/WecomClient.java index 624748e..298ee54 100644 --- a/src/main/java/com/company/agentfoundation/integration/wecom/WecomClient.java +++ b/src/main/java/com/company/agentfoundation/integration/wecom/WecomClient.java @@ -48,6 +48,28 @@ public class WecomClient { log.info("wecom sendText success toUser={}", toUser); } + public WecomUserInfoResponse getUserInfo(String userId) { + log.info("wecom user/get start userid={}", userId); + String token = getAccessToken(); + WecomUserInfoResponse response = webClient.get() + .uri(uriBuilder -> uriBuilder.path("/cgi-bin/user/get") + .queryParam("access_token", token) + .queryParam("userid", userId) + .build()) + .retrieve() + .bodyToMono(WecomUserInfoResponse.class) + .block(); + + if (response == null || !response.success()) { + log.warn("wecom user/get failed userid={} err={}", userId, response == null ? "null" : response.errmsg()); + return response; + } + + log.info("wecom user/get success userid={} name={} department={} mobile={} position={}", + response.userId(), response.name(), response.department(), maskMobile(response.mobile()), response.position()); + return response; + } + private String getAccessToken() { if (cachedToken != null && Instant.now().isBefore(tokenExpireAt)) { log.info("wecom gettoken use cached token expireAt={}", tokenExpireAt); @@ -81,4 +103,11 @@ public class WecomClient { } return text.length() <= 80 ? text : text.substring(0, 80) + "..."; } + + private String maskMobile(String mobile) { + if (mobile == null || mobile.length() < 7) { + return ""; + } + return mobile.substring(0, 3) + "****" + mobile.substring(mobile.length() - 4); + } } diff --git a/src/main/java/com/company/agentfoundation/integration/wecom/WecomUserInfoResponse.java b/src/main/java/com/company/agentfoundation/integration/wecom/WecomUserInfoResponse.java new file mode 100644 index 0000000..d1b9cf3 --- /dev/null +++ b/src/main/java/com/company/agentfoundation/integration/wecom/WecomUserInfoResponse.java @@ -0,0 +1,19 @@ +package com.company.agentfoundation.integration.wecom; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public record WecomUserInfoResponse( + Integer errcode, + String errmsg, + @JsonProperty("userid") String userId, + String name, + List department, + String mobile, + String position +) { + public boolean success() { + return errcode != null && errcode == 0; + } +}