You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.2 KiB
109 lines
3.2 KiB
package com.ruoyi.hl.service; |
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.ruoyi.common.core.redis.RedisCache; |
|
import com.ruoyi.hl.forest.ptoto.WxMiniTokenRes; |
|
import com.ruoyi.hl.config.WxConfig; |
|
import com.ruoyi.hl.forest.clients.MiniWeChatClients; |
|
import lombok.extern.slf4j.Slf4j; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.boot.CommandLineRunner; |
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
@Service |
|
@Slf4j |
|
public class WxTokenService implements CommandLineRunner { |
|
|
|
private static String pTokenK = "k_wx_public_token_29"; |
|
|
|
|
|
|
|
@Autowired |
|
private MiniWeChatClients miniWeChatClients; |
|
|
|
|
|
//@Autowired |
|
private RedisCache redisCache; |
|
|
|
@Autowired |
|
private WxConfig wxConfig; |
|
|
|
@Override |
|
public void run(String... args) throws Exception { |
|
//refreshToken();//刷新token |
|
} |
|
|
|
|
|
public String getPublickToken() { |
|
String pToken = redisCache.getCacheObject(pTokenK); |
|
if (StrUtil.isBlankIfStr(pToken)) { |
|
log.info("---> expire, refresh token"); |
|
return refreshPublicToken(); |
|
} |
|
return pToken; |
|
} |
|
|
|
|
|
/** |
|
* 每分钟检测redis里的token是否到期 |
|
*/ |
|
//@Scheduled(cron = "0 * * * * ?") |
|
public void refreshToken() { |
|
String pToken = redisCache.getCacheObject(pTokenK); |
|
if (pToken == null || pToken.equals("token")) { |
|
refreshPublicToken(); |
|
} |
|
} |
|
|
|
/** |
|
* 刷新公众号token |
|
*/ |
|
public synchronized String refreshPublicToken() { |
|
String pToken = redisCache.getCacheObject(pTokenK); |
|
if (!StrUtil.isBlankIfStr(pToken)) { |
|
log.info("---> valid token"); |
|
return pToken; |
|
} |
|
|
|
|
|
|
|
String token=null; |
|
try { |
|
log.info("--> refreshPublicToken refresh start"); |
|
|
|
String publicSecret= wxConfig.getPublicSecret(); |
|
String publicAppid= wxConfig.getPublicAppid(); |
|
if (StrUtil.isBlank(publicSecret) || StrUtil.isBlank(publicAppid)) { |
|
log.info("---> pSecretV or pAppidV is empty"); |
|
return token; |
|
} |
|
|
|
WxMiniTokenRes wxMiniTokenRes = miniWeChatClients.token(publicAppid, publicSecret); |
|
if (wxMiniTokenRes.getAccessToken() != null && wxMiniTokenRes.getExpiresIn() != null) { |
|
token = wxMiniTokenRes.getAccessToken(); |
|
Long expireTime = wxMiniTokenRes.getExpiresIn() - (wxMiniTokenRes.getExpiresIn() / 4);//redis超时时间是原时间的3/4 |
|
|
|
|
|
|
|
redisCache.setCacheObject(pTokenK, token, expireTime.intValue(), TimeUnit.SECONDS); |
|
//stringRedisTemplate.opsForValue().set(pTokenK, token, expireTime, TimeUnit.SECONDS);//token存放到redis,设置超时时间 |
|
|
|
log.info("---> public expireTime:{}, token:{}", expireTime, token); |
|
} else { |
|
log.info("---> refresh public token fail, msg:{}, code:{}", wxMiniTokenRes.getErrmsg(), wxMiniTokenRes.getErrcode()); |
|
} |
|
log.info("--> refreshPublicToken refresh end"); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
log.error("---> refreshPublicToken() exception, ", e); |
|
} |
|
return token; |
|
} |
|
|
|
|
|
}
|
|
|