check.js
const Koa = require('koa');
const sha1 = require('sha1');
const config = {
wechat: {
appID: 'wx839181b88622f681',
appSecret: '3a2decb179567919cc4adaeb5be8d5cb',
token: 'wx839181b88622f681'
}
};
//生存服务器实例
const app = new Koa()
// 加载认证中间件
// ctx 是Koa 的应用上下文
// next 就是串联中间件的钩子函数
app.use(async (ctx, next) => {
console.log(ctx.query);
const {
signature,
timestamp,
nonce,
echostr
} = ctx.query
const token = config.wechat.token;
let str = [token, timestamp, nonce].sort().join('');
const sha = sha1(str);
if(sha === signature){
ctx.body = echostr
} else{
ctx.body = 'wrong'
}
});
app.listen(8080);
console.log('Listen:' + 8080);