Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yanxuan-wx-store-sharer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wx
yanxuan-wx-store-sharer
Commits
420f1cc3
Commit
420f1cc3
authored
Mar 17, 2025
by
fanjiaxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新加入获取用户分佣比例接口
parent
fc1d82fa
Pipeline
#71689
passed with stages
in 1 minute 6 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
18 deletions
+47
-18
WeChatContextHolder.java
...wx/store/sharer/integration/core/WeChatContextHolder.java
+19
-17
WeChatRestTemplateHandler.java
...sharer/integration/handler/WeChatRestTemplateHandler.java
+9
-1
WeChatErrorCodeEnum.java
...re/sharer/integration/meta/enums/WeChatErrorCodeEnum.java
+19
-0
No files found.
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/core/WeChatContextHolder.java
View file @
420f1cc3
...
...
@@ -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
();
}
}
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/handler/WeChatRestTemplateHandler.java
View file @
420f1cc3
...
...
@@ -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
;
}
...
...
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/meta/enums/WeChatErrorCodeEnum.java
0 → 100644
View file @
420f1cc3
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment