scott 老师您好,在上传临时素材上出现了问题,图片素材上传正常,video 上传出现了错误,感觉发生了3次请求。如何解决
来源:
albion
2016-04-25
listen :1234 { xml: { ToUserName: [ 'gh_8118b29abfcf' ], FromUserName: [ 'oHp_cwig7eK9M2o5x8J1Cdgd1dEA' ], CreateTime: [ '1461570902' ], MsgType: [ 'text' ], Content: [ '4' ], MsgId: [ '6277399225284041633' ] } } { xml: { ToUserName: [ 'gh_8118b29abfcf' ], FromUserName: [ 'oHp_cwig7eK9M2o5x8J1Cdgd1dEA' ], CreateTime: [ '1461570902' ], MsgType: [ 'text' ], Content: [ '4' ], MsgId: [ '6277399225284041633' ] } } { xml: { ToUserName: [ 'gh_8118b29abfcf' ], FromUserName: [ 'oHp_cwig7eK9M2o5x8J1Cdgd1dEA' ], CreateTime: [ '1461570902' ], MsgType: [ 'text' ], Content: [ '4' ], MsgId: [ '6277399225284041633' ] } }
6回答
-
Wechat.prototype.fetchAccessToken =function(data){ var that = this ; // console.log(this.access_token); if (this.access_token && this.expires_in){ if(this.isValidAccessToken(this)){ return Promise.resolve(this); } } this.getAccessToken()
这里的 this.getAccessToken() 需要被 return 回去,可以看这个回答: http://coding.imooc.com/learn/questiondetail/131.html
另外,如果出错,有没有命令行的出错信息的,出错信息也可以贴出来,光看代码,暂时没看到哪里不对,把 wechat ticket txt 里面生成的内容也都删除,可以再试试,如果代码没问题,不排除测试账号本身不稳定造成上传失败
022016-04-25 -
joking_zhang
2016-10-04
我是先后尝试上传8MB 的视频 ,3MB的视频,1MB的视频,以及老师打篮球的视频(不到1MB)。
发现只有老师打篮球的视频上传成功了...
呵呵呵~~~
老师,请问你对于处理这类大视频自动回复的问题,除了处理素材的体积,在服务器端有什么好的应对方法吗?142017-05-24 -
慕莱坞8178471
2017-05-23
多次试验过后 在没有修改代码的情况下 我发现是网络的问题或者是ngrok问题 前面都是莫名其妙的三次的请求 最后一次显示出来了视频 说明代码没问题
00 -
albion
提问者
2016-04-25
else if (content === "4"){
var data = yield wechatApi.uploadMaterial('video',path.join(__dirname+'/3.mp4'));
// console.log(data.media_id)
reply = {
type:'video',
mediaId: data.media_id,
title:'1111',
description:'2222'
}
}
00 -
albion
提问者
2016-04-25
<% } else if (msgType === 'video') { %>
<Video>
<MediaId><![CDATA[<%=content.mediaId%>]]></MediaId>
<Title><![CDATA[<%=content.title%>]]></Title>
<Description><![CDATA[<%=content.description%>]]></Description>
</Video>
00 -
albion
提问者
2016-04-25
'use strict'
var Promise =require('bluebird');
var request = Promise.promisify(require('request'));
var util=require('./util');
var fs = require('fs');
var prefix='https://api.weixin.qq.com/cgi-bin/'
var api = {
accessToken:prefix + 'token?grant_type=client_credential',
upload: prefix + 'media/upload?'
}
function Wechat(opts){
var that = this;
this.appId = opts.appId;
this.appSecret = opts.appSecret;
this.getAccessToken = opts.getAccessToken;
this.saveAccessToken = opts.saveAccessToken;
this.fetchAccessToken();
// console.log(opts);
// console.log(this);
// console.log(that);
// console.log(JSON.parse(this.getAccessToken));
// console.log('W'+opts.getAccessToken());
// console.log(JSON.stringify(this))
}
Wechat.prototype.fetchAccessToken =function(data){
var that = this ;
// console.log(this.access_token);
if (this.access_token && this.expires_in){
if(this.isValidAccessToken(this)){
return Promise.resolve(this);
}
}
this.getAccessToken()
.then(function(data){
try{
data =JSON.stringify(data);
// console.log(JSON.stringify(data));
}
catch(e){
return that.updateAccessToken();
// console.log('update'+data);
}
if(that.isValidAccessToken(data)){
return Promise.resolve(data);
// console.log('is'+data);
}
else{
return that.updateAccessToken(data);
// console.log('update1'+data);
}
})
.then(function(data){
// console.log(data);
that.access_token=data.access_token;
that.expires_in =data.expires_in;
// console.log('111'+data);
that.saveAccessToken(data);
return Promise.resolve(data);
// console.log(data);
})
}
Wechat.prototype.isValidAccessToken =function(data){
if(!data||!data.access_token||!data.expires_in){
return false;
}
var access_token=data.access_token;
var expires_in=data.expires_in;
var now=(new Date().getTime());
if(now < expires_in){
return true;
}
else{
return false;
}
}
Wechat.prototype.updateAccessToken = function(){
var appId= this.appId;
var appSecret= this.appSecret;
var url = api.accessToken + '&appid=' + appId + '&secret=' + appSecret;
// console.log(url);
return new Promise(
function(resolve,reject){
request({url:url,json:true}).then(function(response){
var data= response.body;
var now = (new Date().getTime());
// console.log(data+'|'+now);
var expires_in = now + (data.expires_in - 20)*1000;
data.expires_in = expires_in;
resolve(data);
// console.log(data);
})
})
}
Wechat.prototype.uploadMaterial = function(type, filepath){
var that = this;
// console.log(this)
var form = {
media: fs.createReadStream(filepath),
}
// console.log(form);
return new Promise(
function(resolve,reject){
that
.fetchAccessToken()
.then(function(data){
// console.log(data)
var url = api.upload + 'access_token=' + data.access_token + '&type='+type;
request({method:'POST', url:url,formData:form,json:true} )
.then(function(response){
var _data = response.body;
console.log(_data.media_id)
if(_data){
resolve(_data);
// console.log(_data);
}
else{
throw new Error('Upload错误')
}
})
.catch(function(err){
reject(err);
})
})
})
}
Wechat.prototype.reply = function(){
var content = this.body;
var message = this.weixin;
var xml = util.tpl(content,message);
this.status = 200;
this.type = 'application/xml';
this.body = xml;
}
module.exports=Wechat;00
相似问题