diff --git a/urbanLifelineServ/.bin/database/postgres/sql/initDataConfig.sql b/urbanLifelineServ/.bin/database/postgres/sql/initDataConfig.sql index 5539bfaa..739a7127 100644 --- a/urbanLifelineServ/.bin/database/postgres/sql/initDataConfig.sql +++ b/urbanLifelineServ/.bin/database/postgres/sql/initDataConfig.sql @@ -106,7 +106,7 @@ INSERT INTO config.tb_sys_config ( ('CFG-0706', 'cfg_wechat_kefu_welcome', 'wechat.kefu.welcomeTemplate','欢迎语模板', '您好,您的工单已创建。\n工单编号:{workcaseId}\n问题类型:{type}\n设备:{device}\n我们将尽快为您处理。', 'String', 'textarea', '客服欢迎语消息模板', NULL, NULL, 'wechat', 'mod_workcase', 60, 1, '支持变量:{workcaseId},{type},{device},{username}', 'system', NULL, NULL, now(), NULL, NULL, false), -- 微信小程序配置 -('CFG-0710', 'cfg_wechat_mp_appid', 'wechat.miniprogram.appid', '小程序AppID', 'wx3708f41b1dc31f52', 'String', 'input', '微信小程序的AppID', NULL, NULL, 'wechat', 'mod_workcase', 70, 1, '在微信公众平台获取', 'system', NULL, NULL, now(), NULL, NULL, false), +('CFG-0710', 'cfg_wechat_mp_appid', 'wechat.miniprogram.appid', '小程序AppID', 'wx15e67484db6d431f', 'String', 'input', '微信小程序的AppID', NULL, NULL, 'wechat', 'mod_workcase', 70, 1, '在微信公众平台获取', 'system', NULL, NULL, now(), NULL, NULL, false), ('CFG-0711', 'cfg_wechat_mp_appsecret', 'wechat.miniprogram.appsecret', '小程序AppSecret', '127dcc9c90dd1b66a700b52094922253', 'String', 'password', '微信小程序的AppSecret', NULL, NULL, 'wechat', 'mod_workcase', 80, 1, '在微信公众平台获取,用于获取openid和解密手机号', 'system', NULL, NULL, now(), NULL, NULL, false); diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/account/KefuAccountService.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/account/KefuAccountService.java index bd0b3248..e17bcb9f 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/account/KefuAccountService.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/account/KefuAccountService.java @@ -4,6 +4,7 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -11,7 +12,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.xyzh.common.wechat.kefu.core.KefuAccessTokenManager; import org.xyzh.common.wechat.pojo.kefu.KefuAccount; -import org.xyzh.common.wechat.pojo.kefu.WeChatResponse; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; @@ -28,6 +28,7 @@ import com.alibaba.fastjson2.JSONObject; * @copyright xyzh * @since 2025-12-19 */ +@Lazy @Service public class KefuAccountService { diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/core/KefuAccessTokenManager.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/core/KefuAccessTokenManager.java index 0c999ffc..e1d41319 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/core/KefuAccessTokenManager.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/core/KefuAccessTokenManager.java @@ -3,6 +3,7 @@ package org.xyzh.common.wechat.kefu.core; import org.apache.dubbo.config.annotation.DubboReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.xyzh.api.system.service.SysConfigService; @@ -11,8 +12,6 @@ import org.xyzh.common.core.domain.ResultDomain; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; -import jakarta.annotation.PostConstruct; - /** * @description 微信客服 AccessToken 管理器 * 负责获取和刷新 access_token @@ -21,6 +20,7 @@ import jakarta.annotation.PostConstruct; * @copyright xyzh * @since 2025-12-19 */ +@Lazy @Component public class KefuAccessTokenManager { @@ -29,6 +29,7 @@ public class KefuAccessTokenManager { private final RestTemplate restTemplate = new RestTemplate(); + @Lazy @DubboReference(version = "1.0.0", group = "system", check = false, retries = 0) private SysConfigService sysConfigService; @@ -36,10 +37,20 @@ public class KefuAccessTokenManager { private String secret; private String accessToken; private Long accessTokenExpireTime; + private volatile boolean configLoaded = false; - @PostConstruct - public void init() { - loadConfig(); + /** + * 懒加载配置 + */ + private void ensureConfigLoaded() { + if (!configLoaded) { + synchronized (this) { + if (!configLoaded) { + loadConfig(); + configLoaded = true; + } + } + } } public void loadConfig() { @@ -64,6 +75,7 @@ public class KefuAccessTokenManager { * 获取 access_token,如果过期自动刷新 */ public String getAccessToken() { + ensureConfigLoaded(); if (accessToken != null && accessTokenExpireTime != null && System.currentTimeMillis() < accessTokenExpireTime) { return accessToken; @@ -75,6 +87,7 @@ public class KefuAccessTokenManager { * 刷新 access_token */ public String refreshAccessToken() { + ensureConfigLoaded(); if (corpId == null || secret == null) { logger.error("微信配置不完整,无法获取access_token"); return null; @@ -103,6 +116,7 @@ public class KefuAccessTokenManager { } public String getCorpId() { + ensureConfigLoaded(); return corpId; } diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/info/KefuInfoService.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/info/KefuInfoService.java index b2615e59..b7a344d1 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/info/KefuInfoService.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/info/KefuInfoService.java @@ -4,6 +4,7 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -24,6 +25,7 @@ import com.alibaba.fastjson2.JSONObject; * @copyright xyzh * @since 2025-12-19 */ +@Lazy @Service public class KefuInfoService { diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/message/KefuMessageService.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/message/KefuMessageService.java index a81682fc..02082f03 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/message/KefuMessageService.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/kefu/message/KefuMessageService.java @@ -4,6 +4,7 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -27,6 +28,7 @@ import com.alibaba.fastjson2.JSONObject; * @copyright xyzh * @since 2025-12-19 */ +@Lazy @Service public class KefuMessageService { diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/pojo/WeChatPhoneResult.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/pojo/WeChatPhoneResult.java index be9c4aaf..648e4a35 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/pojo/WeChatPhoneResult.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/pojo/WeChatPhoneResult.java @@ -1,5 +1,7 @@ package org.xyzh.common.wechat.pojo; +import com.alibaba.fastjson2.annotation.JSONField; + import lombok.Data; /** @@ -17,6 +19,7 @@ public class WeChatPhoneResult { private String errmsg; /** 手机号信息 */ + @JSONField(name = "phone_info") private PhoneInfo phoneInfo; /** diff --git a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/service/WeChatMiniProgramService.java b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/service/WeChatMiniProgramService.java index 3ba3cd71..89a4a9c5 100644 --- a/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/service/WeChatMiniProgramService.java +++ b/urbanLifelineServ/common/common-wechat/src/main/java/org/xyzh/common/wechat/service/WeChatMiniProgramService.java @@ -15,6 +15,7 @@ import javax.crypto.spec.SecretKeySpec; import org.apache.dubbo.config.annotation.DubboReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.xyzh.api.system.service.SysConfigService; import org.xyzh.common.core.domain.ResultDomain; @@ -30,13 +31,14 @@ import com.alibaba.fastjson2.JSONObject; * @author cascade * @since 2026-01-09 */ +@Lazy @Service public class WeChatMiniProgramService { private static final Logger logger = LoggerFactory.getLogger(WeChatMiniProgramService.class); /** 微信小程序配置缓存 */ - private WeChatMiniProgramConfig config; + private volatile WeChatMiniProgramConfig config; /** code2Session接口地址 */ private static final String CODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session"; @@ -47,6 +49,7 @@ public class WeChatMiniProgramService { /** 获取手机号接口地址(新版API) */ private static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; + @Lazy @DubboReference(version = "1.0.0", group = "system", timeout = 5000, check = false, retries = 0) private SysConfigService sysConfigService; @@ -72,15 +75,15 @@ public class WeChatMiniProgramService { try { // 从系统配置获取小程序AppID - ResultDomain appIdResult = sysConfigService.getConfigValue("wechat.miniprogram.appid"); - if (appIdResult.getSuccess() && appIdResult.getData() != null) { - config.setAppId(appIdResult.getData()); + String appIdResult = sysConfigService.getStringConfig("wechat.miniprogram.appid"); + if (appIdResult != null && !appIdResult.trim().isEmpty()) { + config.setAppId(appIdResult); } // 从系统配置获取小程序AppSecret - ResultDomain appSecretResult = sysConfigService.getConfigValue("wechat.miniprogram.appsecret"); - if (appSecretResult.getSuccess() && appSecretResult.getData() != null) { - config.setAppSecret(appSecretResult.getData()); + String appSecretResult = sysConfigService.getStringConfig("wechat.miniprogram.appsecret"); + if (appSecretResult != null && !appSecretResult.trim().isEmpty()) { + config.setAppSecret(appSecretResult); } logger.info("微信小程序配置加载成功, appId: {}", config.getAppId());