公众号暂时无法提供服务,mediaId:undefined
来源:4-4 第四天 上传临时素材之图片和视频
dravenxiaokai
2017-03-26
//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
写回答
2回答
-
Scott
2017-03-27
说明拿到的 data 是不正确,也就是 执行上传这个环节有问题。
先把路径修改一下: __dirname+'/2.jpg' 改成 path.join(__dirname+'/2.jpg'),然后把 uploadMaterial 里面的返回内容打印一下:
var _data = response.body
console.log(_data)
我们看看这个 data 提示的是什么
072017-04-01 -
dravenxiaokai
提问者
2017-03-26
//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
00
相似问题
新增临时图片素材,公众号提示无法提供服务
回答 3
回复图片失败
回答 2