chat.js 4.9 KB

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