From 07252b5fea6f9718119c7d84f86d6e03d39d2942 Mon Sep 17 00:00:00 2001 From: walker-sheng Date: Tue, 29 Dec 2020 18:30:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B0=8F=E7=A8=8B=E5=BA=8Fac?= =?UTF-8?q?cess=5Ftoken=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vo/client/map/newmap/MapSectionNewVO.java | 2 +- .../wechat/MiniProgramAccessTokenManager.java | 48 ++++++++++++------- .../rtss/wechat/MiniProgramServiceImpl.java | 27 +++++------ 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/club/joylink/rtss/vo/client/map/newmap/MapSectionNewVO.java b/src/main/java/club/joylink/rtss/vo/client/map/newmap/MapSectionNewVO.java index 1193144b1..1bdb66831 100644 --- a/src/main/java/club/joylink/rtss/vo/client/map/newmap/MapSectionNewVO.java +++ b/src/main/java/club/joylink/rtss/vo/client/map/newmap/MapSectionNewVO.java @@ -149,7 +149,7 @@ public class MapSectionNewVO { private String parentCode; /** - * 区段类型:物理区段/逻辑区段/道岔物理区段/道岔计轴区段04 + * 区段类型:物理区段01/逻辑区段02/道岔物理区段03/道岔计轴区段04 */ @ApiModelProperty(value = "区段类型") @NotNull(message="区段类型不能为Null") diff --git a/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java b/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java index f2f6e9fbd..d77af80ae 100644 --- a/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java +++ b/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java @@ -1,11 +1,14 @@ package club.joylink.rtss.wechat; import club.joylink.rtss.configuration.configProp.WeChatConfig; +import club.joylink.rtss.exception.BusinessExceptionAssertEnum; +import club.joylink.rtss.util.EncryptUtil; +import club.joylink.rtss.vo.CommonJsonResponse; import club.joylink.rtss.wechat.vo.WxAccessToken; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; @Slf4j @@ -44,22 +47,35 @@ public class MiniProgramAccessTokenManager { this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; } - @Scheduled(fixedRate=6600000) - public void getAccessTokenFromWx() { - if(!this.weChatConfig.getMini().isAccessTokenTaskOn()) { - log.debug("微信小程序获取access_token任务未开启"); - return; - } - try { - String accessToken = this.getAccessToken(true); - log.info("get wechat mini program Access Token is: "+accessToken); - } catch (WxErrorException e) { - log.error("获取access token失败", e); - } - } +// @Scheduled(fixedRate=6600000) +// public void getAccessTokenFromWx() { +// if(!this.weChatConfig.getMini().isAccessTokenTaskOn()) { +// log.debug("微信小程序获取access_token任务未开启"); +// return; +// } +// try { +// String accessToken = this.getAccessToken(); +// log.info("get wechat mini program Access Token is: "+accessToken); +// } catch (WxErrorException e) { +// log.error("获取access token失败", e); +// } +// } - public String getAccessToken() throws WxErrorException { - return this.getAccessToken(false); + public static final String URL_GET_ACCESS_TOKEN = "https://api.joylink.club/wechatservice/api/accessToken?clientid=%s&secret=%s&checksum=%s"; + + public static final String clientId = "rtss"; + public static final String secret = "joylink-0503"; + + public String getAccessToken() { + String url = String.format(URL_GET_ACCESS_TOKEN, + clientId, secret, EncryptUtil.md5(String.format("%s:%s", clientId, secret))); + CommonJsonResponse response = this.restTemplate.getForObject(url, CommonJsonResponse.class); + String accessToken = (String) response.getData(); + if (!StringUtils.hasText(accessToken)) { + BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION.assertHasText(accessToken, + String.format("获取access_token失败: %s", response.toJSONString())); + } + return accessToken; } public String getAccessToken(boolean forceRefresh) throws WxErrorException { diff --git a/src/main/java/club/joylink/rtss/wechat/MiniProgramServiceImpl.java b/src/main/java/club/joylink/rtss/wechat/MiniProgramServiceImpl.java index 1effe7f9a..1052104a0 100644 --- a/src/main/java/club/joylink/rtss/wechat/MiniProgramServiceImpl.java +++ b/src/main/java/club/joylink/rtss/wechat/MiniProgramServiceImpl.java @@ -1,6 +1,5 @@ package club.joylink.rtss.wechat; -import club.joylink.rtss.exception.BusinessExceptionAssertEnum; import club.joylink.rtss.wechat.constant.CheckMediaType; import club.joylink.rtss.wechat.vo.WxError; import lombok.extern.slf4j.Slf4j; @@ -39,20 +38,20 @@ public class MiniProgramServiceImpl implements MiniProgramService { public static final int Media_Type_Audio = 1; public static final int Media_Type_Img = 2; - - private String getMpAccessToken() { - try { - String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); - return accessToken; - } catch (WxErrorException e) { - throw BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION - .exception(String.format("获取微信小程序access_token异常"), e); - } - } +// +// private String getMpAccessToken() { +// try { +// String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); +// return accessToken; +// } catch (WxErrorException e) { +// throw BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION +// .exception(String.format("获取微信小程序access_token异常"), e); +// } +// } @Override public WxError msgSecCheck(String content) { - String accessToken = this.getMpAccessToken(); + String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); String url = String.format(URL_MSG_SEC_CHECK, accessToken); log.info(String.format("需要微信小程序检测的内容:[%s]", content)); Map params = new HashMap<>(); @@ -79,7 +78,7 @@ public class MiniProgramServiceImpl implements MiniProgramService { FileSystemResource resource = new FileSystemResource(tempFilePath); body.add("media", resource); HttpEntity> httpEntity = new HttpEntity<>(body, headers); - String accessToken = this.getMpAccessToken(); + String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); String url = String.format(URL_IMG_SEC_CHECK, accessToken); // 调用接口 ResponseEntity responseEntity = this.restTemplate.postForEntity(url, httpEntity, WxError.class); @@ -90,7 +89,7 @@ public class MiniProgramServiceImpl implements MiniProgramService { @Override public WxError imgSecCheckAsnc(String mediaUrl, CheckMediaType mediaType) { - String accessToken = this.getMpAccessToken(); + String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); String url = String.format(URL_MEDIA_SEC_CHECK, accessToken); Map params = new HashMap<>(); params.put("media_url", mediaUrl);