|
@ -7,6 +7,7 @@ import cn.hutool.http.HttpRequest; |
|
|
|
|
|
|
|
|
import com.ccsens.util.DateUtil; |
|
|
import com.ccsens.util.DateUtil; |
|
|
import com.ccsens.util.JacksonUtil; |
|
|
import com.ccsens.util.JacksonUtil; |
|
|
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
import com.ccsens.util.WebConstant; |
|
|
import com.ccsens.util.WebConstant; |
|
|
import com.ccsens.util.bean.wx.po.*; |
|
|
import com.ccsens.util.bean.wx.po.*; |
|
|
import com.ccsens.util.exception.BaseException; |
|
|
import com.ccsens.util.exception.BaseException; |
|
@ -15,7 +16,10 @@ import com.ccsens.util.exception.WxException; |
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
import javax.annotation.Resource; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.security.MessageDigest; |
|
|
import java.security.MessageDigest; |
|
|
import java.util.Arrays; |
|
|
import java.util.Arrays; |
|
@ -24,12 +28,23 @@ import java.util.Map; |
|
|
/** |
|
|
/** |
|
|
* @author __zHangSan |
|
|
* @author __zHangSan |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
@Component |
|
|
public class WxGzhUtil { |
|
|
public class WxGzhUtil { |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
|
private RedisUtil redisUtil; |
|
|
|
|
|
private static WxGzhUtil util; |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
|
public void init(){ |
|
|
|
|
|
util.redisUtil = this.redisUtil; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WxGzhUtil.class); |
|
|
private static final Logger logger = LoggerFactory.getLogger(WxGzhUtil.class); |
|
|
/** |
|
|
/** |
|
|
* 全局唯一accessToken |
|
|
* 全局唯一accessToken |
|
|
*/ |
|
|
*/ |
|
|
private static WxAccessToken globalWxAccessToken; |
|
|
// private static WxAccessToken globalWxAccessToken;
|
|
|
/** |
|
|
/** |
|
|
* accessToken 过期提前10分钟刷新(为防止过期,改成提前一个小时刷新) |
|
|
* accessToken 过期提前10分钟刷新(为防止过期,改成提前一个小时刷新) |
|
|
*/ |
|
|
*/ |
|
@ -161,9 +176,8 @@ public class WxGzhUtil { |
|
|
* 获取Access_token |
|
|
* 获取Access_token |
|
|
*/ |
|
|
*/ |
|
|
public static String getAccessToken() throws BaseException { |
|
|
public static String getAccessToken() throws BaseException { |
|
|
if(globalWxAccessToken == null || |
|
|
Object obj = util.redisUtil.get(WebConstant.Wx.ACCESS_TOKEN); |
|
|
globalWxAccessToken.getCreatedAt() + globalWxAccessToken.getExpiresIn() |
|
|
if(obj == null || StrUtil.isBlank((String)obj) ){ |
|
|
>= DateUtil.currentSeconds() - ACCESS_TOKEN_RESERVED_SECONDS){ |
|
|
|
|
|
WxAccessToken wxAccessToken = null; |
|
|
WxAccessToken wxAccessToken = null; |
|
|
String url = String.format(URL_GET_ACCESS_TOKEN,"client_credential",appid_health,secret_health); |
|
|
String url = String.format(URL_GET_ACCESS_TOKEN,"client_credential",appid_health,secret_health); |
|
|
String response = HttpRequest.get(url).execute().body(); |
|
|
String response = HttpRequest.get(url).execute().body(); |
|
@ -182,10 +196,12 @@ public class WxGzhUtil { |
|
|
if (StrUtil.isEmpty(wxAccessToken.getAccessToken())) { |
|
|
if (StrUtil.isEmpty(wxAccessToken.getAccessToken())) { |
|
|
throw new BusinessException(-1,"can't find the access_token attribute."); |
|
|
throw new BusinessException(-1,"can't find the access_token attribute."); |
|
|
} |
|
|
} |
|
|
globalWxAccessToken = wxAccessToken; |
|
|
util.redisUtil.set(WebConstant.Wx.ACCESS_TOKEN, wxAccessToken.getAccessToken(), WebConstant.Wx.EXPIRE_TIME) |
|
|
globalWxAccessToken.setCreatedAt(DateUtil.currentSeconds()); |
|
|
logger.info("存储access_token:{}", wxAccessToken.getAccessToken()); |
|
|
|
|
|
return wxAccessToken.getAccessToken(); |
|
|
} |
|
|
} |
|
|
return globalWxAccessToken.getAccessToken(); |
|
|
logger.info("读取reids的token:{}", obj); |
|
|
|
|
|
return (String)obj; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|