groupchat.js 4.1 KB

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