2-7 git中取消关注打印不出信息,关注可以打印信息,如何解决?
来源:
menghuanbaolei
2017-03-04
g.js
'use strict' 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 *(ctx, next) { var token = opts.token; var signature = ctx.query.signature; var echostr = ctx.query.echostr; var timestamp = ctx.query.timestamp; var nonce = ctx.query.nonce; var str = [token, timestamp, nonce].sort().join(''); var sha = sha1(str); if (ctx.method === 'GET') { if (sha === signature) { ctx.body = echostr + ''; } else { ctx.body = 'wong'; } } else if(ctx.method === 'POST'){ if (sha !== signature) { ctx.body = 'wrong'; return false; } var data = yield getRawbody(ctx.req, { length: ctx.length, limit: '1mb', encoding: ctx.charset }) var content = yield util.parseXMLAsync(data); console.log(content); } }) }
wechat.js
'use strict' 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.isValidAccressToken(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.isValidAccressToken = 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;
只能打印出关注的,如何解决
写回答
3回答
-
Scott
2017-03-06
不合常理啊,你先往下面走,测试下发送文字消息之类的,看能不能收到其他的类型的 post 请求
052017-03-08 -
Scott
2017-03-05
在 if (sha !== signature) { 前面打印一下 ctx.req,看看取关时候,会不会过来正常的请求和数据,另外你使用的是公众测试号么
022017-03-05 -
menghuanbaolei
提问者
2017-03-04
关注能收到post,移除收不到post请求
00
相似问题