lidongdong
2022-11-18 97bb97c13ad63c40a8baf73b0f5e08d93c0e7559
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java
@@ -1,17 +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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.applets.config.WxMaConfiguration;
import com.panzhihua.common.api.LcApiConstants;
import me.chanjar.weixin.common.error.WxErrorException;
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;
@@ -19,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
@@ -36,9 +31,6 @@
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private WxMaConfiguration wxMaConfiguration;
    // 公众号appid
    private List<String> appidList = new ArrayList<String>() {
        {
@@ -71,8 +63,7 @@
    /**
     * 根据文章链接抓取文章内容
     *
     * @param url
     *            文章链接
     * @param url 文章链接
     * @return 文章内容
     */
    public static String getActicle(String url) {
@@ -89,24 +80,15 @@
    /**
     * 获取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
            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";
        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");
@@ -120,22 +102,23 @@
            sb.append(new String(b, 0, len));
        }
        // System.out.println(sb.toString());
        in.close();
        return sb.toString();*/
        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");
@@ -175,21 +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);
                if (StringUtils.isNotEmpty(token)) {
                    tokenList.add(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);