| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | /** |
| | | * 根据文章链接抓取文章内容 |
| | | * |
| | | * @param url |
| | | * 文章链接 |
| | | * @param url 文章链接 |
| | | * @return 文章内容 |
| | | */ |
| | | public static String getActicle(String url) { |
| | |
| | | /** |
| | | * 获取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); |
| | |
| | | /** |
| | | * 通过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"); |
| | |
| | | } |
| | | 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); |