From 10354e63eb3298beb9ebcc029dd9f48d8936a272 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期五, 19 十二月 2025 23:15:05 +0800
Subject: [PATCH] feat:优化同步

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatTaskNotifyServiceImpl.java |   77 +++++++++-----------------------------
 1 files changed, 19 insertions(+), 58 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatTaskNotifyServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatTaskNotifyServiceImpl.java
index 6b91981..5b7273a 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatTaskNotifyServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatTaskNotifyServiceImpl.java
@@ -13,9 +13,8 @@
 import com.ruoyi.system.domain.SysTaskEmergency;
 import com.ruoyi.system.mapper.SysUserMapper;
 import com.ruoyi.system.service.IWechatTaskNotifyService;
+import com.ruoyi.system.service.IWechatAccessTokenService;
 import com.ruoyi.system.service.ISysConfigService;
-import com.ruoyi.system.mapper.SysConfigMapper;
-import com.ruoyi.system.domain.SysConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,69 +49,26 @@
     private WechatConfig wechatConfig;
     
     @Autowired
-    private ISysConfigService configService;
+    private IWechatAccessTokenService wechatAccessTokenService;
     
     @Autowired
-    private SysConfigMapper configMapper;
+    private ISysConfigService configService;
     
+    
+
     
     /**
-     * 鑾峰彇搴旂敤绾у井淇ccessToken锛堝甫缂撳瓨锛�
-     * 浼樺厛浠巗ys_config璇诲彇骞跺垽鏂湁鏁堟湡锛涜繃鏈熷垯閲嶆柊鑾峰彇骞跺啓鍥瀞ys_config
+     * 妫�鏌ユ槸鍚﹀惎鐢ㄨ闃呮秷鎭彂閫�
+     * 
+     * @return true=鍚敤锛宖alse=绂佺敤
      */
-    private String getAppAccessToken() {
+    private boolean isSubscribeMessageEnabled() {
         try {
-            String appId = wechatConfig.getAppId();
-            String tokenKey = "weixin.access_token." + appId;
-            String expireKey = "weixin.access_token_expires." + appId;
-            
-            String cachedToken = configService.selectConfigByKey(tokenKey);
-            String cachedExpireStr = configService.selectConfigByKey(expireKey);
-            long now = System.currentTimeMillis();
-            long expireTs = 0L;
-            if (StringUtils.isNotEmpty(cachedExpireStr)) {
-                try {
-                    expireTs = Long.parseLong(cachedExpireStr);
-                } catch (NumberFormatException e) {
-                    expireTs = 0L;
-                }
-            }
-            
-            // 缂撳瓨鏈夋晥涓旀湭杩囨湡锛堥鐣�60绉掑畨鍏ㄨ竟鐣岋級
-            if (StringUtils.isNotEmpty(cachedToken) && expireTs > now + 60000L) {
-                return cachedToken;
-            }
-            
-            // 閲嶆柊鑾峰彇锛屽苟鍐欏叆sys_config
-            String newToken = WechatUtils.getAccessToken(wechatConfig.getAppId(), wechatConfig.getAppSecret());
-            if (StringUtils.isEmpty(newToken)) {
-                return null;
-            }
-            long newExpireTs = now + 7200L * 1000L; // 7200绉�
-            upsertConfig(tokenKey, newToken);
-            upsertConfig(expireKey, String.valueOf(newExpireTs));
-            return newToken;
+            String enabled = configService.selectConfigByKey("wechat.subscribe.message.enabled");
+            return "true".equalsIgnoreCase(enabled);
         } catch (Exception e) {
-            log.error("鑾峰彇搴旂敤绾у井淇ccessToken澶辫触", e);
-            return null;
-        }
-    }
-    
-    /**
-     * 鏍规嵁configKey鍐欏叆鎴栨洿鏂皊ys_config
-     */
-    private void upsertConfig(String key, String value) {
-        SysConfig exist = configMapper.checkConfigKeyUnique(key);
-        if (exist != null && exist.getConfigId() != null) {
-            exist.setConfigValue(value);
-            configMapper.updateConfig(exist);
-        } else {
-            SysConfig cfg = new SysConfig();
-            cfg.setConfigKey(key);
-            cfg.setConfigName(key);
-            cfg.setConfigValue(value);
-            cfg.setConfigType("Y"); // 鍐呯疆鍙傛暟
-            configMapper.insertConfig(cfg);
+            log.warn("鑾峰彇璁㈤槄娑堟伅寮�鍏抽厤缃け璐ワ紝榛樿鍚敤", e);
+            return true; // 榛樿鍚敤
         }
     }
     
@@ -126,6 +82,11 @@
      */
     @Override
     public int sendTaskNotifyMessage(Long taskId, List<Long> userIds, Long excludeUserId) {
+        // 妫�鏌ヨ闃呮秷鎭紑鍏�
+        if (!isSubscribeMessageEnabled()) {
+            log.info("璁㈤槄娑堟伅鍙戦�佸凡鍏抽棴锛岃烦杩囧彂閫侊紝taskId={}", taskId);
+            return 0;
+        }
         if (taskId == null || userIds == null || userIds.isEmpty()) {
             log.warn("鍙戦�佸井淇′换鍔¢�氱煡鍙傛暟涓嶅畬鏁达紝taskId={}, userIds={}", taskId, userIds);
             return 0;
@@ -142,7 +103,7 @@
         SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
         
         // 鑾峰彇寰俊AccessToken锛堣蛋搴旂敤绾х紦瀛橈級
-        String accessToken = getAppAccessToken();
+        String accessToken = wechatAccessTokenService.getAppAccessToken();
         if (StringUtils.isEmpty(accessToken)) {
             log.error("鑾峰彇寰俊AccessToken澶辫触锛屾棤娉曞彂閫佷换鍔¢�氱煡");
             return 0;

--
Gitblit v1.9.1