huanghongfa
2021-09-01 9fbf5da44ad44ea95daa441d3b1d9e1e0be77bf9
小程序拉取公众号文章数据缓存数据库
2个文件已修改
19 ■■■■ 已修改文件
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/constants/UserConstants.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java
@@ -9,6 +9,7 @@
import javax.annotation.Resource;
import com.panzhihua.common.api.LcApiConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
@@ -157,6 +158,7 @@
    public R pageDiscuss() throws Exception {
        WxOfficialApi officialApi = new WxOfficialApi();
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        List<String> tokenList = new ArrayList<>();
        if (!appidList.isEmpty()) {
            for (int i = 0; i < appidList.size(); i++) {
@@ -168,10 +170,15 @@
                }
            }
        }
        List<JSONObject> resultList = new ArrayList<>();
        Boolean newsListKey = stringRedisTemplate.hasKey(UserConstants.NEWS_LIST);
        if(newsListKey != null && newsListKey){
            String json = valueOperations.get(UserConstants.NEWS_LIST);
            resultList = JSON.parseArray(json,JSONObject.class);
            return R.ok(resultList);
        }
        if (!tokenList.isEmpty()) {
            tokenList.forEach(token -> {
            for (String token:tokenList) {
                try {
                    String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
                    String result = getActicle(url);
@@ -185,13 +192,12 @@
                                String newsId = object.getString("media_id");
                                JSONObject contentJson = JSON.parseObject(object.getString("content"));
                                List<JSONObject> newsItemList =
                                    JSON.parseArray(contentJson.getString("news_item"), JSONObject.class);
                                        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);
                                ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
                                valueOperations.set(UserConstants.NEWS_ID + newsId, newsUrl + "", 2, TimeUnit.DAYS);
                            }
                        }
@@ -201,7 +207,8 @@
                } catch (Exception e) {
                    log.error("通过token获取文章列表失败,错误原因:" + e.getMessage());
                }
            });
                valueOperations.set(UserConstants.NEWS_LIST, resultList.toString(), 12, TimeUnit.HOURS);
            }
        }
        return R.ok(resultList);
    }
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/constants/UserConstants.java
@@ -14,4 +14,6 @@
    public static final String LOGIN_USER_INFO = "login_user_info:";
    public static final String PHONE_PUT = "phone_put:";
    public static final String NEWS_ID = "news_media_id:";
    public static final String NEWS_LIST = "news_list:";
}