|
@@ -5,6 +5,7 @@ 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 io.netty.util.internal.StringUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -13,10 +14,13 @@ import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
* @author lhm
|
|
@@ -40,6 +44,31 @@ public class TokenRecordService {
|
|
|
@Autowired
|
|
|
WxGeneralConfig wxGeneralConfig;
|
|
|
|
|
|
+ private static Map<String,WxGeneralConfig> configMap = new ConcurrentHashMap<>();
|
|
|
+ @PostConstruct
|
|
|
+ private static void init (){
|
|
|
+ WxGeneralConfig eztConfig = new WxGeneralConfig();
|
|
|
+ eztConfig.setAppId("wxf9360d70bc1406ee");
|
|
|
+ eztConfig.setAppSecret("78413a82d0332ecbf7fdf475d0a8b08e");
|
|
|
+ eztConfig.setUrl("https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s");
|
|
|
+ eztConfig.setGrantType("client_credential");
|
|
|
+ eztConfig.setTicketUrl("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi");
|
|
|
+
|
|
|
+ configMap.put("emato",eztConfig);
|
|
|
+
|
|
|
+
|
|
|
+ WxGeneralConfig ematoConfig = new WxGeneralConfig();
|
|
|
+ ematoConfig.setAppId("wx8735db2da152ce19");
|
|
|
+ ematoConfig.setAppSecret("016edeba9d3f0508c95476ceec5f65e4");
|
|
|
+ ematoConfig.setUrl("https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s");
|
|
|
+ ematoConfig.setGrantType("client_credential");
|
|
|
+ ematoConfig.setTicketUrl("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi");
|
|
|
+
|
|
|
+ configMap.put("qhemato",ematoConfig);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询所有未失效的Token记录
|
|
|
* @return 所有未失效的Token记录
|
|
@@ -87,22 +116,82 @@ public class TokenRecordService {
|
|
|
*
|
|
|
* @return token
|
|
|
*/
|
|
|
- public TokenRecord getLatestToken() throws ParseException {
|
|
|
- // 如果数据库中没有或者是该token已过期,则需要重新生成token
|
|
|
- TokenRecord tokenRecord = tokenRecordMapper.queryLatestAndValidRecord();
|
|
|
+ public TokenRecord getLatestToken(String system) throws ParseException {
|
|
|
+ TokenRecord tokenRecord;
|
|
|
+ if(StringUtil.isNullOrEmpty(system)){
|
|
|
+ // 如果数据库中没有或者是该token已过期,则需要重新生成token
|
|
|
+ tokenRecord = tokenRecordMapper.queryLatestAndValidRecord(wxGeneralConfig.getAppId());
|
|
|
+ }else{
|
|
|
+ WxGeneralConfig wxGeneralConfigTmp = configMap.get(system);
|
|
|
+ tokenRecord = tokenRecordMapper.queryLatestAndValidRecord(wxGeneralConfigTmp.getAppId());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
if (null == tokenRecord) {
|
|
|
// 需要生成Token
|
|
|
- return generateToken();
|
|
|
+ if(StringUtil.isNullOrEmpty(system)){
|
|
|
+ return generateToken();
|
|
|
+ }else{
|
|
|
+ return generateToken(system);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
String expiresTime = tokenRecord.getExpiresTime();
|
|
|
Date tokenExpiresTime = DateUtils.parseString(expiresTime);
|
|
|
Date now = new Date();
|
|
|
if (now.getTime() > tokenExpiresTime.getTime()) {
|
|
|
- return generateToken();
|
|
|
+ if(StringUtil.isNullOrEmpty(system)){
|
|
|
+ return generateToken();
|
|
|
+ }else{
|
|
|
+ return generateToken(system);
|
|
|
+ }
|
|
|
}
|
|
|
return tokenRecord;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主动请求微信生成accessToken
|
|
|
+ *
|
|
|
+ * @return token记录
|
|
|
+ */
|
|
|
+ public TokenRecord generateToken(String system) throws RuntimeException {
|
|
|
+ WxGeneralConfig wxGeneralConfig = configMap.get(system);
|
|
|
+ String url = "";
|
|
|
+ if(Objects.nonNull(wxGeneralConfig)){
|
|
|
+ url = String.format(wxGeneralConfig.getUrl(), wxGeneralConfig.getGrantType(), wxGeneralConfig.getAppId(), wxGeneralConfig.getAppSecret());
|
|
|
+ }
|
|
|
+ Map response = null;
|
|
|
+ try {
|
|
|
+ response = restTemplate.getForEntity(url, Map.class).getBody();
|
|
|
+ } catch (Exception e) {
|
|
|
+ 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,wxGeneralConfig.getAppId());
|
|
|
+
|
|
|
+ if (null == tokenRecord) {
|
|
|
+ Object errcode = response.get("errcode");
|
|
|
+ Object errmsg = response.get("errmsg");
|
|
|
+ logger.error("请求微信平台生成AccessToken失败!errorCode:" + errcode + ",errorMessage:" + errmsg);
|
|
|
+ throw new RuntimeException("请求微信平台生成AccessToken失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存操作记录
|
|
|
+ operationRecordService.insertRecord(operationRecord);
|
|
|
+ int latestId = operationRecordService.queryLatestId();
|
|
|
+ // 保存TokenRecord
|
|
|
+ tokenRecord.setOpId(latestId);
|
|
|
+ insertRecord(tokenRecord);
|
|
|
+ return tokenRecord;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 主动请求微信生成accessToken
|
|
|
*
|
|
@@ -125,7 +214,7 @@ public class TokenRecordService {
|
|
|
}
|
|
|
|
|
|
OperationRecord operationRecord = wrapOperationRecord("目前暂无");
|
|
|
- TokenRecord tokenRecord = wrapTokenRecord(operationRecord, response);
|
|
|
+ TokenRecord tokenRecord = wrapTokenRecord(operationRecord, response,wxGeneralConfig.getAppId());
|
|
|
|
|
|
if (null == tokenRecord) {
|
|
|
Object errcode = response.get("errcode");
|
|
@@ -179,7 +268,7 @@ public class TokenRecordService {
|
|
|
* @param response 请求微信平台响应
|
|
|
* @return Token记录
|
|
|
*/
|
|
|
- private TokenRecord wrapTokenRecord(OperationRecord operationRecord, Map response) {
|
|
|
+ private TokenRecord wrapTokenRecord(OperationRecord operationRecord, Map response,String appid) {
|
|
|
TokenRecord tokenRecord = new TokenRecord();
|
|
|
String accessToken = (String) response.get("access_token");
|
|
|
Integer expiresIn = (Integer) response.get("expires_in");
|
|
@@ -196,6 +285,7 @@ public class TokenRecordService {
|
|
|
tokenRecord.setExpiresTime(DateUtils.formatDate(new Date(expiresTime)));
|
|
|
tokenRecord.setOpId(operationRecord.getOpId());
|
|
|
tokenRecord.setIsValid(0);
|
|
|
+ tokenRecord.setAppId(appid);
|
|
|
return tokenRecord;
|
|
|
}
|
|
|
return null;
|