Completed
Pull Request — master (#346)
by
unknown
20:48
created

WeChatMerchant   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 70.83%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 59
ccs 17
cts 24
cp 0.7083
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from wechatpy.client.api.base import BaseWeChatAPI
4
5 10
from wechatpy.client.api.merchant.category import MerchantCategory
6 10
from wechatpy.client.api.merchant.stock import MerchantStock
7 10
from wechatpy.client.api.merchant.express import MerchantExpress
8 10
from wechatpy.client.api.merchant.group import MerchantGroup
9 10
from wechatpy.client.api.merchant.shelf import MerchantShelf
10 10
from wechatpy.client.api.merchant.order import MerchantOrder
11 10
from wechatpy.client.api.merchant.common import MerchantCommon
12
13
14 10
class WeChatMerchant(BaseWeChatAPI):
15
    API_BASE_URL = 'https://api.weixin.qq.com/'
16 10
17
    def __init__(self, *args, **kwargs):
18 10
        super(WeChatMerchant, self).__init__(*args, **kwargs)
19 10
20
        # sub APIs
21
        self.category = MerchantCategory(self._client)
22 10
        self.stock = MerchantStock(self._client)
23 10
        self.express = MerchantExpress(self._client)
24 10
        self.group = MerchantGroup(self._client)
25 10
        self.shelf = MerchantShelf(self._client)
26 10
        self.order = MerchantOrder(self._client)
27 10
        self.common = MerchantCommon(self._client)
28 10
29
    def create(self, product_data):
30 10
        """增加商品"""
31
        return self._post(
32
            'merchant/create',
33
            data=product_data
34
        )
35
36 10
    def delete(self, product_id):
37
        """删除商品"""
38
        return self._post(
39
            'merchant/del',
40
            data={
41
                'product_id': product_id
42
            }
43
        )
44 10
45
    def update(self, product_id, product_data):
46
        """修改商品"""
47
        product_data['product_id'] = product_id
48
        return self._post(
49
            'merchant/update',
50
            data=product_data
51 10
        )
52
53
    def get(self, product_id):
54
        """查询商品"""
55
        return self._post(
56
            'merchant/get',
57
            data={
58
                'product_id': product_id
59 10
            }
60
        )
61
62
    def get_by_status(self, status):
63
        """获取指定状态的所有商品"""
64
        return self._post(
65
            'merchant/getbystatus',
66
            data={
67 10
                'status': status
68
            }
69
        )
70
71
    def update_product_status(self, product_id, status):
72
        """商品上下架"""
73
        return self._post(
74
            'merchant/modproductstatus',
75
            data={
76
                'product_id': product_id,
77
                'status': status
78
            }
79
        )
80
81
    def get_subcategories(self, cate_id):
82
        """
83
        获取指定分类的所有子分类
84
85
        :param cate_id: 大分类ID(根节点分类id为1)
86
        :return: 返回的 JSON 数据包
87
        """
88
        return self._post(
89
            'merchant/category/getsub',
90
            data={
91
                'cate_id': cate_id
92
            }
93
        )
94
95
    def get_category_sku(self, cate_id):
96
        """
97
        获取指定子分类的所有SKU
98
99
        :param cate_id: 商品子分类ID
100
        :return: 返回的 JSON 数据包
101
        """
102
        return self._post(
103
            'merchant/category/getsku',
104
            data={
105
                'cate_id': cate_id
106
            }
107
        )
108
109
    def get_category_property(self, cate_id):
110
        """
111
        获取指定分类的所有属性
112
113
        :param cate_id: 商品子分类ID
114
        :return: 返回的 JSON 数据包
115
        """
116
        return self._post(
117
            'merchant/category/getproperty',
118
            data={
119
                'cate_id': cate_id
120
            }
121
        )
122
123
    def add_stock(self, product_id, sku_info, quantity):
124
        """
125
        增加库存
126
127
        :param product_id: 商品ID
128
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
129
        :param quantity: 增加的库存数量
130
        :return: 返回的 JSON 数据包
131
        """
132
        return self._post(
133
            'merchant/stock/add',
134
            data={
135
                "product_id": product_id,
136
                "sku_info": sku_info,
137
                "quantity": quantity
138
            }
139
        )
140
141
    def reduce_stock(self, product_id, sku_info, quantity):
142
        """
143
        减少库存
144
145
        :param product_id: 商品ID
146
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
147
        :param quantity: 减少的库存数量
148
        :return: 返回的 JSON 数据包
149
        """
150
        return self._post(
151
            'merchant/stock/reduce',
152
            data={
153
                "product_id": product_id,
154
                "sku_info": sku_info,
155
                "quantity": quantity
156
            }
157
        )
158
159
    def add_express(self, product_data):
160
        """
161
        增加邮费模板
162
163
        :param product_data: 邮费信息
164
        :return: 返回的 JSON 数据包
165
        """
166
        return self._post(
167
            'merchant/express/add',
168
            data=product_data
169
        )
170
171
    def del_express(self, template_id):
172
        """
173
        增加邮费模板
174
175
        :param template_id: 邮费模板ID
176
        :return: 返回的 JSON 数据包
177
        """
178
        return self._post(
179
            'merchant/express/del',
180
            data={
181
                'template_id': template_id
182
            }
183
        )
184
185
    def update_express(self, template_id, delivery_template):
186
        """
187
        增加邮费模板
188
189
        :param template_id: 邮费模板ID
190
        :param delivery_template: 邮费模板信息(字段说明详见增加邮费模板)
191
        :return: 返回的 JSON 数据包
192
        """
193
        delivery_template['template_id'] = template_id
194
        return self._post(
195
            'merchant/express/update',
196
            data=delivery_template
197
        )
198
199
    def get_express(self, template_id):
200
        """
201
        获取指定ID的邮费模板
202
203
        :param template_id: 邮费模板ID
204
        :return: 返回的 JSON 数据包
205
        """
206
        return self._post(
207
            'merchant/express/getbyid',
208
            data={
209
                'template_id': template_id
210
            }
211
        )
212
213
    def get_all_express(self):
214
        """
215
        获取所有邮费模板
216
217
        :param template_id: 邮费模板ID
218
        :return: 返回的 JSON 数据包
219
        """
220
        return self._get(
221
            'merchant/express/getall'
222
        )
223
224
    def add_group(self, group_detail):
225
        """
226
        增加分组
227
228
        :param group_detail: 商品分组信息
229
        :return: 返回的 JSON 数据包
230
        """
231
        return self._post(
232
            'merchant/group/add',
233
            data=group_detail
234
        )
235
236
    def del_group(self, group_id):
237
        """
238
        删除分组
239
240
        :param group_id: 商品分组ID
241
        :return: 返回的 JSON 数据包
242
        """
243
        return self._post(
244
            'merchant/group/del',
245
            data={
246
                'group_id': group_id
247
            }
248
        )
249
250
    def update_group_property(self, group_id, group_name):
251
        """
252
        修改分组属性
253
254
        :param group_id: 商品分组ID
255
        :param group_name: 商品分组名称
256
        :return: 返回的 JSON 数据包
257
        """
258
        return self._post(
259
            'merchant/group/propertymod',
260
            data={
261
                'group_id': group_id,
262
                'group_name': group_name
263
            }
264
        )
265
266
    def update_group_product(self, group_id, product_data):
267
        """
268
        修改分组商品
269
270
        :param group_id: 商品分组ID
271
        :param product_data: 分组商品信息
272
        :return: 返回的 JSON 数据包
273
        """
274
        product_data['group_id'] = group_id
275
        return self._post(
276
            'merchant/group/productmod',
277
            data=product_data
278
        )
279
280
    def get_all_groups(self):
281
        """
282
        获取所有分组
283
284
        :return: 返回的 JSON 数据包
285
        """
286
        return self._get(
287
            'merchant/group/getall'
288
        )
289
290
    def get_group(self, group_id):
291
        """
292
        根据分组ID获取分组信息
293
294
        :param group_id: 商品分组ID
295
        :return: 返回的 JSON 数据包
296
        """
297
        return self._post(
298
            'merchant/group/getbyid',
299
            data={
300
                'group_id': group_id
301
            }
302
        )
303
304
    def add_shelf(self, shelf_data):
305
        """
306
        增加货架
307
308
        :param shelf_data: 货架详情信息
309
        :return: 返回的 JSON 数据包
310
        """
311
        return self._post(
312
            'merchant/shelf/add',
313
            data=shelf_data
314
        )
315
316
    def del_shelf(self, shelf_id):
317
        """
318
        删除货架
319
320
        :param shelf_id: 货架ID
321
        :return: 返回的 JSON 数据包
322
        """
323
        return self._post(
324
            'merchant/shelf/del',
325
            data={
326
                'shelf_id': shelf_id
327
            }
328
        )
329
330
    def update_shelf(self, shelf_id, shelf_data):
331
        """
332
        修改货架
333
334
        :param shelf_id: 货架ID
335
        :param shelf_data: 货架详情
336
        :return: 返回的 JSON 数据包
337
        """
338
        shelf_data['shelf_id'] = shelf_id
339
        return self._post(
340
            'merchant/shelf/mod',
341
            data=shelf_data
342
        )
343
344
    def get_all_shelves(self):
345
        """
346
        获取所有货架
347
348
        :return: 返回的 JSON 数据包
349
        """
350
        return self._get(
351
            'merchant/shelf/getall'
352
        )
353
354
    def get_shelf(self, shelf_id):
355
        """
356
        根据货架ID获取货架信息
357
358
        :param shelf_id: 货架ID
359
        :return: 返回的 JSON 数据包
360
        """
361
        return self._post(
362
            'merchant/shelf/getbyid',
363
            data={
364
                'shelf_id': shelf_id
365
            }
366
        )
367
368
    def get_order(self, order_id):
369
        """
370
        根据订单ID获取订单详情
371
372
        :param order_id: 订单ID
373
        :return: 返回的 JSON 数据包
374
        """
375
        return self._post(
376
            'merchant/order/getbyid',
377
            data={
378
                'order_id': order_id
379
            }
380
        )
381
382
    def query_order(self, status=None, begintime=None, endtime=None):
383
        """
384
        根据订单状态/创建时间获取订单详情
385
386
        :param status: 订单状态(不带该字段-全部状态, 2-待发货, 3-已发货, 5-已完成, 8-维权中, )
387
        :param begintime: 订单创建时间起始时间(不带该字段则不按照时间做筛选)
388
        :param endtime: 订单创建时间终止时间(不带该字段则不按照时间做筛选)
389
        :return: 返回的 JSON 数据包
390
        """
391
        return self._post(
392
            'merchant/order/getbyfilter',
393
            data={
394
                'status': status,
395
                'begintime': begintime,
396
                'endtime': endtime
397
            }
398
        )
399
400
    def set_delivery(self, order_id, delivery_data):
401
        """
402
        修改货架
403
404
        :param order_id: 订单ID
405
        :param delivery_data: 商品物流信息
406
        :return: 返回的 JSON 数据包
407
        """
408
        delivery_data['order_id'] = order_id
409
        return self._post(
410
            'merchant/shelf/setdeliverymod',
411
            data=delivery_data
412
        )
413
414
    def upload_image(self, media_file):
415
        """
416
        上传图片
417
418
        :param media_file: 要上传的文件,一个 File-object
419
        :return: 上传成功时返回图片 URL
420
        """
421
        res = self._post(
422
            url='merchant/common/upload_img',
423
            files={
424
                'media': media_file
425
            },
426
            result_processor=lambda x: x['url']
427
        )
428
        return res
429