1
|
|
|
# -*- coding: utf-8 -*- |
2
|
10 |
|
from __future__ import absolute_import, unicode_literals |
3
|
10 |
|
import time |
4
|
10 |
|
from datetime import datetime |
5
|
|
|
|
6
|
10 |
|
import six |
7
|
10 |
|
from optionaldict import optionaldict |
8
|
|
|
|
9
|
10 |
|
from wechatpy.client.api.base import BaseWeChatAPI |
10
|
|
|
|
11
|
|
|
|
12
|
10 |
|
class WeChatShakeAround(BaseWeChatAPI): |
13
|
|
|
|
14
|
10 |
|
API_BASE_URL = 'https://api.weixin.qq.com/' |
15
|
|
|
|
16
|
10 |
|
@classmethod |
17
|
|
|
def _to_timestamp(cls, date): |
18
|
10 |
|
if isinstance(date, six.string_types): |
19
|
10 |
|
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') |
20
|
10 |
|
if isinstance(date, datetime): |
21
|
10 |
|
timestamp = int(time.mktime(date.timetuple())) |
22
|
10 |
|
return timestamp |
23
|
10 |
|
return int(date) |
24
|
|
|
|
25
|
10 |
|
def apply_device_id(self, quantity, reason, poi_id=None, comment=None): |
26
|
|
|
""" |
27
|
|
|
申请设备ID |
28
|
|
|
详情请参考 |
29
|
|
|
http://mp.weixin.qq.com/wiki/15/b9e012f917e3484b7ed02771156411f3.html |
30
|
|
|
|
31
|
|
|
:param quantity: 申请的设备ID的数量,单次新增设备超过500个,需走人工审核流程 |
32
|
|
|
:param reason: 申请理由,不超过100个字 |
33
|
|
|
:param poi_id: 可选,设备关联的门店ID |
34
|
|
|
:param comment: 可选,备注,不超过15个汉字或30个英文字母 |
35
|
|
|
:return: 申请的设备信息 |
36
|
|
|
""" |
37
|
10 |
|
data = optionaldict() |
38
|
10 |
|
data['quantity'] = quantity |
39
|
10 |
|
data['apply_reason'] = reason |
40
|
10 |
|
data['poi_id'] = poi_id |
41
|
10 |
|
data['comment'] = comment |
42
|
10 |
|
res = self._post( |
43
|
|
|
'shakearound/device/applyid', |
44
|
|
|
data=data, |
45
|
|
|
result_processor=lambda x: x['data'] |
46
|
|
|
) |
47
|
10 |
|
return res |
48
|
|
|
|
49
|
10 |
|
def update_device(self, device_id=None, uuid=None, major=None, |
50
|
|
|
minor=None, comment=None): |
51
|
|
|
""" |
52
|
|
|
更新设备信息 |
53
|
|
|
详情请参考 |
54
|
|
|
http://mp.weixin.qq.com/wiki/15/b9e012f917e3484b7ed02771156411f3.html |
55
|
|
|
|
56
|
|
|
:param device_id: 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先 |
57
|
|
|
:param uuid: UUID |
58
|
|
|
:param major: major |
59
|
|
|
:param minor: minor |
60
|
|
|
:param comment: 设备的备注信息,不超过15个汉字或30个英文字母。 |
61
|
|
|
:return: 返回的 JSON 数据包 |
62
|
|
|
""" |
63
|
10 |
|
data = optionaldict() |
64
|
10 |
|
data['comment'] = comment |
65
|
10 |
|
data['device_identifier'] = { |
66
|
|
|
'device_id': device_id, |
67
|
|
|
'uuid': uuid, |
68
|
|
|
'major': major, |
69
|
|
|
'minor': minor |
70
|
|
|
} |
71
|
10 |
|
return self._post( |
72
|
|
|
'shakearound/device/update', |
73
|
|
|
data=data |
74
|
|
|
) |
75
|
|
|
|
76
|
10 |
|
def bind_device_location(self, poi_id, device_id=None, uuid=None, |
77
|
|
|
major=None, minor=None): |
78
|
|
|
""" |
79
|
|
|
配置设备与门店的关联关系 |
80
|
|
|
详情请参考 |
81
|
|
|
http://mp.weixin.qq.com/wiki/15/b9e012f917e3484b7ed02771156411f3.html |
82
|
|
|
|
83
|
|
|
:param poi_id: 待关联的门店ID |
84
|
|
|
:param device_id: 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先 |
85
|
|
|
:param uuid: UUID |
86
|
|
|
:param major: major |
87
|
|
|
:param minor: minor |
88
|
|
|
:return: 返回的 JSON 数据包 |
89
|
|
|
""" |
90
|
10 |
|
data = optionaldict() |
91
|
10 |
|
data['poi_id'] = poi_id |
92
|
10 |
|
data['device_identifier'] = { |
93
|
|
|
'device_id': device_id, |
94
|
|
|
'uuid': uuid, |
95
|
|
|
'major': major, |
96
|
|
|
'minor': minor |
97
|
|
|
} |
98
|
10 |
|
return self._post( |
99
|
|
|
'shakearound/device/bindlocation', |
100
|
|
|
data=data |
101
|
|
|
) |
102
|
|
|
|
103
|
10 |
|
def search_device(self, identifiers=None, apply_id=None, |
104
|
|
|
begin=0, count=10): |
105
|
|
|
""" |
106
|
|
|
查询设备列表 |
107
|
|
|
详情请参考 |
108
|
|
|
http://mp.weixin.qq.com/wiki/15/b9e012f917e3484b7ed02771156411f3.html |
109
|
|
|
|
110
|
|
|
:param identifiers: 设备 ID 信息列表 |
111
|
|
|
:param apply_id: 批次ID,申请设备ID超出500个时所返回批次ID |
112
|
|
|
:param begin: 设备列表的起始索引值 |
113
|
|
|
:param count: 待查询的设备个数 |
114
|
|
|
:return: 设备列表 |
115
|
|
|
""" |
116
|
10 |
|
data = optionaldict() |
117
|
10 |
|
data['begin'] = begin |
118
|
10 |
|
data['count'] = count |
119
|
10 |
|
data['apply_id'] = apply_id |
120
|
10 |
|
if identifiers: |
121
|
|
|
data['device_identifiers'] = identifiers |
122
|
10 |
|
res = self._post( |
123
|
|
|
'shakearound/device/search', |
124
|
|
|
data=data, |
125
|
|
|
result_processor=lambda x: x['data'] |
126
|
|
|
) |
127
|
10 |
|
return res |
128
|
|
|
|
129
|
10 |
|
def add_page(self, title, description, icon_url, page_url, comment=None): |
130
|
|
|
""" |
131
|
|
|
新增页面 |
132
|
|
|
详情请参考 |
133
|
|
|
http://mp.weixin.qq.com/wiki/5/6626199ea8757c752046d8e46cf13251.html |
134
|
|
|
|
135
|
|
|
:param title: 在摇一摇页面展示的主标题,不超过6个字 |
136
|
|
|
:param description: 在摇一摇页面展示的副标题,不超过7个字 |
137
|
|
|
:param icon_url: 在摇一摇页面展示的图片。图片需先上传至微信侧服务器, |
138
|
|
|
用“素材管理-上传图片素材”接口上传图片,返回的图片URL再配置在此处 |
139
|
|
|
:param page_url: 跳转链接 |
140
|
|
|
:param comment: 可选,页面的备注信息,不超过15个字 |
141
|
|
|
:return: 页面信息 |
142
|
|
|
""" |
143
|
10 |
|
data = optionaldict() |
144
|
10 |
|
data['title'] = title |
145
|
10 |
|
data['description'] = description |
146
|
10 |
|
data['icon_url'] = icon_url |
147
|
10 |
|
data['page_url'] = page_url |
148
|
10 |
|
data['comment'] = comment |
149
|
10 |
|
res = self._post( |
150
|
|
|
'shakearound/page/add', |
151
|
|
|
data=data, |
152
|
|
|
result_processor=lambda x: x['data'] |
153
|
|
|
) |
154
|
10 |
|
return res |
155
|
|
|
|
156
|
10 |
|
def update_page(self, page_id, title, description, |
157
|
|
|
icon_url, page_url, comment=None): |
158
|
|
|
""" |
159
|
|
|
编辑页面信息 |
160
|
|
|
详情请参考 |
161
|
|
|
http://mp.weixin.qq.com/wiki/5/6626199ea8757c752046d8e46cf13251.html |
162
|
|
|
|
163
|
|
|
:param page_id: 摇周边页面唯一ID |
164
|
|
|
:param title: 在摇一摇页面展示的主标题,不超过6个字 |
165
|
|
|
:param description: 在摇一摇页面展示的副标题,不超过7个字 |
166
|
|
|
:param icon_url: 在摇一摇页面展示的图片。图片需先上传至微信侧服务器, |
167
|
|
|
用“素材管理-上传图片素材”接口上传图片,返回的图片URL再配置在此处 |
168
|
|
|
:param page_url: 跳转链接 |
169
|
|
|
:param comment: 可选,页面的备注信息,不超过15个字 |
170
|
|
|
:return: 页面信息 |
171
|
|
|
""" |
172
|
10 |
|
data = optionaldict() |
173
|
10 |
|
data['page_id'] = page_id |
174
|
10 |
|
data['title'] = title |
175
|
10 |
|
data['description'] = description |
176
|
10 |
|
data['icon_url'] = icon_url |
177
|
10 |
|
data['page_url'] = page_url |
178
|
10 |
|
data['comment'] = comment |
179
|
10 |
|
res = self._post( |
180
|
|
|
'shakearound/page/update', |
181
|
|
|
data=data, |
182
|
|
|
result_processor=lambda x: x['data'] |
183
|
|
|
) |
184
|
10 |
|
return res |
185
|
|
|
|
186
|
10 |
|
def search_pages(self, page_ids=None, begin=0, count=10): |
187
|
|
|
""" |
188
|
|
|
查询页面列表 |
189
|
|
|
详情请参考 |
190
|
|
|
http://mp.weixin.qq.com/wiki/5/6626199ea8757c752046d8e46cf13251.html |
191
|
|
|
|
192
|
|
|
:param page_ids: 指定页面的id列表 |
193
|
|
|
:param begin: 页面列表的起始索引值 |
194
|
|
|
:param count: 待查询的页面个数 |
195
|
|
|
:return: 页面查询结果信息 |
196
|
|
|
""" |
197
|
10 |
|
if not page_ids: |
198
|
|
|
data = { |
199
|
|
|
'type': 2, |
200
|
|
|
'begin': begin, |
201
|
|
|
'count': count |
202
|
|
|
} |
203
|
|
|
else: |
204
|
10 |
|
if not isinstance(page_ids, (tuple, list)): |
205
|
10 |
|
page_ids = [page_ids] |
206
|
10 |
|
data = { |
207
|
|
|
'type': 1, |
208
|
|
|
'page_ids': page_ids |
209
|
|
|
} |
210
|
|
|
|
211
|
10 |
|
res = self._post( |
212
|
|
|
'shakearound/page/search', |
213
|
|
|
data=data, |
214
|
|
|
result_processor=lambda x: x['data'] |
215
|
|
|
) |
216
|
10 |
|
return res |
217
|
|
|
|
218
|
10 |
|
def delete_page(self, page_id): |
219
|
|
|
""" |
220
|
|
|
删除页面 |
221
|
|
|
详情请参考 |
222
|
|
|
http://mp.weixin.qq.com/wiki/5/6626199ea8757c752046d8e46cf13251.html |
223
|
|
|
|
224
|
|
|
:param page_id: 指定页面的id列表 |
225
|
|
|
:return: 返回的 JSON 数据包 |
226
|
|
|
""" |
227
|
10 |
|
return self._post( |
228
|
|
|
'shakearound/page/delete', |
229
|
|
|
data={ |
230
|
|
|
'page_id': page_id |
231
|
|
|
} |
232
|
|
|
) |
233
|
|
|
|
234
|
10 |
|
def add_material(self, media_file, media_type='icon'): |
235
|
|
|
""" |
236
|
|
|
上传图片素材 |
237
|
|
|
详情请参考 |
238
|
|
|
http://mp.weixin.qq.com/wiki/5/e997428269ff189d8f9a4b9e177be2d9.html |
239
|
|
|
|
240
|
|
|
:param media_file: 要上传的文件,一个 File-object |
241
|
|
|
:param media_type: 摇一摇素材类型, 取值为 icon或者 license, 默认 icon. |
242
|
|
|
:return: 上传的素材信息 |
243
|
|
|
""" |
244
|
10 |
|
res = self._post( |
245
|
|
|
'shakearound/material/add', |
246
|
|
|
files={ |
247
|
|
|
'media': media_file |
248
|
|
|
}, |
249
|
|
|
params={ |
250
|
|
|
'type': media_type |
251
|
|
|
}, |
252
|
|
|
result_processor=lambda x: x['data'] |
253
|
|
|
) |
254
|
10 |
|
return res |
255
|
|
|
|
256
|
10 |
|
def bind_device_pages(self, page_ids, bind, append, device_id=None, |
257
|
|
|
uuid=None, major=None, minor=None): |
258
|
|
|
""" |
259
|
|
|
配置设备与页面的关联关系 |
260
|
|
|
详情请参考 |
261
|
|
|
http://mp.weixin.qq.com/wiki/12/c8120214ec0ba08af5dfcc0da1a11400.html |
262
|
|
|
|
263
|
|
|
:param page_ids: 待关联的页面列表 |
264
|
|
|
:param bind: 关联操作标志位, 0为解除关联关系,1为建立关联关系 |
265
|
|
|
:param append: 新增操作标志位, 0为覆盖,1为新增 |
266
|
|
|
:param device_id: 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先 |
267
|
|
|
:param uuid: UUID |
268
|
|
|
:param major: major |
269
|
|
|
:param minor: minor |
270
|
|
|
:return: 返回的 JSON 数据包 |
271
|
|
|
""" |
272
|
10 |
|
if not isinstance(page_ids, (tuple, list)): |
273
|
10 |
|
page_ids = [page_ids] |
274
|
10 |
|
data = { |
275
|
|
|
'page_ids': page_ids, |
276
|
|
|
'bind': int(bind), |
277
|
|
|
'append': int(append), |
278
|
|
|
'device_identifier': { |
279
|
|
|
'device_id': device_id, |
280
|
|
|
'uuid': uuid, |
281
|
|
|
'major': major, |
282
|
|
|
'minor': minor |
283
|
|
|
} |
284
|
|
|
} |
285
|
10 |
|
return self._post( |
286
|
|
|
'shakearound/device/bindpage', |
287
|
|
|
data=data |
288
|
|
|
) |
289
|
|
|
|
290
|
10 |
|
def get_shake_info(self, ticket): |
291
|
|
|
""" |
292
|
|
|
获取摇周边的设备及用户信息 |
293
|
|
|
详情请参考 |
294
|
|
|
http://mp.weixin.qq.com/wiki/3/34904a5db3d0ec7bb5306335b8da1faf.html |
295
|
|
|
|
296
|
|
|
:param ticket: 摇周边业务的ticket,可在摇到的URL中得到,ticket生效时间为30分钟 |
297
|
|
|
:return: 设备及用户信息 |
298
|
|
|
""" |
299
|
10 |
|
res = self._post( |
300
|
|
|
'shakearound/user/getshakeinfo', |
301
|
|
|
data={ |
302
|
|
|
'ticket': ticket |
303
|
|
|
}, |
304
|
|
|
result_processor=lambda x: x['data'] |
305
|
|
|
) |
306
|
10 |
|
return res |
307
|
|
|
|
308
|
10 |
|
def get_device_statistics(self, begin_date, end_date, device_id=None, |
309
|
|
|
uuid=None, major=None, minor=None): |
310
|
|
|
""" |
311
|
|
|
以设备为维度的数据统计接口 |
312
|
|
|
http://mp.weixin.qq.com/wiki/0/8a24bcacad40fe7ee98d1573cb8a6764.html |
313
|
|
|
|
314
|
|
|
:param begin_date: 起始时间,最长时间跨度为30天 |
315
|
|
|
:param end_date: 结束时间,最长时间跨度为30天 |
316
|
|
|
:param device_id: 设备编号,若填了UUID、major、minor,则可不填设备编号,若二者都填,则以设备编号为优先 |
317
|
|
|
:param uuid: UUID |
318
|
|
|
:param major: major |
319
|
|
|
:param minor: minor |
320
|
|
|
""" |
321
|
10 |
|
data = { |
322
|
|
|
'device_identifier': { |
323
|
|
|
'device_id': device_id, |
324
|
|
|
'uuid': uuid, |
325
|
|
|
'major': major, |
326
|
|
|
'minor': minor |
327
|
|
|
}, |
328
|
|
|
'begin_date': self._to_timestamp(begin_date), |
329
|
|
|
'end_date': self._to_timestamp(end_date) |
330
|
|
|
} |
331
|
10 |
|
res = self._post( |
332
|
|
|
'shakearound/statistics/device', |
333
|
|
|
data=data, |
334
|
|
|
result_processor=lambda x: x['data'] |
335
|
|
|
) |
336
|
10 |
|
return res |
337
|
|
|
|
338
|
10 |
|
def get_page_statistics(self, page_id, begin_date, end_date): |
339
|
|
|
""" |
340
|
|
|
以页面为维度的数据统计接口 |
341
|
|
|
详情请参考 |
342
|
|
|
http://mp.weixin.qq.com/wiki/0/8a24bcacad40fe7ee98d1573cb8a6764.html |
343
|
|
|
|
344
|
|
|
:param page_id: 页面 ID |
345
|
|
|
:param begin_date: 起始时间,最长时间跨度为30天 |
346
|
|
|
:param end_date: 结束时间,最长时间跨度为30天 |
347
|
|
|
:return: 统计数据 |
348
|
|
|
""" |
349
|
10 |
|
res = self._post( |
350
|
|
|
'shakearound/statistics/page', |
351
|
|
|
data={ |
352
|
|
|
'page_id': page_id, |
353
|
|
|
'begin_date': self._to_timestamp(begin_date), |
354
|
|
|
'end_date': self._to_timestamp(end_date), |
355
|
|
|
}, |
356
|
|
|
result_processor=lambda x: x['data'] |
357
|
|
|
) |
358
|
10 |
|
return res |
359
|
|
|
|
360
|
10 |
|
def get_apply_status(self, apply_id): |
361
|
|
|
""" |
362
|
|
|
查询设备ID申请审核状态 |
363
|
|
|
详情请参考 |
364
|
|
|
http://mp.weixin.qq.com/wiki/15/b9e012f917e3484b7ed02771156411f3.html |
365
|
|
|
|
366
|
|
|
:param apply_id: 批次ID,申请设备ID时所返回的批次ID |
367
|
|
|
:return: 批次状态信息 |
368
|
|
|
""" |
369
|
10 |
|
res = self._post( |
370
|
|
|
'shakearound/device/applystatus', |
371
|
|
|
data={ |
372
|
|
|
'apply_id': apply_id, |
373
|
|
|
}, |
374
|
|
|
result_processor=lambda x: x['data'] |
375
|
|
|
) |
376
|
|
|
return res |
377
|
|
|
|