Commit 420f1cc3 by fanjiaxin

新加入获取用户分佣比例接口

parent fc1d82fa
Pipeline #71689 passed with stages
in 1 minute 6 seconds
......@@ -7,6 +7,7 @@ import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatAccess
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;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
......@@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit;
* @Author fanjiaxin
* @Date 2025/2/24 11:53
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class WeChatContextHolder {
......@@ -70,28 +72,28 @@ public class WeChatContextHolder {
if (StringUtils.isNotBlank(accessToken)) {
return accessToken;
}
try {
return createShopAccessToken();
} catch (InterruptedException e) {
throw new BizException("微信认证获取AccessToken异常", e);
}
return createShopAccessToken();
}
/**
* 创建微信小店认证信息
*/
public String createShopAccessToken() throws InterruptedException {
// 设置10秒的锁
boolean locked = redisClient.setIfAbsent(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_KEY,
CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_VALUE, CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_TIME);
if (!locked) {
// 未获取到锁,等待一段时间后重试
TimeUnit.MILLISECONDS.sleep(RETRY_DELAY_MS);
return getShopAccessToken();
public String createShopAccessToken() {
try {
// 设置10秒的锁
boolean locked = redisClient.setIfAbsent(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_KEY,
CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_VALUE, CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_TIME);
if (!locked) {
// 未获取到锁,等待一段时间后重试
TimeUnit.MILLISECONDS.sleep(RETRY_DELAY_MS);
return getShopAccessToken();
}
WeChatAccessTokenVO handle = weChatShopAccessTokenRequest.handle();
redisClient.setStr(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_KEY, handle.getAccess_token(),
handle.getExpires_in() - EXPIRE_SECONDS);
return handle.getAccess_token();
} catch (InterruptedException e) {
throw new BizException("微信小店认证AccessToken生成失败", e);
}
WeChatAccessTokenVO handle = weChatShopAccessTokenRequest.handle();
redisClient.setStr(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_KEY, handle.getAccess_token(),
handle.getExpires_in() - EXPIRE_SECONDS);
return handle.getAccess_token();
}
}
......@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
import com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException;
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;
import lombok.extern.slf4j.Slf4j;
......@@ -21,6 +23,8 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class WeChatRestTemplateHandler {
private final RestTemplateClient restTemplateClient;
private final WeChatContextHolder weChatContextHolder;
public <T, R extends WeChatCoreVO> R execute(String url, HttpMethod method, T params, Class<R> resType) {
R result;
......@@ -32,7 +36,11 @@ public class WeChatRestTemplateHandler {
}
if (StringUtils.isNotBlank(result.getErrcode())
&& !WeChatApi.WECHAT_REQUEST_SUCCESS_CODE.equals(result.getErrcode())) {
throw new WeChatException("调用微信接口失败," + result.getErrmsg());
if(WeChatErrorCodeEnum.INVALID_CREDENTIAL.getCode().equals(result.getErrcode())){
// token过期重新生成
weChatContextHolder.createShopAccessToken();
}
throw new WeChatException(String.join(",","调用微信接口失败" + result.getErrmsg()));
}
return result;
}
......
package com.netease.yanxuan.wx.store.sharer.integration.meta.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* @Description 微信错误码
* @Author fanjiaxin
* @Date 2025/3/3 13:58
*/
@Getter
@RequiredArgsConstructor
public enum WeChatErrorCodeEnum {
INVALID_CREDENTIAL("40001", "获取 access_token 时 AppSecret 错误,或者 access_token 无效。");
private final String code;
private final String desc;
}
\ No newline at end of file
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