import com.alibaba.fastjson.JSONObject;
import com.jfb.jfbmanager.wx.controller.IndexController;
import com.jfb.jfbmanager.wx.model.AccessToken;
import com.jfb.jfbmanager.wx.model.Menu;
import com.jfb.jfbmanager.wx.model.MyX509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
/**
* @description: 微信公众号工具类
* @author: hqh
* @create: 2020-06-27 10:35
**/
public class WeiXinUtil {
@Autowired
private WXPropertis wxPropertis;
private String EncodingAESKey="";
private final static Logger logger = LoggerFactory.getLogger(WeiXinUtil.class);
public static JSONObject httpRequest(String reqeustUrl,String requestMethod,String outputStr){
JSONObject jsonObject = null;
StringBuffer buffer=new StringBuffer();
try {
//创建sslcontext对象,并使用我们指定的信任的管理器初始化
TrustManager[] tm = {new MyX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL","SunJSSE");
sslContext.init(null,tm,new SecureRandom());
//从上诉sslcontext对象中得到sslsocketfactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(reqeustUrl);
HttpsURLConnection httpsURLConn = (HttpsURLConnection) url.openConnection();
httpsURLConn.setSSLSocketFactory(ssf);
httpsURLConn.setDoOutput(true);
httpsURLConn.setRequestProperty("Content-Type","application/json;charset=utf-8");
httpsURLConn.setDoOutput(true);
httpsURLConn.setUseCaches(false);
httpsURLConn.setRequestMethod(requestMethod);
if("GET".equalsIgnoreCase(requestMethod))
{
httpsURLConn.connect();
}
//当有数据需要提交时
if(null!=outputStr)
{
OutputStream outputStream = httpsURLConn.getOutputStream();
//注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
//将返回的输入流转换成字符串
InputStream inputStream = httpsURLConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str=bufferedReader.readLine())!=null)
{
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream=null;
httpsURLConn.disconnect();
logger.info(">>>>>>>>>>jsonstr:"+buffer.toString());
jsonObject = JSONObject.parseObject(buffer.toString());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* @Description:
* @Param: [appid, appsecret]
* @return: com.jfb.jfbmanager.wx.model.AccessToken
* @Author: hqh
* @Date: 2020/7/10
*/
public static AccessToken getAccessToken(String appid, String appsecret){
logger.info(">>>>>>>>>>>>>>>>appid及appsecret:"+ appid+" "+ appsecret);
AccessToken accessToken = null;
logger.info(">>>>>>>>>>>wxPropertis.getAccess_token_url():"+PropertisUtil.access_token_url);//wxPropertis.getAccess_token_url());
String requestUrl = PropertisUtil.access_token_url.replace("APPID",appid).replace("APPSECRET",appsecret);
logger.info(">>>>>>>>>>>>>>>>请求的url:"+ requestUrl);
JSONObject jsonObject = httpRequest(requestUrl,"GET",null);
if(null!=jsonObject)
{
try{
accessToken = new AccessToken();
logger.info(">>>>>>>>>>>>>>access_token:"+jsonObject.getString("access_token")+" expires_in"+jsonObject.getInteger("expires_in"));
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInteger("expires_in"));
}catch (Exception e){
accessToken = null;
}
}
return accessToken;
}
/**
* @Description:创建菜单
* @Param: [menu, accessToken]
* @return: int
* @Author: hqh
* @Date: 2020/7/10
*/
public static int createMenu(Menu menu, String accessToken){
int result = 0;
//拼装创建菜单的url
String url = PropertisUtil.menu_create_url.replace("ACCESS_TOKEN",accessToken);
logger.info(">>>>>>>>>>url:"+url);
String jsonMenu = JSONObject.toJSONString(menu);
logger.info(">>>>>>>>jsonMenu:"+jsonMenu);
JSONObject jsonObject = httpRequest(url,"POST",jsonMenu);
if(null!=jsonMenu)
{
if(0!=jsonObject.getInteger("errcode"))
{
result = jsonObject.getInteger("errcode");
//创建菜单失败
}
}
return result;
}
}