'use strict'; const amqp = require('amqplib'); const msgValue = require('../util/constants'); const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async sendmq() { const { ctx, app } = this; console.log(msgValue.MsgValues.EXCHANGE_CROP_REG); await this.service.rabbitmq.sendQueueMsg(ctx.query.exchange, ctx.query.routekey, new Buffer(JSON.stringify(ctx.query.msg)), data => { console.log(data); }); ctx.body = '发送成功'; } async receivemq() { const { ctx, app } = this; await this.service.rabbitmq.receiveQueueMsg(ctx.query.exchange, ctx.query.routekey, msg => { console.log(msg); // 插入待办事项到数据库中。 this.service.message.create({ producerid: msg.producerid, consumerid: msg.consumerid, type: msg.type, content: msg.content, remark: msg.remark }); }); ctx.body = '接收成功'; } } module.exports = HomeController;