UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2
3// 库存管理接口
4var util = require('./util');
5var wrapper = util.wrapper;
6var postJSON = util.postJSON;
7
8/**
9 * 增加库存
10 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
11 * Examples:
12 * ```
13 * api.updateStock(10, productId, sku, callback); // 增加10件库存
14 * api.updateStock(-10, productId, sku, callback); // 减少10件库存
15 * ```
16 * Callback:
17 *
18 * - `err`, 调用失败时得到的异常
19 * - `result`, 调用正常时得到的对象
20 *
21 * Result:
22 * ```
23 * {
24 * "errcode": 0,
25 * "errmsg": "success"
26 * }
27 * ```
28 * @param {Number} number 增加或者删除的数量
29 * @param {String} productId 商品ID
30 * @param {String} sku SKU信息
31 * @param {Function} callback 回调函数
32 */
33exports.updateStock = function (number, productId, sku, callback) {
34 this.preRequest(this._updateStock, arguments);
35};
36
37/*!
38 * 更新商品库存的未封装版本
39 */
40exports._updateStock = function (number, productId, sku, callback) {
41 var url;
42 if (number > 0) {
43 url = this.endpoint + '/merchant/stock/add?access_token=' + this.token.accessToken;
44 } else {
45 url = this.endpoint + '/merchant/stock/reduce?access_token=' + this.token.accessToken;
46 }
47 var data = {
48 'product_id': productId,
49 'sku_info': sku,
50 'quantity': Math.abs(number)
51 };
52 this.request(url, postJSON(data), wrapper(callback));
53};