From af8cab142a6b15c06e131a8474574dd5b00df982 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期四, 04 十二月 2025 22:09:58 +0800
Subject: [PATCH] feat: 改造微信accesstoken存放在系统配置表中
---
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatAccessTokenServiceImpl.java | 123 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 123 insertions(+), 0 deletions(-)
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatAccessTokenServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatAccessTokenServiceImpl.java
new file mode 100644
index 0000000..2d43bdb
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatAccessTokenServiceImpl.java
@@ -0,0 +1,123 @@
+package com.ruoyi.system.service.impl;
+
+import com.ruoyi.common.config.WechatConfig;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.WechatUtils;
+import com.ruoyi.system.domain.SysConfig;
+import com.ruoyi.system.mapper.SysConfigMapper;
+import com.ruoyi.system.service.ISysConfigService;
+import com.ruoyi.system.service.IWechatAccessTokenService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * 寰俊AccessToken鏈嶅姟瀹炵幇
+ * 鎻愪緵搴旂敤绾ccessToken鐨勭粺涓�绠$悊锛屼娇鐢╯ys_config琛ㄧ紦瀛�
+ *
+ * @author ruoyi
+ * @date 2025-12-04
+ */
+@Service
+public class WechatAccessTokenServiceImpl implements IWechatAccessTokenService {
+
+ private static final Logger log = LoggerFactory.getLogger(WechatAccessTokenServiceImpl.class);
+
+ @Autowired
+ private WechatConfig wechatConfig;
+
+ @Autowired
+ private ISysConfigService configService;
+
+ @Autowired
+ private SysConfigMapper configMapper;
+
+ /**
+ * 鑾峰彇搴旂敤绾у井淇ccessToken锛堝甫缂撳瓨锛�
+ * 浼樺厛浠巗ys_config璇诲彇骞跺垽鏂湁鏁堟湡锛涜繃鏈熷垯閲嶆柊鑾峰彇骞跺啓鍥瀞ys_config
+ */
+ @Override
+ public String getAppAccessToken() {
+ 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) {
+ log.debug("浣跨敤缂撳瓨鐨凙ccessToken锛屽墿浣欐湁鏁堟湡锛歿}绉�", (expireTs - now) / 1000);
+ return cachedToken;
+ }
+
+ // 閲嶆柊鑾峰彇锛屽苟鍐欏叆sys_config
+ log.info("AccessToken宸茶繃鏈熸垨涓嶅瓨鍦紝閲嶆柊鑾峰彇");
+ return refreshAppAccessToken();
+ } catch (Exception e) {
+ log.error("鑾峰彇搴旂敤绾у井淇ccessToken澶辫触", e);
+ return null;
+ }
+ }
+
+ /**
+ * 寮哄埗鍒锋柊AccessToken
+ * 蹇界暐缂撳瓨锛岀洿鎺ヤ粠寰俊鑾峰彇鏂癟oken骞舵洿鏂扮紦瀛�
+ */
+ @Override
+ public String refreshAppAccessToken() {
+ try {
+ String appId = wechatConfig.getAppId();
+ String appSecret = wechatConfig.getAppSecret();
+ String tokenKey = "weixin.access_token." + appId;
+ String expireKey = "weixin.access_token_expires." + appId;
+
+ String newToken = WechatUtils.getAccessToken(appId, appSecret);
+ if (StringUtils.isEmpty(newToken)) {
+ log.error("浠庡井淇¤幏鍙朅ccessToken澶辫触");
+ return null;
+ }
+
+ long now = System.currentTimeMillis();
+ long newExpireTs = now + 7200L * 1000L; // 7200绉�
+
+ upsertConfig(tokenKey, newToken);
+ upsertConfig(expireKey, String.valueOf(newExpireTs));
+
+ log.info("AccessToken鍒锋柊鎴愬姛锛屾湁鏁堟湡锛�7200绉�");
+ return newToken;
+ } 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);
+ }
+ }
+}
--
Gitblit v1.9.1