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
92630eb6
Commit
92630eb6
authored
Dec 28, 2023
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
小程序二维码token失效问题
parent
f5cd3cad
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
53 deletions
+66
-53
ResourcesController.java
...m/dlbridge/jadp/plugins/cmpt/web/ResourcesController.java
+66
-53
No files found.
src/main/java/com/dlbridge/jadp/plugins/cmpt/web/ResourcesController.java
View file @
92630eb6
...
...
@@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.stereotype.Controller
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.client.RestClientException
;
import
org.springframework.web.client.RestTemplate
;
import
springfox.documentation.annotations.ApiIgnore
;
...
...
@@ -56,25 +57,27 @@ public class ResourcesController {
JSONObject
obj
=
new
JSONObject
();
obj
.
put
(
"scene"
,
"raceId="
+
cmptid
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
// 重新获取小程序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
();
org
.
springframework
.
core
.
io
.
Resource
body
=
resourceResponseEntity
.
getBody
();
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
log
.
info
(
"=====/img/{cmptid}, accessToken:"
+
accessToken
+
", cmptLogo:"
+
cmpt
.
getLogo
());
if
(!
StringUtils
.
isEmpty
(
cmpt
.
getLogo
()))
{
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
overlapImage
(
bg
,
qr
,
response
);
}
else
{
writeBytes
(
body
.
getInputStream
(),
outputStream
);
String
url
=
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
;
try
{
// 重新获取小程序access_token
String
accessToken
=
getMicroAccessToken
(
partnerId
);
url
+=
accessToken
;
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
url
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
ServletOutputStream
outputStream
=
response
.
getOutputStream
();
org
.
springframework
.
core
.
io
.
Resource
body
=
resourceResponseEntity
.
getBody
();
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
log
.
info
(
"===== 获取小程序二维码/img/{cmptid}, accessToken:"
+
accessToken
+
", cmptLogo:"
+
cmpt
.
getLogo
());
if
(!
StringUtils
.
isEmpty
(
cmpt
.
getLogo
()))
{
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
overlapImage
(
bg
,
qr
,
response
);
}
else
{
writeBytes
(
body
.
getInputStream
(),
outputStream
);
}
}
catch
(
Exception
e
)
{
log
.
info
(
"=====获取小程序二维码异常: "
+
e
.
getMessage
());
resetMicroAccessToken
(
partnerId
);
}
}
...
...
@@ -87,24 +90,27 @@ public class ResourcesController {
obj
.
put
(
"width"
,
"1280"
);
String
partnerId
=
cmptService
.
getPartnerId
(
cmptid
);
// 重新获取小程序access_token
String
accessToken
=
getMicroAccessToken
(
partnerId
);
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+
accessToken
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
if
(
resourceResponseEntity
.
getBody
().
getInputStream
()
!=
null
){
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
overlapImageHD
(
bg
,
qr
,
response
);
}
else
{
log
.
info
(
"=====获取小程序码为空:"
+
resourceResponseEntity
.
getBody
().
toString
());
try
{
// 重新获取小程序access_token
String
accessToken
=
getMicroAccessToken
(
partnerId
);
ResponseEntity
<
org
.
springframework
.
core
.
io
.
Resource
>
resourceResponseEntity
=
restTemplate
.
postForEntity
(
"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
+
accessToken
,
obj
.
toString
(),
org
.
springframework
.
core
.
io
.
Resource
.
class
);
Cmpt
cmpt
=
cmptService
.
queryCmptBycmptId
(
cmptid
);
log
.
info
(
"===== 下载小程序二维码/download/{cmptid}, accessToken:"
+
accessToken
+
", cmptLogo:"
+
cmpt
.
getLogo
());
if
(
resourceResponseEntity
.
getBody
().
getInputStream
()
!=
null
){
BufferedImage
bg
=
ImageIO
.
read
(
resourceResponseEntity
.
getBody
().
getInputStream
());
BufferedImage
qr
=
ImageIO
.
read
(
new
URL
(
cmpt
.
getLogo
()));
overlapImageHD
(
bg
,
qr
,
response
);
}
else
{
log
.
info
(
"=====获取小程序码为空:"
+
resourceResponseEntity
.
getBody
().
toString
());
}
}
catch
(
Exception
e
)
{
log
.
info
(
"=====下载小程序二维码异常: "
+
e
.
getMessage
());
resetMicroAccessToken
(
partnerId
);
}
}
public
static
String
overlapImage
(
BufferedImage
bg
,
BufferedImage
qrCodeIps
,
HttpServletResponse
response
)
{
try
{
public
static
String
overlapImage
(
BufferedImage
bg
,
BufferedImage
qrCodeIps
,
HttpServletResponse
response
)
throws
Exception
{
//设置图片大小
BufferedImage
background
=
resizeImage
(
300
,
300
,
bg
);
BufferedImage
qrCode
=
resizeImage
(
140
,
140
,
qrCodeIps
);
...
...
@@ -116,28 +122,21 @@ public class ResourcesController {
g
.
drawImage
(
qrCode
,
80
,
80
,
qrCode
.
getWidth
(),
qrCode
.
getHeight
(),
null
);
g
.
dispose
();
ImageIO
.
write
(
background
,
"jpg"
,
response
.
getOutputStream
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
String
overlapImageHD
(
BufferedImage
bg
,
BufferedImage
qrCodeIps
,
HttpServletResponse
response
)
{
try
{
//设置图片大小
BufferedImage
background
=
resizeImage
(
1200
,
1200
,
bg
);
BufferedImage
qrCode
=
resizeImage
(
560
,
560
,
qrCodeIps
);
qrCode
=
converToRadius
(
qrCode
,
560
,
560
);
qrCode
.
createGraphics
().
setBackground
(
Color
.
WHITE
);
Graphics2D
g
=
background
.
createGraphics
();
//在背景图片上添加二维码图片
g
.
drawImage
(
qrCode
,
320
,
320
,
qrCode
.
getWidth
(),
qrCode
.
getHeight
(),
null
);
g
.
dispose
();
ImageIO
.
write
(
background
,
"jpg"
,
response
.
getOutputStream
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
public
static
String
overlapImageHD
(
BufferedImage
bg
,
BufferedImage
qrCodeIps
,
HttpServletResponse
response
)
throws
Exception
{
//设置图片大小
BufferedImage
background
=
resizeImage
(
1200
,
1200
,
bg
);
BufferedImage
qrCode
=
resizeImage
(
560
,
560
,
qrCodeIps
);
qrCode
=
converToRadius
(
qrCode
,
560
,
560
);
qrCode
.
createGraphics
().
setBackground
(
Color
.
WHITE
);
Graphics2D
g
=
background
.
createGraphics
();
//在背景图片上添加二维码图片
g
.
drawImage
(
qrCode
,
320
,
320
,
qrCode
.
getWidth
(),
qrCode
.
getHeight
(),
null
);
g
.
dispose
();
ImageIO
.
write
(
background
,
"jpg"
,
response
.
getOutputStream
());
return
null
;
}
...
...
@@ -201,4 +200,18 @@ public class ResourcesController {
return
accessToken
;
}
private
String
resetMicroAccessToken
(
String
partnerId
){
String
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
+
", 重新获取小程序token返回结果:"
+
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
,
accessToken
,
Long
.
parseLong
(
expiresIn
),
TimeUnit
.
SECONDS
);
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