wechatpy.client.api.wifi.WeChatWiFi.set_homepage()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 4
dl 0
loc 19
rs 10
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
crap 2.032
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from datetime import datetime, date
4
5 10
from optionaldict import optionaldict
6 10
from wechatpy.client.api.base import BaseWeChatAPI
7
8
9 10
class WeChatWiFi(BaseWeChatAPI):
10
11 10
    API_BASE_URL = 'https://api.weixin.qq.com/bizwifi/'
12
13 10
    def list_shops(self, page_index=1, page_size=20):
14
        """
15
        获取门店列表
16
17
        详情请参考
18
        http://mp.weixin.qq.com/wiki/15/bcfb5d4578ea818b89913472cf2bbf8f.html
19
20
        :param page_index: 可选,分页下标,默认从1开始
21
        :param page_size: 可选,每页的个数,默认20个,最大20个
22
        :return: 返回的 JSON 数据包
23
        """
24 10
        res = self._post(
25
            'shop/list',
26
            data={
27
                'pageindex': page_index,
28
                'pagesize': page_size,
29
            },
30
            result_processor=lambda x: x['data']
31
        )
32 10
        return res
33
34 10
    def get_shop(self, shop_id=0):
35
        """
36
        查询门店的WiFi信息
37
        http://mp.weixin.qq.com/wiki/15/bcfb5d4578ea818b89913472cf2bbf8f.html
38
39
        :param shop_id: 门店 ID
40
        :return: 返回的 JSON 数据包
41
        """
42 10
        res = self._post(
43
            'shop/get',
44
            data={
45
                'shop_id': shop_id,
46
            },
47
            result_processor=lambda x: x['data']
48
        )
49 10
        return res
50
51 10
    def add_device(self, shop_id, ssid, password, bssid):
52
        """
53
        添加设备
54
55
        详情请参考
56
        http://mp.weixin.qq.com/wiki/10/6232005bdc497f7cf8e19d4e843c70d2.html
57
58
        :param shop_id: 门店 ID
59
        :param ssid: 无线网络设备的ssid。非认证公众号添加的ssid必需是“WX”开头(“WX”为大写字母),
60
                     认证公众号和第三方平台无此限制;所有ssid均不能包含中文字符
61
        :param password: 无线网络设备的密码,大于8个字符,不能包含中文字符
62
        :param bssid: 无线网络设备无线mac地址,格式冒号分隔,字符长度17个,并且字母小写
63
        :return: 返回的 JSON 数据包
64
        """
65 10
        return self._post(
66
            'device/add',
67
            data={
68
                'shop_id': shop_id,
69
                'ssid': ssid,
70
                'password': password,
71
                'bssid': bssid,
72
            }
73
        )
74
75 10
    def list_devices(self, shop_id=None, page_index=1, page_size=20):
76
        """
77
        查询设备
78
79
        详情请参考
80
        http://mp.weixin.qq.com/wiki/10/6232005bdc497f7cf8e19d4e843c70d2.html
81
82
        :param shop_id: 可选,门店 ID
83
        :param page_index: 可选,分页下标,默认从1开始
84
        :param page_size: 可选,每页的个数,默认20个,最大20个
85
        :return: 返回的 JSON 数据包
86
        """
87 10
        data = optionaldict(
88
            shop_id=shop_id,
89
            pageindex=page_index,
90
            pagesize=page_size
91
        )
92 10
        res = self._post(
93
            'device/list',
94
            data=data,
95
            result_processor=lambda x: x['data']
96
        )
97 10
        return res
98
99 10
    def delete_device(self, bssid):
100
        """
101
        删除设备
102
103
        详情请参考
104
        http://mp.weixin.qq.com/wiki/10/6232005bdc497f7cf8e19d4e843c70d2.html
105
106
        :param bssid: 无线网络设备无线mac地址,格式冒号分隔,字符长度17个,并且字母小写
107
        :return: 返回的 JSON 数据包
108
        """
109 10
        return self._post('device/delete', data={'bssid': bssid})
110
111 10
    def get_qrcode_url(self, shop_id, img_id):
112
        """
113
        获取物料二维码图片网址
114
115
        详情请参考
116
        http://mp.weixin.qq.com/wiki/7/fcd0378ef00617fc276be2b3baa80973.html
117
118
        :param shop_id: 门店 ID
119
        :param img_id: 物料样式编号:0-二维码,可用于自由设计宣传材料;
120
                       1-桌贴(二维码),100mm×100mm(宽×高),可直接张贴
121
        :return: 二维码图片网址
122
        """
123 10
        res = self._post(
124
            'qrcode/get',
125
            data={
126
                'shop_id': shop_id,
127
                'img_id': img_id,
128
            },
129
            result_processor=lambda x: x['data']['qrcode_url']
130
        )
131 10
        return res
132
133 10
    def set_homepage(self, shop_id, template_id, url=None):
134
        """
135
        设置商家主页
136
137
        详情请参考
138
        http://mp.weixin.qq.com/wiki/6/2732f3cf83947e0e4971aa8797ee9d6a.html
139
140
        :param shop_id: 门店 ID
141
        :param template_id: 模板ID,0-默认模板,1-自定义url
142
        :param url: 自定义链接,当template_id为1时必填
143
        :return: 返回的 JSON 数据包
144
        """
145 10
        data = {
146
            'shop_id': shop_id,
147
            'template_id': template_id,
148
        }
149 10
        if url:
150
            data['struct'] = {'url': url}
151 10
        return self._post('homepage/set', data=data)
152
153 10
    def get_homepage(self, shop_id):
154
        """
155
        查询商家主页
156
157
        详情请参考
158
        http://mp.weixin.qq.com/wiki/6/2732f3cf83947e0e4971aa8797ee9d6a.html
159
160
        :param shop_id: 门店 ID
161
        :return: 返回的 JSON 数据包
162
        """
163 10
        res = self._post(
164
            'homepage/get',
165
            data={'shop_id': shop_id},
166
            result_processor=lambda x: x['data']
167
        )
168 10
        return res
169
170 10
    def list_statistics(self, begin_date, end_date, shop_id=-1):
171
        """
172
        Wi-Fi数据统计
173
174
        详情请参考
175
        http://mp.weixin.qq.com/wiki/8/dfa2b756b66fca5d9b1211bc18812698.html
176
177
        :param begin_date: 起始日期时间,最长时间跨度为30天
178
        :param end_date: 结束日期时间戳,最长时间跨度为30天
179
        :param shop_id: 可选,门店 ID,按门店ID搜索,-1为总统计
180
        :return: 返回的 JSON 数据包
181
        """
182 10
        if isinstance(begin_date, (datetime, date)):
183
            begin_date = begin_date.strftime('%Y-%m-%d')
184 10
        if isinstance(end_date, (datetime, date)):
185
            end_date = end_date.strftime('%Y-%m-%d')
186 10
        res = self._post(
187
            'statistics/list',
188
            data={
189
                'begin_date': begin_date,
190
                'end_date': end_date,
191
                'shop_id': shop_id
192
            },
193
            result_processor=lambda x: x['data']
194
        )
195
        return res
196