Sfoglia il codice sorgente

对接企微群列表和企业负责人列表

hxl13994548489 7 mesi fa
parent
commit
4a5befd734

+ 41 - 0
linkwe-service/src/main/java/com/linkwechat/config/common/WxCpConfig.java

@@ -0,0 +1,41 @@
+package com.linkwechat.config.common;
+
+
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 企微SDK配置类
+ */
+@Configuration
+public class WxCpConfig {
+
+    @Value("${wechat.corp-id}")
+    private String corpId;
+
+    @Value("${wechat.corp-secret}")
+    private String corpSecret;
+
+    @Value("${wechat.agent-id}")
+    private Integer agentId;
+
+    /**
+     * 初始化企微客户端
+     */
+    @Bean
+    public WxCpService wxCpService() {
+        // 1. 创建配置对象
+        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
+        config.setCorpId(corpId);
+        config.setCorpSecret(corpSecret);
+        config.setAgentId(agentId);
+        // 2. 创建客户端并绑定配置
+        WxCpServiceImpl wxCpService = new WxCpServiceImpl();
+        wxCpService.setWxCpConfigStorage(config);
+        return wxCpService;
+    }
+}

+ 42 - 32
linkwe-service/src/main/java/com/linkwechat/service/impl/WeGroupFissionServiceImpl.java

@@ -36,6 +36,10 @@ import com.linkwechat.framework.service.TokenService;
 import com.linkwechat.mapper.WeGroupFissionMapper;
 import com.linkwechat.service.*;
 import me.chanjar.weixin.cp.api.WxCpExternalContactService;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.WxCpUserService;
+import me.chanjar.weixin.cp.bean.WxCpUser;
+import me.chanjar.weixin.cp.bean.external.WxCpUserExternalGroupChatInfo;
 import me.chanjar.weixin.cp.bean.external.WxCpUserExternalGroupChatList;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
@@ -78,10 +82,7 @@ public class WeGroupFissionServiceImpl extends ServiceImpl<WeGroupFissionMapper,
     private LocalRightsInfoService localRightsInfoService;
 
     @Autowired
-    private IWeGroupService weGroupService;
-
-    @Autowired
-    private QwCustomerClient qwCustomerClient;
+    private  WxCpService wxCpService;
 
 
     /**
@@ -430,23 +431,27 @@ public class WeGroupFissionServiceImpl extends ServiceImpl<WeGroupFissionMapper,
     @Override
     public List<DistDto> getWecomGroupList() {
         List<DistDto> list = new ArrayList<>();
-        List<WeGroupChatListVo.GroupChat> groupChatList=new ArrayList<>();
-        WeGroupChatListQuery chatListQuery =new WeGroupChatListQuery(0, null, null, 1000) ;
-        WeGroupChatListVo groupChatListVo = qwCustomerClient.getGroupChatList(chatListQuery).getData();
-        if (groupChatListVo.getErrCode().equals(WeErrorCodeEnum.ERROR_CODE_0.getErrorCode())
-                && CollectionUtil.isNotEmpty(groupChatListVo.getGroupChatList())) {
-            groupChatList.addAll(groupChatListVo.getGroupChatList());
-        }
-        if(groupChatList!=null && !groupChatList.isEmpty()){
-            for (WeGroupChatListVo.GroupChat groupChat : groupChatList) {
-                WeGroupChatDetailQuery  weGroupChatDetailQuery =new WeGroupChatDetailQuery();
-                weGroupChatDetailQuery.setChat_id("wwfe67d19509d43ec5");
-                AjaxResult<WeGroupChatDetailVo> groupChatDetail = qwCustomerClient.getGroupChatDetail(weGroupChatDetailQuery);
-                DistDto dto = new DistDto();
-                dto.setVal(groupChat.getChatId());
-                dto.setName(groupChatDetail.getData().getGroupChat().getName());
-                list.add(dto);
-            }
+        //获取企业微信群列表
+        WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
+        try{
+            // 调用获取客户群列表接口
+             WxCpUserExternalGroupChatList wxCpUserExternalGroupChatList = externalContactService.listGroupChat(0, 100, 0, null, null);
+             if(wxCpUserExternalGroupChatList!=null && wxCpUserExternalGroupChatList.getGroupChatList()!=null && !wxCpUserExternalGroupChatList.getGroupChatList().isEmpty()){
+                 List<WxCpUserExternalGroupChatList.ChatStatus> groupChatList = wxCpUserExternalGroupChatList.getGroupChatList();
+                 for (WxCpUserExternalGroupChatList.ChatStatus chat : groupChatList) {
+                     WxCpUserExternalGroupChatInfo groupChat = externalContactService.getGroupChat(chat.getChatId());
+                     if (groupChat == null) {
+                         continue;
+                     }
+                     DistDto dto = new DistDto();
+                     dto.setVal(chat.getChatId());
+                     dto.setName(groupChat.getGroupChat().getName());
+                     list.add(dto);
+                 }
+             }
+        }catch (Exception e){
+             log.error(e.getMessage());
+             return new ArrayList<>();
         }
         return list;
     }
@@ -459,20 +464,25 @@ public class WeGroupFissionServiceImpl extends ServiceImpl<WeGroupFissionMapper,
     @Override
     public List<DistDto> getWecomEmployeeList() {
         List<DistDto> list = new ArrayList<>();
-        WeBaseQuery query = new WeBaseQuery();
-        query.setCorpid("wwfe67d19509d43ec5");
-        AjaxResult<WeFollowUserListVo> result = qwCustomerClient.getFollowUserList(query);
-        if(result!=null ){
-            for (String followUserId : result.getData().getFollowUser()) {
-                WeCustomerQuery weCustomerQuery = new WeCustomerQuery();
-                weCustomerQuery.setExternal_userid(followUserId);
-                weCustomerQuery.setCorpid("wwfe67d19509d43ec5");
-                WeCustomerDetailVo customerDetail = qwCustomerClient.getCustomerDetail(weCustomerQuery).getData();
+        //获取外部联系人列表
+        WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
+        try{
+            List<String> followerUserIdList = externalContactService.listFollowers();
+            // 4. 补充成员详情(沿用 4.1.0 版本支持的 getUserService())
+            WxCpUserService userService = wxCpService.getUserService();
+            for (String userId : followerUserIdList) {
+                WxCpUser userDetail = userService.getById(userId);
+                if (userDetail == null) {
+                    continue;
+                }
                 DistDto dto = new DistDto();
-                dto.setVal(customerDetail.getExternalContact().getExternalUserId());
-                dto.setName(customerDetail.getExternalContact().getName());
+                dto.setVal(userId);
+                dto.setName(userDetail.getName());
                 list.add(dto);
             }
+        }catch (Exception e){
+            log.error(e.getMessage());
+            return new ArrayList<>();
         }
         return list;
     }