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); //ticket换取二维码图片
}
console.log('你订阅了这个号');
this.body = '哈哈,你订阅了这个号';
console.log(this.body);
} 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; //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://res.cloudinary.com/moveha/image/upload/v1441184110/assets/images/Mask-min.png',
url: 'http://www.baidu.com'
}]
} else if(content === '5') {
var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg');
reply = {
type: 'image',
mediaId: data.media_id
}
} else if(content === '6') {
var data = yield wechatApi.uploadMaterial('video', __dirname + '/6.mp4');
reply = {
type: 'video',
title: '回复视频内容',
description: '打篮球运动喽!',
mediaId: data.media_id
}
} else if(content === '7') {
var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg');
reply = {
type: 'music',
title: '回复音乐内容',
description: '网易云音乐,精品好听的音乐',
musicUrl: 'http://mpge.5nd.com/2015/2015-9-12/66325/1.mp3',
thumbMediaId: data.media_id
}
} else if(content === '8') {
var data = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg', {type: 'image'});
reply = {
type: 'image',
mediaId: data.media_id
}
} else if(content === '9') {
var data = yield wechatApi.uploadMaterial('video', __dirname + '/6.mp4', {type: 'video', description: '{"title": "Really a nice place","introduction":"Never think it so easy"}'});
console.log(data);
reply = {
type: 'video',
title: '回复视频内容',
description: '打篮球运动喽!',
mediaId: data.media_id
}
} else if(content === '10') {
var picData = yield wechatApi.uploadMaterial('image', __dirname + '/2.jpg', {}); //有了第三个参数,说明是是永久素材,为了拿到图片的素材id,为了上传图文
var media = {
articles: [{
title: 'lol',
thumb_media_id: picData.media_id,
author: 'lrn',
digest: '没有摘要',
show_cover_pic: 1,
content: '没有内容',
content_source_url: 'http://www.baidu.com'//阅读全文的url地址
},{
title: 'tututu5',
thumb_media_id: picData.media_id,
author: 'my',
digest: '没有摘要',
show_cover_pic: 1,
content: '没有内容',
content_source_url: 'https://github.com'
}]
}
data = yield wechatApi.uploadMaterial('news', media, {});
console.log(data.media_id);
data = yield wechatApi.fetchMaterial(data.media_id, 'news', {});
console.log(data);
var items = data.news_item; //news_item图文数组看微信api
var news = [];
console.log(items);
items.forEach(function(item) {
news.push({
title: item.title,
description: item.digest,
picUrl: picData.url,
url: item.url
})
})
reply = news;
}
this.body = reply;
}
yield next;
}
其实我在9的时候,有这样的问题:
超过了接口调用限制,是不是这个原因,导致我的饿素材上传不上去,所以获取的时候,就是undefined哇