UNPKG

6.45 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.createGoodsGroup(groupName, productList, callback);
14 * ```
15 * Callback:
16 *
17 * - `err`, 调用失败时得到的异常
18 * - `result`, 调用正常时得到的对象
19 *
20 * Result:
21 * ```
22 * {
23 * "errcode": 0,
24 * "errmsg": "success",
25 * "group_id": 19
26 * }
27 * ```
28 * @param {String} groupName 分组名
29 * @param {Array} productList 该组商品列表
30 * @param {Function} callback 回调函数
31 */
32exports.createGoodsGroup = function (groupName, productList, callback) {
33 this.preRequest(this._createGoodsGroup, arguments);
34};
35
36exports._createGoodsGroup = function (groupName, productList, callback) {
37 var data = {
38 'group_detail': {
39 'group_name': groupName,
40 'product_list': productList && productList.length ? productList: []
41 }
42 };
43 var url = this.endpoint + '/merchant/group/add?access_token=' + this.token.accessToken;
44 this.request(url, postJSON(data), wrapper(callback));
45};
46
47/**
48 * 删除商品分组
49 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
50 * Examples:
51 * ```
52 * api.deleteGoodsGroup(groupId, callback);
53 * ```
54 * Callback:
55 *
56 * - `err`, 调用失败时得到的异常
57 * - `result`, 调用正常时得到的对象
58 *
59 * Result:
60 * ```
61 * {
62 * "errcode": 0,
63 * "errmsg": "success"
64 * }
65 * ```
66 * @param {String} groupId 分组ID
67 * @param {Function} callback 回调函数
68 */
69
70exports.deleteGoodsGroup = function (groupId, callback) {
71 this.preRequest(this._deleteGoodsGroup, arguments);
72};
73
74exports._deleteGoodsGroup = function (groupId, callback) {
75 var data = {
76 'group_id': groupId
77 };
78 var url = this.endpoint + '/merchant/group/del?access_token=' + this.token.accessToken;
79 this.request(url, postJSON(data), wrapper(callback));
80};
81
82/**
83 * 修改商品分组属性
84 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
85 * Examples:
86 * ```
87 * api.updateGoodsGroup(groupId, groupName, callback);
88 * ```
89 * Callback:
90 *
91 * - `err`, 调用失败时得到的异常
92 * - `result`, 调用正常时得到的对象
93 *
94 * Result:
95 * ```
96 * {
97 * "errcode": 0,
98 * "errmsg": "success"
99 * }
100 * ```
101 * @param {String} groupId 分组ID
102 * @param {String} groupName 分组名
103 * @param {Function} callback 回调函数
104 */
105exports.updateGoodsGroup = function (groupId, groupName, callback) {
106 this.preRequest(this._updateGoodsGroup, arguments);
107};
108
109exports._updateGoodsGroup = function (groupId, groupName, callback) {
110 var data = {
111 'group_id': groupId,
112 'group_name': groupName
113 };
114 var url = this.endpoint + '/merchant/group/propertymod?access_token=' + this.token.accessToken;
115 this.request(url, postJSON(data), wrapper(callback));
116};
117
118/**
119 * 修改商品分组内的商品
120 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
121 * Examples:
122 * ```
123 * api.updateGoodsForGroup(groupId, addProductList, delProductList, callback);
124 * ```
125 * Callback:
126 *
127 * - `err`, 调用失败时得到的异常
128 * - `result`, 调用正常时得到的对象
129 *
130 * Result:
131 * ```
132 * {
133 * "errcode": 0,
134 * "errmsg": "success"
135 * }
136 * ```
137 * @param {Object} groupId 分组ID
138 * @param {Array} addProductList 待添加的商品数组
139 * @param {Array} delProductList 待删除的商品数组
140 * @param {Function} callback 回调函数
141 */
142exports.updateGoodsForGroup = function (groupId, addProductList, delProductList, callback) {
143 this.preRequest(this._updateGoodsForGroup, arguments);
144};
145
146exports._updateGoodsForGroup = function (groupId, addProductList, delProductList, callback) {
147 var data = {
148 'group_id': groupId,
149 'product': []
150 };
151
152 if (addProductList && addProductList.length) {
153 addProductList.forEach(function (val) {
154 data.product.push({
155 'product_id': val,
156 'mod_action': 1
157 });
158 });
159 }
160
161 if (delProductList && delProductList.length) {
162 delProductList.forEach(function (val) {
163 data.product.push({
164 'product_id': val,
165 'mod_action': 0
166 });
167 });
168 }
169
170 var url = this.endpoint + '/merchant/group/productmod?access_token=' + this.token.accessToken;
171 this.request(url, postJSON(data), wrapper(callback));
172};
173
174/**
175 * 获取所有商品分组
176 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
177 * Examples:
178 * ```
179 * api.getAllGroups(callback);
180 * ```
181 * Callback:
182 *
183 * - `err`, 调用失败时得到的异常
184 * - `result`, 调用正常时得到的对象
185 *
186 * Result:
187 * ```
188 * {
189 * "errcode": 0,
190 * "errmsg": "success"
191 * "groups_detail": [
192 * {
193 * "group_id": 200077549,
194 * "group_name": "新品上架"
195 * },{
196 * "group_id": 200079772,
197 * "group_name": "全球热卖"
198 * }
199 * ]
200 * }
201 * ```
202 * @param {Function} callback 回调函数
203 */
204exports.getAllGroups = function (callback) {
205 this.preRequest(this._getAllGroups, arguments);
206};
207
208exports._getAllGroups = function (callback) {
209 var url = this.endpoint + '/merchant/group/getall?access_token=' + this.token.accessToken;
210 this.request(url, {dataType: 'json'}, wrapper(callback));
211};
212
213/**
214 * 根据ID获取商品分组
215 * 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
216 * Examples:
217 * ```
218 * api.getGroupById(groupId, callback);
219 * ```
220 * Callback:
221 *
222 * - `err`, 调用失败时得到的异常
223 * - `result`, 调用正常时得到的对象
224 *
225 * Result:
226 * ```
227 * {
228 * "errcode": 0,
229 * "errmsg": "success"
230 * "group_detail": {
231 * "group_id": 200077549,
232 * "group_name": "新品上架",
233 * "product_list": [
234 * "pDF3iYzZoY-Budrzt8O6IxrwIJAA",
235 * "pDF3iY3pnWSGJcO2MpS2Nxy3HWx8",
236 * "pDF3iY33jNt0Dj3M3UqiGlUxGrio"
237 * ]
238 * }
239 * }
240 * ```
241 * @param {String} groupId 分组ID
242 * @param {Function} callback 回调函数
243 */
244exports.getGroupById = function (groupId, callback) {
245 this.preRequest(this._getGroupById, arguments);
246};
247
248exports._getGroupById = function (groupId, callback) {
249 var data = {
250 'group_id': groupId
251 };
252 var url = this.endpoint + '/merchant/group/getbyid?access_token=' + this.token.accessToken;
253 this.request(url, postJSON(data), wrapper(callback));
254};