123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 'use strict';
- const msgvalue = require('../util/constants');
- const sd = require('silly-datetime');
- const Service = require('egg').Service;
- class RabbitmqService extends Service {
- constructor(ctx) {
- super(ctx);
- this.exType = 'topic';
- this.durable = true;
- }
- // 发送消息
- async sendQueueMsg(ex, routeKey, msg, parm) {
- const self = this;
- const { mq } = self.ctx;
- if (mq) {
- await mq.topic(ex, routeKey, msg, parm);
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- // 接收消息
- async receiveQueueMsg(ex) {
- const self = this;
- const { mq } = self.ctx;
- if (mq) {
- const ch = await mq.conn.createChannel();
- try {
- await ch.assertExchange(ex, self.exType, { durable: self.durable });
- const q = await ch.assertQueue('', { exclusive: true });
- console.log('==q=', q);
- // 队列绑定 exchange
- await ch.bindQueue(q.queue, ex, '*');
- ch.consume(q.queue, msg => {
- console.log('收到消息: ', msg);
- const result = msg.content.toString();
- // const fields = msg.fields;
- // const properties = msg.properties;
- const headers = msg.properties.headers;
- // 插入待办事项到数据库中。
- if (result != null) {
- // const routingKey = msg.fields.routingKey;
- const updatetimes = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
- this.service.message.create({ userid: headers.userId, name: headers.name, createtime: updatetimes, type: headers.type, content: result, remark: '别忘记了' });
- }
- }, { noAck: true });
- } catch (e) {
- console.log('==e==', e);
- await ch.close();
- }
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- // 接收学校审核消息并发送模板消息到企业微信
- async receiveMsgSendWxSch(ex) {
- const self = this;
- const { mq } = self.ctx;
- if (mq) {
- const ch = await mq.conn.createChannel();
- try {
- await ch.assertExchange(ex, self.exType, { durable: self.durable });
- const q = await ch.assertQueue('', { exclusive: true });
- console.log('==q=', q);
- // 队列绑定 exchange
- await ch.bindQueue(q.queue, ex, '*');
- ch.consume(q.queue, msg => {
- console.log('收到消息: ', msg);
- const result = msg.content.toString();
- // const fields = msg.fields;
- // const properties = msg.properties;
- const headers = msg.properties.headers;
- // 插入待办事项到数据库中。
- const path = self.ctx.app.config.baseDir + self.ctx.app.config.corpsDir + headers.userid;
- const corp = self.ctx.curl(path, {
- method: 'GET',
- dataType: 'json',
- });
- // 插入待办事项到数据库中。
- if (corp != null) {
- self.service.wechat.sendTemplateMsg(msgvalue.REVIEW_TEMPLATE_ID, corp.openid, '您的申请已经得到批复', result, headers.name, '企业申请', headers.remark);
- }
- }, { noAck: true });
- } catch (e) {
- console.log('==e==', e);
- await ch.close();
- }
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- // 接收企业职位发布消息并发送模板消息到学生微信
- async receiveMsgSendWxCorp(ex) {
- const self = this;
- const { mq } = self.ctx;
- if (mq) {
- const ch = await mq.conn.createChannel();
- try {
- await ch.assertExchange(ex, self.exType, { durable: self.durable });
- const q = await ch.assertQueue('', { exclusive: true });
- console.log('==q=', q);
- // 队列绑定 exchange
- await ch.bindQueue(q.queue, ex, '*');
- ch.consume(q.queue, msg => {
- console.log('收到消息: ', msg);
- const result = msg.content.toString();
- // const fields = msg.fields;
- // const properties = msg.properties;
- const headers = msg.properties.headers;
- // 插入待办事项到数据库中。
- // const path = self.ctx.app.config.baseDir + self.ctx.app.config.stusDir + headers.userid;
- const path = 'http://10.16.5.15:8101/api/studentcorp' + headers.userid;
- const stus = self.ctx.curl(path, {
- method: 'GET',
- dataType: 'json',
- });
- for (const elem of stus.data) {
- const pathStu = self.ctx.app.config.baseDir + self.ctx.app.config.strDir + elem.id;
- const stud = self.ctx.curl(pathStu, {
- method: 'GET',
- dataType: 'json',
- });
- // 插入待办事项到数据库中。
- if (result != null) {
- self.service.wechat.sendTemplateMsg(msgvalue.REVIEW_TEMPLATE_ID, stud.data.openid, '您订阅的企业已经有新职位发布', result, headers.name, '职位发布', headers.remark);
- }
- }
- }, { noAck: true });
- } catch (e) {
- console.log('==e==', e);
- await ch.close();
- }
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- }
- module.exports = RabbitmqService;
|