lrf402788946 4 years ago
parent
commit
2139861f0a
5 changed files with 105 additions and 0 deletions
  1. 31 0
      app/controller/.trainchat.js
  2. 16 0
      app/controller/trainchat.js
  3. 20 0
      app/model/trainchat.js
  4. 4 0
      app/router.js
  5. 34 0
      app/service/trainchat.js

+ 31 - 0
app/controller/.trainchat.js

@@ -0,0 +1,31 @@
+module.exports = {
+  create: {
+    requestBody: ["sender_id", "sender_name", "!content", "send_time", "role", "unit_id"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["sender_id", "sender_name", "content", "send_time", "role", "unit_id"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: ["sender_id", "sender_name", "content", "send_time", "role", "unit_id"],
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 16 - 0
app/controller/trainchat.js

@@ -0,0 +1,16 @@
+'use strict';
+
+// const _ = require('lodash');
+const meta = require('./.trainchat.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 公共聊天表管理
+class TrainchatController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.trainchat;
+  }
+}
+
+module.exports = CrudController(TrainchatController, meta);

+ 20 - 0
app/model/trainchat.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const Chat = {
+  unit_id: { type: String, maxLength: 200 }, // 公共聊天区id
+  sender_id: { type: String, maxLength: 200 }, // 发言人id
+  sender_name: { type: String, required: true, maxLength: 200 }, // 发言人名称
+  content: { type: String, required: true, maxLength: 1000 }, // 发言内容
+  send_time: { type: String, required: true, maxLength: 100 }, // 发言时间:年月日时分秒
+  role: { type: String, maxLength: 200 }, // 用户身份
+};
+const schema = new Schema(Chat, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Trainchat', schema, 'train_chat');
+};

+ 4 - 0
app/router.js

@@ -123,4 +123,8 @@ module.exports = app => {
   router.post('trainlive', '/api/live/trainlive/user/update/:id', controller.trainlive.updateUser);
   router.post('trainlive', '/api/live/trainlive/update/:id', controller.trainlive.update);
   router.resources('trainlive', '/api/live/trainlive', controller.trainlive); // index、create、show、destroy
+
+  router.resources('trainchat', '/api/live/trainchat', controller.trainchat); // index、create、show、destroy
+  router.post('trainchat', '/api/live/trainchat/update/:id', controller.trainchat.update);
+
 };

+ 34 - 0
app/service/trainchat.js

@@ -0,0 +1,34 @@
+'use strict';
+const assert = require('assert');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const moment = require('moment');
+
+// 培训问诊聊天
+class TrainchatService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'trainchat');
+    this.model = this.ctx.model.Trainchat;
+  }
+
+  async create(query, { unit_id, sender_id, sender_name, content, role }) {
+    assert(sender_name, '缺少发言人信息');
+    assert(unit_id, '缺少会场id');
+    assert(content, '缺少发言内容');
+    const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
+    const res = await this.model.create({ sender_id, sender_name, content, send_time, role, unit_id });
+    // TODO MQ
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = 'train_live';
+      const parm = {
+        durable: true,
+        headers: {
+          userid: 1,
+        } };
+      await mq.fanout(exchange, unit_id, JSON.stringify(res), parm);
+    }
+    return res;
+  }
+}
+
+module.exports = TrainchatService;