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
6ccfba22
Commit
6ccfba22
authored
Mar 17, 2025
by
fanjiaxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加入token过期逻辑
parent
420f1cc3
Pipeline
#71692
passed with stages
in 59 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
7 additions
and
71 deletions
+7
-71
WeChatContextHolder.java
...wx/store/sharer/integration/core/WeChatContextHolder.java
+2
-37
WeChatRestTemplateHandler.java
...sharer/integration/handler/WeChatRestTemplateHandler.java
+5
-4
WeChatAuthTask.java
...xuan/wx/store/sharer/integration/task/WeChatAuthTask.java
+0
-30
No files found.
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/core/WeChatContextHolder.java
View file @
6ccfba22
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
integration
.
core
;
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.constant.CoreConstant
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.
Biz
Exception
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.
WeChat
Exception
;
import
com.netease.yanxuan.wx.store.sharer.common.handler.RedisClient
;
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.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
;
...
@@ -23,45 +22,11 @@ import java.util.concurrent.TimeUnit;
...
@@ -23,45 +22,11 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
@RequiredArgsConstructor
public
class
WeChatContextHolder
{
public
class
WeChatContextHolder
{
private
final
RedisClient
redisClient
;
private
final
RedisClient
redisClient
;
private
final
WeChatAccessTokenRequest
weChatAccessTokenRequest
;
private
final
WeChatShopAccessTokenRequest
weChatShopAccessTokenRequest
;
private
final
WeChatShopAccessTokenRequest
weChatShopAccessTokenRequest
;
private
static
final
long
RETRY_DELAY_MS
=
200
;
private
static
final
long
RETRY_DELAY_MS
=
200
;
private
static
final
int
EXPIRE_SECONDS
=
30
*
60
;
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 {
...
@@ -93,7 +58,7 @@ public class WeChatContextHolder {
handle
.
getExpires_in
()
-
EXPIRE_SECONDS
);
handle
.
getExpires_in
()
-
EXPIRE_SECONDS
);
return
handle
.
getAccess_token
();
return
handle
.
getAccess_token
();
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
throw
new
Biz
Exception
(
"微信小店认证AccessToken生成失败"
,
e
);
throw
new
WeChat
Exception
(
"微信小店认证AccessToken生成失败"
,
e
);
}
}
}
}
}
}
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/handler/WeChatRestTemplateHandler.java
View file @
6ccfba22
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
integration
.
handler
;
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
integration
.
handler
;
import
com.alibaba.fastjson.JSON
;
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.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.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.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
;
...
@@ -23,7 +24,7 @@ import org.springframework.stereotype.Service;
...
@@ -23,7 +24,7 @@ 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
;
private
final
RedisClient
redisClient
;
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
)
{
...
@@ -37,8 +38,8 @@ public class WeChatRestTemplateHandler {
...
@@ -37,8 +38,8 @@ 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
()))
{
if
(
WeChatErrorCodeEnum
.
INVALID_CREDENTIAL
.
getCode
().
equals
(
result
.
getErrcode
())){
if
(
WeChatErrorCodeEnum
.
INVALID_CREDENTIAL
.
getCode
().
equals
(
result
.
getErrcode
())){
// token过期
重新生成
// token过期
,清除微信小店认证信息
weChatContextHolder
.
createShopAccessToken
(
);
redisClient
.
delete
(
CoreConstant
.
REDIS_TOKEN_WECHAT_SHOP_KEY
);
}
}
throw
new
WeChatException
(
String
.
join
(
","
,
"调用微信接口失败"
+
result
.
getErrmsg
()));
throw
new
WeChatException
(
String
.
join
(
","
,
"调用微信接口失败"
+
result
.
getErrmsg
()));
}
}
...
...
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/task/WeChatAuthTask.java
deleted
100644 → 0
View file @
420f1cc3
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
);
}
}
}
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