当普通微信用户向公众账号发消息时,微信服务器将post消息的xml数据包到开发者填写的url上。
2.1 接收普通消息数据格式
xml的结构基本固定,不同的消息类型略有不同。
用户发送文本消息时,微信公众账号接收到的xml数据格式如下所示:
<xml> <tousername><![cdata[touser]]></tousername> <fromusername><![cdata[fromuser]]></fromusername> <createtime>createtime</createtime> <msgtype><![cdata[text]]></msgtype> <content><![cdata[this is a test]]></content> <msgid>1234567890123456</msgid> </xml>
用户发送图片消息时,微信公众账号接收到的xml数据格式如下所示:
<xml> <tousername><![cdata[touser]]></tousername> <fromusername><![cdata[fromuser]]></fromusername> <createtime>1348831860</createtime> <msgtype><![cdata[image]]></msgtype> <picurl><![cdata[this is a url]]></picurl> <mediaid><![cdata[media_id]]></mediaid> <msgid>1234567890123456</msgid> </xml>
其他消息消息类型的结构请查阅【微信公众平台开发文档】
对于post请求的处理,koa2没有封装获取参数的方法,需要通过自己解析上下文context中的原生node.js请求对象request。我们将用到row-body这个模块来拿到数据。
2.2 先来优化之前的代码
这一节的代码紧接着上一届实现的代码,在上一届的基础上轻微改动了下。
'use strict' const koa = require('koa') const app = new koa() const crypto = require('crypto') // 将配置文件独立到config.js const config = require('./config') app.use(async ctx => { // get 验证服务器 if (ctx.method === 'get') { const { signature, timestamp, nonce, echostr } = ctx.query const token = config.wechat.token let hash = crypto.createhash('sha1') const arr = [token, timestamp, nonce].sort() hash.update(arr.join('')) const shasum = hash.digest('hex') if (shasum === signature) { return ctx.body = echostr } ctx.status = 401 ctx.body = 'invalid signature' } else if (ctx.method === 'post') { // post接收数据 // todo } }); app.listen(7001);
这儿我们在只在get中验证了签名值是否合法,实际上我们在post中也应该验证签名。
将签名验证写成一个函数
function getsignature (timestamp, nonce, token) { let hash = crypto.createhash('sha1') const arr = [token, timestamp, nonce].sort() hash.update(arr.join('')) return hash.digest('hex') }
优化代码,再post中也加入验证
... app.use(async ctx => { const { signature, timestamp, nonce, echostr } = ctx.query const token = config.wechat.token if (ctx.method === 'get') { if (signature === getsignature(timestamp, nonce, token)) { return ctx.body = echostr } ctx.status = 401 ctx.body = 'invalid signature' }else if (ctx.method === 'post') { if (signature !== getsignature(timestamp, nonce, token)) { ctx.status = 401 return ctx.body = 'invalid signature' } // todo } }); ...
到这儿我们都没有开始实现接受xml数据包的功能,而是在修改之前的代码。这是为了演示在实际开发中的过程,写任何代码都不是一步到位的,好的代码都是改出来的。
2.3 接收公众号普通消息的xml数据包
现在开始进入本节的重点,接受xml数据包并转为json
$ npm install raw-body --save
... const getrawbody = require('raw-body') ... // todo // 取原始数据 const xml = await getrawbody(ctx.req, { length: ctx.request.length, limit: '1mb', encoding: ctx.request.charset || 'utf-8' }); console.log(xml) return ctx.body = 'success' // 直接回复success,微信服务器不会对此作任何处理
给你的测试号发送文本消息,你可以在命令行看见打印出如下数据
<xml> <tousername><![cdata[gh_9d2d49e7e006]]></tousername> <fromusername><![cdata[obp2t0wk8lm4vikmmtjffpk6owlo]]></fromusername> <createtime>1516940059</createtime> <msgtype><![cdata[text]]></msgtype> <content><![cdata[javascript之禅]]></content> <msgid>6515207943908059832</msgid> </xml>
恭喜,到此你已经可以接收到xml数据了。
以上就是如何操作koa2微信公众号实现消息管理的详细内容。