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); } + }