Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
3bc0798f
Commit
3bc0798f
authored
Oct 26, 2023
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公众号刷新token方式修改
parent
ff854114
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
17 deletions
+43
-17
ch-core/src/main/resources/application-dev.yml
ch-core/src/main/resources/application-dev.yml
+1
-1
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
.../src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
+42
-16
No files found.
ch-core/src/main/resources/application-dev.yml
View file @
3bc0798f
...
...
@@ -20,7 +20,7 @@ dts:
officialaccount-secret
:
daecb3de53841fe1a787bc9849ac3406
officialaccount-token
:
Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q
officialaccount-key
:
noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM
get-officialaccount-accesstoken-url
:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
get-officialaccount-accesstoken-url
:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
&appid=%s&secret=%s
#腾讯IM配置
im
:
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
View file @
3bc0798f
...
...
@@ -39,7 +39,7 @@ public class OfficialAccountApi {
private
String
OFFICIAL_ACCOUNT_APPID
;
@Value
(
"${dts.wx.officialaccount-secret}"
)
private
String
APPSECRET
;
private
String
OFFICIAL_ACCOUNT_
APPSECRET
;
@Value
(
"${dts.wx.app-id}"
)
private
String
APPLET_APPID
;
...
...
@@ -47,8 +47,6 @@ public class OfficialAccountApi {
@Value
(
"${dts.wx.msg-url}"
)
private
String
MSG_URL
;
@Autowired
RedisUtils
redisUtils
;
...
...
@@ -56,6 +54,15 @@ public class OfficialAccountApi {
WxLoginManager
wxLoginManager
;
public
String
getAccessToken
()
{
//测试环境
if
(!
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
.
contains
(
"stable_token"
))
{
String
accessTokenUrl
=
String
.
format
(
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
,
OFFICIAL_ACCOUNT_APPID
,
OFFICIAL_ACCOUNT_APPSECRET
);
String
res
=
OkHttpUtil
.
builder
().
url
(
accessTokenUrl
)
.
get
().
sync
();
String
token
=
JsonUtils
.
getValueByPath
(
res
,
"access_token"
);
return
token
;
}
//正式环境先从缓存中获取,没有再刷新token
//根据商户的appid从redis中获取商户的token
String
token
=
redisUtils
.
hasKey
(
OFFICIAL_ACCOUNT_APPID
)?
redisUtils
.
get
(
OFFICIAL_ACCOUNT_APPID
).
toString
()
:
null
;
if
(
StringUtils
.
isEmpty
(
token
))
{
...
...
@@ -65,21 +72,40 @@ public class OfficialAccountApi {
}
public
String
refreshAccessToken
()
{
String
url
=
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
+
"&appid="
+
OFFICIAL_ACCOUNT_APPID
+
"&secret="
+
APPSECRET
;
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
okHttpUtil
.
get
();
String
responseStr
=
okHttpUtil
.
async
();
logger
.
info
(
"appid : {}, 获取token response :{}"
,
OFFICIAL_ACCOUNT_APPID
,
responseStr
);
try
{
String
accessTokenUrl
=
null
;
String
responseStr
=
null
;
//正式环境的url中包含了"stable_token", 要使用post请求, 测试环境是get请求
if
(!
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
.
contains
(
"stable_token"
))
{
accessTokenUrl
=
String
.
format
(
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
,
OFFICIAL_ACCOUNT_APPID
,
OFFICIAL_ACCOUNT_APPSECRET
);
logger
.
info
(
"测试环境获取公众号access_token url :{}"
,
accessTokenUrl
);
responseStr
=
OkHttpUtil
.
builder
().
url
(
accessTokenUrl
)
.
get
().
sync
();
}
else
{
accessTokenUrl
=
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
;
logger
.
info
(
"正式环境获取公众号access_token url :{}"
,
accessTokenUrl
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"grant_type"
,
"client_credential"
);
map
.
put
(
"appid"
,
OFFICIAL_ACCOUNT_APPID
);
map
.
put
(
"secret"
,
OFFICIAL_ACCOUNT_APPSECRET
);
responseStr
=
OkHttpUtil
.
builder
().
addParams
(
map
).
url
(
accessTokenUrl
)
.
post
(
true
).
sync
();
}
logger
.
info
(
"公众号刷新access_token response :{}"
,
responseStr
);
if
(!
responseStr
.
contains
(
"access_token"
))
{
String
errorCode
=
JsonUtils
.
getValueByPath
(
responseStr
,
"errcode"
);
String
errmsg
=
JsonUtils
.
getValueByPath
(
responseStr
,
"errmsg"
);
logger
.
error
(
"appid : {},
获取token出错, errorCode : {}, errmsg : {} "
,
OFFICIAL_ACCOUNT_APPID
,
errorCode
,
errmsg
);
logger
.
error
(
"appid : {}, 公众号
获取token出错, errorCode : {}, errmsg : {} "
,
OFFICIAL_ACCOUNT_APPID
,
errorCode
,
errmsg
);
return
null
;
}
String
token
=
JsonUtils
.
getValueByPath
(
responseStr
,
"access_token"
);
String
expires_in
=
JsonUtils
.
getValueByPath
(
responseStr
,
"expires_in"
);
redisUtils
.
set
(
OFFICIAL_ACCOUNT_APPID
,
token
,
Long
.
valueOf
(
expires_in
)
-
10
,
TimeUnit
.
SECONDS
);
return
token
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取公众号access_token失败"
,
e
);
return
null
;
}
}
/**
...
...
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