12345678910111213141516171819202122232425262728293031323334 |
- 'use strict';
- const xml2js = require('../util/xml2js');
- module.exports = () => {
- return async (ctx, next) => {
- if (ctx.method === 'POST' && ctx.is('text/xml')) {
- const promise = new Promise((resolve, reject) => {
- let data = '';
- ctx.req.on('data', chunk => {
- data += chunk;
- });
- ctx.req.on('end', () => {
- xml2js.parseXml(data).then(result => {
- resolve(result);
- }).catch(err => {
- reject(err);
- });
- });
- });
- await promise.then(result => {
- ctx.req.body = result;
- }).catch(() => {
- ctx.req.body = '';
- });
- } else {
- if (ctx.query.echostr) {
- ctx.body = ctx.query.echostr;
- return;
- }
- }
- await next();
- };
- };
|