serv微信登录修正

This commit is contained in:
2026-01-09 12:46:32 +08:00
parent 0ae5343b11
commit bfd06dd8f6
7 changed files with 39 additions and 14 deletions

View File

@@ -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-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); ('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);

View File

@@ -4,6 +4,7 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -11,7 +12,6 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.xyzh.common.wechat.kefu.core.KefuAccessTokenManager; import org.xyzh.common.wechat.kefu.core.KefuAccessTokenManager;
import org.xyzh.common.wechat.pojo.kefu.KefuAccount; 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.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
@@ -28,6 +28,7 @@ import com.alibaba.fastjson2.JSONObject;
* @copyright xyzh * @copyright xyzh
* @since 2025-12-19 * @since 2025-12-19
*/ */
@Lazy
@Service @Service
public class KefuAccountService { public class KefuAccountService {

View File

@@ -3,6 +3,7 @@ package org.xyzh.common.wechat.kefu.core;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.xyzh.api.system.service.SysConfigService; 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.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.PostConstruct;
/** /**
* @description 微信客服 AccessToken 管理器 * @description 微信客服 AccessToken 管理器
* 负责获取和刷新 access_token * 负责获取和刷新 access_token
@@ -21,6 +20,7 @@ import jakarta.annotation.PostConstruct;
* @copyright xyzh * @copyright xyzh
* @since 2025-12-19 * @since 2025-12-19
*/ */
@Lazy
@Component @Component
public class KefuAccessTokenManager { public class KefuAccessTokenManager {
@@ -29,6 +29,7 @@ public class KefuAccessTokenManager {
private final RestTemplate restTemplate = new RestTemplate(); private final RestTemplate restTemplate = new RestTemplate();
@Lazy
@DubboReference(version = "1.0.0", group = "system", check = false, retries = 0) @DubboReference(version = "1.0.0", group = "system", check = false, retries = 0)
private SysConfigService sysConfigService; private SysConfigService sysConfigService;
@@ -36,10 +37,20 @@ public class KefuAccessTokenManager {
private String secret; private String secret;
private String accessToken; private String accessToken;
private Long accessTokenExpireTime; 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() { public void loadConfig() {
@@ -64,6 +75,7 @@ public class KefuAccessTokenManager {
* 获取 access_token如果过期自动刷新 * 获取 access_token如果过期自动刷新
*/ */
public String getAccessToken() { public String getAccessToken() {
ensureConfigLoaded();
if (accessToken != null && accessTokenExpireTime != null if (accessToken != null && accessTokenExpireTime != null
&& System.currentTimeMillis() < accessTokenExpireTime) { && System.currentTimeMillis() < accessTokenExpireTime) {
return accessToken; return accessToken;
@@ -75,6 +87,7 @@ public class KefuAccessTokenManager {
* 刷新 access_token * 刷新 access_token
*/ */
public String refreshAccessToken() { public String refreshAccessToken() {
ensureConfigLoaded();
if (corpId == null || secret == null) { if (corpId == null || secret == null) {
logger.error("微信配置不完整无法获取access_token"); logger.error("微信配置不完整无法获取access_token");
return null; return null;
@@ -103,6 +116,7 @@ public class KefuAccessTokenManager {
} }
public String getCorpId() { public String getCorpId() {
ensureConfigLoaded();
return corpId; return corpId;
} }

View File

@@ -4,6 +4,7 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,6 +25,7 @@ import com.alibaba.fastjson2.JSONObject;
* @copyright xyzh * @copyright xyzh
* @since 2025-12-19 * @since 2025-12-19
*/ */
@Lazy
@Service @Service
public class KefuInfoService { public class KefuInfoService {

View File

@@ -4,6 +4,7 @@ import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -27,6 +28,7 @@ import com.alibaba.fastjson2.JSONObject;
* @copyright xyzh * @copyright xyzh
* @since 2025-12-19 * @since 2025-12-19
*/ */
@Lazy
@Service @Service
public class KefuMessageService { public class KefuMessageService {

View File

@@ -1,5 +1,7 @@
package org.xyzh.common.wechat.pojo; package org.xyzh.common.wechat.pojo;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data; import lombok.Data;
/** /**
@@ -17,6 +19,7 @@ public class WeChatPhoneResult {
private String errmsg; private String errmsg;
/** 手机号信息 */ /** 手机号信息 */
@JSONField(name = "phone_info")
private PhoneInfo phoneInfo; private PhoneInfo phoneInfo;
/** /**

View File

@@ -15,6 +15,7 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.xyzh.api.system.service.SysConfigService; import org.xyzh.api.system.service.SysConfigService;
import org.xyzh.common.core.domain.ResultDomain; import org.xyzh.common.core.domain.ResultDomain;
@@ -30,13 +31,14 @@ import com.alibaba.fastjson2.JSONObject;
* @author cascade * @author cascade
* @since 2026-01-09 * @since 2026-01-09
*/ */
@Lazy
@Service @Service
public class WeChatMiniProgramService { public class WeChatMiniProgramService {
private static final Logger logger = LoggerFactory.getLogger(WeChatMiniProgramService.class); private static final Logger logger = LoggerFactory.getLogger(WeChatMiniProgramService.class);
/** 微信小程序配置缓存 */ /** 微信小程序配置缓存 */
private WeChatMiniProgramConfig config; private volatile WeChatMiniProgramConfig config;
/** code2Session接口地址 */ /** code2Session接口地址 */
private static final String CODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session"; private static final String CODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";
@@ -47,6 +49,7 @@ public class WeChatMiniProgramService {
/** 获取手机号接口地址新版API */ /** 获取手机号接口地址新版API */
private static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; 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) @DubboReference(version = "1.0.0", group = "system", timeout = 5000, check = false, retries = 0)
private SysConfigService sysConfigService; private SysConfigService sysConfigService;
@@ -72,15 +75,15 @@ public class WeChatMiniProgramService {
try { try {
// 从系统配置获取小程序AppID // 从系统配置获取小程序AppID
ResultDomain<String> appIdResult = sysConfigService.getConfigValue("wechat.miniprogram.appid"); String appIdResult = sysConfigService.getStringConfig("wechat.miniprogram.appid");
if (appIdResult.getSuccess() && appIdResult.getData() != null) { if (appIdResult != null && !appIdResult.trim().isEmpty()) {
config.setAppId(appIdResult.getData()); config.setAppId(appIdResult);
} }
// 从系统配置获取小程序AppSecret // 从系统配置获取小程序AppSecret
ResultDomain<String> appSecretResult = sysConfigService.getConfigValue("wechat.miniprogram.appsecret"); String appSecretResult = sysConfigService.getStringConfig("wechat.miniprogram.appsecret");
if (appSecretResult.getSuccess() && appSecretResult.getData() != null) { if (appSecretResult != null && !appSecretResult.trim().isEmpty()) {
config.setAppSecret(appSecretResult.getData()); config.setAppSecret(appSecretResult);
} }
logger.info("微信小程序配置加载成功, appId: {}", config.getAppId()); logger.info("微信小程序配置加载成功, appId: {}", config.getAppId());