请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

没有任何报错,视频素材上传不了。

图片,音乐都可以上传。只有视频不行,视频格式MP4,小于10M。

https://img1.sycdn.imooc.com/szimg//57668f9600016bf002420280.jpg

https://img1.sycdn.imooc.com/szimg//57668f100001b97906770602.jpg

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//weixin.js
'use strict'
var Wechat =require('./wechat/wechat');
var config = require('./config')
var path = require('path')
var wechatApi = new Wechat(config.wechat)
var filepath = {
    jpg2:path.join(__dirname,'/2.jpg'),
    video:path.join(__dirname,'./WoW.mp4'),
    jpg3:path.join(__dirname,'/3.jpg')
};
 
exports.reply = function *(next) {
    var message = this.weixin;
 
    if (message.MsgType === 'event') {
        if (message.Event === 'subscribe') {
            if (message.EventKey) {
                console.log('扫二维码进来' +message.EventKey+message.ticket)
            }
            this.body = '哈哈,订阅成功';
        }
         
        else if (message.Event === 'unsubscribe') {
            console.log('取消关注');
            this.body='1'
        }
        else if (message.Event === 'LOCATION') {
            console.log('取消关注')
            this.body = 'Your location is: '+message.Latitude;+'/'+
            message.longitude+message.Precision;
        }
        else if (message.Event === 'CLICK') {
            this.body= '你点击了餐单: '+message.EventKey;
        }
        else if (message.Event === 'SCAN') {
            console.log('关注二维码' +message.EventKey+message.Ticket);
            this.body = '看到了扫一下哈'
        }
        else if (message.Event === 'VIEW'){
            this.body  = '你点击了餐单中的链接:'+message.EventKey;
        }
         
    }
    else if (message.MsgType === 'text'){
        var content = message.Content;
        var reply = '你说的是 '+message.Content+'太复杂了'
        this.body  = '你点击了餐单中的链接:'+message.EventKey;
        if (content === '1') {
            reply = 'WoW';
        }
        else if (content === '2') {
            reply = 'Dota2';
        }
        else if (content === '3') {
            reply = 'LOL';
        }
        else if (content === '4') {
            reply = [{
                title:'技术改变世界',
                description:'只是个描述',
                picUrl:'http://img3.3lian.com/2013/v8/72/d/61.jpg',
                url:'https://www.baidu.com/'
            },{
                title:'湖和小船',
                description:'风景',
                picUrl:'http://img01.taopic.com/150116/240448-15011610051136.jpg',
                url:'www.sina.com'
            }];
        }
        else if (content === '5') {
            var data = yield wechatApi.uploadMaterial('image',filepath.jpg2)
             
            reply ={
                type:'image',
                media_id: data.media_id
            };
        }
        else if (content === '6'){
            var data = yield wechatApi.uploadMaterial('video',filepath.video)
            console.log(data);
            reply ={
                type:'video',
                title:'WoW',
                description:'Wars of World',
                media_id: data.media_id
            };
        }
        else if (content === '7'){
            var data = yield wechatApi.uploadMaterial('image',filepath.jpg3)
            console.log(da)
            reply ={
                type:'music',
                title:'一声所爱',
                description:'大话西游',
                MusicURL:'http://music.163.com/#/m/song?id=32785700',
                thumbMediaId:data.media_id
            };
        }
        this.body = reply;
    }
    yield next
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//wechat.js
'use strict'
 
var Promise=require('bluebird');
var request=Promise.promisify(require('request'));
var prefix='https://api.weixin.qq.com/cgi-bin/';
var fs = require('fs')
var util = require('./util')
var api={
    accessToken:prefix+'token?grant_type=client_credential',
    upload:prefix+'media/upload?'
}
 
 
function Wechat(opts){
     
    this.appID=opts.appID;
    this.appsecret=opts.appsecret;
    this.getAccessToken=opts.getAccessToken;
    this.saveAccessToken=opts.saveAccessToken;
 
    this.fetchAccessToken()
}
 
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.fetchAccessToken = function() {
    var that = this;
    if (this.access_token && this.expires_in) {
        if (this.isValidAccessToken(this)) {
            return Promise.resolve(this)
        }
    }
 
    this.getAccessToken()
        .then(function(data){
            try{
                data=JSON.parse(data);
            }
            catch(e){
                return that.updataAccessToken()
            }
            if (that.isValidAccessToken(data)) {
                return Promise.resolve(data)
            }else{
                return that.updataAccessToken()
            }
        })
        .then(function(data){
            that.access_token=data.access_token;
            that.expires_in=data.expires_in;
            that.saveAccessToken(data);
             
            return Promise.resolve(data);
        })
 
}
Wechat.prototype.uploadMaterial = function(type,filepath) {
     
    var form ={
        media:fs.createReadStream(filepath)
    }
    var that = this;
 
      return new Promise(function(resolve, reject) {
        that.fetchAccessToken()
            .then(function(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;
                         
                        if (_data) {
                            resolve(_data);
                        else {
                            throw new Error("Upload meterial fails");
                        }
                    })
                    .catch(function(err) {
                        reject(err)
                    })
  
            })
    })
}
 
Wechat.prototype.updataAccessToken = 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.body;
             
            var now = (new Date().getTime())
 
            var expires_in = now+(data.expires_in-20)*1000
            data.expires_in = expires_in;
            resolve(data)
        })
    })
}
 
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;


正在回答 回答被采纳积分+3

插入代码

6回答

Scott 2016-06-20 08:31:31

这个跟家里的 wifi,宽带服务上提供的上下行速度也有关系,之前遇到过一个案例,同学把家里的宽带商换成了另外一家,问题神奇解决。

0 回复 有任何疑惑可以回复我~
提问者 甫里 2016-06-19 22:06:42

貌似视频稍微超过4M就不行了~

1 回复 有任何疑惑可以回复我~
惟独爱衣 2017-11-03 01:04:05

一样的错误啊........

0 回复 有任何疑惑可以回复我~
Apieceofcake 2017-03-12 23:54:47

我是只有文字和图片可以,音乐和视频都干不动,xml数据半天才打出来,网速自我感觉还可以啊,想不通啊

0 回复 有任何疑惑可以回复我~
olango 2016-09-22 18:20:40

之前我也是这样,我是模板那里定义有问题,你有可能吗?检查一下定义的跟模板的是不是不一样?


0 回复 有任何疑惑可以回复我~
  • 您好,我的也是这个问题,跟了几天课程遇到了太多的坑,你那有源码么,能发我下么(2653651700@qq.com),多谢了!!
    回复 有任何疑惑可以回复我~ 2017-05-22 16:51:27
一头飞奔的猪 2016-06-21 21:49:45

我跟你的情况一样。音乐还放不出来

0 回复 有任何疑惑可以回复我~
  • 提问者 甫里 #1
    选个小一点的文件试试
    回复 有任何疑惑可以回复我~ 2016-06-22 11:15:02
  • 一头飞奔的猪 回复 提问者 甫里 #2
    2.25M的
    回复 有任何疑惑可以回复我~ 2016-06-22 11:30:59
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号