Browse Source

修改消息机制

liuyu 5 years ago
parent
commit
93c3d89ec2
1 changed files with 67 additions and 84 deletions
  1. 67 84
      app/service/rabbitmq.js

+ 67 - 84
app/service/rabbitmq.js

@@ -32,15 +32,7 @@ class RabbitmqService extends Service {
         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 headers = msg.properties.headers;
-          // 插入待办事项到数据库中。
-          if (result != null) {
-            this.service.message.create({ userid: headers.userId, name: headers.name, createtime: headers.createtime, type: headers.type, content: result, remark: headers.remark });
-          }
-        }, { noAck: true });
+        await ch.consume(q.queue, msg => this.logMessage(msg, this), { noAck: true });
       } catch (e) {
         console.log('==e==', e);
         await ch.close();
@@ -50,6 +42,16 @@ class RabbitmqService extends Service {
     }
   }
 
+  // 消息回调方法
+  async logMessage(msg) {
+    const result = msg.content.toString();
+    const headers = msg.properties.headers;
+    // 插入待办事项到数据库中。
+    if (result != null) {
+      await this.service.message.create({ userid: headers.userId, name: headers.name, createtime: headers.createtime, type: headers.type, content: result, remark: headers.remark });
+    }
+  }
+
   // 接收学校审核消息并发送模板消息到企业微信
   async receiveMsgSendWxSch(ex) {
     const self = this;
@@ -62,29 +64,7 @@ class RabbitmqService extends Service {
         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.baseDirMq + self.ctx.app.config.corpsDirMq + headers.userId + '/users';
-          self.ctx.curl(path, {
-            method: 'GET',
-            dataType: 'json',
-          }).then(
-            function(corpUser) {
-              if (corpUser != null) {
-                const _datas = corpUser.data.data;
-                const _self = self;
-                for (let i = 0; i < _datas.length; i++) {
-                  const _data = _datas[i];
-                  self.service.wechat.sendTemplateMsg(_self.ctx.app.config.REVIEW_TEMPLATE_ID, _data.openid, '您的申请已经得到批复', result, headers.name, '企业申请', headers.remark);
-                }
-              }
-            });
-        }, { noAck: true });
+        await ch.consume(q.queue, msg => this.logMessageSendWxSch(msg, this), { noAck: true });
       } catch (e) {
         console.log('==e==', e);
         await ch.close();
@@ -94,6 +74,23 @@ class RabbitmqService extends Service {
     }
   }
 
+  // 消息回调方法
+  async logMessageSendWxSch(msg) {
+    const result = msg.content.toString();
+    const headers = msg.properties.headers;
+    const path = this.ctx.app.config.baseDirMq + this.ctx.app.config.corpsDirMq + headers.userId + '/users';
+    const corpUser = await this.ctx.curl(path, {
+      method: 'GET',
+      dataType: 'json',
+    });
+    if (corpUser != null) {
+      const _datas = corpUser.data.data;
+      for (const elm of _datas) {
+        await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, elm.openid, '您的申请已经得到批复', result, headers.name, '企业申请', headers.remark);
+      }
+    }
+  }
+
   // 接收企业职位发布消息并发送模板消息到学生微信
   async receiveMsgSendWxCorp(ex) {
     const self = this;
@@ -106,39 +103,7 @@ class RabbitmqService extends Service {
         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.baseDirMq + self.ctx.app.config.stusDirMq + '?corpid=' + headers.userId;
-          // const path = 'http://10.16.5.15:8101/api/studentcorp' + headers.userid;
-          self.ctx.curl(path, {
-            method: 'GET',
-            dataType: 'json',
-          }).then(
-            function(stus) {
-              if (stus != null) {
-                for (const elem of stus.data.data) {
-                  const pathStu = self.ctx.app.config.baseDirMq + self.ctx.app.config.strDirMq + elem.studid;
-                  self.ctx.curl(pathStu, {
-                    method: 'GET',
-                    dataType: 'json',
-                  }).then(
-                    // eslint-disable-next-line no-loop-func
-                    function(stud) {
-                      if (stud != null) {
-                        self.service.wechat.sendTemplateMsg(self.ctx.app.config.REVIEW_TEMPLATE_ID, stud.data.data.openid, '您订阅的企业已经有新职位发布', result, headers.name, '职位发布', headers.remark);
-                      }
-                    });
-                }
-              }
-            });
-
-
-        }, { noAck: true });
+        await ch.consume(q.queue, msg => this.logMessageSendWxCorp(msg, this), { noAck: true });
       } catch (e) {
         console.log('==e==', e);
         await ch.close();
@@ -148,6 +113,28 @@ class RabbitmqService extends Service {
     }
   }
 
+  // 消息回调方法
+  async logMessageSendWxCorp(msg) {
+    const result = msg.content.toString();
+    const headers = msg.properties.headers;
+    const path = this.ctx.app.config.baseDirMq + this.ctx.app.config.stusDirMq + '?corpid=' + headers.userId;
+    // const path = 'http://10.16.5.15:8101/api/studentcorp' + headers.userid;
+    const stus = await this.ctx.curl(path, {
+      method: 'GET',
+      dataType: 'json',
+    });
+    for (const elem of stus.data.data) {
+      const pathStu = this.ctx.app.config.baseDirMq + this.ctx.app.config.strDirMq + elem.studid;
+      const stud = await this.ctx.curl(pathStu, {
+        method: 'GET',
+        dataType: 'json',
+      });
+      if (stud != null) {
+        await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stud.data.data.openid, '您订阅的企业已经有新职位发布', result, headers.name, '职位发布', headers.remark);
+      }
+    }
+  }
+
   // 接收企业发送面试邀请发送给学生消息
   async receiveQueueMsgOffer(ex) {
     const self = this;
@@ -160,25 +147,7 @@ class RabbitmqService extends Service {
         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 pathStu = self.ctx.app.config.baseDirMq + self.ctx.app.config.strDirMq + headers.userId;
-          self.ctx.curl(pathStu, {
-            method: 'GET',
-            dataType: 'json',
-          }).then(
-            // eslint-disable-next-line no-loop-func
-            function(stud) {
-              if (stud != null) {
-                self.service.wechat.sendTemplateMsg(self.ctx.app.config.OFFER_TEMPLATE_ID, stud.data.data.openid, '您有新的面试邀请,请及时查看。', result, headers.name, headers.createtime, headers.remark);
-              }
-            });
-        }, { noAck: true });
+        await ch.consume(q.queue, msg => this.logMessageSendOffer(msg, this), { noAck: true });
       } catch (e) {
         console.log('==e==', e);
         await ch.close();
@@ -188,6 +157,20 @@ class RabbitmqService extends Service {
     }
   }
 
+  // 消息回调方法
+  async logMessageSendOffer(msg) {
+    const result = msg.content.toString();
+    const headers = msg.properties.headers;
+    const pathStu = this.ctx.app.config.baseDirMq + this.ctx.app.config.strDirMq + headers.userId;
+    const stud = await this.ctx.curl(pathStu, {
+      method: 'GET',
+      dataType: 'json',
+    });
+    if (stud != null) {
+      await this.service.wechat.sendTemplateMsg(this.ctx.app.config.OFFER_TEMPLATE_ID, stud.data.data.openid, '您有新的面试邀请,请及时查看。', result, headers.name, headers.createtime, headers.remark);
+    }
+  }
+
 }
 
 module.exports = RabbitmqService;