package com.util;
import javax.net.ssl.*;
import com.Common.AccessTokenInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.entry.AccessToken;
import com.entry.WeixinUserInfo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 用户管理用到的工具类
* By CLiang
* https://blog.csdn.net/u011752195/article/details/81782526
*/
public class GroupUtil {
/**
* 创建群组
* @param appId
* @param appSecret
* @param groupName 群组名称
* @return 如{"group": { "id": 107, "name": "test" } }
*/
public static JSONObject createGroup(String appId, String appSecret, String groupName){
String url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
JSONObject group = new JSONObject();
try {
j.put("name",groupName);
group.put("group",j);
} catch (JSONException e) {
e.printStackTrace();
}
String rtn = weixinRequest(url, group.toString(), "POST");
System.out.println("创建群组:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 删除群组
* @param appId
* @param appSecret
* @param groupName 群组名称
* @return 如{"group": { "id": 107, "name": "test" } }
*/
public static JSONObject deleteGroup(String appId, String appSecret, String groupName){
String url = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
JSONObject group = new JSONObject();
try {
j.put("id",groupName);
group.put("group",j);
} catch (JSONException e) {
e.printStackTrace();
}
String rtn = weixinRequest(url, group.toString(), "POST");
System.out.println("deleteGroup()"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 查询所有分组
* @param appId
* @param appSecret
* @return
*/
public static JSONObject getAllGroups(String appId, String appSecret){
String url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
//https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN
String rtn = weixinRequest(url, null, "GET");
System.out.println("分组信息:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 查询所有用户
* @param appId
* @param appSecret
* @return
*/
public static JSONObject getAllUsers(String appId, String appSecret, String next){
//String url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+AccessTokenInfo.accessToken.getAccessToken()+"&next_openid="+next;
//https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN
String rtn = weixinRequest(url, null, "GET");
System.out.println("用户列表:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 通过用户的OpenID查询其所在的GroupID
* @param appId
* @param appSecret
* @param openId 用户的OpenID
* @return 如:{ "groupid": 102 }
*/
public static JSONObject getUserGroup(String appId, String appSecret, String openId){
String url = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
try {
j.put("openid", openId);
} catch (JSONException e1) {
e1.printStackTrace();
}
String rtn = weixinRequest(url, j.toString(), "POST");
System.out.println("用户分组:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 修改分组名
* @param appId
* @param appSecret
* @param groupId
* @param newGroupName
* @return 如 {"errcode": 0, "errmsg": "ok"}
*/
public static JSONObject updateGroup(String appId, String appSecret, String groupId, String newGroupName){
String url = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
JSONObject group = new JSONObject();
try {
j.put("id", groupId);
j.put("name",newGroupName);
group.put("group",j);
} catch (JSONException e) {
e.printStackTrace();
}
String rtn = weixinRequest(url, group.toString(), "POST");
System.out.println("修改分组名:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 移动用户分组
* @param appId
* @param appSecret
* @param toGroupId 新分组的id
* @param openId 用户id
* @return 如 {"errcode": 0, "errmsg": "ok"}
*/
public static JSONObject updateUserGroup(String appId, String appSecret, String toGroupId, String openId){
String url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
try {
j.put("openid", openId);
j.put("to_groupid", toGroupId);
} catch (JSONException e) {
e.printStackTrace();
}
String rtn = weixinRequest(url, j.toString(), "POST");
System.out.println("更新用户组:"+rtn);
JSONObject json;
try {
json = new JSONObject(JSON.parseObject(rtn));
} catch (JSONException e) {
throw new RuntimeException(e.getMessage(),e);
}
return json;
}
/**
* 获取分组下粉丝列表
* @param appId
* @param appSecret
* @param id 分组id
* @return 如 {"errcode": 0, "errmsg": "ok"}
*/
public static JSONObject ListGroup(String appId, String appSecret, String id){
String url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" + AccessTokenInfo.accessToken.getAccessToken();
JSONObject j = new JSONObject();
try {
j.put("tagid",id);
} catch (JSONException e) {
e.printStackTrace();
}
String rtn = weixinRequest(url, j.toString(), "GET");
System.out.println("分组粉丝列表:"+rtn);
JSONObject json;
try {
json
V亮亮
- 粉丝: 43
- 资源: 13
最新资源
- (源码)基于C语言的系统服务框架.zip
- (源码)基于Spring MVC和MyBatis的选课管理系统.zip
- (源码)基于ArcEngine的GIS数据处理系统.zip
- (源码)基于JavaFX和MySQL的医院挂号管理系统.zip
- (源码)基于IdentityServer4和Finbuckle.MultiTenant的多租户身份认证系统.zip
- (源码)基于Spring Boot和Vue3+ElementPlus的后台管理系统.zip
- (源码)基于C++和Qt框架的dearoot配置管理系统.zip
- (源码)基于 .NET 和 EasyHook 的虚拟文件系统.zip
- (源码)基于Python的金融文档智能分析系统.zip
- (源码)基于Java的医药管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈