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
de585692
Commit
de585692
authored
Jul 03, 2024
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
小程序订阅消息,获取token
parent
8ea975a9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
4 deletions
+72
-4
ch-core/src/main/java/com/wwdz/ch/core/api/WxAppletApi.java
ch-core/src/main/java/com/wwdz/ch/core/api/WxAppletApi.java
+66
-2
ch-core/src/main/java/com/wwdz/ch/core/impl/SendMsgServiceImpl.java
...c/main/java/com/wwdz/ch/core/impl/SendMsgServiceImpl.java
+6
-2
No files found.
ch-core/src/main/java/com/wwdz/ch/core/api/WxAppletApi.java
View file @
de585692
...
...
@@ -4,7 +4,9 @@ import com.alibaba.fastjson.JSONArray;
import
com.wwdz.ch.core.entity.WxCheckMediaRequestDto
;
import
com.wwdz.ch.core.type.Result
;
import
com.wwdz.ch.core.util.OkHttpUtil
;
import
com.wwdz.ch.core.util.RedisUtils
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -14,6 +16,7 @@ import org.springframework.stereotype.Component;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
@Component
public
class
WxAppletApi
{
...
...
@@ -26,6 +29,15 @@ public class WxAppletApi {
@Value
(
"${dts.wx.app-secret}"
)
private
String
APPLET_APP_SECRET
;
@Autowired
RedisUtils
redisUtils
;
/**
* 测试环境和正式环境的url不同,相互隔离不会冲突
*/
@Value
(
"${dts.wx.get-officialaccount-accesstoken-url}"
)
private
String
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
;
@Value
(
"${dts.wx.auction-msg-url}"
)
private
String
AUCTION_MSG_URL
;
...
...
@@ -52,8 +64,6 @@ public class WxAppletApi {
}
public
String
checkMedia
(
WxCheckMediaRequestDto
dto
)
{
String
accessToken
=
wxLoginManager
.
getAccessToken
();
String
url
=
String
.
format
(
MEDIA_CHECK_URL
,
accessToken
);
...
...
@@ -144,5 +154,59 @@ public class WxAppletApi {
return
Result
.
failed
();
}
public
String
getAppletAccessToken
()
{
//测试环境
if
(!
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
.
contains
(
"stable_token"
))
{
String
accessTokenUrl
=
String
.
format
(
GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL
,
APPLET_APPID
,
APPLET_APP_SECRET
);
String
res
=
OkHttpUtil
.
builder
().
url
(
accessTokenUrl
)
.
get
().
sync
();
logger
.
info
(
"测试环境获取小程序token返回结果: {}"
,
res
);
String
token
=
JsonUtils
.
getValueByPath
(
res
,
"access_token"
);
return
token
;
}
//正式环境先从缓存中获取,没有再刷新token
//根据商户的appid从redis中获取商户的token
String
token
=
redisUtils
.
hasKey
(
APPLET_APPID
)?
redisUtils
.
get
(
APPLET_APP_SECRET
).
toString
()
:
null
;
if
(
StringUtils
.
isEmpty
(
token
))
{
token
=
refreshAppletAccessToken
();
}
return
token
;
}
public
String
refreshAppletAccessToken
()
{
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
,
APPLET_APPID
,
APPLET_APP_SECRET
);
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"
,
APPLET_APPID
);
map
.
put
(
"secret"
,
APPLET_APP_SECRET
);
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 : {} "
,
APPLET_APPID
,
errorCode
,
errmsg
);
return
null
;
}
String
token
=
JsonUtils
.
getValueByPath
(
responseStr
,
"access_token"
);
String
expires_in
=
JsonUtils
.
getValueByPath
(
responseStr
,
"expires_in"
);
redisUtils
.
set
(
APPLET_APPID
,
token
,
Long
.
valueOf
(
expires_in
)
-
10
,
TimeUnit
.
SECONDS
);
return
token
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取小程序access_token失败"
,
e
);
return
null
;
}
}
}
ch-core/src/main/java/com/wwdz/ch/core/impl/SendMsgServiceImpl.java
View file @
de585692
package
com.wwdz.ch.core.impl
;
import
com.wwdz.ch.core.api.OfficialAccountApi
;
import
com.wwdz.ch.core.api.WxAppletApi
;
import
com.wwdz.ch.core.consts.CommConsts
;
import
com.wwdz.ch.core.entity.*
;
import
com.wwdz.ch.core.type.Result
;
...
...
@@ -61,6 +62,9 @@ public class SendMsgServiceImpl implements SendMsgService {
@Autowired
UserDao
userDao
;
@Autowired
WxAppletApi
wxAppletApi
;
@Autowired
OfficialAccountSubscribeRecordDao
officialAccountSubscribeRecordDao
;
...
...
@@ -614,7 +618,7 @@ public class SendMsgServiceImpl implements SendMsgService {
*/
public
Result
sendSpecialPerformanceStartMsg
(
AuctionOfferNoticeMsg
auctionOfferNoticeMsg
)
{
String
openId
=
auctionOfferNoticeMsg
.
getOpenId
();
String
token
=
officialAccountApi
.
g
etAccessToken
();
String
token
=
wxAppletApi
.
getAppl
etAccessToken
();
String
url
=
SEND_SUBSCRIBE_MSG_URL
+
"?access_token="
+
token
;
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
...
...
@@ -657,7 +661,7 @@ public class SendMsgServiceImpl implements SendMsgService {
*/
public
Result
sendSpecialPerformanceEndMsg
(
AuctionOfferNoticeMsg
auctionOfferNoticeMsg
)
{
String
openId
=
auctionOfferNoticeMsg
.
getOpenId
();
String
token
=
officialAccountApi
.
g
etAccessToken
();
String
token
=
wxAppletApi
.
getAppl
etAccessToken
();
String
url
=
SEND_SUBSCRIBE_MSG_URL
+
"?access_token="
+
token
;
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
...
...
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