package ke.fang.da.weixin.wxtools.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/*
* String appId,String appSecret可配置到properties里面去,暂时直接写出来
*/
public class CustomerUtil {
public static String createGroup(String gName,String appId,String appSecret)throws Exception{
/*
* 此处json格式可以从数据库获取
*/
String gJson= "{\"group\":{\"name\":\""+gName+"\"}}";
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String action = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
http.connect();
OutputStream os= http.getOutputStream();
os.write(gJson.getBytes("UTF-8"));//传入参数
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "返回信息"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "create 失败";
}
public static String updateGroup(int id,String newName,String appId,String appSecret)throws Exception{
/*
* 此处json格式可以从数据库获取
*/
String gJson= "{\"group\":{\"id\":\""+id+",\"name\":\""+newName+"\"}}";
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String action = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
http.connect();
OutputStream os= http.getOutputStream();
os.write(gJson.getBytes("UTF-8"));//传入参数
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "返回信息"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "更新 失败";
}
/*
* 将用户移动到指定分组
*/
public static String moveCustomer(String openId,int groupId,String appId,String appSecret)throws Exception{
String gJson= "{\"openid\":\""+openId+"\",\"to_groupid\":"+groupId+"}";
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String action ="https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
http.connect();
OutputStream os= http.getOutputStream();
os.write(gJson.getBytes("UTF-8"));//传入参数
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "返回信息"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "移动 失败";
}
/*
* 查询所以分组
*/
public static String getAccessToken(String appId,String appSecret)throws Exception{
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token"+access_token;
return basicGet(url);
}
/*
* 获取用户基本信息
*
*/
public static String getCustomInf(String openId,String appId,String appSecret)throws Exception{
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
return basicGet(url);
}
/*
* 获取关注列表
*/
public static String getCustomInf(String appId,String appSecret)throws Exception{
String access_token= CommonUtil.getAccessToken(appId, appSecret);
String url ="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+access_token;
return basicGet(url);
}
public static String basicGet(String url)throws Exception{
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); //必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
http.connect();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return message;
}
}