From b68fde365fbbbf6b6b46c0f0eb224ea73de438c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Tue, 25 Apr 2023 01:40:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=A0=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WeChatTokenController.java | 24 ++++++++++++++++++- .../service/WeChatTokenServiceImpl.java | 16 +++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WeChatTokenController.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WeChatTokenController.java index fcfc2c6..e96f2f8 100644 --- a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WeChatTokenController.java +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/WeChatTokenController.java @@ -1,5 +1,6 @@ package com.cxyxiaomo.epp.access.controller; +import com.alibaba.fastjson2.JSONObject; import com.cxyxiaomo.epp.access.pojo.UnlimitedQRCodeParam; import com.cxyxiaomo.epp.access.service.WeChatTokenServiceImpl; import com.cxyxiaomo.epp.common.response.Res; @@ -47,7 +48,28 @@ public class WeChatTokenController { unlimitedQRCodeParam.setIsHyaline(isHyaline); okhttp3.ResponseBody responseBody = weChatTokenService.getUnlimitedQRCodeFromApi(accessToken, unlimitedQRCodeParam); - return responseBody.bytes(); + try { + // {"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 6446bac4-4a6b0410-118a16e7"} + if (Objects.requireNonNull(responseBody.contentType()).subtype().equals("json")) { + // 返回了 JSON 说明失败了 + String jsonString = responseBody.string(); + JSONObject jsonObject = JSONObject.parseObject(jsonString); + String errcode = jsonObject.getString("errcode"); + if (errcode.equals("40001")) { + // 重新获取 Access Token + accessToken = weChatTokenService.getAccessToken(true); + responseBody = weChatTokenService.getUnlimitedQRCodeFromApi(accessToken, unlimitedQRCodeParam); + } else if (errcode.equals("40013")) { + System.out.println("40013 invalid appid 不合法的 AppID ,请开发者检查 AppID 的正确性,避免异常字符,注意大小写\n" + + "docs: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html"); + return null; + } + } + // 返回了图片,直接返回 + return responseBody.bytes(); + } catch (NullPointerException ignored) { + return null; + } } @GetMapping(value = "/rpc/getOpenIdFromApi") diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/WeChatTokenServiceImpl.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/WeChatTokenServiceImpl.java index 00d397a..b947646 100644 --- a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/WeChatTokenServiceImpl.java +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/WeChatTokenServiceImpl.java @@ -35,11 +35,17 @@ public class WeChatTokenServiceImpl implements WeChatTokenService { @Override public String getAccessToken() { - // 首先从数据库中查询是否存在 access_token - // 如果存在且没有过期,那么就直接返回(距离失效时间小于 3 分钟就当作过期) - Setting atSetting = accessDao.getValueByKey(SETTING_KEY); - if (atSetting != null && LocalDateTime.now().plusMinutes(3L).compareTo(atSetting.getTime()) < 0) { - return atSetting.getValue(); + return getAccessToken(false); + } + + public String getAccessToken(Boolean forceUpdate) { + if (!forceUpdate) { + // 首先从数据库中查询是否存在 access_token + // 如果存在且没有过期,那么就直接返回(距离失效时间小于 3 分钟就当作过期) + Setting atSetting = accessDao.getValueByKey(SETTING_KEY); + if (atSetting != null && LocalDateTime.now().plusMinutes(3L).compareTo(atSetting.getTime()) < 0) { + return atSetting.getValue(); + } } // 否则则去请求一个新的 access_token