Commit 85e5443f authored by chenlin's avatar chenlin

获取小程序码access_token失效问题

parent 823cc9dd
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.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 org.json.JSONObject;
import org.springframework.data.redis.core.ValueOperations;
......@@ -21,6 +26,8 @@ import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Controller
@RequestMapping(value = "/img")
......@@ -34,6 +41,9 @@ public class ResourcesController {
@Resource
CmptService cmptService;
@Resource
PartnerService partnerService;
@Resource
private ValueOperations valueOperations;
......@@ -42,12 +52,19 @@ public class ResourcesController {
JSONObject obj = new JSONObject();
obj.put("scene", "raceId=" + 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();
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()));
......@@ -64,9 +81,13 @@ public class ResourcesController {
JSONObject obj = new JSONObject();
obj.put("scene", "raceId=" + cmptid);
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="
+ (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);
if(resourceResponseEntity.getBody().getInputStream() != null){
......@@ -102,7 +123,6 @@ public class ResourcesController {
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
......@@ -110,7 +130,6 @@ public class ResourcesController {
g2.fill(new RoundRectangle2D.Float(0, 0, 140, 140, 140,
140));
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null);
g2.dispose();
......@@ -137,4 +156,25 @@ public class ResourcesController {
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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment