修改小程序access_token获取
This commit is contained in:
parent
c36f7ab639
commit
4628dc3fcb
|
@ -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 {
|
||||
|
|
|
@ -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<String, String> params = new HashMap<>();
|
||||
|
@ -79,7 +69,7 @@ public class MiniProgramServiceImpl implements MiniProgramService {
|
|||
FileSystemResource resource = new FileSystemResource(tempFilePath);
|
||||
body.add("media", resource);
|
||||
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers);
|
||||
String accessToken = this.getMpAccessToken();
|
||||
String accessToken = this.miniProgramAccessTokenManager.getAccessToken();
|
||||
String url = String.format(URL_IMG_SEC_CHECK, accessToken);
|
||||
// 调用接口
|
||||
ResponseEntity<WxError> 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<String, Object> params = new HashMap<>();
|
||||
params.put("media_url", mediaUrl);
|
||||
|
|
Loading…
Reference in New Issue