import { Controller } from 'egg' ; import * as getRawBody from 'raw-body' ; import { QyWeChat , QyApiConfigKit } from 'tnwx' ; import { HandMsgAdapter } from '../../HandMsgAdapter' ; export default class QyMsgController extends Controller { msgAdapter = new HandMsgAdapter ( ) ; public async get ( ) { const { ctx } = this ; let appId = ctx . query . appId ; let corpId = ctx . query . corpId ; if ( appId && corpId ) { QyApiConfigKit . setCurrentAppId ( appId , corpId ) ; } let signature = ctx . query . signature , timestamp = ctx . query . timestamp , nonce = ctx . query . nonce , echostr = ctx . query . echostr ; ctx . body = QyWeChat . checkSignature ( signature , timestamp , nonce , echostr ) ; } public async post ( ) { const { ctx } = this ; let appId = ctx . query . appId ; let corpId = ctx . query . corpId ; if ( appId && corpId ) { QyApiConfigKit . setCurrentAppId ( appId , corpId ) ; } let msgSignature = ctx . query . msg_signature , timestamp = ctx . query . timestamp , nonce = ctx . query . nonce ; let buffer = await getRawBody ( ctx . req ) ; let msgXml = buffer . toString ( 'utf-8' ) ; QyWeChat . handleMsg ( this . msgAdapter , msgXml , msgSignature , timestamp , nonce ) . then ( data => { ctx . body = data ; } ) . catch ( error => console . log ( error ) ) ; } }