huanghongfa
2021-07-23 df1de6a8ca9bf6af14d858fb51d077c00dd2dd48
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java
@@ -3,10 +3,10 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;
import java.io.*;
@@ -14,10 +14,8 @@
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.nio.charset.Charset;
import java.util.*;
@Slf4j
@RestController
@@ -73,12 +71,12 @@
        Map<String, Object> map = new HashMap<>();
        map.put("type", "news"); // news表示图文类型的素材,具体看API文档
        map.put("offset", 0);
        map.put("count", 10);
        map.put("count", 5);
        // 将map转换成json字符串
        String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson
        OutputStream out = connection.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
        bw.write(paramBody); // 向流中写入参数字符串
        bw.flush();
@@ -94,9 +92,42 @@
        return sb.toString();
    }
    /**
     * 微信公众号请求头设置
     */
    public static Map<String, String> getWxHeaderMap() {
        Map<String, String> map = new HashMap<>(new LinkedHashMap());
        map.put("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
        map.put("Accept-Encoding", "gzip, deflate");
        map.put("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3");
        map.put("Host", "mp.weixin.qq.com");
        map.put("If-Modified-Since", "Sat, 04 Jan 2020 12:23:43 GMT");
        map.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
        return map;
    }
    /**
     * 根据文章链接抓取文章内容
     *
     * @param url 文章链接
     * @return 文章内容
     */
    public static R getActicle(String url) {
        // 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);
        // 请求与响应
        String resp = HttpClientUtil.get(url, getWxHeaderMap(),paramBody);
        log.info(resp);
        return R.ok(resp);
    }
    @ApiOperation(value = "拉取公众号列表")
    @GetMapping("/list/noToken")
    @GetMapping(value = "/list/noToken",produces="application/json;charset=utf-8")
    public R pageDiscuss() throws Exception{
        WxOfficialApi officialApi = new WxOfficialApi();
@@ -116,14 +147,18 @@
        if(!tokenList.isEmpty()){
            tokenList.forEach(token -> {
                try {
                    String result = officialApi.getContentList(token);
                    String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
                    R result = getActicle(url);
//                  String result = officialApi.getContentList(token);
                    log.info("通过token获取文章列表成功,返回结果:" + result);
                    resultList.add(JSON.parseObject(result));
//                    resultList.add(JSON.parseObject(result));
                }catch (Exception e){
                    log.error("通过token获取文章列表失败,错误原因:" + e.getMessage());
                }
            });
        }
        return R.ok(resultList);
    }