|
@@ -16,6 +16,7 @@ import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
@@ -37,6 +38,13 @@ import java.util.*;
|
|
@Component
|
|
@Component
|
|
public class WxOpUtils {
|
|
public class WxOpUtils {
|
|
|
|
|
|
|
|
+ private static RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ public void setRestTemplate(RestTemplate restTemplate){
|
|
|
|
+ WxOpUtils.restTemplate = restTemplate;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static final String SESSION_KEY = "session_key";
|
|
public static final String SESSION_KEY = "session_key";
|
|
public static final String WX_OPEN_ID_KEY = "openid";
|
|
public static final String WX_OPEN_ID_KEY = "openid";
|
|
public static final String WX_ACCESS_TOKEN = "WX_ACCESS_TOKEN_";
|
|
public static final String WX_ACCESS_TOKEN = "WX_ACCESS_TOKEN_";
|
|
@@ -52,9 +60,6 @@ public class WxOpUtils {
|
|
private static final String UP_LOAD_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/upload";
|
|
private static final String UP_LOAD_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/upload";
|
|
private static final String GET_TEMP_DATA_URL = "https://api.weixin.qq.com/cgi-bin/media/get";
|
|
private static final String GET_TEMP_DATA_URL = "https://api.weixin.qq.com/cgi-bin/media/get";
|
|
|
|
|
|
-
|
|
|
|
- private static RestTemplate restTemplate = new RestTemplate();
|
|
|
|
-
|
|
|
|
private static String agencyWxAppId;
|
|
private static String agencyWxAppId;
|
|
|
|
|
|
private static String agencyWxAppSecret;
|
|
private static String agencyWxAppSecret;
|
|
@@ -82,10 +87,10 @@ public class WxOpUtils {
|
|
param.put("grant_type", "client_credential");
|
|
param.put("grant_type", "client_credential");
|
|
param.put("appid", appId);
|
|
param.put("appid", appId);
|
|
param.put("secret", secret);
|
|
param.put("secret", secret);
|
|
- String vxResult = HttpClientUtil.doGet(SUBSCRIBE_ACCESS_TOKEN_URL, param);
|
|
|
|
- log.info(vxResult);
|
|
|
|
|
|
+ ResponseEntity<String> vxResult = restTemplate.getForEntity(SUBSCRIBE_ACCESS_TOKEN_URL,String.class, param);
|
|
|
|
+ log.info(vxResult.getBody());
|
|
|
|
|
|
- AccessTokenModel accessTokenModel = JsonUtils.jsonToPojo(vxResult, AccessTokenModel.class);
|
|
|
|
|
|
+ AccessTokenModel accessTokenModel = JsonUtils.jsonToPojo(vxResult.getBody(), AccessTokenModel.class);
|
|
//采用默认的缓存时长一个半小时
|
|
//采用默认的缓存时长一个半小时
|
|
LettuceRedisClientUtils.setStrKeyValue(redisKey , accessTokenModel.getAccess_token() , 5400);
|
|
LettuceRedisClientUtils.setStrKeyValue(redisKey , accessTokenModel.getAccess_token() , 5400);
|
|
return accessTokenModel.getAccess_token();
|
|
return accessTokenModel.getAccess_token();
|
|
@@ -131,9 +136,9 @@ public class WxOpUtils {
|
|
String accessToken = getAccessToken(appId, appSecret);
|
|
String accessToken = getAccessToken(appId, appSecret);
|
|
String url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
|
|
String url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
|
|
String json = JsonUtils.objectToJson(wxMssVO);
|
|
String json = JsonUtils.objectToJson(wxMssVO);
|
|
- String vxResult = HttpClientUtil.doPostJson(url, json);
|
|
|
|
|
|
+ ResponseEntity<String> vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
|
|
//把信息封装为json
|
|
//把信息封装为json
|
|
- JSONObject res = JSONObject.parseObject(vxResult);
|
|
|
|
|
|
+ JSONObject res = JSONObject.parseObject(vxResult.getBody());
|
|
//把信息封装到map
|
|
//把信息封装到map
|
|
Map<String, Object> map = parseJSON2Map(res);
|
|
Map<String, Object> map = parseJSON2Map(res);
|
|
if (null != map && map.containsKey("errcode")) {
|
|
if (null != map && map.containsKey("errcode")) {
|
|
@@ -145,10 +150,9 @@ public class WxOpUtils {
|
|
log.info("token过期了");
|
|
log.info("token过期了");
|
|
accessToken = updateAccessToken(appId, appSecret);
|
|
accessToken = updateAccessToken(appId, appSecret);
|
|
url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
|
|
url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
|
|
- json = JsonUtils.objectToJson(wxMssVO);
|
|
|
|
- vxResult = HttpClientUtil.doPostJson(url, json);
|
|
|
|
|
|
+ vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
|
|
//把信息封装为json
|
|
//把信息封装为json
|
|
- res = JSONObject.parseObject(vxResult);
|
|
|
|
|
|
+ res = JSONObject.parseObject(vxResult.getBody());
|
|
//把信息封装到map
|
|
//把信息封装到map
|
|
map = parseJSON2Map(res);
|
|
map = parseJSON2Map(res);
|
|
}
|
|
}
|
|
@@ -179,10 +183,9 @@ public class WxOpUtils {
|
|
wxMssVO.setLang("zh_CN");
|
|
wxMssVO.setLang("zh_CN");
|
|
wxMssVO.setPage(page);
|
|
wxMssVO.setPage(page);
|
|
String url = SUBSCRIBE_REQUEST_URL + accessToken;
|
|
String url = SUBSCRIBE_REQUEST_URL + accessToken;
|
|
- String json = JsonUtils.objectToJson(wxMssVO);
|
|
|
|
- String vxResult = HttpClientUtil.doPostJson(url, json);
|
|
|
|
|
|
+ ResponseEntity<String> vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
|
|
//把信息封装为json
|
|
//把信息封装为json
|
|
- JSONObject res = JSONObject.parseObject(vxResult);
|
|
|
|
|
|
+ JSONObject res = JSONObject.parseObject(vxResult.getBody());
|
|
//把信息封装到map
|
|
//把信息封装到map
|
|
Map<String, Object> map = parseJSON2Map(res);
|
|
Map<String, Object> map = parseJSON2Map(res);
|
|
if (null != map && map.containsKey("errcode")) {
|
|
if (null != map && map.containsKey("errcode")) {
|
|
@@ -195,10 +198,9 @@ public class WxOpUtils {
|
|
accessToken = updateAccessToken(appId, appSecret);
|
|
accessToken = updateAccessToken(appId, appSecret);
|
|
wxMssVO.setAccess_token(accessToken);
|
|
wxMssVO.setAccess_token(accessToken);
|
|
url = SUBSCRIBE_REQUEST_URL + accessToken;
|
|
url = SUBSCRIBE_REQUEST_URL + accessToken;
|
|
- json = JsonUtils.objectToJson(wxMssVO);
|
|
|
|
- vxResult = HttpClientUtil.doPostJson(url, json);
|
|
|
|
|
|
+ vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
|
|
//把信息封装为json
|
|
//把信息封装为json
|
|
- res = JSONObject.parseObject(vxResult);
|
|
|
|
|
|
+ res = JSONObject.parseObject(vxResult.getBody());
|
|
//把信息封装到map
|
|
//把信息封装到map
|
|
map = parseJSON2Map(res);
|
|
map = parseJSON2Map(res);
|
|
}
|
|
}
|
|
@@ -215,10 +217,10 @@ public class WxOpUtils {
|
|
param.put("appid", appId);
|
|
param.put("appid", appId);
|
|
param.put("secret", secret);
|
|
param.put("secret", secret);
|
|
|
|
|
|
- String vxResult = HttpClientUtil.doGet(SUBSCRIBE_ACCESS_TOKEN_URL, param);
|
|
|
|
- log.info(vxResult);
|
|
|
|
|
|
+ ResponseEntity<String> vxResult = restTemplate.getForEntity(SUBSCRIBE_ACCESS_TOKEN_URL,String.class, param);
|
|
|
|
+ log.info(vxResult.getBody());
|
|
|
|
|
|
- AccessTokenModel accessTokenModel = JsonUtils.jsonToPojo(vxResult, AccessTokenModel.class);
|
|
|
|
|
|
+ AccessTokenModel accessTokenModel = JsonUtils.jsonToPojo(vxResult.getBody(), AccessTokenModel.class);
|
|
//TODO:这里需要改成缓存到redis上去
|
|
//TODO:这里需要改成缓存到redis上去
|
|
//采用默认的缓存时长一个半小时
|
|
//采用默认的缓存时长一个半小时
|
|
LettuceRedisClientUtils.setAndExpStrKeyValue(redisKey , accessTokenModel.getAccess_token() , 5400);
|
|
LettuceRedisClientUtils.setAndExpStrKeyValue(redisKey , accessTokenModel.getAccess_token() , 5400);
|
|
@@ -318,9 +320,9 @@ public class WxOpUtils {
|
|
throw new CommonBaseException(ResultCodeEnum.D3014);
|
|
throw new CommonBaseException(ResultCodeEnum.D3014);
|
|
}
|
|
}
|
|
String url = CREATE_LIVE_ROOM_URL + "?access_token=" + token;
|
|
String url = CREATE_LIVE_ROOM_URL + "?access_token=" + token;
|
|
- String wxRspStr = HttpClientUtil.doPostJson(url , JSON.toJSONString(wxLiveRoomRequestVo));
|
|
|
|
- log.info("创建直播间,微信返回内容为:{}" , wxRspStr);
|
|
|
|
- JSONObject wxLiveRoomRspVo = JsonUtils.jsonToPojo(wxRspStr , JSONObject.class);
|
|
|
|
|
|
+ ResponseEntity<String> wxRspStr = restTemplate.postForEntity(url , JSON.toJSONString(wxLiveRoomRequestVo),String.class);
|
|
|
|
+ log.info("创建直播间,微信返回内容为:{}" , wxRspStr.getBody());
|
|
|
|
+ JSONObject wxLiveRoomRspVo = JsonUtils.jsonToPojo(wxRspStr.getBody() , JSONObject.class);
|
|
return wxLiveRoomRspVo;
|
|
return wxLiveRoomRspVo;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -336,9 +338,9 @@ public class WxOpUtils {
|
|
param.put("start" , start);
|
|
param.put("start" , start);
|
|
param.put("limit" , limit);
|
|
param.put("limit" , limit);
|
|
String url = GET_LIVE_ROOM_LIST_URL + "?access_token=" + token;
|
|
String url = GET_LIVE_ROOM_LIST_URL + "?access_token=" + token;
|
|
- String wxRspStr = HttpClientUtil.doPostJson(url , JSON.toJSONString(param));
|
|
|
|
- log.info("创建直播间,微信返回内容为:{}" , wxRspStr);
|
|
|
|
- JSONObject wxLiveRoomRspVo = JsonUtils.jsonToPojo(wxRspStr , JSONObject.class);
|
|
|
|
|
|
+ ResponseEntity<String> wxRspStr = restTemplate.postForEntity(url , JSON.toJSONString(param),String.class);
|
|
|
|
+ log.info("创建直播间,微信返回内容为:{}" , wxRspStr.getBody());
|
|
|
|
+ JSONObject wxLiveRoomRspVo = JsonUtils.jsonToPojo(wxRspStr.getBody() , JSONObject.class);
|
|
return wxLiveRoomRspVo;
|
|
return wxLiveRoomRspVo;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -356,9 +358,9 @@ public class WxOpUtils {
|
|
}
|
|
}
|
|
|
|
|
|
String url = GET_TEMP_DATA_URL + "?access_token=" + token + "&media_id=" + mediaId;
|
|
String url = GET_TEMP_DATA_URL + "?access_token=" + token + "&media_id=" + mediaId;
|
|
- String wxRsp = HttpClientUtil.doGet(url);
|
|
|
|
- log.info("微信返回的照片链接为:{}" , wxRsp);
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(wxRsp);
|
|
|
|
|
|
+ ResponseEntity<String> wxRsp = restTemplate.getForEntity(url,String.class);
|
|
|
|
+ log.info("微信返回的照片链接为:{}" , wxRsp.getBody());
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(wxRsp.getBody());
|
|
log.info("微信小程序返回数据为:{}" , jsonObject.toJSONString());
|
|
log.info("微信小程序返回数据为:{}" , jsonObject.toJSONString());
|
|
if(null != jsonObject){
|
|
if(null != jsonObject){
|
|
if(jsonObject.containsKey("video_url")){
|
|
if(jsonObject.containsKey("video_url")){
|