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 ...@@ -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.handler.impl.WeChatShopAccessTokenRequest;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatAccessTokenVO; import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatAccessTokenVO;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit; ...@@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit;
* @Author fanjiaxin * @Author fanjiaxin
* @Date 2025/2/24 11:53 * @Date 2025/2/24 11:53
*/ */
@Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class WeChatContextHolder { public class WeChatContextHolder {
...@@ -70,17 +72,14 @@ public class WeChatContextHolder { ...@@ -70,17 +72,14 @@ public class WeChatContextHolder {
if (StringUtils.isNotBlank(accessToken)) { if (StringUtils.isNotBlank(accessToken)) {
return accessToken; return accessToken;
} }
try {
return createShopAccessToken(); return createShopAccessToken();
} catch (InterruptedException e) {
throw new BizException("微信认证获取AccessToken异常", e);
}
} }
/** /**
* 创建微信小店认证信息 * 创建微信小店认证信息
*/ */
public String createShopAccessToken() throws InterruptedException { public String createShopAccessToken() {
try {
// 设置10秒的锁 // 设置10秒的锁
boolean locked = redisClient.setIfAbsent(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_KEY, 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); CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_VALUE, CoreConstant.REDIS_TOKEN_WECHAT_SHOP_LOCK_TIME);
...@@ -93,5 +92,8 @@ public class WeChatContextHolder { ...@@ -93,5 +92,8 @@ public class WeChatContextHolder {
redisClient.setStr(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_KEY, handle.getAccess_token(), redisClient.setStr(CoreConstant.REDIS_TOKEN_WECHAT_SHOP_KEY, handle.getAccess_token(),
handle.getExpires_in() - EXPIRE_SECONDS); handle.getExpires_in() - EXPIRE_SECONDS);
return handle.getAccess_token(); return handle.getAccess_token();
} catch (InterruptedException e) {
throw new BizException("微信小店认证AccessToken生成失败", e);
}
} }
} }
...@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON; ...@@ -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.exception.WeChatException;
import com.netease.yanxuan.wx.store.sharer.common.handler.RestTemplateClient; 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.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 com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatCoreVO;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -21,6 +23,8 @@ import org.springframework.stereotype.Service; ...@@ -21,6 +23,8 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor @RequiredArgsConstructor
public class WeChatRestTemplateHandler { public class WeChatRestTemplateHandler {
private final RestTemplateClient restTemplateClient; private final RestTemplateClient restTemplateClient;
private final WeChatContextHolder weChatContextHolder;
public <T, R extends WeChatCoreVO> R execute(String url, HttpMethod method, T params, Class<R> resType) { public <T, R extends WeChatCoreVO> R execute(String url, HttpMethod method, T params, Class<R> resType) {
R result; R result;
...@@ -32,7 +36,11 @@ public class WeChatRestTemplateHandler { ...@@ -32,7 +36,11 @@ public class WeChatRestTemplateHandler {
} }
if (StringUtils.isNotBlank(result.getErrcode()) if (StringUtils.isNotBlank(result.getErrcode())
&& !WeChatApi.WECHAT_REQUEST_SUCCESS_CODE.equals(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; 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