|
@@ -3,11 +3,13 @@ package com.caimei365.order.components;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.caimei365.order.model.ResponseJson;
|
|
|
+import com.caimei365.order.utils.JwtUtil;
|
|
|
import com.caimei365.order.utils.RequestUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -58,6 +60,11 @@ public class WeChatService {
|
|
|
@Value("${wx.message-template.receipt}")
|
|
|
private String messageTemplateReceipt;
|
|
|
|
|
|
+ private RedisService redisService;
|
|
|
+ @Autowired
|
|
|
+ public void setRedisService(RedisService redisService) {
|
|
|
+ this.redisService = redisService;
|
|
|
+ }
|
|
|
|
|
|
public static class Keys {
|
|
|
public static final String OPEN_ID = "openid";
|
|
@@ -223,13 +230,21 @@ public class WeChatService {
|
|
|
* @return access_token
|
|
|
*/
|
|
|
public String getAccessToken() throws Exception {
|
|
|
+ String access_token = String.valueOf(redisService.get("access_token:"+crmAppId));
|
|
|
+ // 过期后会得到"null"值,所以需判断字符串"null"
|
|
|
+ if (access_token != null && access_token.length() != 0 && !"null".equals(access_token)) {
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
String link = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
|
|
|
link = link.replace("APPID", crmAppId);
|
|
|
link = link.replace("APPSECRET", crmAppSecret);
|
|
|
String result = RequestUtil.sendGet(link);
|
|
|
log.info("微信公众号获取access_token>>>" + result);
|
|
|
Map<String, Object> map = JSONObject.parseObject(result, Map.class);
|
|
|
- return (String) map.get("access_token");
|
|
|
+ access_token = (String) map.get("access_token");
|
|
|
+ // 将token存入redis, access_token的有效期目前为2个小时(redis存1.5小时)
|
|
|
+ redisService.set("access_token:"+crmAppId, access_token, 5400L);
|
|
|
+ return access_token;
|
|
|
}
|
|
|
|
|
|
/**
|