View Javadoc
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   * @author fengyapeng
22   * @auther: Feng Yapeng feng27156@gmail.com
23   * @since: 2016 /10/12 21:13
24   * @since 2016 -10-13 10:49:39
25   */
26  public class ShakeAroundApi extends MpApi {
27  
28  
29      private final TokenManager tokenManager;
30  
31  
32      /**
33       * Instantiates a new Shake around api.
34       *
35       * @param tokenManager the token manager
36       */
37      public ShakeAroundApi(TokenManager tokenManager) {
38          this.tokenManager = tokenManager;
39      }
40  
41      /**
42       * 申请配置设备所需的UUID、Major、Minor。申请成功后返回批次ID,可用返回的批次ID通过“查询设备ID申请状态”接口查询目前申请的审核状态。
43       * 若单次申请的设备ID数量小于500个,系统会进行快速审核;若单次申请的设备ID数量大于等 500个 ,会在三个工作日内完成审核。
44       * 如果已审核通过,可用返回的批次ID通过“查询设备列表”接口拉取本次申请的设备ID。 通过接口申请的设备ID,需先配置页面,若未配置页面,则摇不出页面信息。
45       *
46       * @param quantity    the quantity  申请的设备ID的数量,单次新增设备超过500个,需走人工审核流程
47       * @param applyReason the apply reason 申请理由,不超过100个汉字或200个英文字母
48       * @param comment     the comment 备注,不超过15个汉字或30个英文字母
49       * @return the api result
50       * @throws WeixinException the weixin exception
51       * @author fengyapeng
52       * @see <a href= "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1459246241&token=&lang=zh_CN"></a>
53       * @since 2016 -10-12 21:21:47
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       * 查询设备ID申请的审核状态。若单次申请的设备ID数量小于等于500个,
72       * 系统会进行快速审核;若单次申请的设备ID数量大于500个,则在三个工作日内完成审核。
73       *
74       * @param applyId the apply id 批次ID,申请设备ID时所返回的批次ID
75       * @return the device audit state
76       * @throws WeixinException the weixin exception
77       * @author fengyapeng
78       * @see <a href= "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1459246243&token=&lang=zh_CN"></a>
79       * @since 2016 -10-12 21:57:04
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       * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息。
95       * 查询指定设备的信息
96       *
97       * @param device the device
98       * @return the list
99       * @throws WeixinException the weixin exception
100      * @author fengyapeng
101      * @since 2016 -10-13 10:11:34
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      * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息。
117      * 按照分页信息查询设备
118      *
119      * @return the list
120      * @throws WeixinException the weixin exception
121      * @author fengyapeng
122      * @since 2016 -10-13 10:11:34
123      */
124     public Pagedata<Device> deviceSearchDevices(int pageSize) throws WeixinException {
125         return this.deviceSearchDevices(0, pageSize);
126     }
127 
128     /**
129      * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息
130      * 根据上次查询的最后的设备编号按照分页查询
131      *
132      * @param lastDeviceId the last device id
133      * @param pageSize     the page size
134      * @return list pagedata
135      * @throws WeixinException the weixin exception
136      * @author fengyapeng
137      * @since 2016 -10-13 10:52:20
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      * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息
160      * 查询 设备Id 下的所有的设备
161      *
162      * @param applyId the apply id
163      * @return the list
164      * @author fengyapeng
165      * @since 2016 -10-13 10:49:39
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      * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息
183      * 分页获取设备id下的前多少设备
184      *
185      * @param applyId the apply id
186      * @return the list
187      * @author fengyapeng
188      * @since 2016 -10-13 10:49:39
189      */
190     public Pagedata<Device> deviceSearchDevicesByApplyId(Integer applyId, int pageSize) throws WeixinException {
191         return this.deviceSearchDevicesByApplyId(applyId, 0, pageSize);
192     }
193 
194     /**
195      * 查询已有的设备ID、UUID、Major、Minor、激活状态、备注信息、关联门店、关联页面等信息
196      * 分页获取设备id下的根据上次查询的最后的设备编号前多少设备
197      *
198      * @param applyId the apply id
199      * @return the list
200      * @author fengyapeng
201      * @since 2016 -10-13 10:49:39
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      * 编辑设备的备注信息。可用设备ID或完整的UUID、Major、Minor指定设备,二者选其一。
226      *
227      * @param device  the device
228      * @param comment the comment
229      * @return api result
230      * @author fengyapeng
231      * @since 2016 -10-13 14:33:06
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      * 获取设备信息,包括UUID、major、minor,以及距离、openID等信息.
254      *
255      *
256      * <a herf="
257      * http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443447963&token=&lang=zh_CN"></a>
258      *
259      * @param ticket     the ticket 摇周边业务的ticket,可在摇到的URL中得到,ticket生效时间为30分钟,每一次摇都会重新生成新的ticket
260      * @return shake user info
261      * @author fengyapeng
262      * @since 2016 -10-21 19:34:38
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 }