关于4-5,4-6,4-7的一点疑问,以及测试fetchMaterial和countMaterial等方法出现的问题
来源:
jypblurWzx
2016-09-05
在学习4-5到4-7小节的过程中有一个地方不太明白,我用的微信测试号。
在uploadMaterial方法中声明了图片type是'pic',但实际传入永久素材的时候参数确实'image',神奇的是测试的时候运行正常,费解;
我用的测试号,按照视频在测试uploadMaterial,fetchMaterial,以及返回图文时候,发现按照老师的方法,可以获取到上传的media_id,但是拉取函数运行后日志打印出来是undifined,但是老师在视频中运行成功了,不知道那儿有问题,一一有对照,日志截图见后,
countMaterial是get请求,以及batchMaterial方法,但是却一直报41001错误,不知为何。
写回答
3回答
-
Scott
2016-09-05
传入的 type 应该是用来区分素材类型,可以比对下我这里的代码,看下有么有问题:
Wechat.prototype.updateAccessToken = function() { var appID = this.appID var appSecret = this.appSecret var url = api.accessToken + '&appid=' + appID + '&secret=' + appSecret return new Promise(function(resolve, reject) { request({url: url, json: true}).then(function(response) { var data = response[1] var now = (new Date().getTime()) var expires_in = now + (data.expires_in - 20) * 1000 data.expires_in = expires_in resolve(data) }) }) } Wechat.prototype.uploadMaterial = function(type, material, permanent) { var that = this var form = {} var uploadUrl = api.temporary.upload if (permanent) { uploadUrl = api.permanent.upload _.extend(form, permanent) } if (type === 'pic') { uploadUrl = api.permanent.uploadNewsPic } if (type === 'news') { uploadUrl = api.permanent.uploadNews form = material } else { form.media = fs.createReadStream(material) } return new Promise(function(resolve, reject) { that .fetchAccessToken() .then(function(data) { var url = uploadUrl + 'access_token=' + data.access_token if (!permanent) { url += '&type=' + type } else { form.access_token = data.access_token } var options = { method: 'POST', url: url, json: true } if (type === 'news') { options.body = form } else { options.formData = form } request(options).then(function(response) { var _data = response[1] if (_data) { resolve(_data) } else { throw new Error('Upload material fails') } }) .catch(function(err) { reject(err) }) }) }) } Wechat.prototype.fetchMaterial = function(mediaId, type, permanent) { var that = this var fetchUrl = api.temporary.fetch if (permanent) { fetchUrl = api.permanent.fetch } return new Promise(function(resolve, reject) { that .fetchAccessToken() .then(function(data) { var url = fetchUrl + 'access_token=' + data.access_token var form = {} var options = {method: 'POST', url: url, json: true} if (permanent) { form.media_id = mediaId form.access_token = data.access_token options.body = form } else { if (type === 'video') { url = url.replace('https://', 'http://') } url += '&media_id=' + mediaId } if (type === 'news' || type === 'video') { request(options).then(function(response) { var _data = response[1] if (_data) { resolve(_data) } else { throw new Error('fetch material fails') } }) .catch(function(err) { reject(err) }) } else { resolve(url) } }) }) }
如果从代码里看不出问题,可以先跳过这两个 API,往后面跟课程,跟完课程后,再回头看这个,我怀疑是测试号的稳定性问题是其一,代码问题是其二
012016-09-05 -
jypblurWzx
提问者
2016-09-05
00 -
jypblurWzx
提问者
2016-09-05
41001是缺少token,但是之前的的上传临时文件返回正常
00
相似问题