修改小程序access_token获取

This commit is contained in:
walker-sheng 2020-12-29 18:30:50 +08:00
parent d50431919c
commit 07252b5fea
3 changed files with 46 additions and 31 deletions

View File

@ -149,7 +149,7 @@ public class MapSectionNewVO {
private String parentCode; private String parentCode;
/** /**
* 区段类型物理区段/逻辑区段/道岔物理区段/道岔计轴区段04 * 区段类型物理区段01/逻辑区段02/道岔物理区段03/道岔计轴区段04
*/ */
@ApiModelProperty(value = "区段类型") @ApiModelProperty(value = "区段类型")
@NotNull(message="区段类型不能为Null") @NotNull(message="区段类型不能为Null")

View File

@ -1,11 +1,14 @@
package club.joylink.rtss.wechat; package club.joylink.rtss.wechat;
import club.joylink.rtss.configuration.configProp.WeChatConfig; 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 club.joylink.rtss.wechat.vo.WxAccessToken;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@Slf4j @Slf4j
@ -44,22 +47,35 @@ public class MiniProgramAccessTokenManager {
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
} }
@Scheduled(fixedRate=6600000) // @Scheduled(fixedRate=6600000)
public void getAccessTokenFromWx() { // public void getAccessTokenFromWx() {
if(!this.weChatConfig.getMini().isAccessTokenTaskOn()) { // if(!this.weChatConfig.getMini().isAccessTokenTaskOn()) {
log.debug("微信小程序获取access_token任务未开启"); // log.debug("微信小程序获取access_token任务未开启");
return; // return;
} // }
try { // try {
String accessToken = this.getAccessToken(true); // String accessToken = this.getAccessToken();
log.info("get wechat mini program Access Token is: "+accessToken); // log.info("get wechat mini program Access Token is: "+accessToken);
} catch (WxErrorException e) { // } catch (WxErrorException e) {
log.error("获取access token失败", e); // log.error("获取access token失败", e);
} // }
} // }
public String getAccessToken() throws WxErrorException { public static final String URL_GET_ACCESS_TOKEN = "https://api.joylink.club/wechatservice/api/accessToken?clientid=%s&secret=%s&checksum=%s";
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 { public String getAccessToken(boolean forceRefresh) throws WxErrorException {

View File

@ -1,6 +1,5 @@
package club.joylink.rtss.wechat; package club.joylink.rtss.wechat;
import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.wechat.constant.CheckMediaType; import club.joylink.rtss.wechat.constant.CheckMediaType;
import club.joylink.rtss.wechat.vo.WxError; import club.joylink.rtss.wechat.vo.WxError;
import lombok.extern.slf4j.Slf4j; 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_Audio = 1;
public static final int Media_Type_Img = 2; public static final int Media_Type_Img = 2;
//
private String getMpAccessToken() { // private String getMpAccessToken() {
try { // try {
String accessToken = this.miniProgramAccessTokenManager.getAccessToken(); // String accessToken = this.miniProgramAccessTokenManager.getAccessToken();
return accessToken; // return accessToken;
} catch (WxErrorException e) { // } catch (WxErrorException e) {
throw BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION // throw BusinessExceptionAssertEnum.THIRD_SERVICE_CALL_EXCEPTION
.exception(String.format("获取微信小程序access_token异常"), e); // .exception(String.format("获取微信小程序access_token异常"), e);
} // }
} // }
@Override @Override
public WxError msgSecCheck(String content) { public WxError msgSecCheck(String content) {
String accessToken = this.getMpAccessToken(); String accessToken = this.miniProgramAccessTokenManager.getAccessToken();
String url = String.format(URL_MSG_SEC_CHECK, accessToken); String url = String.format(URL_MSG_SEC_CHECK, accessToken);
log.info(String.format("需要微信小程序检测的内容:[%s]", content)); log.info(String.format("需要微信小程序检测的内容:[%s]", content));
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
@ -79,7 +78,7 @@ public class MiniProgramServiceImpl implements MiniProgramService {
FileSystemResource resource = new FileSystemResource(tempFilePath); FileSystemResource resource = new FileSystemResource(tempFilePath);
body.add("media", resource); body.add("media", resource);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, headers); 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); String url = String.format(URL_IMG_SEC_CHECK, accessToken);
// 调用接口 // 调用接口
ResponseEntity<WxError> responseEntity = this.restTemplate.postForEntity(url, httpEntity, WxError.class); ResponseEntity<WxError> responseEntity = this.restTemplate.postForEntity(url, httpEntity, WxError.class);
@ -90,7 +89,7 @@ public class MiniProgramServiceImpl implements MiniProgramService {
@Override @Override
public WxError imgSecCheckAsnc(String mediaUrl, CheckMediaType mediaType) { 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); String url = String.format(URL_MEDIA_SEC_CHECK, accessToken);
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("media_url", mediaUrl); params.put("media_url", mediaUrl);