chat.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const { ObjectId } = require('mongoose').Types;
  7. const moment = require('moment');
  8. class ChatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'chat');
  11. this.model = this.ctx.model.Chat;
  12. this.cDoctor = this.ctx.model.Doctor;
  13. this.cPatient = this.ctx.model.Patient;
  14. this.cRoom = this.ctx.model.Room;
  15. this.remark = this.ctx.model.Remark;
  16. }
  17. async query({ sendid, receiveid, type, sort = 1, userid }, { skip, limit }) {
  18. assert(sendid, 'sendid不能为空');
  19. assert(receiveid, 'receiveid不能为空');
  20. const chatsall = await this.model.find({ $or: [{ sendid, receiveid }, { sendid: receiveid, receiveid: sendid }] });
  21. const chats = await this.model.find({ $or: [{ sendid, receiveid }, { sendid: receiveid, receiveid: sendid }] }).sort({ sendtime: sort }).limit(limit)
  22. .skip(skip);
  23. const newchats = [];
  24. const remarks = await this.remark.find({ remarkid: userid });
  25. for (const el of chats) {
  26. let icon;
  27. if (el.type === '1') {
  28. const patient = await this.cPatient.findById(el.sendid);
  29. if (patient) icon = patient.icon;
  30. }
  31. if (el.type === '0') {
  32. const doctor = await this.cDoctor.findById(el.sendid);
  33. if (doctor) icon = doctor.icon;
  34. }
  35. let { id, type, sendid, sendname, receiveid, receivename, sendtime, contenttype, content, status, audiotime } = el;
  36. const s_r = remarks.find(f => ObjectId(f.benoteid).equals(sendid));
  37. if (s_r) sendname = s_r.name;
  38. const r_r = remarks.find(f => ObjectId(f.benoteid).equals(receiveid));
  39. if (r_r) receivename = r_r.name;
  40. newchats.push({ id, type, sendid, sendname, receiveid, receivename, sendtime, contenttype, content, status, icon, audiotime });
  41. }
  42. const result = { total: chatsall.length, data: newchats };
  43. return result;
  44. }
  45. // 创建群信息
  46. async create(data) {
  47. const { sendid, receiveid, type, content, audiotime } = data;
  48. assert(sendid, '发送人不能为空');
  49. assert(type, '类别不能为空');
  50. assert(content, '发送内容不能为空');
  51. assert(receiveid, '收件人不能为空');
  52. // 通过群id取得群内医生信息
  53. let sendname;
  54. let receivename;
  55. let receiveopenid;
  56. let patientid;
  57. let patientname;
  58. let doctorid;
  59. let doctorname;
  60. let icon;
  61. if (type === '0') {
  62. const doctor = await this.cDoctor.findById(sendid);
  63. if (!doctor) {
  64. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  65. }
  66. sendname = doctor.name;
  67. const patient = await this.cPatient.findById(receiveid);
  68. if (!patient) {
  69. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  70. }
  71. receivename = patient.name;
  72. receiveopenid = patient.openid;
  73. doctorid = sendid;
  74. patientid = receiveid;
  75. doctorname = sendname;
  76. patientname = receivename;
  77. icon = doctor.icon;
  78. } else {
  79. const patient = await this.cPatient.findById(sendid);
  80. if (!patient) {
  81. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  82. }
  83. sendname = patient.name;
  84. const doctor = await this.cDoctor.findById(receiveid);
  85. if (!doctor) {
  86. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  87. }
  88. receivename = doctor.name;
  89. receiveopenid = doctor.openid;
  90. patientid = sendid;
  91. doctorid = receiveid;
  92. doctorname = receivename;
  93. patientname = sendname;
  94. icon = patient.icon;
  95. }
  96. // TODO: 检查是否已经注册
  97. const newdata = { type, sendid, audiotime, sendname, receiveid, receivename, content, contenttype: data.contenttype, sendtime: moment().format('YYYY-MM-DD HH:mm:ss') };
  98. const entity = await this.model.create(newdata);
  99. // 发送成功的时候创建房间,首先判断是已经创建房间
  100. let room = await this.cRoom.findOne({ patientid, doctorid });
  101. if (!room) {
  102. const newroom = { patientid, patientname, doctorid, doctorname };
  103. room = await this.cRoom.create(newroom);
  104. }
  105. // 调用mq发送消息 只有在医生的情况下才会触发群消息
  106. const exchange = 'chat';
  107. const routeKey = room.id;
  108. const contentMq = sendname + '给' + receivename + '发布了一条消息请及时查收。';
  109. const parm = {
  110. durable: true,
  111. headers: {
  112. sendid,
  113. receiveid,
  114. type,
  115. icon,
  116. sendname,
  117. audiotime,
  118. receivename,
  119. createtime: new Date().toLocaleDateString(),
  120. content,
  121. contenttype: data.contenttype,
  122. remark: contentMq,
  123. },
  124. };
  125. const { mq } = this.ctx;
  126. if (mq) {
  127. try {
  128. await mq.topic(exchange, routeKey, content, parm);
  129. } catch (error) {
  130. this.ctx.logger.error(error);
  131. }
  132. } else {
  133. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  134. }
  135. // 微信提醒
  136. try {
  137. await this.service.wechat.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, receiveopenid, contentMq, sendname, new Date().toLocaleDateString(), '私聊', '请及时查看');
  138. } catch (e) {
  139. this.ctx.logger.error(e.toString());
  140. }
  141. const d = await this.fetch({ id: entity.id });
  142. return d;
  143. }
  144. async delete({ id }) {
  145. await this.model.findByIdAndDelete(id);
  146. return 'deleted';
  147. }
  148. }
  149. module.exports = ChatService;