-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathapi_shop_common.js
49 lines (46 loc) · 1.26 KB
/
api_shop_common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
var path = require('path');
var fs = require('fs');
var util = require('./util');
var wrapper = util.wrapper;
/**
* 上传图片
* 详细请看:<http://mp.weixin.qq.com/wiki/8/703923b7349a607f13fb3100163837f0.html>
* Examples:
* ```
* api.uploadPicture('/path/to/your/img.jpg', callback);
* ```
* Callback:
*
* - `err`, 调用失败时得到的异常
* - `result`, 调用正常时得到的对象
*
* Result:
* ```
* {
* "errcode": 0,
* "errmsg": "success"
* "image_url": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2ibl4JWwwnW3icSJGqecVtRiaPxwWEIr99eYYL6AAAp1YBo12CpQTXFH6InyQWXITLvU4CU7kic4PcoXA/0"
* }
* ```
* @param {String} filepath 文件路径
* @param {Function} callback 回调函数
*/
exports.uploadPicture = function (filepath, callback) {
this.preRequest(this._uploadPicture, arguments);
};
/*!
* 更新商品库存的未封装版本
*/
exports._uploadPicture = function (filepath, callback) {
var basename = path.basename(filepath);
var url = this.endpoint + '/merchant/common/upload_img?access_token=' +
this.token.accessToken + '&filename=' + basename;
var reader = fs.createReadStream(filepath);
var opts = {
dataType: 'json',
type: 'POST',
stream: reader
};
this.request(url, opts, wrapper(callback));
};