自动回复失败,命令行也看不到打印出的xml数据,(win10下QQ 浏览器代理调试端口)代码如下:
来源:
gyuntong
2016-06-01
'use strict' //g.js var sha1 = require('sha1'); var getRawBody = require('raw-body') var Wechat = require('./wechat') var util = require('./util') module.exports = function(opts) { var wechat = new Wechat(opts) return function*(next) { 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('') 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, { length: this.length, limit: '1mb', encoding: this.charset }) var content = yield util.parseXMLAsync(data) console.log(content) var message=util.formatMessage(content.xml) console.log(message) if (message.MsgType==='event') { if (message.Event==='subscribe') { var now = new Date().getTime() that.status =200 that.type ='application/xml' that.body ='<xml>'+ '<ToUserName><![CDATA['+message.FromUserName+']]></ToUserName>'+ '<FromUserName><![CDATA['+message.ToUserName+']]></FromUserName>'+ '<CreateTime>'+now+'</CreateTime>'+ '<MsgType><![CDATA[text]]></MsgType>'+ '<Content><![CDATA[你好!这里是慕课网]]></Content>'+ '</xml>' return } } } } }
'use strict' //util.js var xml2js = require('xml2js') var Promise = require('bluebird') exports.parseXMLAsync = function(xml) { return new Promise(function(resolve, reject) { xml2js.parseString(xml, {trim: true}, function(err, content) { if (err) reject(err) else reslove(content) }) }) } function formatMessage(result) { var message = {} if (typeof result === 'object') { var keys = object.keys(result) for (var i = 0; i < keys.length; i++) { var item = result[keys[i]] var key = keys[i] if (!(item instanceof Array) || item.length === 0) { continue } if (item.length === 1) { var val = item[0] if (typeof val ==='object') { message[key]=formatMessage(val) } else { message[key] = (val||'').trim() } } else { message[key] = [] for (var j = 0,k = item.length; j < k; j++) { message[key].push(formatMessage(item[j])) } } } } return message } exports.formatMessage = formatMessage exports.formatMessage = function(xml) { return new Promise(function(resolve, reject) { xml2js.parseString(xml, {trim: true}, function(err, content) { if (err) reject(err) else reslove(content) }) }) }
'use strict' //wechat.js var Promise = require('bluebird') var request = Promise.promisify(require('request')) var prefix = 'https://api.weixin.qq.com/cgi-bin/' var api = { accessToken: prefix + 'token?grant_type=client_credential' } function Wechat(opts) { var that = this this.appID = opts.appID this.appSecret = opts.appSecret this.getAccessToken = opts.getAccessToken this.saveAccessToken = opts.saveAccessToken this.getAccessToken() .then(function(data) { try { data = JSON.parse(data) } catch (e) { return that.updateAccessToken() } if (that.isValidAccessToken(data)) { return Promise.resolve(data) } else { return that.updateAccessToken() } }) .then(function(data) { that.access_token = data.access_token that.expires_in = data.expires_in that.saveAccessToken(data) }) } Wechat.prototype.isValidAccessToken = function(data) { if (!data || !data.access_token || !data.expires_in) { return false } var access_token = data.access_token var expires_in = data.expires_in var now = (new Date().getTime()) if (now < expires_in) { return true } else { return false } } Wechat.prototype.updateAccessToken = function() { var appID = this.appID var appSecret = this.appSecret var url = api.accessToken + '&appid=' + appID + '&secret=' + appSecret return new Promise(function(resolve, reject) { request({ url: url, json: true }).then(function(response) { // var data = response.body var now = (new Date().getTime()) var expires_in = now + (data.expires_in - 20) * 1000 data.expires_in = expires_in resolve(data) }) }) } module.exports = Wechat;
'use strict' //app.js var Koa = require('koa'); var path = require('path') var wechat = require('./wechat/g') var util = require('./libs/util') var wechat_file = path.join(__dirname,'./config/wechat.txt') var config = { wechat:{ appID:'wx4b9d6607322760b8', appSecret:'1d7bac3f84cc53e3cdb455a1c657172d', token:'qbtest', getAccessToken:function(){ return util.readFileAsync(wechat_file) }, saveAccessToken: function(data){ data = JSON.stringify(data) return util.writeFileAsync(wechat_file,data) } } } var app = new Koa(); app.use(wechat(config.wechat)) app.listen(4040) console.log('listening:4040')
写回答
3回答
-
Scott
2016-06-01
第一个错误,resolve 拼错了,拼成了 reslove
exports.parseXMLAsync = function(xml) { return new Promise(function(resolve, reject) { xml2js.parseString(xml, {trim: true}, function(err, content) { if (err) reject(err) else reslove(content) }) }) }
第二个错误,util.js 里面,多写了一个 formatMessage 把上面的给覆盖了。
exports.formatMessage = formatMessage exports.formatMessage = function(xml) { return new Promise(function(resolve, reject) { xml2js.parseString(xml, {trim: true}, function(err, content) { if (err) reject(err) else reslove(content) }) }) }
第三个错误,object.keys(result) 的 object 是大写了 Object,写错成了小写
function formatMessage(result) { var message = {} if (typeof result === 'object') { var keys = object.keys(result)
10 -
gyuntong
提问者
2016-06-01
改正了几个错误后还是没有解决问题,真的不知道是什么原因了
'use strict' //util.js var xml2js = require('xml2js') var Promise = require('bluebird') 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) }) }) } function formatMessage(result) { var message = {} if (typeof result === 'object') { var keys = Object.keys(result) for (var i = 0; i < keys.length; i++) { var item = result[keys[i]] var key = keys[i] if (!(item instanceof Array) || item.length === 0) { continue } if (item.length === 1) { var val = item[0] if (typeof val ==='object') { message[key]=formatMessage(val) } else { message[key] = (val||'').trim() } } else { message[key] = [] for (var j = 0,k = item.length; j < k; j++) { message[key].push(formatMessage(item[j])) } } } } return message } exports.formatMessage = formatMessage
012016-06-02 -
gyuntong
提问者
2016-06-01
我真是太粗心了。谢谢老师
00
相似问题