获取微信 accessToken redis 过期时间提前1分钟过期

This commit is contained in:
Jack 2025-09-29 10:15:39 +08:00
parent 62b5d71b3b
commit 129ba5b394

View File

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