Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gk
Project
Project
Details
Activity
Releases
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
micro_app
gk
Commits
85e5443f
Commit
85e5443f
authored
Sep 03, 2022
by
chenlin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
获取小程序码access_token失效问题
parent
823cc9dd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
5 deletions
+45
-5
ResourcesController.java
...m/dlbridge/jadp/plugins/cmpt/web/ResourcesController.java
+45
-5
No files found.
src/main/java/com/dlbridge/jadp/plugins/cmpt/web/ResourcesController.java
View file @
85e5443f
package
com
.
dlbridge
.
jadp
.
plugins
.
cmpt
.
web
;
package
com
.
dlbridge
.
jadp
.
plugins
.
cmpt
.
web
;
import
com.dlbridge.jadp.plugins.cmpt.bean.Partner
;
import
com.dlbridge.jadp.plugins.cmpt.dao.model.Cmpt
;
import
com.dlbridge.jadp.plugins.cmpt.dao.model.Cmpt
;
import
com.dlbridge.jadp.plugins.cmpt.service.CmptService
;
import
com.dlbridge.jadp.plugins.cmpt.service.CmptService
;
import
com.dlbridge.jadp.plugins.cmpt.service.PartnerService
;
import
com.dlbridge.jadp.plugins.cmpt.utils.HttpUtil
;
import
com.dlbridge.jadp.plugins.cmpt.utils.json.JsonUtil
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
lombok.extern.log4j.Log4j2
;
import
lombok.extern.log4j.Log4j2
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.springframework.data.redis.core.ValueOperations
;
import
org.springframework.data.redis.core.ValueOperations
;
...
@@ -21,6 +26,8 @@ import java.awt.geom.RoundRectangle2D;
...
@@ -21,6 +26,8 @@ import java.awt.geom.RoundRectangle2D;
import
java.awt.image.BufferedImage
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.io.*
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
@Controller
@Controller
@RequestMapping
(
value
=
"/img"
)
@RequestMapping
(
value
=
"/img"
)
...
@@ -34,6 +41,9 @@ public class ResourcesController {
...
@@ -34,6 +41,9 @@ public class ResourcesController {
@Resource
@Resource
CmptService
cmptService
;
CmptService
cmptService
;
@Resource
PartnerService
partnerService
;
@Resource
@Resource
private
ValueOperations
valueOperations
;
private
ValueOperations
valueOperations
;
...
@@ -42,12 +52,19 @@ public class ResourcesController {
...
@@ -42,12 +52,19 @@ public class ResourcesController {
JSONObject
obj
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
();
obj
.
put
(
"scene"
,
"raceId="
+
cmptid
);
obj
.
put
(
"scene"
,
"raceId="
+
cmptid
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+
(
String
)
valueOperations
.
get
(
"micro_gk:access_token_"
+
partnerId
)
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
// 重新获取小程序access_token
String
accessToken
=
getMicroAccessToken
(
partnerId
);
// String accessToken = (String) valueOperations.get("micro_gk:access_token_" + partnerId);
String
url
=
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+
accessToken
;
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
url
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
ServletOutputStream
outputStream
=
response
.
getOutputStream
();
ServletOutputStream
outputStream
=
response
.
getOutputStream
();
org
.
springframework
.
core
.
io
.
Resource
body
=
resourceResponseEntity
.
getBody
();
org
.
springframework
.
core
.
io
.
Resource
body
=
resourceResponseEntity
.
getBody
();
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
log
.
info
(
"=====/img/{cmptid}, accessToken:"
+
accessToken
+
", cmptLogo:"
+
cmpt
.
getLogo
());
if
(!
StringUtils
.
isEmpty
(
cmpt
.
getLogo
()))
{
if
(!
StringUtils
.
isEmpty
(
cmpt
.
getLogo
()))
{
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
...
@@ -64,9 +81,13 @@ public class ResourcesController {
...
@@ -64,9 +81,13 @@ public class ResourcesController {
JSONObject
obj
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
();
obj
.
put
(
"scene"
,
"raceId="
+
cmptid
);
obj
.
put
(
"scene"
,
"raceId="
+
cmptid
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
// 重新获取小程序access_token
String
accessToken
=
getMicroAccessToken
(
partnerId
);
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+
(
String
)
valueOperations
.
get
(
"micro_gk:access_token_"
+
partnerId
)
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
+
accessToken
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
if
(
resourceResponseEntity
.
getBody
().
getInputStream
()
!=
null
){
if
(
resourceResponseEntity
.
getBody
().
getInputStream
()
!=
null
){
...
@@ -102,7 +123,6 @@ public class ResourcesController {
...
@@ -102,7 +123,6 @@ public class ResourcesController {
BufferedImage
.
TYPE_INT_ARGB
);
BufferedImage
.
TYPE_INT_ARGB
);
Graphics2D
g2
=
output
.
createGraphics
();
Graphics2D
g2
=
output
.
createGraphics
();
g2
.
setComposite
(
AlphaComposite
.
Src
);
g2
.
setComposite
(
AlphaComposite
.
Src
);
g2
.
setRenderingHint
(
RenderingHints
.
KEY_ANTIALIASING
,
g2
.
setRenderingHint
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
RenderingHints
.
VALUE_ANTIALIAS_ON
);
...
@@ -110,7 +130,6 @@ public class ResourcesController {
...
@@ -110,7 +130,6 @@ public class ResourcesController {
g2
.
fill
(
new
RoundRectangle2D
.
Float
(
0
,
0
,
140
,
140
,
140
,
g2
.
fill
(
new
RoundRectangle2D
.
Float
(
0
,
0
,
140
,
140
,
140
,
140
));
140
));
g2
.
setComposite
(
AlphaComposite
.
SrcAtop
);
g2
.
setComposite
(
AlphaComposite
.
SrcAtop
);
g2
.
drawImage
(
image
,
0
,
0
,
null
);
g2
.
drawImage
(
image
,
0
,
0
,
null
);
g2
.
dispose
();
g2
.
dispose
();
...
@@ -137,4 +156,25 @@ public class ResourcesController {
...
@@ -137,4 +156,25 @@ public class ResourcesController {
out
.
close
();
out
.
close
();
}
}
/**
* 重新获取小程序access_token
* @param partnerId 小程序组委会ID
* @return access_token
*/
private
String
getMicroAccessToken
(
String
partnerId
){
String
accessToken
=
(
String
)
valueOperations
.
get
(
"micro_gk:access_token_"
+
partnerId
);
log
.
info
(
"===== redis access_token:"
+
accessToken
);
// if(StringUtils.isEmpty(accessToken)){
Partner
partner
=
partnerService
.
getPartnerById
(
partnerId
);
String
aokenUrl
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+
partner
.
getAppId
()+
"&secret="
+
partner
.
getAppSecret
();
String
json
=
HttpUtil
.
doGet
(
aokenUrl
,
null
);
log
.
info
(
"URL:"
+
aokenUrl
+
", 返回结果:"
+
json
);
Map
<
String
,
Object
>
resultMap
=
JsonUtil
.
fromJson
(
json
,
new
TypeReference
<
Map
<
String
,
Object
>>()
{});
accessToken
=
resultMap
.
get
(
"access_token"
).
toString
();
String
expiresIn
=
resultMap
.
get
(
"expires_in"
).
toString
();
valueOperations
.
set
(
"micro_gk:access_token_"
+
partnerId
,
TimeUnit
.
SECONDS
,
Long
.
parseLong
(
expiresIn
));
// }
return
accessToken
;
}
}
}
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