图片,音乐都可以上传。只有视频不行,视频格式MP4,小于10M。
//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 }
//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;