1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.alibaba.fastjson.JSON;
7 import com.alibaba.fastjson.JSONObject;
8 import com.foxinmy.weixin4j.exception.WeixinException;
9 import com.foxinmy.weixin4j.http.weixin.ApiResult;
10 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11 import com.foxinmy.weixin4j.model.Token;
12 import com.foxinmy.weixin4j.model.paging.Pagedata;
13 import com.foxinmy.weixin4j.mp.model.shakearound.Device;
14 import com.foxinmy.weixin4j.mp.model.shakearound.DeviceAuditState;
15 import com.foxinmy.weixin4j.mp.model.shakearound.ShakeUserInfo;
16 import com.foxinmy.weixin4j.token.TokenManager;
17
18
19
20
21
22
23
24
25
26 public class ShakeAroundApi extends MpApi {
27
28
29 private final TokenManager tokenManager;
30
31
32
33
34
35
36
37 public ShakeAroundApi(TokenManager tokenManager) {
38 this.tokenManager = tokenManager;
39 }
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public DeviceAuditState deviceApply(Integer quantity, String applyReason, String comment) throws WeixinException {
56 String device_apply_uri = getRequestUri("shake_around_device_apply");
57 Token token = this.tokenManager.getCache();
58 JSONObject jsonObject = new JSONObject();
59 jsonObject.put("quantity", quantity);
60 jsonObject.put("apply_reason", applyReason);
61 jsonObject.put("comment", comment);
62 WeixinResponse response = weixinExecutor.post(String.format(device_apply_uri, token.getAccessToken()), jsonObject.toJSONString());
63 DeviceAuditState result = JSON.parseObject(response.getAsJson().getString("data"), DeviceAuditState.class);
64 result.setApplyTime(System.currentTimeMillis() / 1000);
65 result.setAuditTime(0);
66 return result;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public DeviceAuditState deviceQueryApplyStatus(int applyId) throws WeixinException {
82 String device_apply_status_uri = getRequestUri("shake_around_device_apply_status_uri");
83 Token token = this.tokenManager.getCache();
84 JSONObject jsonObject = new JSONObject();
85 jsonObject.put("apply_id", applyId);
86 WeixinResponse response = weixinExecutor
87 .post(String.format(device_apply_status_uri, token.getAccessToken()), jsonObject.toJSONString());
88 DeviceAuditState result = JSON.parseObject(response.getAsJson().getString("data"), DeviceAuditState.class);
89 result.setApplyId(applyId);
90 return result;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public List<Device> deviceSearchDevices(List<Device> device) throws WeixinException {
104 String device_search_uri = getRequestUri("shake_around_device_search_uri");
105 JSONObject jsonObject = new JSONObject();
106 jsonObject.put("type", 1);
107 jsonObject.put("device_identifiers", device);
108 WeixinResponse response = weixinExecutor
109 .post(String.format(device_search_uri, tokenManager.getAccessToken()), jsonObject.toJSONString());
110 JSONObject json = response.getAsJson();
111 String deviceStr = json.getJSONObject("data").getString("devices");
112 return JSON.parseArray(deviceStr, Device.class);
113 }
114
115
116
117
118
119
120
121
122
123
124 public Pagedata<Device> deviceSearchDevices(int pageSize) throws WeixinException {
125 return this.deviceSearchDevices(0, pageSize);
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public Pagedata<Device> deviceSearchDevices(int lastDeviceId, int pageSize) throws WeixinException {
140 String device_search_uri = getRequestUri("shake_around_device_search_uri");
141 JSONObject jsonObject = new JSONObject();
142 jsonObject.put("type", 2);
143 jsonObject.put("last_seen", lastDeviceId);
144 if (pageSize > 50) {
145 pageSize = 50;
146 }
147 jsonObject.put("count", pageSize);
148 WeixinResponse response = weixinExecutor
149 .post(String.format(device_search_uri, tokenManager.getAccessToken()), jsonObject.toJSONString());
150 JSONObject json = response.getAsJson();
151 JSONObject data = json.getJSONObject("data");
152 String deviceStr = data.getString("devices");
153 List<Device> devices = JSON.parseArray(deviceStr, Device.class);
154 Pagedata<Device> pagedata = new Pagedata<Device>(null, data.getIntValue("total_count"), devices);
155 return pagedata;
156 }
157
158
159
160
161
162
163
164
165
166
167 public List<Device> deviceSearchDevicesByApplyId(Integer applyId) throws WeixinException {
168 List<Device> devices = new ArrayList<Device>();
169 Pagedata<Device> pagedata = this.deviceSearchDevicesByApplyId(applyId, 50);
170 devices = pagedata.getContent();
171 for (int page = 50; page < pagedata.getTotalElements(); page = page + 50) {
172 List<Device> _devices = pagedata.getContent();
173 pagedata = this.deviceSearchDevicesByApplyId(applyId, _devices.get(_devices.size() - 1).getDeviceId(), 50);
174 _devices = pagedata.getContent();
175 devices.addAll(_devices);
176 }
177 return devices;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 public Pagedata<Device> deviceSearchDevicesByApplyId(Integer applyId, int pageSize) throws WeixinException {
191 return this.deviceSearchDevicesByApplyId(applyId, 0, pageSize);
192 }
193
194
195
196
197
198
199
200
201
202
203 public Pagedata<Device> deviceSearchDevicesByApplyId(Integer applyId, int lastDeviceId, int pageSize) throws WeixinException {
204 String device_search_uri = getRequestUri("shake_around_device_search_uri");
205 JSONObject jsonObject = new JSONObject();
206 jsonObject.put("type", 3);
207 jsonObject.put("apply_id", applyId);
208 jsonObject.put("last_seen", lastDeviceId);
209 if (pageSize > 50) {
210 pageSize = 50;
211 }
212 jsonObject.put("count", pageSize);
213 WeixinResponse response = weixinExecutor
214 .post(String.format(device_search_uri, tokenManager.getAccessToken()), jsonObject.toJSONString());
215 JSONObject json = response.getAsJson();
216 JSONObject data = json.getJSONObject("data");
217 String deviceStr = data.getString("devices");
218 List<Device> devices = JSON.parseArray(deviceStr, Device.class);
219 Pagedata<Device> pagedata = new Pagedata<Device>(null, data.getIntValue("total_count"), devices);
220 return pagedata;
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public ApiResult deviceUpdateComment(Device device, String comment) throws WeixinException {
234 String device_update_uri = getRequestUri("shake_around_device_update_uri");
235 JSONObject jsonObject = new JSONObject();
236 JSONObject deviceJsonObj = new JSONObject();
237 jsonObject.put("device_identifier", deviceJsonObj);
238 jsonObject.put("comment", comment);
239 if (device.getDeviceId() == null) {
240 deviceJsonObj.put("uuid", device.getUuid());
241 deviceJsonObj.put("major", device.getMajor());
242 deviceJsonObj.put("minor", device.getMinor());
243 } else {
244 deviceJsonObj.put("device_id", device.getDeviceId());
245 }
246 WeixinResponse weixinResponse = weixinExecutor
247 .post(String.format(device_update_uri, tokenManager.getAccessToken()), jsonObject.toJSONString());
248 return weixinResponse.getAsResult();
249
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264 public ShakeUserInfo getShakeUserInfo(String ticket) throws WeixinException {
265 String user_get_shake_info_url = getRequestUri("shake_around_user_get_shake_info");
266 JSONObject jsonObject = new JSONObject();
267 jsonObject.put("ticket", ticket);
268 WeixinResponse weixinResponse = weixinExecutor
269 .post(String.format(user_get_shake_info_url, tokenManager.getAccessToken()), jsonObject.toJSONString());
270 return weixinResponse.getAsJson().getObject("data", ShakeUserInfo.class);
271
272 }
273 }