From 1eeb41d475eb4bec1700b0dccbd8410a369ba81d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Jan 2026 03:04:46 +0000 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BB=A5=E6=B6=88=E9=99=A4=E9=87=8D=E5=A4=8D=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- ...WxCpServiceGetMsgAuditAccessTokenTest.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpServiceGetMsgAuditAccessTokenTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpServiceGetMsgAuditAccessTokenTest.java index 25941a3f4..da74c1d13 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpServiceGetMsgAuditAccessTokenTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpServiceGetMsgAuditAccessTokenTest.java @@ -96,6 +96,22 @@ public class WxCpServiceGetMsgAuditAccessTokenTest { } } + /** + * 检查token是否需要刷新的公共逻辑 + */ + private boolean shouldRefreshToken(WxCpConfigStorage storage, boolean forceRefresh) { + return storage.isMsgAuditAccessTokenExpired() || forceRefresh; + } + + /** + * 验证会话存档secret是否已配置的公共逻辑 + */ + private void validateMsgAuditSecret(String msgAuditSecret) throws WxErrorException { + if (msgAuditSecret == null || msgAuditSecret.trim().isEmpty()) { + throw new WxErrorException("会话存档secret未配置"); + } + } + /** * 创建一个用于测试的BaseWxCpServiceImpl实现, * 用于测试缓存和过期逻辑 @@ -125,15 +141,13 @@ public class WxCpServiceGetMsgAuditAccessTokenTest { @Override public String getMsgAuditAccessToken(boolean forceRefresh) throws WxErrorException { // 检查是否需要刷新 - if (!getWxCpConfigStorage().isMsgAuditAccessTokenExpired() && !forceRefresh) { + if (!shouldRefreshToken(getWxCpConfigStorage(), forceRefresh)) { return getWxCpConfigStorage().getMsgAuditAccessToken(); } // 使用会话存档secret获取access_token String msgAuditSecret = getWxCpConfigStorage().getMsgAuditSecret(); - if (msgAuditSecret == null || msgAuditSecret.trim().isEmpty()) { - throw new WxErrorException("会话存档secret未配置"); - } + validateMsgAuditSecret(msgAuditSecret); // 返回缓存的token(用于测试缓存机制) return getWxCpConfigStorage().getMsgAuditAccessToken(); @@ -183,15 +197,13 @@ public class WxCpServiceGetMsgAuditAccessTokenTest { lock.lock(); try { // 检查是否需要刷新 - if (!getWxCpConfigStorage().isMsgAuditAccessTokenExpired() && !forceRefresh) { + if (!shouldRefreshToken(getWxCpConfigStorage(), forceRefresh)) { return getWxCpConfigStorage().getMsgAuditAccessToken(); } // 使用会话存档secret获取access_token String msgAuditSecret = getWxCpConfigStorage().getMsgAuditSecret(); - if (msgAuditSecret == null || msgAuditSecret.trim().isEmpty()) { - throw new WxErrorException("会话存档secret未配置"); - } + validateMsgAuditSecret(msgAuditSecret); // 模拟获取新token并更新配置 getWxCpConfigStorage().updateMsgAuditAccessToken(mockToken, 7200);