weixin.js中的上下文没有拿到
来源:
慕粉3173836
2016-04-18
报了这个错
就把g.js和weixin.js相关值打印出来打印出来
得到下面的结果:
看了上一个同学的回答,变量应该没拼错吧.而且我也试过改成yield* handler.call(this, next),也是报同样的错,this上下文好像没有改变,附上代码,方便调试:
g.js
// 中间件 var sha1 = require("sha1") // 帮助转化XML的模块 var getRawBody = require("raw-body") var Wechat = require("./wechat") var util = require("./util") module.exports = function(opts,handler) { // 实例WeChat是处理票据检查,票据更新,opts 为appjs传入的config var wechat = new Wechat(opts) // 中间件是处理数据等等 return function *(next) { // 请求参数this.query console.log(this.query) var that = this 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("") // sha1加密 var sha = sha1(str) // 判断微信是验证还是请求数据 if (this.method === "GET") { if (sha === signature) { // 微信的请求 this.body = echostr + "" } else { // 非微信请求 this.body = "wrong" } } else if (this.method === "POST") { if (sha !== signature) { // 非微信请求 this.body = "wrong" return false } // 处理微信请求的数据 var data = yield getRawBody(this.req, { // buffer的配置项 length: this.length, limit: "1mb", encoding: this.charset }) // 解析XML var content = yield util.parseXMLAsync(data) console.log(content) // 对json的value值进行格式化 var message = util.formatMessage(content.xml) console.log(message) // 将解析后的json挂载到this上,以便后边能拿到这个json this.weixin = message console.log("this.weixin = " + this.weixin) console.log("messagetype = " + this.weixin.MsgType) // 暂停这里,通过业务层处理返回变量,通过call来改变上下文 yield handler.call(this, next) // 业务层处理完毕,返回这里 wechat.reply.call(this) //if (message.MsgType === "event") { // if (message.Event === "subscribe"){ // var now = new Date().getTime() // // 设置回复消息 // that.status = 200 // that.type = "application/xml" // // CDATA里的内容将不会当做XML来解析 // var reply = "<xml>"+ // "<ToUserName><![CDATA["+ message.FromUserName + "]]></ToUserName>" + // "<FromUserName><![CDATA[" + message.ToUserName + "]]></FromUserName>" + // "<CreateTime>" + now + "</CreateTime>" + // "<MsgType><![CDATA[text]]></MsgType>"+ // "<Content><![CDATA[你好]]></Content>"+ // "</xml>" // that.body = reply // console.log(reply) // return // } //} } } }
weixin.js
"use strict" // 自动回复设置 exports.reply = function* (next) { var message = this.weixin console.log("微信的Messge : " + this.weixin) if (message.MsgType === "event") { if (message.Event === "subscribe") { if (message.EventKey) { console.log("扫二维码进来: " + message.EventKey + " " + message.ticket) } this.body = "您关注了订阅号 消息ID: " } 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") { // 这里的eventkey是URL地址 this.body = "您点击了菜单中的链接 : " + message.EventKey } } else if (message.MsgType === "text") { var content = message.Content var reply = "额,你说的 " + message.Content + " 太复杂了" } yield next }
------------------------------------------------------------分割线------------------------------------------------------------------
app.js
'use strict' var Koa = require("Koa") var path = require("path") var wechat = require("./wechat/g") var util = require("./libs/util") var config = require("./config") var weixin = require("./weixin") var wechat_file = path.join(__dirname, "./config/wechat.txt") var app = new Koa() // 使用中间件 app.use(wechat(config.wechat, weixin.reply)) app.listen(3001) console.log("listening: 3001")
写回答
1回答
-
Scott
2016-04-18
中间件的入口代码也贴一下吧
012016-04-18
相似问题