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