liuyu 5 years ago
parent
commit
cb5a47ac56
7 changed files with 182 additions and 8 deletions
  1. 12 2
      app.js
  2. 18 4
      app/controller/home.js
  3. 1 1
      app/router.js
  4. 87 1
      app/service/rabbitmq.js
  5. 52 0
      app/service/wechat.js
  6. 2 0
      app/util/constants.js
  7. 10 0
      config/config.default.js

+ 12 - 2
app.js

@@ -6,9 +6,19 @@ class AppBootHook {
 
   async didReady() {
     // 应用已经启动完毕
-
     const ctx = await this.app.createAnonymousContext();
-    await ctx.service.rabbitmq.receiveQueueMsg('schedule');
+    // 企业入驻申请消息接收事件
+    await ctx.service.rabbitmq.receiveQueueMsg('corp_register');
+    // 企业申请三会消息接收事件
+    await ctx.service.rabbitmq.receiveQueueMsg('corp_apply');
+    // 学生投递简历消息接收事件
+    await ctx.service.rabbitmq.receiveQueueMsg('stu_apply');
+
+    // 发送模板消息
+    // 学校审核企业入驻情况消息
+    await ctx.service.rabbitmq.receiveMsgSendWxSch('sch_review');
+    // 企业发布职位情况消息
+    await ctx.service.rabbitmq.receiveMsgSendWxCorp('corp_jobs');
   }
 
   async serverDidReady() {

+ 18 - 4
app/controller/home.js

@@ -5,15 +5,29 @@ const Controller = require('egg').Controller;
 class HomeController extends Controller {
   async index() {
     const { ctx } = this;
-    ctx.body = 'hi, egg';
+    ctx.body = '111';
   }
 
-  async sendmq() {
+  async test() {
     const { ctx } = this;
+    const url = 'https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=26_ltUEKUrKLYOaiDQ0s6H0L3Mis1A4xQEuYElcRrU93k9k8h0g_Eya349E-hwS9pRd66SxVnmIPVdkwfPN1OL4_kpMJdcwNUs2mo5vfBEeM0CPYNhWXdWM4mcsewDVQDoq0SNCkUqIxM4yLMqKWKHiAGACQN';
+    const stus = await this.ctx.curl(url, {
+      method: 'POST',
+      body: { tagid: 100, next_openid: '' },
+    });
+    console.log('111--' + stus.toString());
+    const res = JSON.stringify(stus);
+    const obj2 = JSON.parse(res);
+    console.log(obj2);
+    console.log('111--' + obj2.data.data.toString());
+    ctx.body = obj2.data.data.toString();
+  }
+
+  async sendmq() {
     // const msg = { userid: ctx.body.userid, name: ctx.body.name, createtime: ctx.body.createtime, type: ctx.body.type, content: ctx.body.content, remark: ctx.body.remark };
-    const ex = 'schedule';
+    const ex = 'sch_review';
     const routekey = '11';
-    const parm = { durable: true, headers: { userId: '2', type: '1', name: '哈哈' } };
+    const parm = { durable: true, headers: { userId: '5da47c54a24aa17d33309ead', type: '1', name: '哈哈' } };
     await this.service.rabbitmq.sendQueueMsg(ex, routekey, 'hhhsss', parm);
     this.ctx.body = '发送成功';
   }

+ 1 - 1
app/router.js

@@ -6,8 +6,8 @@
 module.exports = app => {
   const { router, controller } = app;
   router.get('/', controller.home.index);
+  router.get('/api/test', controller.home.test);
   router.get('/api/sendmq', controller.home.sendmq);
-  router.get('/api/receivemq', controller.home.receivemq);
   // 待办事项
   router.resources('message', '/api/message', controller.message); // index、create、show、destroy
 

+ 87 - 1
app/service/rabbitmq.js

@@ -1,5 +1,5 @@
 'use strict';
-require('../util/constants');
+const msgvalue = require('../util/constants');
 const sd = require('silly-datetime');
 const Service = require('egg').Service;
 
@@ -56,6 +56,92 @@ class RabbitmqService extends Service {
     }
   }
 
+  // 接收学校审核消息并发送模板消息到企业微信
+  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;

+ 52 - 0
app/service/wechat.js

@@ -0,0 +1,52 @@
+'use strict';
+require('../util/constants');
+const Service = require('egg').Service;
+
+class WechatService extends Service {
+
+  constructor(ctx) {
+    super(ctx);
+    this.appid = 'wxdf3ed83c095be97a';
+  }
+
+  // 发送微信模板消息
+  async sendTemplateMsg(templateid, openid, first, keyword1, keyword2, keyword3, remark) {
+    const url = this.ctx.app.config.sendDir + this.appid;
+    const requestData = { // 发送模板消息的数据
+      touser: openid,
+      template_id: templateid,
+      url: 'http://weixin.qq.com/download',
+      data: {
+        first: {
+          value: first,
+          color: '#173177',
+        },
+        keyword1: {
+          value: keyword1,
+          color: '#1d1d1d',
+        },
+        keyword2: {
+          value: keyword2,
+          color: '#1d1d1d',
+        },
+        keyword3: {
+          value: keyword3,
+          color: '#1d1d1d',
+        },
+        remark: {
+          value: remark,
+          color: '#173177',
+        },
+      },
+    };
+    const result = await this.ctx.curl(url, {
+      method: 'post',
+      body: requestData,
+    });
+    console.log(result);
+
+  }
+
+}
+
+module.exports = WechatService;

+ 2 - 0
app/util/constants.js

@@ -7,4 +7,6 @@ exports.MsgValues = {
   EXCHANGE_CROP_REG: 'crop_register', // 企业注册
   EXCHANGE_CROP_APPLY: 'crop_apply', // 企业申请三会
   EXCHANGE_STU_APPLY: 'stu_apply', // 学生投递简历
+  REVIEW_TEMPLATE_ID: 'BI4h0AQpdctm74I7-7PyHAspSMX2oJOTJVQsgrppOag',
+  JOBS_TEMPLATE_ID: 'DLNlkgXVWgWU7cO6pZjpqQiFu0CgWXTPHooJ08d_-xU',
 };

+ 10 - 0
config/config.default.js

@@ -50,6 +50,16 @@ module.exports = appInfo => {
   //     useCreateIndex: true,
   //   },
   // };
+  // base路径
+  config.sendDir = 'http://wx.cc-lotus.info/api.weixin.qq.com/cgi-bin/message/template/send?appid=';
+  // base路径
+  config.baseDir = 'http://smart.cc-lotus.info';
+  // 企业信息url
+  config.corpsDir = '/api/corp/corps/';
+  // 学生信息url
+  config.stusDir = '/api/studentcorp';
+  // 学生详细信息
+  config.strDir = '/api/stud/registers/';
 
   // mq config
   config.amqp = {