yanghui
2022-11-01 f04c6bf8e117789ec0e1cc069cd5786c1bee4746
#feat 获取access_token从redis获取
1个文件已修改
66 ■■■■■ 已修改文件
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
@@ -71,8 +66,7 @@
    /**
     * 根据文章链接抓取文章内容
     *
     * @param url
     *            文章链接
     * @param url 文章链接
     * @return 文章内容
     */
    public static String getActicle(String url) {
@@ -89,22 +83,17 @@
    /**
     * 获取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;
        //改从redis中获取
        return stringRedisTemplate.boundValueOps("access_token:access_token:" + appid).get();
/*        String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        URL url = new URL(path + "&appid=" + appid + "&secret=" + secret);
@@ -128,14 +117,13 @@
    /**
     * 通过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");
@@ -183,13 +171,13 @@
        }
        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);