From 07740cac800cdaa6f47ccf607edbb33bb5512b87 Mon Sep 17 00:00:00 2001 From: yanghui <2536613402@qq.com> Date: 星期二, 01 十一月 2022 15:39:25 +0800 Subject: [PATCH] #feat 获取access_token从redis获取 --- springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java | 38 ++++++++++++++++++++++++++++---------- 1 files changed, 28 insertions(+), 10 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java b/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java index 226de90..a24f5e8 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java +++ b/springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java @@ -9,6 +9,9 @@ import javax.annotation.Resource; +import com.panzhihua.applets.config.WxMaConfiguration; +import com.panzhihua.common.api.LcApiConstants; +import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.web.bind.annotation.GetMapping; @@ -33,6 +36,8 @@ @Resource private StringRedisTemplate stringRedisTemplate; + @Resource + private WxMaConfiguration wxMaConfiguration; // 公众号appid private List<String> appidList = new ArrayList<String>() { @@ -93,7 +98,15 @@ private String getToken(String appid, String secret) throws IOException { // access_token接口https请求方式: GET // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET - String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; + try { + //改从redis中获取 + return wxMaConfiguration.getMaService(appid).getAccessToken(); + } catch (WxErrorException e) { + e.printStackTrace(); + } + return null; + +/* String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; URL url = new URL(path + "&appid=" + appid + "&secret=" + secret); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); @@ -109,7 +122,7 @@ // System.out.println(sb.toString()); in.close(); - return sb.toString(); + return sb.toString();*/ } /** @@ -157,21 +170,26 @@ public R pageDiscuss() throws Exception { WxOfficialApi officialApi = new WxOfficialApi(); + ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue(); List<String> tokenList = new ArrayList<>(); if (!appidList.isEmpty()) { for (int i = 0; i < appidList.size(); i++) { String token = officialApi.getToken(appidList.get(i), secretList.get(i)); log.info("通过appid:" + appidList.get(i) + "获取token返回参数:" + token); - JSONObject tokenJson = JSON.parseObject(token); - if (StringUtils.isNotEmpty(tokenJson)) { - tokenList.add(tokenJson.getString("access_token")); + if (StringUtils.isNotEmpty(token)) { + tokenList.add(token); } } } - List<JSONObject> resultList = new ArrayList<>(); + Boolean newsListKey = stringRedisTemplate.hasKey(UserConstants.NEWS_LIST); + if(newsListKey != null && newsListKey){ + String json = valueOperations.get(UserConstants.NEWS_LIST); + resultList = JSON.parseArray(json,JSONObject.class); + return R.ok(resultList); + } if (!tokenList.isEmpty()) { - tokenList.forEach(token -> { + for (String token:tokenList) { try { String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token; String result = getActicle(url); @@ -185,13 +203,12 @@ String newsId = object.getString("media_id"); JSONObject contentJson = JSON.parseObject(object.getString("content")); List<JSONObject> newsItemList = - JSON.parseArray(contentJson.getString("news_item"), JSONObject.class); + JSON.parseArray(contentJson.getString("news_item"), JSONObject.class); String newsUrl = newsItemList.get(0).getString("url"); newsItemList.get(0).put("news_id", newsId); contentJson.put("news_item", newsItemList); object.put("content", contentJson); - ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue(); valueOperations.set(UserConstants.NEWS_ID + newsId, newsUrl + "", 2, TimeUnit.DAYS); } } @@ -201,7 +218,8 @@ } catch (Exception e) { log.error("通过token获取文章列表失败,错误原因:" + e.getMessage()); } - }); + valueOperations.set(UserConstants.NEWS_LIST, resultList.toString(), 12, TimeUnit.HOURS); + } } return R.ok(resultList); } -- Gitblit v1.7.1