message.js 875 B

1234567891011121314151617181920212223242526272829
  1. const { message: { checkSignature } } = require('../qcloud')
  2. /**
  3. * 响应 GET 请求(响应微信配置时的签名检查请求)
  4. */
  5. async function get (ctx, next) {
  6. const { signature, timestamp, nonce, echostr } = ctx.query
  7. if (checkSignature(signature, timestamp, nonce)) ctx.body = echostr
  8. else ctx.body = 'ERR_WHEN_CHECK_SIGNATURE'
  9. }
  10. async function post (ctx, next) {
  11. // 检查签名,确认是微信发出的请求
  12. const { signature, timestamp, nonce } = ctx.query
  13. if (!checkSignature(signature, timestamp, nonce)) ctx.body = 'ERR_WHEN_CHECK_SIGNATURE'
  14. /**
  15. * 解析微信发送过来的请求体
  16. * 可查看微信文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/receive.html#接收消息和事件
  17. */
  18. const body = ctx.request.body
  19. ctx.body = 'success'
  20. }
  21. module.exports = {
  22. post,
  23. get
  24. }