.EntityUtils;
import org.jeecg.modules.video.utitls.Constants;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @description:
* @author: Mr.Fang
* @create: 2023-04-03 17:06
**/
public class HttpUtils {
/**
* description: 获取token,返回结果为 JSON 自行转 map
* create by: Mr.Fang
*
* @return: java.lang.String
* @date: 2023/4/3 17:46
*/
public String getToken() throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
Map<String, Object> params = new HashMap<>();
params.put("appid", Constants.APPLET_APP_ID);
params.put("secret", Constants.APPLET_SECRET);
params.put("grant_type", "client_credential");
String url = handleParams("https://api.weixin.qq.com/cgi-bin/token", params);
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity(); // 响应结果
return EntityUtils.toString(entity, CharSetType.UTF8.getType());
}
/**
* description: 获取 open id,返回结果为 JSON 自行转 map
* create by: Mr.Fang
*
* @param: [code]
* @return: java.lang.String
* @date: 2023/4/3 17:46
*/
public String getOpenId(String code) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
Map<String, Object> params = new HashMap<>();
params.put("appid", Constants.APPLET_APP_ID);
params.put("secret", Constants.APPLET_SECRET);
params.put("js_code", code);
params.put("grant_type", "authorization_code");
String url = handleParams("https://api.weixin.qq.com/sns/jscode2session", params);
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity(); // 响应结果
return EntityUtils.toString(entity, CharSetType.UTF8.getType());
}
/**
* description: 消息推送 返回结果为 JSON 自行转 map;token 调用 getToken获取
* create by: Mr.Fang
*
* @param: [token]
* @return: java.lang.String
* @date: 2023/4/3 17:46
*/
public String pushMsg(String token) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
Map<String, Object> params = new HashMap<>();
// 处理微信推送数据结构
JSONObject mapData = new JSONObject();
Map<String, Object> map1 = new HashMap<>();
map1.put("value", "任务名称");
mapData.put("thing2", map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("value", "2023-04-03 12:00:00");
mapData.put("time3", map2);
Map<String, Object> map3 = new HashMap<>();
map3.put("value", "描述信息");
mapData.put("thing4", map3);
Map<String, Object> map4 = new HashMap<>();
map4.put("value", "备注系信息");
mapData.put("thing10", map4);
Map<String, Object> map5 = new HashMap<>();
map5.put("value", "抖音");
mapData.put("thing11", map5);
params.put("templa |