UNPKG

15.8 kBJavaScriptView Raw
1'use strict';
2
3var util = require('./util');
4var wrapper = util.wrapper;
5var postJSON = util.postJSON;
6
7/**
8 * 增加商品
9 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
10 * Examples:
11 * ```
12 * api.createGoods(goods, callback);
13 * ```
14 * Goods:
15 * ```
16 * {
17 * "product_base":{
18 * "category_id":[
19 * "537074298"
20 * ],
21 * "property":[
22 * {"id":"1075741879","vid":"1079749967"},
23 * {"id":"1075754127","vid":"1079795198"},
24 * {"id":"1075777334","vid":"1079837440"}
25 * ],
26 * "name":"testaddproduct",
27 * "sku_info":[
28 * {
29 * "id":"1075741873",
30 * "vid":["1079742386","1079742363"]
31 * }
32 * ],
33 * "main_img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
34 * "img":[
35 * "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"
36 * ],
37 * "detail":[
38 * {"text":"testfirst"},
39 * {"img": 4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655ZK6ibnlibCCErbKQtReySaVA/0"},
40 * {"text":"testagain"}
41 * ],
42 * "buy_limit":10
43 * },
44 * "sku_list":[
45 * {
46 * "sku_id":"1075741873:1079742386",
47 * "price":30,
48 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5K Wq1QGP3fo6TOTSYD3TBQjuw/0",
49 * "product_code":"testing",
50 * "ori_price":9000000,
51 * "quantity":800
52 * },
53 * {
54 * "sku_id":"1075741873:1079742363",
55 * "price":30,
56 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5K Wq1QGP3fo6TOTSYD3TBQjuw/0",
57 * "product_code":"testingtesting",
58 * "ori_price":9000000,
59 * "quantity":800
60 * }
61 * ],
62 * "attrext":{
63 * "location":{
64 * "country":"中国",
65 * "province":"广东省",
66 * "city":"广州市",
67 * "address":"T.I.T创意园"
68 * },
69 * "isPostFree":0,
70 * "isHasReceipt":1,
71 * "isUnderGuaranty":0,
72 * "isSupportReplace":0
73 * },
74 * "delivery_info":{
75 * "delivery_type":0,
76 * "template_id":0,
77 * "express":[
78 * {"id":10000027,"price":100},
79 * {"id":10000028,"price":100},
80 * {"id":10000029,"price":100}
81 * ]
82 * }
83 * }
84 * ```
85 * Callback:
86 *
87 * - `err`, 调用失败时得到的异常
88 * - `result`, 调用正常时得到的对象
89 *
90 * Result:
91 * ```
92 * {
93 * "errcode": 0,
94 * "errmsg": "success",
95 * "product_id": "pDF3iYwktviE3BzU3BKiSWWi9Nkw"
96 * }
97 * ```
98 * @param {Object} goods 商品信息
99 * @param {Function} callback 回调函数
100 */
101exports.createGoods = function (goods, callback) {
102 this.preRequest(this._createGoods, arguments);
103};
104
105/*!
106 * 增加商品的未封装版本
107 */
108exports._createGoods = function (goods, callback) {
109 var url = this.endpoint + '/merchant/create?access_token=' + this.token.accessToken;
110 this.request(url, postJSON(goods), wrapper(callback));
111};
112
113/**
114 * 删除商品
115 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
116 * Examples:
117 * ```
118 * api.deleteGoods(productId, callback);
119 * ```
120 * Callback:
121 *
122 * - `err`, 调用失败时得到的异常
123 * - `result`, 调用正常时得到的对象
124 *
125 * Result:
126 * ```
127 * {
128 * "errcode": 0,
129 * "errmsg": "success",
130 * }
131 * ```
132 * @param {String} productId 商品Id
133 * @param {Function} callback 回调函数
134 */
135exports.deleteGoods = function (productId, callback) {
136 this.preRequest(this._deleteGoods, arguments);
137};
138
139/*!
140 * 删除商品的未封装版本
141 */
142exports._deleteGoods = function (productId, callback) {
143 var data = {
144 'product_id': productId
145 };
146 var url = this.endpoint + '/merchant/del?access_token=' + this.token.accessToken;
147 this.request(url, postJSON(data), wrapper(callback));
148};
149
150/**
151 * 修改商品
152 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
153 * Examples:
154 * ```
155 * api.updateGoods(goods, callback);
156 * ```
157 * Goods:
158 * ```
159 * {
160 * "product_id":"pDF3iY6Kr_BV_CXaiYysoGqJhppQ",
161 * "product_base":{
162 * "category_id":[
163 * "537074298"
164 * ],
165 * "property":[
166 * {"id":"1075741879","vid":"1079749967"},
167 * {"id":"1075754127","vid":"1079795198"},
168 * {"id":"1075777334","vid":"1079837440"}
169 * ],
170 * "name":"testaddproduct",
171 * "sku_info":[
172 * {
173 * "id":"1075741873",
174 * "vid":["1079742386","1079742363"]
175 * }
176 * ],
177 * "main_img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
178 * "img":[
179 * "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"
180 * ],
181 * "detail":[
182 * {"text":"testfirst"},
183 * {"img": 4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655ZK6ibnlibCCErbKQtReySaVA/0"},
184 * {"text":"testagain"}
185 * ],
186 * "buy_limit":10
187 * },
188 * "sku_list":[
189 * {
190 * "sku_id":"1075741873:1079742386",
191 * "price":30,
192 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5K Wq1QGP3fo6TOTSYD3TBQjuw/0",
193 * "product_code":"testing",
194 * "ori_price":9000000,
195 * "quantity":800
196 * },
197 * {
198 * "sku_id":"1075741873:1079742363",
199 * "price":30,
200 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5K Wq1QGP3fo6TOTSYD3TBQjuw/0",
201 * "product_code":"testingtesting",
202 * "ori_price":9000000,
203 * "quantity":800
204 * }
205 * ],
206 * "attrext":{
207 * "location":{
208 * "country":"中国",
209 * "province":"广东省",
210 * "city":"广州市",
211 * "address":"T.I.T创意园"
212 * },
213 * "isPostFree":0,
214 * "isHasReceipt":1,
215 * "isUnderGuaranty":0,
216 * "isSupportReplace":0
217 * },
218 * "delivery_info":{
219 * "delivery_type":0,
220 * "template_id":0,
221 * "express":[
222 * {"id":10000027,"price":100},
223 * {"id":10000028,"price":100},
224 * {"id":10000029,"price":100}
225 * ]
226 * }
227 * }
228 * ```
229 * Callback:
230 *
231 * - `err`, 调用失败时得到的异常
232 * - `result`, 调用正常时得到的对象
233 *
234 * Result:
235 * ```
236 * {
237 * "errcode": 0,
238 * "errmsg": "success"
239 * }
240 * ```
241 * @param {Object} goods 商品信息
242 * @param {Function} callback 回调函数
243 */
244exports.updateGoods = function (goods, callback) {
245 this.preRequest(this._updateGoods, arguments);
246};
247
248/*!
249 * 修改商品的未封装版本
250 */
251exports._updateGoods = function (goods, callback) {
252 var url = this.endpoint + '/merchant/update?access_token=' + this.token.accessToken;
253 this.request(url, postJSON(goods), wrapper(callback));
254};
255
256/**
257 * 查询商品
258 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
259 * Examples:
260 * ```
261 * api.getGoods(productId, callback);
262 * ```
263 * Callback:
264 *
265 * - `err`, 调用失败时得到的异常
266 * - `result`, 调用正常时得到的对象
267 *
268 * Result:
269 * ```
270 * {
271 * "errcode": 0,
272 * "errmsg": "success",
273 * "product_info":{
274 * "product_id":"pDF3iY6Kr_BV_CXaiYysoGqJhppQ",
275 * "product_base":{
276 * "name":"testaddproduct",
277 * "category_id":[537074298],
278 * "img":[
279 * "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"
280 * ],
281 * "property":[
282 * {"id":"品牌","vid":"Fujifilm/富⼠士"},
283 * {"id":"屏幕尺⼨寸","vid":"1.8英⼨寸"},
284 * {"id":"防抖性能","vid":"CCD防抖"}
285 * ],
286 * "sku_info":[
287 * {
288 * "id":"1075741873",
289 * "vid":[
290 * "1079742386",
291 * "1079742363"
292 * ]
293 * }
294 * ],
295 * "buy_limit":10,
296 * "main_img": 4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic 0FD3vN0V8PILcibEGb2fPfEOmw/0",
297 * "detail_html": "<div class=\"item_pic_wrp\" style= \"margin-bottom:8px;font-size:0;\"><img class=\"item_pic\" style= \"width:100%;\" alt=\"\" src=\"http://mmbiz.qpic.cn/mmbiz/ 4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic 0FD3vN0V8PILcibEGb2fPfEOmw/0\" ></div><p style=\"margin-bottom: 11px;margin-top:11px;\">test</p><div class=\"item_pic_wrp\" style=\"margin-bottom:8px;font-size:0;\"><img class=\"item_pic\" style=\"width:100%;\" alt=\"\" src=\"http://mmbiz.qpic.cn/mmbiz/ 4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655 ZK6ibnlibCCErbKQtReySaVA/0\" ></div><p style=\"margin-bottom: 11px;margin-top:11px;\">test again</p>"
298 * },
299 * "sku_list":[
300 * {
301 * "sku_id":"1075741873:1079742386",
302 * "price":30,
303 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
304 * "quantity":800,
305 * "product_code":"testing",
306 * "ori_price":9000000
307 * },
308 * {
309 * "sku_id":"1075741873:1079742363",
310 * "price":30,
311 * "icon_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
312 * "quantity":800,
313 * "product_code":"testingtesting",
314 * "ori_price":9000000
315 * }
316 * ],
317 * "attrext":{
318 * "isPostFree":0,
319 * "isHasReceipt":1,
320 * "isUnderGuaranty":0,
321 * "isSupportReplace":0,
322 * "location":{
323 * "country":"中国",
324 * "province":"广东省",
325 * "city":"⼲州市",
326 * "address":"T.I.T创意园"
327 * }
328 * },
329 * "delivery_info":{
330 * "delivery_type":1,
331 * "template_id":103312920
332 * }
333 * }
334 * }
335 * ```
336 * @param {String} productId 商品Id
337 * @param {Function} callback 回调函数
338 */
339exports.getGoods = function (productId, callback) {
340 this.preRequest(this._getGoods, arguments);
341};
342
343/*!
344 * 根据状态获取商品列表
345 */
346exports._getGoods = function (productId, callback) {
347 var url = this.endpoint + '/merchant/get?product_id=' + productId + '&access_token=' + this.token.accessToken;
348 this.request(url, {dataType: 'json'}, wrapper(callback));
349};
350
351/**
352 * 获取指定状态的所有商品
353 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
354 * Examples:
355 * ```
356 * api.deleteGoods(productId, callback);
357 * ```
358 * Callback:
359 *
360 * - `err`, 调用失败时得到的异常
361 * - `result`, 调用正常时得到的对象
362 *
363 * Result:
364 * ```
365 * {
366 * "errcode": 0,
367 * "errmsg": "success",
368 * "products_info": [
369 * {
370 * "product_base": ...,
371 * "sku_list": ...,
372 * "attrext": ...,
373 * "delivery_info": ...,
374 * "product_id": "pDF3iY-mql6CncpbVajaB_obC321",
375 * "status": 1
376 * }
377 * ]
378 * }
379 * ```
380 * @param {Number} status 状态码。(0-全部, 1-上架, 2-下架)
381 * @param {Function} callback 回调函数
382 */
383exports.getGoodsByStatus = function (status, callback) {
384 this.preRequest(this._getGoodsByStatus, arguments);
385};
386
387/*!
388 * 获取指定状态的所有商品的未封装版本
389 */
390exports._getGoodsByStatus = function (status, callback) {
391 var data = {status: status};
392 var url = this.endpoint + '/merchant/getbystatus?access_token=' + this.token.accessToken;
393 this.request(url, postJSON(data), wrapper(callback));
394};
395
396/**
397 * 商品上下架
398 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
399 * Examples:
400 * ```
401 * api.updateGoodsStatus(productId, status, callback);
402 * ```
403 * Callback:
404 *
405 * - `err`, 调用失败时得到的异常
406 * - `result`, 调用正常时得到的对象
407 *
408 * Result:
409 * ```
410 * {
411 * "errcode": 0,
412 * "errmsg": "success"
413 * }
414 * ```
415 * @param {String} productId 商品Id
416 * @param {Number} status 状态码。(0-全部, 1-上架, 2-下架)
417 * @param {Function} callback 回调函数
418 */
419exports.updateGoodsStatus = function (productId, status, callback) {
420 this.preRequest(this._updateGoodsStatus, arguments);
421};
422
423/*!
424 * 商品上下架的未封装版本
425 */
426exports._updateGoodsStatus = function (productId, status, callback) {
427 var data = {
428 product_id: productId,
429 status: status
430 };
431 var url = this.endpoint + '/merchant/modproductstatus?access_token=' + this.token.accessToken;
432 this.request(url, postJSON(data), wrapper(callback));
433};
434
435/**
436 * 获取指定分类的所有子分类
437 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
438 * Examples:
439 * ```
440 * api.getSubCats(catId, callback);
441 * ```
442 * Callback:
443 *
444 * - `err`, 调用失败时得到的异常
445 * - `result`, 调用正常时得到的对象
446 *
447 * Result:
448 * ```
449 * {
450 * "errcode": 0,
451 * "errmsg": "success"
452 * "cate_list": [!
453 * {"id": "537074292","name": "数码相机"},
454 * {"id": "537074293","name": "家⽤用摄像机"},
455 * {"id": "537074298",! "name": "单反相机"}
456 * ]
457 * }
458 * ```
459 * @param {Number} catId 大分类ID
460 * @param {Function} callback 回调函数
461 */
462exports.getSubCats = function (catId, callback) {
463 this.preRequest(this._getSubCats, arguments);
464};
465
466/*!
467 * 获取指定分类的所有子分类的未封装版本
468 */
469exports._getSubCats = function (catId, callback) {
470 var data = {
471 cate_id: catId
472 };
473 var url = this.endpoint + '/merchant/category/getsub?access_token=' + this.token.accessToken;
474 this.request(url, postJSON(data), wrapper(callback));
475};
476
477/**
478 * 获取指定子分类的所有SKU
479 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
480 * Examples:
481 * ```
482 * api.getSKUs(catId, callback);
483 * ```
484 * Callback:
485 *
486 * - `err`, 调用失败时得到的异常
487 * - `result`, 调用正常时得到的对象
488 *
489 * Result:
490 * ```
491 * {
492 * "errcode": 0,
493 * "errmsg": "success"
494 * "sku_table": [
495 * {
496 * "id": "1075741873",
497 * "name": "颜⾊色",
498 * "value_list": [
499 * {"id": "1079742375", "name": "撞⾊色"},
500 * {"id": "1079742376","name": "桔⾊色"}
501 * ]
502 * }
503 * ]
504 * }
505 * ```
506 * @param {Number} catId 大分类ID
507 * @param {Function} callback 回调函数
508 */
509exports.getSKUs = function (catId, callback) {
510 this.preRequest(this._getSKUs, arguments);
511};
512
513/*!
514 * 获取指定子分类的所有SKU的未封装版本
515 */
516exports._getSKUs = function (catId, callback) {
517 var data = {
518 cate_id: catId
519 };
520 var url = this.endpoint + '/merchant/category/getsku?access_token=' + this.token.accessToken;
521 this.request(url, postJSON(data), wrapper(callback));
522};
523
524/**
525 * 获取指定分类的所有属性
526 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
527 * Examples:
528 * ```
529 * api.getProperties(catId, callback);
530 * ```
531 * Callback:
532 *
533 * - `err`, 调用失败时得到的异常
534 * - `result`, 调用正常时得到的对象
535 *
536 * Result:
537 * ```
538 * {
539 * "errcode": 0,
540 * "errmsg": "success"
541 * "properties": [
542 * {
543 * "id": "1075741879",
544 * "name": "品牌",
545 * "property_value": [
546 * {"id": "200050867","name": "VIC&#38"},
547 * {"id": "200050868","name": "Kate&#38"},
548 * {"id": "200050971","name": "M&#38"},
549 * {"id": "200050972","name": "Black&#38"}
550 * ]
551 * },
552 * {
553 * "id": "123456789",
554 * "name": "颜⾊色",
555 * "property_value": ...
556 * }
557 * ]
558 * }
559 * ```
560 * @param {Number} catId 分类ID
561 * @param {Function} callback 回调函数
562 */
563exports.getProperties = function (catId, callback) {
564 this.preRequest(this._getProperties, arguments);
565};
566
567/*!
568 * 获取指定分类的所有属性的未封装版本
569 */
570exports._getProperties = function (catId, callback) {
571 var data = {
572 cate_id: catId
573 };
574 var url = this.endpoint + '/merchant/category/getproperty?access_token=' + this.token.accessToken;
575 this.request(url, postJSON(data), wrapper(callback));
576};
577