请导师或同学帮忙,解答一下,非常感谢!问题如下:问题1:为什么这里的yield下面 console.log()都能打印处理,这里并没有调用next,按照es6的理解是需要手动调用next才会往下执行的???问题2:如果把 var content = yield util.parseXMLAsync(data);中yield去掉,打印处理则是一个Promise对象,如下所示:加上yield则打印出具体的内容,如下图所示:在app.js文件有代码如下:'use strict'var Koa = require('koa');var path = require('path');var util = require('./libs/util.js');var wechar = require('./wechar/g.js');var config = require('./config.js');var weixin = require('./weixin.js'); var wechar_file = path.join(__dirname,'./config/wechat.txt');var app = new Koa();app.use(wechar(config.wechat, weixin.reply));app.listen(3000);**********************************************/wechar/g.js代码如下:'use strict'var sha1 = require('sha1');var Wechat = require('./wechat.js');var getRawBody = require('raw-body');var util = require('./util.js');module.exports = function(opts, handler){ var wechat = new Wechat(opts); //在测试发送方式get/post时,可以先屏蔽掉 return function *(next) { var that = this; var token = opts.token; var signature = this.query.signature; var nonce = this.query.nonce; var echostr = this.query.echostr; var timestamp = this.query.timestamp; var str = [token, timestamp, nonce].sort().join(''); var sha = sha1(str); if(this.method === 'GET'){ if (sha === signature && echostr) { this.body = echostr + ''; //console.log(echostr); return; } else { this.body = 'wrong --- echostr的验证出错!'; return; } } else if (this.method === 'POST'){ if (sha !== signature){ this.body = 'wrong --- signature不匹配啊!'; return false; } var data = yield getRawBody(this.req, { length: this.length, limit: '1mb', encoding: this.charset, }); var content = yield util.parseXMLAsync(data); console.log(content); var message = yield util.formatMessage(content.xml); console.log(message); } }}*************************************************************************./util.js代码如下:'use strict'var xml2js = require('xml2js'); var Promise = require('bluebird'); var tpl = require('./tpl.js');//解析xml格式的数据exports.parseXMLAsync = function (xml) { return new Promise(function (resolve, reject) { xml2js.parseString(xml, {trim:true}, function (err,content) { if (err) reject(err); else resolve(content); }); });}
1 回答
- 1 回答
- 1 关注
- 1866 浏览
添加回答
举报
0/150
提交
取消