From 7de1396e315896dbc72a9d54e44f77434ea90f18 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 14 十二月 2025 23:47:34 +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