From 4628dc3fcbd3ba992ddf9183e3fe42cb226eb939 Mon Sep 17 00:00:00 2001 From: walker-sheng Date: Tue, 29 Dec 2020 18:37:29 +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 --- .../wechat/MiniProgramAccessTokenManager.java | 48 ++++++++++++------- .../rtss/wechat/MiniProgramServiceImpl.java | 16 ++----- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java b/src/main/java/club/joylink/rtss/wechat/MiniProgramAccessTokenManager.java index f2f6e9fbd..e4050e729 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 @@ -43,23 +46,36 @@ public class MiniProgramAccessTokenManager { this.expiresIn = expiresInSeconds; 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(true); - log.info("get wechat mini program Access Token is: "+accessToken); - } catch (WxErrorException e) { - log.error("获取access token失败", e); - } - } + public static final String URL_GET_ACCESS_TOKEN = "https://api.joylink.club/wechatservice/api/accessToken?clientid=%s&secret=%s&checksum=%s"; - public String getAccessToken() throws WxErrorException { - return this.getAccessToken(false); + 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..757d2131f 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; @@ -40,19 +39,10 @@ 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); - } - } @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 +69,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 +80,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);