Commit 6ccfba22 by fanjiaxin

加入token过期逻辑

parent 420f1cc3
Pipeline #71692 passed with stages
in 59 seconds
package com.netease.yanxuan.wx.store.sharer.integration.core;
import com.netease.yanxuan.wx.store.sharer.common.constant.CoreConstant;
import com.netease.yanxuan.wx.store.sharer.common.exception.BizException;
import com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException;
import com.netease.yanxuan.wx.store.sharer.common.handler.RedisClient;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatAccessTokenRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopAccessTokenRequest;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatAccessTokenVO;
import lombok.RequiredArgsConstructor;
......@@ -23,45 +22,11 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class WeChatContextHolder {
private final RedisClient redisClient;
private final WeChatAccessTokenRequest weChatAccessTokenRequest;
private final WeChatShopAccessTokenRequest weChatShopAccessTokenRequest;
private static final long RETRY_DELAY_MS = 200;
private static final int EXPIRE_SECONDS = 30 * 60;
/**
* 获取微信认证信息
*/
public String getAccessToken() {
// 双重检查锁定
String accessToken = redisClient.getStr(CoreConstant.REDIS_TOKEN_WECHAT_KEY);
if (StringUtils.isNotBlank(accessToken)) {
return accessToken;
}
try {
return createAccessToken();
} catch (InterruptedException e) {
throw new BizException("微信认证获取AccessToken异常", e);
}
}
/**
* 创建微信认证信息
*/
public String createAccessToken() throws InterruptedException {
// 设置10秒的锁
boolean locked = redisClient.setIfAbsent(CoreConstant.REDIS_TOKEN_WECHAT_LOCK_KEY,
CoreConstant.REDIS_TOKEN_WECHAT_LOCK_VALUE, CoreConstant.REDIS_TOKEN_WECHAT_LOCK_TIME);
if (!locked) {
// 未获取到锁,等待一段时间后重试
TimeUnit.MILLISECONDS.sleep(RETRY_DELAY_MS);
return getAccessToken();
}
WeChatAccessTokenVO handle = weChatAccessTokenRequest.handle();
redisClient.setStr(CoreConstant.REDIS_TOKEN_WECHAT_KEY, handle.getAccess_token(),
handle.getExpires_in() - EXPIRE_SECONDS);
return handle.getAccess_token();
}
/**
* 获取微信小店认证信息
......@@ -93,7 +58,7 @@ public class WeChatContextHolder {
handle.getExpires_in() - EXPIRE_SECONDS);
return handle.getAccess_token();
} catch (InterruptedException e) {
throw new BizException("微信小店认证AccessToken生成失败", e);
throw new WeChatException("微信小店认证AccessToken生成失败", e);
}
}
}
package com.netease.yanxuan.wx.store.sharer.integration.handler;
import com.alibaba.fastjson.JSON;
import com.netease.yanxuan.wx.store.sharer.common.constant.CoreConstant;
import com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException;
import com.netease.yanxuan.wx.store.sharer.common.handler.RedisClient;
import com.netease.yanxuan.wx.store.sharer.common.handler.RestTemplateClient;
import com.netease.yanxuan.wx.store.sharer.integration.constant.WeChatApi;
import com.netease.yanxuan.wx.store.sharer.integration.core.WeChatContextHolder;
import com.netease.yanxuan.wx.store.sharer.integration.meta.enums.WeChatErrorCodeEnum;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatCoreVO;
import lombok.RequiredArgsConstructor;
......@@ -23,7 +24,7 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class WeChatRestTemplateHandler {
private final RestTemplateClient restTemplateClient;
private final WeChatContextHolder weChatContextHolder;
private final RedisClient redisClient;
public <T, R extends WeChatCoreVO> R execute(String url, HttpMethod method, T params, Class<R> resType) {
......@@ -37,8 +38,8 @@ public class WeChatRestTemplateHandler {
if (StringUtils.isNotBlank(result.getErrcode())
&& !WeChatApi.WECHAT_REQUEST_SUCCESS_CODE.equals(result.getErrcode())) {
if(WeChatErrorCodeEnum.INVALID_CREDENTIAL.getCode().equals(result.getErrcode())){
// token过期重新生成
weChatContextHolder.createShopAccessToken();
// token过期,清除微信小店认证信息
redisClient.delete(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_KEY);
}
throw new WeChatException(String.join(",","调用微信接口失败" + result.getErrmsg()));
}
......
package com.netease.yanxuan.wx.store.sharer.integration.task;
import com.netease.yanxuan.wx.store.sharer.integration.core.WeChatContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @Description 微信小店认证信息初始化
* @Date 2025/3/11 11:57
*/
@Slf4j
@RequiredArgsConstructor
@Component
public class WeChatAuthTask implements CommandLineRunner {
private final WeChatContextHolder weChatContextHolder;
@Override
public void run(String... args) {
log.info("微信认证信息初始化开始...");
try {
weChatContextHolder.createAccessToken();
log.info("微信认证信息初始化完成");
} catch (Exception e) {
log.error("微信认证信息初始化失败", e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment