From bdbe40f077aa9ca2ec184707d3eab41af13def03 Mon Sep 17 00:00:00 2001 From: lidongdong <1459917685@qq.com> Date: 星期四, 10 十一月 2022 18:12:54 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/zigonggao_dev' into zigonggao_dev --- springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java | 65 +++++++++++++++----------------- 1 files changed, 31 insertions(+), 34 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 6cdbafc..32c450d 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 @@ -1,15 +1,14 @@ package com.panzhihua.applets.api; -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Resource; - -import com.panzhihua.common.api.LcApiConstants; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.panzhihua.applets.config.WxMaConfiguration; +import com.panzhihua.common.constants.UserConstants; +import com.panzhihua.common.model.vos.R; +import com.panzhihua.common.utlis.HttpClientUtil; +import com.panzhihua.common.utlis.StringUtils; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.web.bind.annotation.GetMapping; @@ -17,15 +16,13 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.panzhihua.common.constants.UserConstants; -import com.panzhihua.common.model.vos.R; -import com.panzhihua.common.utlis.HttpClientUtil; -import com.panzhihua.common.utlis.StringUtils; - -import io.swagger.annotations.ApiOperation; -import lombok.extern.slf4j.Slf4j; +import javax.annotation.Resource; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.concurrent.TimeUnit; @Slf4j @RestController @@ -34,7 +31,6 @@ @Resource private StringRedisTemplate stringRedisTemplate; - // 公众号appid private List<String> appidList = new ArrayList<String>() { { @@ -67,8 +63,7 @@ /** * 根据文章链接抓取文章内容 * - * @param url - * 文章链接 + * @param url 文章链接 * @return 文章内容 */ public static String getActicle(String url) { @@ -85,15 +80,14 @@ /** * 获取token * - * @param appid - * 公众号appid - * @param secret - * 公众号secret + * @param appid 公众号appid + * @param secret 公众号secret * @return token */ 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"; URL url = new URL(path + "&appid=" + appid + "&secret=" + secret); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); @@ -108,22 +102,23 @@ sb.append(new String(b, 0, len)); } - // System.out.println(sb.toString()); in.close(); + + return sb.toString(); + } /** * 通过token获取公众号文章 * - * @param token - * token + * @param token token * @return 获取的文章列表结果 */ private String getContentList(String token) throws IOException { String path = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token; URL url = new URL(path); - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("content-type", "application/json;charset=utf-8"); @@ -163,22 +158,24 @@ 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){ + if (newsListKey != null && newsListKey) { String json = valueOperations.get(UserConstants.NEWS_LIST); - resultList = JSON.parseArray(json,JSONObject.class); + resultList = JSON.parseArray(json, JSONObject.class); return R.ok(resultList); } if (!tokenList.isEmpty()) { - for (String token:tokenList) { + for (String token : tokenList) { try { String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token; String result = getActicle(url); -- Gitblit v1.7.1