groupchat.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class GroupChatService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'groupchat');
  10. this.model = this.ctx.model.Groupchat;
  11. this.cGroup = this.ctx.model.Group;
  12. this.cDoctor = this.ctx.model.Doctor;
  13. this.cPatient = this.ctx.model.Patient;
  14. this.remark = this.ctx.model.Remark;
  15. }
  16. async query({ groupid, sort = 1, userid }, { skip, limit }) {
  17. assert(groupid, 'groupid不能为空');
  18. const groupall = await this.model.find({ groupid });
  19. const groups = await this.model.find({ groupid }).sort({ sendtime: sort }).limit(limit)
  20. .skip(skip);
  21. const newgroups = [];
  22. const remarks = await this.remark.find({ remarkid: userid });
  23. for (const el of groups) {
  24. let icon;
  25. let { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, audiotime } = el;
  26. if (el.type === '1') {
  27. const patient = await this.cPatient.findById(el.sendid);
  28. if (patient) {
  29. icon = patient.icon;
  30. }
  31. }
  32. if (el.type === '0') {
  33. const doctor = await this.cDoctor.findById(el.sendid);
  34. if (doctor) icon = doctor.icon;
  35. }
  36. const d_r = remarks.find(f => ObjectId(f.benoteid).equals(doctorid));
  37. if (d_r) doctorname = d_r.name;
  38. const p_r = remarks.find(f => ObjectId(f.benoteid).equals(sendid));
  39. if (p_r) sendname = p_r.name;
  40. const group = { id, doctorid, doctorname, groupid, groupname, type, sendid, sendname, sendtime, contenttype, content, icon, audiotime };
  41. newgroups.push(group);
  42. }
  43. const result = { total: groupall.length, data: newgroups };
  44. return result;
  45. }
  46. // 创建群信息
  47. async create({ groupid }, data) {
  48. const { sendid, type, content, audiotime } = data;
  49. assert(sendid, '发送人不能为空');
  50. assert(type, '类别不能为空');
  51. assert(content, '发送内容不能为空');
  52. assert(groupid, '缺少群信息项');
  53. // 通过群id取得群内医生信息
  54. const group = await this.cGroup.findById(groupid, '+patients');
  55. if (!group) {
  56. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '群不存在');
  57. }
  58. const doctor = await this.cDoctor.findById(group.doctorid);
  59. if (!doctor) {
  60. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '医生不存在');
  61. }
  62. let sendname;
  63. let icon;
  64. if (type === '0') {
  65. sendname = doctor.name;
  66. icon = doctor.icon;
  67. } else {
  68. const patient = await this.cPatient.findById(sendid);
  69. if (!patient) {
  70. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '患者信息不存在');
  71. }
  72. sendname = patient.name;
  73. icon = patient.icon;
  74. }
  75. // TODO: 检查是否已经注册
  76. const newdata = { doctorid: group.doctorid, audiotime, doctorname: doctor.name, groupid, groupname: group.name, type, sendid, sendname, sendtime: new Date().toLocaleString(), content, contenttype: data.contenttype };
  77. const entity = await this.model.create(newdata);
  78. // 调用mq发送消息 只有在医生的情况下才会触发群消息
  79. const exchange = 'group_chat';
  80. const routeKey = groupid;
  81. const contentMq = doctor.name + '医生的' + group.name + '群内发布了一条消息请及时查收。';
  82. const parm = {
  83. durable: true,
  84. headers: {
  85. sendid,
  86. groupid,
  87. icon,
  88. type,
  89. audiotime,
  90. sendname,
  91. createtime: new Date().toLocaleDateString(),
  92. content,
  93. contenttype: data.contenttype,
  94. remark: contentMq,
  95. },
  96. };
  97. const { mq } = this.ctx;
  98. if (mq) {
  99. await mq.fanout(exchange, routeKey, content, parm);
  100. } else {
  101. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  102. }
  103. // 当医生发送群消息时微信提醒所有群内患者
  104. const self = this;
  105. if (type === '0') {
  106. for (const elem of group.patients) {
  107. await self.service.wechat.sendTemplateMsg(self.ctx.app.config.REVIEW_TEMPLATE_ID, elem.openid, contentMq, doctor.name, new Date().toLocaleDateString(), '群聊', '请及时查看');
  108. }
  109. }
  110. return await this.fetch({ id: entity.id });
  111. }
  112. async delete({ id }) {
  113. await this.model.findByIdAndDelete(id);
  114. return 'deleted';
  115. }
  116. }
  117. module.exports = GroupChatService;