GroupsApi.java
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.jfinal.weixin.sdk.api;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
/**
* 分组Api
* @author L.cn
* 文档地址:http://mp.weixin.qq.com/wiki/5/0d8acdd6d4433c877fbea938a2f133cd.html
*/
public class GroupsApi {
private static String createUrl = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=";
/**
* 创建分组,一个公众账号,最多支持创建100个分组。
* @param name 分组名
* @return ApiResult
*/
public static ApiResult create(String name) {
String url = createUrl + AccessTokenApi.getAccessTokenStr();
Map<String, Map<String, String>> groupData = new HashMap<String, Map<String, String>>();
Map<String, String> mapData = new HashMap<String, String>();
mapData.put("name", name);
groupData.put("group", mapData);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(groupData));
return new ApiResult(jsonResult);
}
private static String getUrl = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=";
/**
* 查询所有分组
* @return ApiResult
*/
public static ApiResult get() {
String url = getUrl + AccessTokenApi.getAccessTokenStr();
String jsonResult = HttpUtils.get(url);
return new ApiResult(jsonResult);
}
private static String getIdUrl = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=";
/**
* 通过用户的OpenID查询其所在的GroupID
* @param openid 普通用户的标识,对当前开发者帐号唯一
* @return ApiResult
*/
public static ApiResult getId(String openid) {
String url = getIdUrl + AccessTokenApi.getAccessTokenStr();
Map<String, String> mapData = new HashMap<String, String>();
mapData.put("openid", openid);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(mapData));
return new ApiResult(jsonResult);
}
private static String updateUrl = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=";
/**
* 修改分组名
* @param id 分组id,由微信分配
* @param name 分组名字(30个字符以内)
* @return ApiResult
*/
public static ApiResult update(int id, String name) {
String url = updateUrl + AccessTokenApi.getAccessTokenStr();
Map<String, Map<String, Object>> groupData = new HashMap<String, Map<String, Object>>();
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put("id", id);
mapData.put("name", name);
groupData.put("group", mapData);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(groupData));
return new ApiResult(jsonResult);
}
private static String membersUpdateUrl = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=";
/**
* 移动用户分组
* @param openid 用户唯一标识符
* @param to_groupid 分组id
* @return ApiResult
*/
public static ApiResult membersUpdate(String openid, int to_groupid) {
String url = membersUpdateUrl + AccessTokenApi.getAccessTokenStr();
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put("openid", openid);
mapData.put("to_groupid", to_groupid);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(mapData));
return new ApiResult(jsonResult);
}
private static String membersBatchUpdateUrl = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=";
/**
* 批量移动用户分组
* @param openidList 用户唯一标识符openid的列表(size不能超过50)
* @param to_groupid 分组id
* @return ApiResult
*/
public static ApiResult membersBatchUpdate(List<String> openidList, int to_groupid) {
String url = membersBatchUpdateUrl + AccessTokenApi.getAccessTokenStr();
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put("openid_list", openidList);
mapData.put("to_groupid", to_groupid);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(mapData));
return new ApiResult(jsonResult);
}
private static String deleteUrl = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=";
/**
* 删除分组
* @param id 分组的id
* @return ApiResult
*/
public static ApiResult delete(int id) {
String url = deleteUrl + AccessTokenApi.getAccessTokenStr();
Map<String, Map<String, Object>> groupData = new HashMap<String, Map<String, Object>>();
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put("id", id);
groupData.put("group", mapData);
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(groupData));
return new ApiResult(jsonResult);
}
}