lrf402788946 4 年之前
父节点
当前提交
89bb49d806
共有 4 个文件被更改,包括 32 次插入8 次删除
  1. 6 4
      app/controller/users/.person_chat.js
  2. 1 0
      app/model/person_chat.js
  3. 21 0
      app/service/users/person_chat.js
  4. 4 4
      config/plugin.js

+ 6 - 4
app/controller/users/.person_chat.js

@@ -5,6 +5,7 @@ module.exports = {
       "sender_name",
       "!receiver_id",
       "receiver_name",
+      "!content",
       "!room_id",
     ],
   },
@@ -15,11 +16,12 @@ module.exports = {
   update: {
     params: ["!id"],
     requestBody: [
-      "!sender_id",
+      "sender_id",
       "sender_name",
-      "!receiver_id",
+      "receiver_id",
       "receiver_name",
-      "!room_id",
+      "room_id",
+      "content",
       "is_read",
     ],
   },
@@ -46,7 +48,7 @@ module.exports = {
     options: {
       query: ["skip", "limit"],
       sort: ["send_time"],
-      desc: true,
+      asc: true,
       count: true,
     },
   },

+ 1 - 0
app/model/person_chat.js

@@ -11,6 +11,7 @@ const person_chat = {
   receiver_id: { type: ObjectId, required: true }, // 接收人id
   receiver_name: { type: String, required: false }, // 接收人名字
   room_id: { type: ObjectId, required: true }, // 聊天房间id
+  content: { type: String, required: true }, // 内容
   is_read: { type: Boolean, default: false }, // 是否已读
   send_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
 };

+ 21 - 0
app/service/users/person_chat.js

@@ -11,6 +11,27 @@ class Person_chatService extends CrudService {
     super(ctx, 'person_chat');
     this.model = this.ctx.model.PersonChat;
   }
+  async create(data) {
+    const res = await this.model.create(data);
+    if (res) {
+      const exchange = 'personChat';
+      // const routeKey = `${res.room_id}/${res.receiver_id}`;
+      const routeKey = 'test';
+      const content = JSON.stringify(_.pick(res, [ 'sender_id', 'sender_name', 'receiver_id', 'receiver_name', 'room_id', 'content', 'is_read', 'send_time' ]));
+      const param = { durable: true };
+      const { mq } = this.ctx;
+      if (mq) {
+        try {
+          await mq.topic(exchange, routeKey, content, param);
+        } catch (error) {
+          this.ctx.logger.error(error);
+        }
+      } else {
+        this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
+      }
+    }
+    return res;
+  }
 }
 
 module.exports = Person_chatService;

+ 4 - 4
config/plugin.js

@@ -1,10 +1,10 @@
 'use strict';
 
 /** @type Egg.EggPlugin */
-// exports.amqp = {
-//   enable: true,
-//   package: 'egg-naf-amqp',
-// };
+exports.amqp = {
+  enable: true,
+  package: 'egg-naf-amqp',
+};
 // exports.redis = {
 //   enable: true,
 //   package: 'egg-redis',