//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