From 129ba5b394f39822b771a3695820413d42a46d52 Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Mon, 29 Sep 2025 10:15:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BE=AE=E4=BF=A1=20accessTo?= =?UTF-8?q?ken=20redis=20=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=E6=8F=90?= =?UTF-8?q?=E5=89=8D1=E5=88=86=E9=92=9F=E8=BF=87=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/suisung/mall/shop/wechat/utils/WxUtil.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/wechat/utils/WxUtil.java b/mall-shop/src/main/java/com/suisung/mall/shop/wechat/utils/WxUtil.java index 1d6f6fc2..64b51146 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/wechat/utils/WxUtil.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/wechat/utils/WxUtil.java @@ -58,18 +58,24 @@ public class WxUtil { params.put("secret", xcx_app_secret); params.put("grant_type", "client_credential"); - JSONObject resultObj = new JSONObject(); String response = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", params); JSONObject jsonObject = JSONUtil.parseObj(response); + // 修正:使用 jsonObject 而不是 resultObj 来检查错误信息 Object errmsg = jsonObject.get("errmsg"); if (ObjectUtil.isNotEmpty(errmsg)) { - String errmsgStr = Convert.toStr(resultObj.get("errmsg")); - if (!errmsgStr.equals("0")) { + String errmsgStr = Convert.toStr(errmsg); + if (!"0".equals(errmsgStr)) { throw new ApiException(I18nUtil._("获取access_token失败")); } } - Integer expiresIn = Convert.toInt(jsonObject.get("expires_in"), 0); + + // 优化:expiresIn 逻辑更清晰 + Long expiresIn = Convert.toLong(jsonObject.get("expires_in"), 7200L) - 60L; + if (expiresIn <= 0L) { + expiresIn = 7140L; + } redisService.set(StateCode.WX_XCX_ACCESSTOKEN, Convert.toStr(jsonObject.get("access_token")), expiresIn); } + }