train_chat.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const moment = require('moment');
  6. const assert = require('assert');
  7. // 培训问诊聊天
  8. class Train_chatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'train_chat');
  11. this.model = this.ctx.model.TrainChat;
  12. }
  13. async create(query, { unit_id, sender_id, sender_name, content, role }) {
  14. assert(sender_name, '缺少发言人信息');
  15. assert(unit_id, '缺少会场id');
  16. assert(content, '缺少发言内容');
  17. const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
  18. const res = await this.model.create({ sender_id, sender_name, content, send_time, role, unit_id });
  19. // TODO MQ
  20. const { mq } = this.ctx;
  21. if (mq) {
  22. const exchange = 'train_live';
  23. const parm = {
  24. durable: true,
  25. headers: {
  26. userid: 1,
  27. } };
  28. await mq.fanout(exchange, unit_id, JSON.stringify(res), parm);
  29. }
  30. return res;
  31. }
  32. }
  33. module.exports = Train_chatService;