Przeglądaj źródła

refactor: 生产和测试环境的根部门不同,分别调整

wuwenyi 4 dni temu
rodzic
commit
860e7b2cd3

+ 3 - 0
src/main/java/com/qqflow/engine/domain/ewechat/config/EnterpriseWechatProperties.java

@@ -30,4 +30,7 @@ public class EnterpriseWechatProperties {
 
     /** 授权方应用id */
     private Integer agentId;
+
+    /** 根部门 parentId:测试环境 0,生产环境 1 */
+    private Long parentId = 0L;
 }

+ 6 - 3
src/main/java/com/qqflow/engine/domain/ewechat/controller/WecomContactController.java

@@ -1,6 +1,7 @@
 package com.qqflow.engine.domain.ewechat.controller;
 
 import com.qqflow.engine.common.Result;
+import com.qqflow.engine.domain.ewechat.config.EnterpriseWechatProperties;
 import com.qqflow.engine.domain.ewechat.po.EnterpriseWechatDepartment;
 import com.qqflow.engine.domain.ewechat.po.EnterpriseWechatMember;
 import com.qqflow.engine.domain.ewechat.service.WecomContactSyncService;
@@ -19,11 +20,13 @@ import java.util.Map;
 public class WecomContactController {
 
     private final WecomContactSyncService syncService;
+    private final EnterpriseWechatProperties wechatProperties;
 
     @GetMapping("/departments")
-    @Operation(summary = "获取子部门列表")
-    public Result<List<EnterpriseWechatDepartment>> departments(@RequestParam(defaultValue = "0") Long parentId) {
-        return Result.ok(syncService.listSubDepartments(parentId));
+    @Operation(summary = "获取子部门列表(不传 parentId 时使用配置的根部门 ID)")
+    public Result<List<EnterpriseWechatDepartment>> departments(@RequestParam(required = false) Long parentId) {
+        Long effectiveParentId = parentId != null ? parentId : wechatProperties.getParentId();
+        return Result.ok(syncService.listSubDepartments(effectiveParentId));
     }
 
     @GetMapping("/members")

+ 16 - 2
src/main/java/com/qqflow/engine/domain/ewechat/service/EnterpriseWechatMsgService.java

@@ -1,6 +1,7 @@
 package com.qqflow.engine.domain.ewechat.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.qqflow.engine.common.service.RedissonService;
 import com.qqflow.engine.domain.ewechat.api.EnterpriseWechatApi;
 import com.qqflow.engine.domain.ewechat.config.EnterpriseWechatProperties;
 import com.qqflow.engine.domain.ewechat.mapper.EnterpriseWechatCorpMapper;
@@ -14,6 +15,7 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.time.Duration;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -31,6 +33,11 @@ public class EnterpriseWechatMsgService {
     private final EnterpriseWechatCorpMapper wechatCorpMapper;
     private final SysUserMapper sysUserMapper;
     private final SysUserRoleMapper sysUserRoleMapper;
+    private final RedissonService redissonService;
+
+    private static final String ACCESS_TOKEN_CACHE_KEY = "wecom:access_token:";
+    /** access_token 有效期 7200 秒,缓存 7000 秒留余量 */
+    private static final Duration ACCESS_TOKEN_TTL = Duration.ofSeconds(7000);
 
     /** 给指定企微用户ID列表发送文本消息 */
     public void sendTextToUsers(List<String> wecomUserIds, String content) {
@@ -76,7 +83,7 @@ public class EnterpriseWechatMsgService {
                 .collect(Collectors.toList());
     }
 
-    /** 获取企业 access_token(供同步服务等复用) */
+    /** 获取企业 access_token(Redis 缓存,TTL 7000s) */
     public String getCorpAccessToken() {
         List<EnterpriseWechatCorp> corps = wechatCorpMapper.selectList(
                 new LambdaQueryWrapper<EnterpriseWechatCorp>()
@@ -85,8 +92,15 @@ public class EnterpriseWechatMsgService {
                         .last("LIMIT 1"));
         if (corps.isEmpty()) return null;
         EnterpriseWechatCorp corp = corps.get(0);
+
+        String cacheKey = ACCESS_TOKEN_CACHE_KEY + corp.getCorpId();
+        String cached = redissonService.get(cacheKey);
+        if (cached != null) return cached;
+
         try {
-            return enterpriseWechatApi.getAccessToken(corp.getCorpId(), corp.getPermanentCode()).getAccessToken();
+            String token = enterpriseWechatApi.getAccessToken(corp.getCorpId(), corp.getPermanentCode()).getAccessToken();
+            redissonService.set(cacheKey, token, ACCESS_TOKEN_TTL);
+            return token;
         } catch (Exception e) {
             log.error("[企微消息] 获取 access_token 失败, corpId={}", corp.getCorpId(), e);
             return null;

+ 1 - 0
src/main/resources/application-dev.yml

@@ -50,6 +50,7 @@ enterprise-wechat:
   encodingAESKey: IycRI8M3tgTIHrFiwWnYWJn9I1o8iIsQrhNy8M4MWnT
   suite-id: dk0580416972e402b8
   suite-secret: 3aV48BTZHxDBeltZgdiRAUhttqsE5RxNs5pzElGhHHg
+  parent-id: 0
 
 logging:
   level:

+ 2 - 1
src/main/resources/application-prod.yml

@@ -34,4 +34,5 @@ enterprise-wechat:
   encoding-a-eSKey: IycRI8M3tgTIHrFiwWnYWJn9I1o8iIsQrhNy8M4MWnT
   suite-id: dk0580416972e402b8
   suite-secret: 3aV48BTZHxDBeltZgdiRAUhttqsE5RxNs5pzElGhHHg
-  agent-id: 1000095
+  agent-id: 1000095
+  parent-id: 1

+ 2 - 1
src/main/resources/application-test.yml

@@ -40,4 +40,5 @@ enterprise-wechat:
   encoding-a-eSKey: IycRI8M3tgTIHrFiwWnYWJn9I1o8iIsQrhNy8M4MWnT
   suite-id: dk0580416972e402b8
   suite-secret: 3aV48BTZHxDBeltZgdiRAUhttqsE5RxNs5pzElGhHHg
-  agent-id: 1000095
+  agent-id: 1000095
+  parent-id: 0