lrf402788946 4 年之前
父节点
当前提交
f41fc3b1ab

+ 46 - 0
app/controller/dock/.dock_chat.js

@@ -0,0 +1,46 @@
+module.exports = {
+  create: {
+    requestBody: ["!dock_id", "!content", "!sender_id", "!sender_name"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "!dock_id",
+      "!content",
+      "!sender_id",
+      "!sender_name",
+      "remark",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        dock_id: "dock_id",
+        sender_id: "sender_id",
+        sender_name: "%sender_name%",
+        "send_time@start": "send_time@start",
+        "send_time@end": "send_time@end",
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["send_time"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 13 - 0
app/controller/dock/dock_chat.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.dock_chat.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 展会公共聊天
+class Dock_chatController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.dock.dockChat;
+  }
+}
+module.exports = CrudController(Dock_chatController, meta);

+ 25 - 0
app/model/dock_chat.js

@@ -0,0 +1,25 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+// 展会公共聊天表
+const dock_chat = {
+  dock_id: { type: ObjectId, required: true }, // 展会id
+  content: { type: String, required: true }, // 内容
+  sender_id: { type: String, required: true }, // 发送人id
+  sender_name: { type: String, required: true }, // 发送人
+  remark: { type: String, maxLength: 200 },
+  send_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(dock_chat, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ dock_id: 1 });
+schema.index({ send_time: 1 });
+schema.index({ sender_id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Dock_chat', schema, 'dock_chat');
+};

+ 1 - 0
app/router.js

@@ -25,6 +25,7 @@ module.exports = app => {
   require('./router/dock/index')(app); // 展会
   require('./router/dock/dock_pw')(app); // 展会-图文
   require('./router/dock/dock_user')(app); // 展会-用户
+  require('./router/dock/dock_chat')(app); // 展会-公共聊天
   require('./router/dock/transaction')(app); // 交易合同+备案
   require('./router/dock/road_show')(app); // 路演
   require('./router/dock/channel')(app); // 科技频道

+ 12 - 0
app/router/dock/dock_chat.js

@@ -0,0 +1,12 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/live/';
+  const vision = 'v0';
+  const index = 'dock';
+  const target = 'dockChat';
+  router.resources(target, `${profix}${vision}/${index}/${target}`, controller[index][target]); // index、create、show、destroy
+  router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
+};

+ 35 - 0
app/service/dock/dock_chat.js

@@ -0,0 +1,35 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 展会公共聊天
+class Dock_chatService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'dock_chat');
+    this.model = this.ctx.model.DockChat;
+  }
+  async create(data) {
+    const res = await this.model.create(data);
+    if (res) {
+      const exchange = 'dockChat';
+      const routeKey = `${res.dock_id}`;
+      const content = JSON.stringify(_.pick(res, [ 'sender_id', 'sender_name', 'dock_id', '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 = Dock_chatService;