请稍等 ...
×

采纳答案成功!

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

公众号暂时无法提供服务,mediaId:undefined

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
128
//weixin.js
'use strict'
 
var config = require('./config')
var Wechat = require('./wechat/wechat')
 
var wechatApi = new Wechat(config.wechat)
 
 
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 = '哈哈,你订阅成功啦!\r\n欢迎来到draven_小凯的世界!\r\n输入任意数字会有意外惊喜哦~'
        }
        else if(message.Event === 'unsubscribe'){
            console.log('无情取关')
            this.body = ''
        }
        else if(message.Event === 'LOCATION'){
            this.body = '您上报的位置是:'+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 + ' 太复杂了'
 
        if(content === '1'){
            reply = '天下第一吃大米'
        }else if(content === '2'){
            reply = '天下第二吃豆腐'
        }else if(content === '3'){
            reply = '天下第三吃仙丹'
        }else if(content === '4'){
            reply = [{
                title:'技术改变世界',
                description:'我只是个描述而已',
                picUrl:'http://wx3.sinaimg.cn/mw690/e41455a1gy1fe07rkyhi2j20e50800tc.jpg',
                url:'https://github.com/'
            },{
                title:'Nodejs 开发微信',
                description:'就是这个feel倍儿爽!',
                picUrl:'http://wx2.sinaimg.cn/small/e41455a1gy1fe07ptwn36j20ch0783zo.jpg',
                url:'https://nodejs.org/'
            }]
        }else if(content === '5'){
            // console.log(__dirname)
            var data = yield (wechatApi.uploadMaterial('image',__dirname+'/2.jpg'))
            reply = {
                type:'image',
                mediaId:data.mediaId
                // mediaId:data.media_id
            }
            console.log(reply)
        }
        this.body = reply
    }
    yield next
//tpl.js
'use strict'
 
var ejs = require('ejs')
var heredoc = require('heredoc')
 
var tpl = heredoc(function(){/*
    <xml>
    <ToUserName><![CDATA[<%= toUserName %>]]></ToUserName>
    <FromUserName><![CDATA[<%= fromUserName %>]]></FromUserName>
    <CreateTime><%= createTime %></CreateTime>
    <MsgType><![CDATA[<%= msgType %>]]></MsgType>
    <% if(msgType==='text'){ %>
    <Content><![CDATA[<%- content %>]]></Content>
    <% }else if(msgType ==='image'){ %>
    <Image>
    <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
    </Image>
    <% }else if(msgType==='voice'){ %>
    <Voice>
    <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
    </Voice>
    <% }else if(msgType==='video'){ %>
    <Video>
    <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
    <Title><![CDATA[<%= content.title %>]]></Title>
    <Description><![CDATA[<%= content.description %>]]></Description>
    </Video>
    <% }else if(msgType==='music'){ %>
    <Music>
    <Title><![CDATA[<%= content.title %>]]></Title>
    <Description><![CDATA[<%= content.description %>]]></Description>
    <MusicUrl><![CDATA[<%= content.musicUrl %>]]></MusicUrl>
    <HQMusicUrl><![CDATA[<%= content.hqMusicUrl %>]]></HQMusicUrl>
    <ThumbMediaId><![CDATA[<%= content.thumbMediaId %>]]></ThumbMediaId>
    </Music>
    <% }else if(msgType==='news'){ %>
    <ArticleCount><%= content.length %></ArticleCount>
    <Articles>
    <% content.forEach(function(item){ %>
    <item>
    <Title><![CDATA[<%=item.title%>]]></Title> 
    <Description><![CDATA[<%=item.description%>]]></Description>
    <PicUrl><![CDATA[<%=item.picUrl%>]]></PicUrl>
    <Url><![CDATA[<%=item.url%>]]></Url>
    </item>
    <% }) %>
    </Articles>
    <% } %>
    </xml>
*/})
 
var compiled = ejs.compile(tpl)
exports = module.exports = {
    compiled:compiled
}
//wechat.js
1
 

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

插入代码

2回答

Scott 2017-03-27 13:00:40

说明拿到的 data 是不正确,也就是 执行上传这个环节有问题。


先把路径修改一下: __dirname+'/2.jpg' 改成 path.join(__dirname+'/2.jpg'),然后把 uploadMaterial 里面的返回内容打印一下:


var _data = response.body

console.log(_data)


我们看看这个 data 提示的是什么

0 回复 有任何疑惑可以回复我~
  • 提问者 dravenxiaokai #1
    _data打印出来undefined
    回复 有任何疑惑可以回复我~ 2017-03-29 12:16:54
  • Scott 回复 提问者 dravenxiaokai #2
    这么诡异呢,那打印这个 response 呢
    回复 有任何疑惑可以回复我~ 2017-03-29 16:01:51
  • 提问者 dravenxiaokai 回复 Scott #3
    内容有点多,不过最后一行是:body:{errcode:48001,errmsg:'api unauthorized hint:[0363vr45!]'}
    回复 有任何疑惑可以回复我~ 2017-03-30 00:07:22
提问者 dravenxiaokai 2017-03-26 19:50:16
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
//wechat.js
'use strict'
 
var Promise = require('bluebird')
var request = Promise.promisify(require('request'))
var util = require('./util')
var fs = require('fs')
var tpl = require('./tpl')
 
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()
}
 
Wechat.prototype.updateAccessToken = function(data) {
    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 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.uploadMaterial = function(type,filepath) {
    var that = this
    var form = {
        media:fs.createReadStream(filepath)
    }
 
    return new Promise(function(resolve,reject){
        that
        .fetchAccessToken()
        .then(function(data){
            var url = api.upload + 'access_token='+data.access_token+'&type='+type
            // console.log(url)
            request({method:'POST',url:url,formData:form,json:true})
            .then(function(response){
                // var _data = response[1] 这是个坑!
                var _data = response.body
                // console.log('hhhhhhhhhhhhhhhh'+_data.data)
                if(_data){
                    resolve(_data)
                }else{
                    throw new Error('Upload marterial fails')
                }
            })
            .catch(function(err){
                reject(err)
            })
        })
         
 
    }) 
}
Wechat.prototype.fetchAccessToken = function(data){
    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)
            data = JSON.parse(data)
        }catch(e){
            return that.updateAccessToken(data)
        }
        if(that.isValidAccessToken(data)){//判断票据是否有效
            // Promise.resolve(data)
            return Promise.resolve(data);//这里加return
        }else{
            return that.updateAccessToken()//否则更新票据
        }
    })
    .then(function(data){
        //存储access_token,expires_in
        that.access_token = data.access_token
        that.expires_in = data.expires_in
 
        that.saveAccessToken(data)
        return Promise.resolve(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.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


0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

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

帮助反馈 APP下载

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

公众号

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