采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
wechat.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 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 'use strict' var Promise = require( 'bluebird' ) var request=Promise.promisify(require( 'request' )) //promise化 var util = require( './util' ) var fs=require( 'fs' ) var prefix= 'https://api.weixin.qq.com/cgi-bin/' var api={ access_token: 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 //promise实现 this .fetchAccessToken() } Wechat.prototype.fetchAccessToken = function (data) { var that= this //如果有效 if ( this .access_token&& this .expires_in){ if ( this .isValidAccessToken( this )){ //console.log('data.access_token') //console.log(data.access_token) return Promise.resolve( this ) } } //promise实现 return this .getAccessToken() .then( function (data){ try { data=JSON.parse(data) console.log( 'fetch_data::::::' ) console.log(data) console.log( 'fetch_accesstoken::::::' +data.access_token) } catch (e){ return that.updateAccessToken() } if (that.isValidAccessToken(data)) { return Promise.resolve(data) } else { return that.updateAccessToken() } }) .then( function (data) { that.access_token=data.access_token that.expires_in=data.expires_in //console.log(that.access_token) //console.log(that.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.updateAccessToken = function () { //???data var appID= this .appID var appSecret= this .appSecret var url=api.access_token+ '&appID=' +appID+ '&secret=' +appSecret return new Promise( function (resolve,reject){ //向服务器发起get请求 request({url: url,json: true }).then( function (response){ var data =response //修改 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) { //???data var that= this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise( function (resolve,reject){ that .fetchAccessToken() .then( function (data){ //console.log(data) //console.log(data.access_token) var url=api.upload+ 'access_token=' +data.access_token+ '&type=' +type console.log(url) //向服务器发起get请求 request({method: 'POST' ,url: url,formData:form,json: true }).then( function (response){ var _data =response //console.log(response[1]) //console.log("__________________________________________________________") //console.log(response.body) //console.log(_data) if (_data){ resolve(_data) } else { throw new Error( 'upload material fails' ) } }) . catch ( function (err){ reject(err) }) }) }) } Wechat.prototype.reply = function () { var content = this .body var message = this .weixin console.log(content); var xml = util.tpl(content,message) this .status = 200 this .type = 'application/xml' this .body = xml }; module.exports=Wechat |
恩,那就是 response.body 是对的,用 response.body 吧,另外, Wechat.prototype.uploadMaterial = function(type,filepath) console.log(data) console.log(data.access_token) 这里的 console 的 data 是对的,但是 data.access_token 是 undefined,你再查查为什么通过 data 获取不到里面的 access_token ,是哪里代码写错了,还是 data 不是一个 json 对象呢
我自己打印了一下,发现uploadMaterial能获取到data,但是无法得到票据access_token,不知道是什么原因,我将wechat的代码贴上来麻烦大神帮我看看~
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 'use strict' var Promise = require( 'bluebird' ) var request=Promise.promisify(require( 'request' )) //promise化 var util = require( './util' ) var fs=require( 'fs' ) var prefix= 'https://api.weixin.qq.com/cgi-bin/' var api={ access_token: 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 //promise实现 this .fetchAccessToken() } Wechat.prototype.fetchAccessToken = function (data) { var that= this //如果有效 if ( this .access_token&& this .expires_in){ if ( this .isValidAccessToken( this )){ return Promise.resolve( this ) } } //promise实现 return this .getAccessToken() .then( function (data){ try { data=JSON.parse(data) } catch (e){ return that.updateAccessToken() } if (that.isValidAccessToken(data)) { return promise.resolve(data) } else { return that.updateAccessToken() } }) .then( function (data) { 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.updateAccessToken = function () { //???data var appID= this .appID var appSecret= this .appSecret var url=api.access_token+ '&appID=' +appID+ '&secret=' +appSecret return new Promise( function (resolve,reject){ //向服务器发起get请求 request({url: url,json: true }).then( function (response){ var data =response //修改 //console.log(data) 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) { //???data var that= this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise( function (resolve,reject){ that .fetchAccessToken() .then( function (data){ console.log(data) console.log(data.access_token) var url=api.upload+ 'access_token=' +data.access_token+ '&type=' +type console.log(url) //向服务器发起get请求 request({method: 'POST' ,url: url,formData:form,json: true }).then( function (response){ var _data =response //console.log(_data) if (_data){ resolve(_data) } else { throw new Error( 'upload material fails' ) } }) . catch ( function (err){ reject(err) }) }) }) } Wechat.prototype.reply = function () { var content = this .body var message = this .weixin console.log(content); var xml = util.tpl(content,message) this .status = 200 this .type = 'application/xml' this .body = xml }; module.exports=Wechat |
tpl.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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | '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 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 'use strict' var Promise = require( 'bluebird' ) var request=Promise.promisify(require( 'request' )) //promise化 var util = require( './util' ) var fs=require( 'fs' ) var prefix= 'https://api.weixin.qq.com/cgi-bin/' var api={ access_token: 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 //promise实现 this .fetchAccessToken() } Wechat.prototype.fetchAccessToken = function (data) { var that= this //如果有效 if ( this .access_token&& this .expires_in){ if ( this .isValidAccessToken( this )){ return Promise.resolve( this ) } } //promise实现 return this .getAccessToken() .then( function (data){ try { data=JSON.parse(data) } catch (e){ return that.updateAccessToken(data) } if (that.isValidAccessToken(data)) { return promise.resolve(data) } else { return that.updateAccessToken() } }) .then( function (data) { 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.updateAccessToken = function () { //???data var appID= this .appID var appSecret= this .appSecret var url=api.access_token+ '&appID=' +appID+ '&secret=' +appSecret return new Promise( function (resolve,reject){ //向服务器发起get请求 request({url: url,json: true }).then( function (response){ var data =response //修改 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) { //???data var that= this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise( function (resolve,reject){ that .fetchAccessToken() .then( function (data){ var url=api.upload+ 'access_token=' +data.access_token+ '&type=' +type //向服务器发起get请求 request({method: 'POST' ,url: url,formData:form,json: true }).then( function (response){ var _data =response[1] console.log(_data) if (_data){ resolve(_data) } else { throw new Error( 'upload material fails' ) } }) . catch ( function (err){ reject(err) }) }) }) } Wechat.prototype.reply = function () { var content = this .body var message = this .weixin console.log(content); var xml = util.tpl(content,message) this .status = 200 this .type = 'application/xml' this .body = xml }; module.exports=Wechat |
weixin.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 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 | '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' + '消息ID:' +message.MsgId } 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 //菜单的URL地址 } } 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://a0.att.hudong.com/50/85/20300534894500139519859925271.jpg' , url: 'https://console.qcloud.com/cvm' }] } else if (content=== '5' ){ var data=yield wechatApi.uploadMaterial( 'image' ,__dirname+ '/2.jpg' ) reply={ type: 'image' , mediaId: data.media_id } //console.log(reply) } this .body=reply } yield next } |