Selaa lähdekoodia

提交CID搜索联系人接口+添加好友接口

cgp 1 viikko sitten
vanhempi
commit
31e3060442

+ 27 - 0
fs-service/src/main/java/com/fs/wxcid/dto/friend/SearchContactRequest.java

@@ -0,0 +1,27 @@
+package com.fs.wxcid.dto.friend;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+
+/**
+ * 搜索联系人请求参数
+ * 对应接口:/friend/SearchContact
+ */
+@Data
+public class SearchContactRequest {
+    /** 来源场景(如 0 表示通用搜索) */
+    @JsonProperty("FromScene")
+    private Integer FromScene;
+
+    /** 操作码(通常为 2) */
+    @JsonProperty("OpCode")
+    private Integer OpCode;
+
+    /** 搜索场景(如 1 表示微信号搜索) */
+    @JsonProperty("SearchScene")
+    private Integer SearchScene;
+
+    /** 要搜索的微信号 */
+    @JsonProperty("UserName")
+    private String UserName;
+}

+ 86 - 0
fs-service/src/main/java/com/fs/wxcid/dto/friend/SearchContactResponse.java

@@ -0,0 +1,86 @@
+package com.fs.wxcid.dto.friend;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import lombok.Data;
+
+@Data
+public class SearchContactResponse {
+
+    @JSONField(name = "country")
+    private String country;
+
+    @JSONField(name = "small_head_img_url")
+    private String smallHeadImgUrl;
+
+    @JSONField(name = "big_head_img_url")
+    private String bigHeadImgUrl;
+
+    @JSONField(name = "signature")
+    private String signature;
+
+    @JSONField(name = "sex")
+    private Integer sex;
+
+    @JSONField(name = "verify_flag")
+    private Integer verifyFlag;
+
+    @JSONField(name = "personal_card")
+    private Integer personalCard;
+
+    @JSONField(name = "antispam_ticket") // 👈 新增字段
+    private String antispamTicket;
+
+    @JSONField(name = "nick_name")
+    private NickNameWrapper nickName;
+
+    @JSONField(name = "user_name")
+    private UserNameWrapper userName;
+
+    @JSONField(name = "quan_pin")
+    private QuanPinWrapper quanPin;
+
+    @JSONField(name = "pyinitial")
+    private PyInitialWrapper pyinitial;
+
+    // --- Wrappers ---
+    @Data
+    public static class NickNameWrapper {
+        @JSONField(name = "str")
+        private String str;
+    }
+
+    @Data
+    public static class UserNameWrapper {
+        @JSONField(name = "str")
+        private String str;
+    }
+
+    @Data
+    public static class QuanPinWrapper {
+        @JSONField(name = "str")
+        private String str;
+    }
+
+    @Data
+    public static class PyInitialWrapper {
+        @JSONField(name = "str")
+        private String str;
+    }
+
+    // --- Helper Getters ---
+    public String getNickNameStr() {
+        return nickName != null ? nickName.getStr() : null;
+    }
+
+    public String getUserNameStr() {
+        return userName != null ? userName.getStr() : null;
+    }
+
+    public String getQuanPinStr() {
+        return quanPin != null ? quanPin.getStr() : null;
+    }
+
+    public String getPyInitialStr() {
+        return pyinitial != null ? pyinitial.getStr() : null;
+    }
+}

+ 30 - 0
fs-service/src/main/java/com/fs/wxcid/service/FriendService.java

@@ -3,6 +3,7 @@
 package com.fs.wxcid.service;
 
 import com.fs.wxcid.dto.common.ApiResponseCommon;
+import com.fs.wxcid.dto.common.BaseResponse;
 import com.fs.wxcid.dto.friend.*;
 
 /**
@@ -40,5 +41,34 @@ public interface FriendService {
      */
     // 反序列化为泛型响应对象
     ApiResponseCommon<ContactListResponse> getContactList(String authKey, GetContactListRequest request);
+
+
+
+    /**
+     * 搜索联系人
+     * <p>
+     * 对应接口:POST /friend/SearchContact
+     * 支持按昵称、微信号、手机号等模糊搜索。
+     * </p>
+     *
+     * @param authKey    账号唯一标识
+     * @param request 搜索参数(UserName + 场景配置)
+     * @return 统一响应结果
+     */
+    ApiResponseCommon<SearchContactResponse> searchContact(String authKey, SearchContactRequest request);
+
+    /**
+     * 发起好友验证或添加请求
+     * <p>
+     * 对应接口:POST /friend/VerifyUser
+     * 用于主动添加他人,需提供对方的 V3/V4(通常来自扫码或推荐)。
+     * 若无 V3/V4,部分场景可能无法添加。
+     * </p>
+     *
+     * @param authKey    账号唯一标识
+     * @param request 添加请求参数(含 V3、V4、Scene、验证语等)
+     * @return 统一响应结果
+     */
+    ApiResponseCommon<BaseResponse> verifyUser(String authKey, AgreeAddRequest request);
     ContactListResponse getContactListNotKey(Long accountId);
 }

+ 48 - 0
fs-service/src/main/java/com/fs/wxcid/service/impl/FriendServiceImpl.java

@@ -4,6 +4,7 @@ import com.fs.common.exception.CustomException;
 import com.fs.wxcid.ServiceUtils;
 import com.fs.wxcid.dto.common.ApiResponse;
 import com.fs.wxcid.dto.common.ApiResponseCommon;
+import com.fs.wxcid.dto.common.BaseResponse;
 import com.fs.wxcid.dto.friend.*;
 import com.fs.wxcid.dto.login.RequestBaseVo;
 import com.fs.wxcid.service.FriendService;
@@ -71,6 +72,53 @@ public class FriendServiceImpl implements FriendService {
 
     }
 
+    @Override
+    public ApiResponseCommon<SearchContactResponse> searchContact(String authKey, SearchContactRequest request) {
+        log.info("开始搜索联系人,authKey: {}, request: {}", authKey, request);
+
+        String url = BASE_URL + "/friend/SearchContact?key=" + authKey;
+
+        ApiResponseCommon<SearchContactResponse> response = WxWorkHttpUtil.postWithType(
+                url,
+                request,
+                new TypeReference<ApiResponseCommon<SearchContactResponse>>() {}
+        );
+
+        // 校验响应
+        if (response.getCode() != 200 || response.getData() == null) {
+            throw new CustomException("搜索联系人失败: " + response.getText());
+        }
+
+        SearchContactResponse data = response.getData();
+        String userNameStr = data.getUserNameStr();
+        String antispamTicket = data.getAntispamTicket();
+
+        log.info("搜索成功 - V3: {}, V4: {}",
+                userNameStr != null ? userNameStr : "null",
+                antispamTicket != null ? antispamTicket : "null"
+        );
+
+        return response;
+    }
+
+    @Override
+    public ApiResponseCommon<BaseResponse> verifyUser(String authKey, AgreeAddRequest request) {
+        String url = BASE_URL + "/friend/VerifyUser?key=" + authKey;
+
+        ApiResponseCommon<BaseResponse> response = WxWorkHttpUtil.postWithType(
+                url,
+                request,
+                new TypeReference<ApiResponseCommon<BaseResponse>>() {}
+        );
+
+        // 校验通用响应
+        if (response.getCode() != 200 || response.getData() == null) {
+            throw new CustomException("发起好友验证失败: " + response.getText());
+        }
+        log.info("好友验证请求已成功发送");
+        return response;
+    }
+
     @Override
     public ContactListResponse getContactListNotKey(Long accountId) {
         GetContactListRequest request = new GetContactListRequest();