From 686d42b4cde1fcab525f7e6e15e6f181ab4c2477 Mon Sep 17 00:00:00 2001 From: wxz Date: Wed, 24 Mar 2021 15:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=201.=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E5=B9=B3=E5=8F=B0=E6=8E=A5=E5=85=A5=EF=BC=8C?= =?UTF-8?q?AccessToken=E7=BC=93=E5=AD=98=E5=8A=A8=E4=BD=9C=E4=BB=8E?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E7=9A=84=E5=AD=90=E7=B1=BB=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E5=88=B0=E4=BA=86ApiService=E4=B8=AD=EF=BC=8C=E5=AD=90?= =?UTF-8?q?=E7=B1=BB=E5=8F=AA=E9=9C=80=E8=A6=81=E8=B0=83=E7=94=A8ApiServic?= =?UTF-8?q?e=E6=8F=90=E4=BE=9B=E7=9A=84addAccessTokenToCache()/getAccessTo?= =?UTF-8?q?kenFromCache()=E6=96=B9=E6=B3=95=E5=8D=B3=E5=8F=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/epmet/apiservice/ApiService.java | 43 +++++++++++++++++++ .../impl/LuzhouGridPlatformApiService.java | 5 +-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java index ef54d00d93..89bb8f8e24 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/ApiService.java @@ -2,6 +2,7 @@ package com.epmet.apiservice; import com.alibaba.fastjson.JSON; import com.epmet.apiservice.result.LZGridPlatformBaseResult; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.dto.result.ProjectAssistResult; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.utils.HttpClientManager; @@ -15,6 +16,7 @@ import com.epmet.dto.form.TPFDemoFormDTO; import com.epmet.entity.ThirdplatformActionEntity; import com.epmet.entity.ThirdplatformCustomerRegisterEntity; import com.epmet.entity.ThirdplatformEntity; +import org.springframework.data.redis.core.RedisTemplate; import java.util.HashMap; import java.util.Map; @@ -116,6 +118,14 @@ public abstract class ApiService { } LZGridPlatformBaseResult platformResult = JSON.parseObject(result.getData(), LZGridPlatformBaseResult.class); + + // token过期重试逻辑,先不加 + //if ("402".equals(platformResult.getCode())) { + // // 如果token过期 + // deleteAccessTokenFromCache(platformId); + // getAccessToken(platformId); + // return sendPostRequest(platformId, action, jsonString, headers); + //} judgeResultSuccess(platformResult); return result.getData(); } @@ -128,6 +138,39 @@ public abstract class ApiService { */ public abstract String getAccessToken(String platformId); + /** + * @Description 从缓存中删除AccessToken + * @return + * @author wxz + * @date 2021.03.24 15:35 + */ + protected void deleteAccessTokenFromCache(String platformId) { + RedisTemplate stringRedisTemplate = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class); + stringRedisTemplate.delete(RedisKeys.getThirdPlatformAccessTokenKey(platformId)); + } + + /** + * @Description 添加AccessToken到缓存 + * @return + * @author wxz + * @date 2021.03.24 15:32 + */ + protected void addAccessTokenToCache(String platformId, String accessToken) { + RedisTemplate stringRedisTemplate = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class); + stringRedisTemplate.opsForValue().set(RedisKeys.getThirdPlatformAccessTokenKey(platformId), accessToken); + } + + /** + * @Description 从缓存中取token + * @return + * @author wxz + * @date 2021.03.24 15:40 + */ + protected String getAccessTokenFromCache(String platformId) { + RedisTemplate stringRedisTemplate = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class); + return stringRedisTemplate.opsForValue().get(platformId); + } + /** * @Description 判断客户是否注册了指定的平台,如果没有注册,则抛出异常 * @return diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java index 25586f743a..5a903a6b08 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/apiservice/impl/LuzhouGridPlatformApiService.java @@ -30,8 +30,7 @@ public class LuzhouGridPlatformApiService extends ApiService { @Override public String getAccessToken(String platformId) { - RedisTemplate rt = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class); - String token = rt.opsForValue().get(RedisKeys.getThirdPlatformAccessTokenKey(platformId)); + String token = getAccessTokenFromCache(platformId); if (StringUtils.isBlank(token)) { ThirdplatformEntity thirdplatform = SpringContextUtils.getBean(ThirdplatformDao.class).selectById(platformId); ThirdplatformActionEntity actionEntity = SpringContextUtils.getBean(ThirdplatformActionDao.class) @@ -58,7 +57,7 @@ public class LuzhouGridPlatformApiService extends ApiService { judgeResultSuccess(platformResult); token = platformResult.getResult(); - rt.opsForValue().set(RedisKeys.getThirdPlatformAccessTokenKey(platformId), token); + addAccessTokenToCache(platformId, token); } return token; }