From 177249c76aeea0b4bf8d8816d4994e3b445b45ce Mon Sep 17 00:00:00 2001
From: huanghongfa <huanghongfa123456>
Date: 星期四, 02 九月 2021 10:39:34 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/zzj' into zzj

---
 springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java |  222 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 122 insertions(+), 100 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 7d79ab8..226de90 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,26 +1,30 @@
 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 org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.data.redis.core.ValueOperations;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+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 org.springframework.data.redis.core.StringRedisTemplate;
-import org.springframework.data.redis.core.ValueOperations;
-import org.springframework.web.bind.annotation.*;
-
-import javax.annotation.Resource;
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.ProtocolException;
-import java.net.URL;
-import java.nio.charset.Charset;
-import java.util.*;
-import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @RestController
@@ -30,75 +34,20 @@
     @Resource
     private StringRedisTemplate stringRedisTemplate;
 
-    //公众号appid
-    private List<String> appidList = new ArrayList<String>(){{this.add("wx7c733ebbf6c55ecf");this.add("wxc94f0cddf13577d5");}};
-    //公众号secret
-    private List<String> secretList = new ArrayList<String>(){{this.add("500290552cbfdd1c1c18131c5807b6ae");this.add("3418127405845701497a09f65678953f");}};
-
-    /**
-     * 获取token
-     * @param appid     公众号appid
-     * @param secret    公众号secret
-     * @return  token
-     */
-    private String getToken(String appid,String secret) throws MalformedURLException, IOException, ProtocolException {
-        // 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();
-        connection.setRequestMethod("GET");
-        connection.connect();
-
-        InputStream in = connection.getInputStream();
-        byte[] b = new byte[100];
-        int len = -1;
-        StringBuffer sb = new StringBuffer();
-        while((len = in.read(b)) != -1) {
-            sb.append(new String(b,0,len));
+    // 公众号appid
+    private List<String> appidList = new ArrayList<String>() {
+        {
+            this.add("wx7c733ebbf6c55ecf");
+            this.add("wxc94f0cddf13577d5");
         }
-
-//        System.out.println(sb.toString());
-        in.close();
-        return sb.toString();
-    }
-
-    /**
-     * 通过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();
-        connection.setRequestMethod("POST");
-        connection.setDoOutput(true);
-        connection.setRequestProperty("content-type", "application/json;charset=utf-8");
-        connection.connect();
-        // post发送的参数
-        Map<String, Object> map = new HashMap<>();
-        map.put("type", "news"); // news表示图文类型的素材,具体看API文档
-        map.put("offset", 0);
-        map.put("count", 5);
-        // 将map转换成json字符串
-        String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson
-
-        OutputStream out = connection.getOutputStream();
-        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
-        bw.write(paramBody); // 向流中写入参数字符串
-        bw.flush();
-
-        InputStream in = connection.getInputStream();
-        byte[] b = new byte[100];
-        int len = -1;
-        StringBuffer sb = new StringBuffer();
-        while((len = in.read(b)) != -1) {
-            sb.append(new String(b,0,len,"UTF-8"));
+    };
+    // 公众号secret
+    private List<String> secretList = new ArrayList<String>() {
+        {
+            this.add("500290552cbfdd1c1c18131c5807b6ae");
+            this.add("3418127405845701497a09f65678953f");
         }
-
-        in.close();
-        return sb.toString();
-    }
+    };
 
     /**
      * 微信公众号请求头设置
@@ -117,7 +66,8 @@
     /**
      * 根据文章链接抓取文章内容
      *
-     * @param url 文章链接
+     * @param url
+     *            文章链接
      * @return 文章内容
      */
     public static String getActicle(String url) {
@@ -128,55 +78,127 @@
         map.put("count", 5);
         // 将map转换成json字符串
         String paramBody = JSON.toJSONString(map);
-        return  HttpClientUtil.get(url, getWxHeaderMap(),paramBody);
+        return HttpClientUtil.get(url, getWxHeaderMap(), paramBody);
+    }
+
+    /**
+     * 获取token
+     *
+     * @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();
+        connection.setRequestMethod("GET");
+        connection.connect();
+
+        InputStream in = connection.getInputStream();
+        byte[] b = new byte[100];
+        int len = -1;
+        StringBuffer sb = new StringBuffer();
+        while ((len = in.read(b)) != -1) {
+            sb.append(new String(b, 0, len));
+        }
+
+        // System.out.println(sb.toString());
+        in.close();
+        return sb.toString();
+    }
+
+    /**
+     * 通过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();
+        connection.setRequestMethod("POST");
+        connection.setDoOutput(true);
+        connection.setRequestProperty("content-type", "application/json;charset=utf-8");
+        connection.connect();
+        // post发送的参数
+        Map<String, Object> map = new HashMap<>();
+        map.put("type", "news"); // news表示图文类型的素材,具体看API文档
+        map.put("offset", 0);
+        map.put("count", 5);
+        // 将map转换成json字符串
+        String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson
+
+        OutputStream out = connection.getOutputStream();
+        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
+        bw.write(paramBody); // 向流中写入参数字符串
+        bw.flush();
+
+        InputStream in = connection.getInputStream();
+        byte[] b = new byte[100];
+        int len = -1;
+        StringBuffer sb = new StringBuffer();
+        while ((len = in.read(b)) != -1) {
+            sb.append(new String(b, 0, len, StandardCharsets.UTF_8));
+        }
+
+        in.close();
+        return sb.toString();
     }
 
     @ApiOperation(value = "拉取公众号列表")
-    @GetMapping(value = "/list/noToken",produces="application/json;charset=utf-8")
-    public R pageDiscuss() throws Exception{
+    @GetMapping(value = "/list/noToken", produces = "application/json;charset=utf-8")
+    public R pageDiscuss() throws Exception {
         WxOfficialApi officialApi = new WxOfficialApi();
 
         List<String> tokenList = new ArrayList<>();
-        if(!appidList.isEmpty()){
+        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);
+                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)){
+                if (StringUtils.isNotEmpty(tokenJson)) {
                     tokenList.add(tokenJson.getString("access_token"));
                 }
             }
         }
 
         List<JSONObject> resultList = new ArrayList<>();
-        if(!tokenList.isEmpty()){
+        if (!tokenList.isEmpty()) {
             tokenList.forEach(token -> {
                 try {
                     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
                     String result = getActicle(url);
-//                    log.info("通过token获取文章列表成功,返回结果:" + result);
+                    // log.info("通过token获取文章列表成功,返回结果:" + result);
 
                     JSONObject resultJson = JSON.parseObject(result);
-                    if(resultJson != null){
-                        List<JSONObject> itemList = JSON.parseArray(resultJson.getString("item"),JSONObject.class);
-                        if(!itemList.isEmpty()){
+                    if (resultJson != null) {
+                        List<JSONObject> itemList = JSON.parseArray(resultJson.getString("item"), JSONObject.class);
+                        if (!itemList.isEmpty()) {
                             for (JSONObject object : itemList) {
                                 String newsId = object.getString("media_id");
                                 JSONObject contentJson = JSON.parseObject(object.getString("content"));
-                                List<JSONObject> newsItemList = JSON.parseArray(contentJson.getString("news_item"),JSONObject.class);
+                                List<JSONObject> newsItemList =
+                                    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);
+                                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);
                             }
                         }
-                        resultJson.put("item",itemList);
+                        resultJson.put("item", itemList);
                     }
                     resultList.add(resultJson);
-                }catch (Exception e){
+                } catch (Exception e) {
                     log.error("通过token获取文章列表失败,错误原因:" + e.getMessage());
                 }
             });
@@ -186,7 +208,7 @@
 
     @ApiOperation(value = "获取公众号文章链接")
     @GetMapping(value = "/get/noToken")
-    public R getDiscuss(@RequestParam("mediaId") String mediaId){
+    public R getDiscuss(@RequestParam("mediaId") String mediaId) {
 
         String key = UserConstants.NEWS_ID + mediaId;
         Boolean hasKey = stringRedisTemplate.hasKey(key);

--
Gitblit v1.7.1