From cbf825f4e63e77241a11ec90841637991e43940f Mon Sep 17 00:00:00 2001 From: 罗元桥 <2376770955@qq.com> Date: 星期六, 24 七月 2021 18:11:28 +0800 Subject: [PATCH] Merge branch 'test' into 'master' --- springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxOfficialApi.java | 90 ++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 81 insertions(+), 9 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 1130d79..3ee65a7 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,27 +2,33 @@ 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.checkerframework.checker.units.qual.A; +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; 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.*; +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");}}; @@ -78,7 +84,7 @@ 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 +100,39 @@ 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 String 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); + return HttpClientUtil.get(url, getWxHeaderMap(),paramBody); + } @ApiOperation(value = "拉取公众号列表") - @GetMapping(value = "/list/noToken",produces="application/json;charset=UTF-8") + @GetMapping(value = "/list/noToken",produces="application/json;charset=utf-8") public R pageDiscuss() throws Exception{ WxOfficialApi officialApi = new WxOfficialApi(); @@ -116,9 +152,30 @@ 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; + 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 newsId = 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",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); + } + } + resultJson.put("item",itemList); + } + resultList.add(resultJson); }catch (Exception e){ log.error("通过token获取文章列表失败,错误原因:" + e.getMessage()); } @@ -127,4 +184,19 @@ return R.ok(resultList); } + @ApiOperation(value = "获取公众号文章链接") + @GetMapping(value = "/get/noToken") + public R getDiscuss(@RequestParam("mediaId") String 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