//使用mongodb来储存sesion信息 var cookieParser = require('cookie-parser'); var session = require("express-session"); var MongoStore = require('connect-mongo')(session);
var flash = require('connect-flash') //存储变量的中间件,相当全局变量 app.use(flash())
使用 var error = req.flash('error')
logger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var logger = require('morgan'); //处理日志模块
//日志文件 var fs = require('fs'); var accessLogfile = fs.createWriteStream('access.log', {flags:'a'}); var errorLogfile = fs.createWriteStream('error.log', {flags:'a'});
doctype html html head meta(charset="utf-8") title #{title} - imoocMovie include ./includes/head body include ./includes/header block content include ./includes/footer
var sha1 = require('sha1') var getRawBody = require('raw-body') varWechat = require('./wechat') var util = require('./util')
module.exports = function(opts, handler) { var wechat = newWechat(opts)
returnfunction *(next){
var token = opts.token var signature = this.query.signature var nonce = this.query.nonce var timestamp = this.query.timestamp var echostr = this.query.echostr var str = [token, timestamp, nonce].sort().join('') var sha = sha1(str)
if(type === 'news' || type === 'video'){ request(options, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete material fails') } }) } else{ resolve(url) }
})
}) }
Wechat.prototype.deleteMaterial = function(mediaId){ var that = this var form = { media_id:mediaId } returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.permanent.del + 'access_token=' + data.access_token + '&media_id=' + mediaId
request({method:'POST', url: url, body:form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete material fails') } }) }) }) }
Wechat.prototype.updateMaterial = function(mediaId, news){ var that = this var form = { media_id:mediaId }
_.extend(form, news) returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.permanent.update + 'access_token=' + data.access_token + '&media_id=' + mediaId
request({method:'POST', url: url, body:form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete material fails') } }) }) }) }
Wechat.prototype.countMaterial = function(){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.permanent.count + 'access_token=' + data.access_token
request({method:'GET', url: url, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('count material fails') } }) }) }) }
Wechat.prototype.batchMaterial = function(options){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.permanent.batch + 'access_token=' + data.access_token
request({method:'POST', url: url, body:options, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('batch material fails') } }) }) }) }
//用户分组 Wechat.prototype.createGroup = function(name){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.group.create + 'access_token=' + data.access_token var form = { group: { name: name } }
request({method:'POST', url: url, body:form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('create group fails') } }) }) }) }
Wechat.prototype.fetchGroup = function(){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.group.fetch + 'access_token=' + data.access_token
request({url: url, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('fetch group fails') } }) }) }) }
Wechat.prototype.checkGroup = function(openId){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.group.check + 'access_token=' + data.access_token var form = { openid : openId }
request({method:'POST', url: url, body: form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('check group fails') } }) }) }) }
Wechat.prototype.updateGroup = function(id, name){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.group.update + 'access_token=' + data.access_token var form = { group: { id: id, name: name } }
request({method:'POST', url: url, body: form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('update group fails') } }) }) }) }
Wechat.prototype.moveGroup = function(openIds, to_groupid){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url var form = { to_groupid : to_groupid }
request({method:'POST', url: url, body: form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('move group fails') } }) }) }) }
Wechat.prototype.deleteGroup = function(id){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.group.del + 'access_token=' + data.access_token var form = { group: { id: id } }
request({method:'POST', url: url, body: form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete group fails') } }) }) }) }
//获取用户信息 Wechat.prototype.remarkUser = function(openId, remark){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.user.remark + 'access_token=' + data.access_token var form = { openid: openId, remark: remark }
request({method:'POST', url: url, body: form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('remark user fails') } }) }) }) }
Wechat.prototype.fetchUser = function(openIds, lang){ var that = this var lang = lang || 'zh_CN'
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var options = { json: true }
Wechat.prototype.sendByGroup = function(type, message, groupId){ var that = this var msg = { filter:{}, msgtype:type } msg[type] = message if(!groupId){ msg.filter.is_to_all = true }else{ msg.filter.is_to_all = false msg.filter.group_id = groupId }
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.mass.group + 'access_token=' + data.access_token
request({method:'POST', url: url,body:msg, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('sell by group fails') } }) }) }) }
Wechat.prototype.sendByOpenId = function(type, message, openIds){ var that = this
var msg = { touser:openIds, msgtype:type }
msg[type] = message
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.mass.openId + 'access_token=' + data.access_token
request({method:'POST', url: url, body:msg, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('sell by openid fails') } }) }) }) }
Wechat.prototype.deleteMass = function(msgId){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.mass.del + 'access_token=' + data.access_token var form = { msg_id : msgId }
request({method:'POST', url: url,body:form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete Mass fails') } }) }) }) }
Wechat.prototype.previewMass = function(type, message, openId){ var that = this var msg = { touser:openId, msgtype:type } msg[type] = message
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.mass.preview + 'access_token=' + data.access_token
request({method:'POST', url: url,body:msg, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('preview mass fails') } }) }) }) }
Wechat.prototype.checkMass = function(msgId){ var that = this var form = { msg_id : msgId }
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.mass.check + 'access_token=' + data.access_token
request({method:'POST', url: url,body:form, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('check mass fails') } }) }) }) }
Wechat.prototype.createMenu = function(menu){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.menu.create + 'access_token=' + data.access_token
request({method:'POST', url: url,body:menu, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('create menu fails') } }) }) }) }
Wechat.prototype.getMenu = function(){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.menu.get + 'access_token=' + data.access_token
request({url: url, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('get menu fails') } }) }) }) }
Wechat.prototype.deleteMenu = function(){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.menu.del + 'access_token=' + data.access_token
request({url: url, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('delete menu fails') } }) }) }) }
Wechat.prototype.currentSelfMenu = function(){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.menu.current + 'access_token=' + data.access_token
request({url: url, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('get current selfmenu fails') } }) }) }) }
Wechat.prototype.semantic = function(semanticData){ var that = this
returnnewPromise(function(resolve, reject){ that .fetchAccessToken() .then(function(data){ var url = api.semantic + 'access_token=' + data.access_token semanticData.appid = data.appID
request({method:'POST', url: url,body:semanticData, json: true}, function (error, response, body) { if (!error && response.statusCode === 200) { var _data = body; if(_data){ resolve(_data); } } else { thrownewError('semantic data fails') } }) }) }) }
Wechat.prototype.reply = function(){ //实现完整的回复逻辑,放回给客户端xml格式数据 var content = this.body//此时的this.body是json数据格式 var message = this.weixin var xml = util.tpl(content, message) //将content与message中的内容提出整合,此时已经变成了xml格式 //console.log('xml:'+ xml)
this.status = 200 this.type = 'application/xml' this.body = xml
var media = { articles:[{ title:'angularjs入门', thumb_media_id:picData.media_id, show_cover_pic:1, author:'Leo', digest:'关于angularjs摘要', content:'augular是目前前端最火框架之一', content_source_url:'https://docs.angularjs.org/' } ] }
var data = yield wechatApi.uploadMaterial('news', media, {}) console.log('我要的mediaId:'+ data.media_id) data = yield wechatApi.fetchMaterial(data.media_id, 'news', {})