View Javadoc
1   package com.foxinmy.weixin4j.mp.api;
2   
3   import java.util.List;
4   
5   import com.alibaba.fastjson.JSON;
6   import com.alibaba.fastjson.JSONObject;
7   import com.foxinmy.weixin4j.exception.WeixinException;
8   import com.foxinmy.weixin4j.http.weixin.ApiResult;
9   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10  import com.foxinmy.weixin4j.model.Token;
11  import com.foxinmy.weixin4j.mp.model.Group;
12  import com.foxinmy.weixin4j.token.TokenManager;
13  
14  /**
15   * 分组相关API
16   * 
17   * @className GroupApi
18   * @author jinyu(foxinmy@gmail.com)
19   * @date 2014年9月25日
20   * @since JDK 1.6
21   * @see com.foxinmy.weixin4j.mp.model.Group
22   */
23  @Deprecated
24  public class GroupApi extends MpApi {
25  
26  	private final TokenManager tokenManager;
27  
28  	public GroupApi(TokenManager tokenManager) {
29  		this.tokenManager = tokenManager;
30  	}
31  
32  	/**
33  	 * 创建分组
34  	 * 
35  	 * @param name
36  	 *            组名称
37  	 * @return group对象
38  	 * @throws WeixinException
39  	 * @see <a
40  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">创建分组</a>
41  	 * @see com.foxinmy.weixin4j.mp.model.Group
42  	 * @see com.foxinmy.weixin4j.mp.model.Group#toCreateJson()
43  	 */
44  	public Group createGroup(String name) throws WeixinException {
45  		String group_create_uri = getRequestUri("group_create_uri");
46  		Token token = tokenManager.getCache();
47  		Group group = new Group(name);
48  		WeixinResponse response = weixinExecutor.post(
49  				String.format(group_create_uri, token.getAccessToken()),
50  				group.toCreateJson());
51  		return JSON.parseObject(response.getAsJson().getString("group"),
52  				Group.class);
53  	}
54  
55  	/**
56  	 * 查询所有分组
57  	 * 
58  	 * @return 组集合
59  	 * @throws WeixinException
60  	 * @see <a
61  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">查询所有分组</a>
62  	 * @see com.foxinmy.weixin4j.mp.model.Group
63  	 */
64  	public List<Group> getGroups() throws WeixinException {
65  		String group_get_uri = getRequestUri("group_get_uri");
66  		Token token = tokenManager.getCache();
67  		WeixinResponse response = weixinExecutor.get(String.format(
68  				group_get_uri, token.getAccessToken()));
69  
70  		return JSON.parseArray(response.getAsJson().getString("groups"),
71  				Group.class);
72  	}
73  
74  	/**
75  	 * 查询用户所在分组
76  	 * 
77  	 * @param openId
78  	 *            用户对应的ID
79  	 * @return 组ID
80  	 * @throws WeixinException
81  	 * @see <a
82  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">查询用户所在分组</a>
83  	 * @see com.foxinmy.weixin4j.mp.model.Group
84  	 */
85  	public int getGroupByOpenId(String openId) throws WeixinException {
86  		String group_getid_uri = getRequestUri("group_getid_uri");
87  		Token token = tokenManager.getCache();
88  		WeixinResponse response = weixinExecutor.post(
89  				String.format(group_getid_uri, token.getAccessToken()),
90  				String.format("{\"openid\":\"%s\"}", openId));
91  
92  		return response.getAsJson().getIntValue("groupid");
93  	}
94  
95  	/**
96  	 * 修改分组名
97  	 * 
98  	 * @param groupId
99  	 *            组ID
100 	 * @param name
101 	 *            组名称
102 	 * @throws WeixinException
103 	 * @see <a
104 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">修改分组名</a>
105 	 * @see com.foxinmy.weixin4j.mp.model.Group
106 	 * @see com.foxinmy.weixin4j.mp.model.Group#toModifyJson()
107 	 */
108 	public ApiResult modifyGroup(int groupId, String name)
109 			throws WeixinException {
110 		String group_modify_uri = getRequestUri("group_modify_uri");
111 		Token token = tokenManager.getCache();
112 		Group group = new Group(groupId, name);
113 
114 		WeixinResponse response = weixinExecutor.post(
115 				String.format(group_modify_uri, token.getAccessToken()),
116 				group.toModifyJson());
117 		return response.getAsResult();
118 	}
119 
120 	/**
121 	 * 移动用户到分组
122 	 * 
123 	 * @param groupId
124 	 *            组ID
125 	 * @param openId
126 	 *            用户对应的ID
127 	 * @throws WeixinException
128 	 * @see <a
129 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">移动分组</a>
130 	 * @see com.foxinmy.weixin4j.mp.model.Group
131 	 */
132 	public ApiResult moveGroup(int groupId, String openId)
133 			throws WeixinException {
134 		String group_move_uri = getRequestUri("group_move_uri");
135 		Token token = tokenManager.getCache();
136 		WeixinResponse response = weixinExecutor.post(String.format(
137 				group_move_uri, token.getAccessToken()), String.format(
138 				"{\"openid\":\"%s\",\"to_groupid\":%d}", openId, groupId));
139 
140 		return response.getAsResult();
141 	}
142 
143 	/**
144 	 * 批量移动分组
145 	 * 
146 	 * @param groupId
147 	 *            组ID
148 	 * @param openIds
149 	 *            用户ID列表(不能超过50个)
150 	 * @throws WeixinException
151 	 * @see <a
152 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">批量移动分组</a>
153 	 * @see com.foxinmy.weixin4j.mp.model.Group
154 	 */
155 	public ApiResult moveGroup(int groupId, String... openIds)
156 			throws WeixinException {
157 		String group_batchmove_uri = getRequestUri("group_batchmove_uri");
158 		Token token = tokenManager.getCache();
159 		JSONObject obj = new JSONObject();
160 		obj.put("to_groupid", groupId);
161 		obj.put("openid_list", openIds);
162 		WeixinResponse response = weixinExecutor.post(
163 				String.format(group_batchmove_uri, token.getAccessToken()),
164 				obj.toJSONString());
165 
166 		return response.getAsResult();
167 	}
168 
169 	/**
170 	 * 删除用户分组,所有该分组内的用户自动进入默认分组.
171 	 * 
172 	 * @param groupId
173 	 *            组ID
174 	 * @throws WeixinException
175 	 * @see <a
176 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">删除用户分组</a>
177 	 * @see com.foxinmy.weixin4j.mp.model.Group
178 	 */
179 	public ApiResult deleteGroup(int groupId) throws WeixinException {
180 		String group_delete_uri = getRequestUri("group_delete_uri");
181 		Token token = tokenManager.getCache();
182 		WeixinResponse response = weixinExecutor.post(
183 				String.format(group_delete_uri, token.getAccessToken()),
184 				String.format("{\"group\":{\"id\":%d}}", groupId));
185 
186 		return response.getAsResult();
187 	}
188 }