|
@@ -5,12 +5,15 @@ import com.ematou.wxbase.config.WxGeneralConfig;
|
|
|
import com.ematou.wxbase.entity.OperationRecord;
|
|
|
import com.ematou.wxbase.entity.TokenRecord;
|
|
|
import com.ematou.wxbase.mapper.TokenRecordMapper;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -23,6 +26,8 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class TokenRecordService {
|
|
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(TokenRecordService.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
OperationRecordService operationRecordService;
|
|
|
|
|
@@ -40,7 +45,12 @@ public class TokenRecordService {
|
|
|
* @return 所有未失效的Token记录
|
|
|
*/
|
|
|
public List<TokenRecord> queryTokenIsValid() {
|
|
|
- return tokenRecordMapper.queryTokenIsValid();
|
|
|
+ try {
|
|
|
+ return tokenRecordMapper.queryTokenIsValid();
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("查询所有未失效的Token记录失败!errorMessage:" + e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -48,19 +58,28 @@ public class TokenRecordService {
|
|
|
* @return token记录总数
|
|
|
*/
|
|
|
public int queryLatestAndValidRecordCount() {
|
|
|
- return tokenRecordMapper.queryLatestAndValidRecordCount();
|
|
|
+ try {
|
|
|
+ return tokenRecordMapper.queryLatestAndValidRecordCount();
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("查询最新且有效的Token记录总数失败!errorMessage:" + e.getMessage());
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 新增一条Token记录
|
|
|
* @param tokenRecord token记录
|
|
|
- * @return 是否成功
|
|
|
*/
|
|
|
public void insertRecord(TokenRecord tokenRecord) {
|
|
|
if (null == tokenRecord) {
|
|
|
+ logger.error("Token记录为空,插入失败!");
|
|
|
return;
|
|
|
}
|
|
|
- tokenRecordMapper.insertRecord(tokenRecord);
|
|
|
+ try {
|
|
|
+ tokenRecordMapper.insertRecord(tokenRecord);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("插入Token记录失败!errorMessage:" + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -68,13 +87,19 @@ public class TokenRecordService {
|
|
|
*
|
|
|
* @return token
|
|
|
*/
|
|
|
- public TokenRecord getLatestToken() {
|
|
|
-
|
|
|
+ public TokenRecord getLatestToken() throws ParseException {
|
|
|
+ // 如果数据库中没有或者是该token已过期,则需要重新生成token
|
|
|
TokenRecord tokenRecord = tokenRecordMapper.queryLatestAndValidRecord();
|
|
|
if (null == tokenRecord) {
|
|
|
// 需要生成Token
|
|
|
return generateToken();
|
|
|
}
|
|
|
+ String expiresTime = tokenRecord.getExpiresTime();
|
|
|
+ Date tokenExpiresTime = DateUtils.parseString(expiresTime);
|
|
|
+ Date now = new Date();
|
|
|
+ if (now.getTime() > tokenExpiresTime.getTime()) {
|
|
|
+ return generateToken();
|
|
|
+ }
|
|
|
return tokenRecord;
|
|
|
}
|
|
|
|
|
@@ -83,7 +108,7 @@ public class TokenRecordService {
|
|
|
*
|
|
|
* @return token记录
|
|
|
*/
|
|
|
- public TokenRecord generateToken() {
|
|
|
+ public TokenRecord generateToken() throws RuntimeException {
|
|
|
|
|
|
String url = String.format(wxGeneralConfig.getUrl(), wxGeneralConfig.getGrantType(), wxGeneralConfig.getAppId(), wxGeneralConfig.getAppSecret());
|
|
|
|
|
@@ -91,16 +116,22 @@ public class TokenRecordService {
|
|
|
try {
|
|
|
response = restTemplate.getForEntity(url, Map.class).getBody();
|
|
|
} catch (Exception e) {
|
|
|
- throw new RuntimeException("请求微信平台失败!errorMessage:" + e.getMessage());
|
|
|
+ logger.error("请求微信平台失败!errorMessage:" + e.getMessage());
|
|
|
+ throw new RuntimeException("请求微信平台失败!");
|
|
|
+ }
|
|
|
+ if (null == response) {
|
|
|
+ logger.error("请求微信平台失败!errorMessage:response is null");
|
|
|
+ throw new RuntimeException("请求微信平台失败!");
|
|
|
}
|
|
|
|
|
|
OperationRecord operationRecord = wrapOperationRecord("目前暂无");
|
|
|
TokenRecord tokenRecord = wrapTokenRecord(operationRecord, response);
|
|
|
|
|
|
if (null == tokenRecord) {
|
|
|
- Object errcode = response.getOrDefault("errcode", 0);
|
|
|
- Object errmsg = response.getOrDefault("errmsg", "");
|
|
|
- throw new RuntimeException("请求微信平台生成AccessToken失败!errorCode:" + errcode + ",errorMessage:" + errmsg);
|
|
|
+ Object errcode = response.get("errcode");
|
|
|
+ Object errmsg = response.get("errmsg");
|
|
|
+ logger.error("请求微信平台生成AccessToken失败!errorCode:" + errcode + ",errorMessage:" + errmsg);
|
|
|
+ throw new RuntimeException("请求微信平台生成AccessToken失败!");
|
|
|
}
|
|
|
|
|
|
// 保存操作记录
|
|
@@ -118,9 +149,15 @@ public class TokenRecordService {
|
|
|
*/
|
|
|
public void updateBatchRecord(List<Integer> resultList) {
|
|
|
if (CollectionUtils.isEmpty(resultList)) {
|
|
|
+ logger.warn("需要批量更新的Token记录集合为空,无需更新!");
|
|
|
return;
|
|
|
}
|
|
|
- tokenRecordMapper.updateBatchRecord(resultList);
|
|
|
+ try {
|
|
|
+ tokenRecordMapper.updateBatchRecord(resultList);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("批量更新Token记录状态失败!errorMessage:" + e.getMessage());
|
|
|
+ throw new RuntimeException("批量更新Token记录状态失败!");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -144,12 +181,12 @@ public class TokenRecordService {
|
|
|
*/
|
|
|
private TokenRecord wrapTokenRecord(OperationRecord operationRecord, Map response) {
|
|
|
TokenRecord tokenRecord = new TokenRecord();
|
|
|
- String accessToken = (String) response.getOrDefault("access_token", "");
|
|
|
- Integer expiresIn = (Integer) response.getOrDefault("expires_in", 0);
|
|
|
- Integer errCode = (Integer) response.getOrDefault("errcode", 0); // 如果没有返回该字段,默认为0,0表示请求成功
|
|
|
- String errMsg = (String) response.getOrDefault("errmsg", "");
|
|
|
+ String accessToken = (String) response.get("access_token");
|
|
|
+ Integer expiresIn = (Integer) response.get("expires_in");
|
|
|
+ Integer errCode = (Integer) response.get("errcode");
|
|
|
+ String errMsg = (String) response.get("errmsg");
|
|
|
|
|
|
- if (StringUtils.hasLength(accessToken) && expiresIn != 0 && errCode == 0 && !StringUtils.hasLength(errMsg)) {
|
|
|
+ if (StringUtils.hasLength(accessToken) && expiresIn != null && errCode == null && !StringUtils.hasLength(errMsg)) {
|
|
|
Date now = new Date();
|
|
|
long expiresTime = now.getTime() + (expiresIn * 1000);
|
|
|
tokenRecord.setAccessToken(accessToken);
|