liuyu 4 年之前
父节点
当前提交
367bc2ac47
共有 7 个文件被更改,包括 54 次插入2 次删除
  1. 2 2
      app/controller/.chat.js
  2. 1 0
      app/model/chat.js
  3. 22 0
      app/service/chat.js
  4. 11 0
      config/config.default.js
  5. 11 0
      config/config.local.js
  6. 6 0
      config/plugin.js
  7. 1 0
      package.json

+ 2 - 2
app/controller/.chat.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["roomid", "type", "!content", "!sendid","sendname"],
+    requestBody: ["roomid", "type", "!content", "!sendid","sendname","sendtime"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["roomid", "type", "content", "sendid","sendname"],
+    requestBody: ["roomid", "type", "content", "sendid","sendname","sendtime"],
   },
   show: {
     parameters: {

+ 1 - 0
app/model/chat.js

@@ -8,6 +8,7 @@ const ChatSchema = {
   content: { type: String, required: false, maxLength: 200 }, // 封面图片
   sendid: { type: String, required: false, maxLength: 200 }, // 发送人id
   sendname: { type: String, required: false, maxLength: 200 }, // 发送人
+  sendtime: { type: String, required: false, maxLength: 200 }, // 发送时间
 };
 
 const schema = new Schema(ChatSchema, { toJSON: { virtuals: true } });

+ 22 - 0
app/service/chat.js

@@ -4,6 +4,7 @@
 const assert = require('assert');
 const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
+const moment = require('moment');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 
@@ -13,6 +14,27 @@ class ChatService extends CrudService {
     this.model = this.ctx.model.Chat;
   }
 
+  async create(data) {
+    const { sendid, content, roomid } = data;
+    assert(sendid, '缺少发言人信息');
+    assert(roomid, '缺少房间信息');
+    assert(content, '缺少发言内容');
+    data.sendtime = moment().format('YYYY-MM-DD HH:mm:ss');
+    const res = await this.model.create(data);
+    // TODO MQ
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = 'public_chat_' + roomid;
+      const parm = {
+        durable: true,
+        headers: {
+          userid: sendid,
+        } };
+      await mq.fanout(exchange, roomid, JSON.stringify(res), parm);
+    }
+    return res;
+  }
+
 }
 
 module.exports = ChatService;

+ 11 - 0
config/config.default.js

@@ -77,6 +77,17 @@ module.exports = appInfo => {
     },
   };
 
+  config.amqp = {
+    client: {
+      hostname: '127.0.0.1',
+      username: 'live',
+      password: 'live',
+      vhost: 'live',
+    },
+    app: true,
+    agent: true,
+  };
+
   // 安全配置
   config.security = {
     csrf: {

+ 11 - 0
config/config.local.js

@@ -20,5 +20,16 @@ module.exports = () => {
     },
   };
 
+  config.amqp = {
+    client: {
+      hostname: '127.0.0.1',
+      username: 'wy',
+      password: '1',
+      vhost: 'smart',
+    },
+    app: true,
+    agent: true,
+  };
+
   return config;
 };

+ 6 - 0
config/plugin.js

@@ -8,3 +8,9 @@ exports.redis = {
   enable: true,
   package: 'egg-redis',
 };
+
+/** @type Egg.EggPlugin */
+exports.amqp = {
+  enable: true,
+  package: 'egg-naf-amqp',
+};

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "raml2html": "^6.1.0",
     "uuid": "^3.3.3",
     "xlsx": "^0.15.1",
+    "egg-naf-amqp": "0.0.13",
     "xmlreader": "^0.2.3"
   },
   "devDependencies": {