__init__.py 11 KB

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