From 08b805a0564593a85939b8dc1a9e26497967013d Mon Sep 17 00:00:00 2001
From: huanghongfa <huanghongfa123456>
Date: 星期六, 24 七月 2021 11:32:20 +0800
Subject: [PATCH] 修改bug以及测试社区动态接口

---
 springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java |   42 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 41 insertions(+), 1 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 27ffd93..77c244c 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
@@ -2,13 +2,17 @@
 
 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;
@@ -16,11 +20,15 @@
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @RestController
 @RequestMapping("/official")
 public class WxOfficialApi {
+
+    @Resource
+    private StringRedisTemplate stringRedisTemplate;
 
     //公众号appid
     private List<String> appidList = new ArrayList<String>(){{this.add("wx7c733ebbf6c55ecf");this.add("wxc94f0cddf13577d5");}};
@@ -147,7 +155,24 @@
                     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
                     String result = getActicle(url);
                     log.info("通过token获取文章列表成功,返回结果:" + result);
-                    resultList.add(JSON.parseObject(result));
+
+                    JSONObject resultJson = JSON.parseObject(result);
+                    if(resultJson != null){
+                        List<JSONObject> itemList = JSON.parseArray(resultJson.getString("item"),JSONObject.class);
+                        if(!itemList.isEmpty()){
+                            for (JSONObject object : itemList) {
+                                String mediaId = object.getString("media_id");
+                                JSONObject contentJson = JSON.parseObject(object.getString("content"));
+                                List<JSONObject> newsItemList = JSON.parseArray(contentJson.getString("news_item"),JSONObject.class);
+                                String newsUrl = newsItemList.get(0).getString(url);
+                                newsItemList.get(0).put("news_id",mediaId);
+
+                                ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
+                                valueOperations.set(UserConstants.NEWS_ID + mediaId, newsUrl + "", 2, TimeUnit.DAYS);
+                            }
+                        }
+                    }
+                    resultList.add(resultJson);
                 }catch (Exception e){
                     log.error("通过token获取文章列表失败,错误原因:" + e.getMessage());
                 }
@@ -156,4 +181,19 @@
         return R.ok(resultList);
     }
 
+    @ApiOperation(value = "获取公众号文章链接")
+    @GetMapping(value = "/get/noToken")
+    public R getDiscuss(@RequestParam("mediaId") Long mediaId){
+
+        String key = UserConstants.NEWS_ID + mediaId;
+        Boolean hasKey = stringRedisTemplate.hasKey(key);
+        if (!hasKey) {
+            return R.fail("未找到该文章的链接地址");
+        }
+
+        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
+        String url = valueOperations.get(key);
+        return R.ok(url);
+    }
+
 }

--
Gitblit v1.7.1