patient.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * 患者模型。
  3. */
  4. "use strict";
  5. let configFile = require('../include/commons').CONFIG_FILE;
  6. let config = require('../resources/config/' + configFile);
  7. let log = require("../util/log.js");
  8. let BaseModel = require('./base.model');
  9. let patientRepo = require('../repository/patient.repo');
  10. let statsRepo = require("../repository/stats.msg.repo.js");
  11. let pmRepo = require('../repository/private.msg.repo');
  12. let objectUtil = require("../util/objectUtil.js");
  13. let modelUtil = require('../util/modelUtil');
  14. let Doctor = require('../models/doctor');
  15. const CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
  16. let clientCache = require('./socket.io/client.cache').clientCache();
  17. let doctorRepo = require('../repository/doctor.repo');
  18. let groupRepo = require('../repository/group.repo');
  19. let wechatUtil = require('../util/wechatUtil');
  20. class Patient extends BaseModel {
  21. constructor() {
  22. super();
  23. }
  24. /**
  25. * 是否为患者代码。
  26. *
  27. * @param code
  28. * @param passedCallback 测试通过回调
  29. * @param failedCallback 测试失败回调
  30. */
  31. static isPatientCode(code, passedCallback, failedCallback) {
  32. patientRepo.isPatientCode(code, function (err, result) {
  33. if (err) {
  34. log.error('Send message to patient failed: ', err);
  35. return;
  36. }
  37. if (result[0].c > 0) {
  38. passedCallback();
  39. } else {
  40. failedCallback();
  41. }
  42. });
  43. }
  44. /**
  45. * 向患者发送消息。
  46. *
  47. * 注意:患者消息的保存发送与医生的实现不同。
  48. *
  49. * @param message 消息
  50. */
  51. sendMessage(message) {
  52. // 保存消息
  53. let self = this;
  54. let tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
  55. pmRepo.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
  56. if (err) {
  57. modelUtil.emitDbError(self.eventEmitter, 'Save private message failed', err);
  58. return;
  59. }
  60. // 结束网络连接,后续操作继续执行
  61. pmRepo.findOnePatientMessage(result.insertId, function (err, msg) {
  62. if (err) {
  63. modelUtil.emitDbError(self.eventEmitter, 'Save private message success, but return last message failed', err);
  64. return;
  65. }
  66. modelUtil.emitData(self.eventEmitter, Doctor.fillMessages(msg));
  67. // 通过Web Socket推送给患者
  68. let patientClient = clientCache.findById(message.to);
  69. if (!patientClient) {
  70. log.warn("User is not online, user id: ", message.to);
  71. //发送微信模板消息
  72. self.sendConsultWechatReplyTempMsg(message);
  73. return;
  74. }
  75. let row = msg[0];
  76. row.timestamp = objectUtil.timestampToLong(row.timestamp);
  77. patientClient.socketServer.sockets.emit('message', row);
  78. });
  79. // 更新自身的聊天统计信息
  80. statsRepo.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
  81. if (err) log.error(err);
  82. });
  83. // 更新对端的聊天统计信息
  84. statsRepo.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
  85. if (err) log.error(err);
  86. });
  87. });
  88. };
  89. /**
  90. * 推送群组消息给居民
  91. *
  92. * @param message
  93. */
  94. pushGroupMessage(message) {
  95. let self = this;
  96. // 通过Web Socket推送给患者
  97. let patientClient = clientCache.findById(message.to);
  98. if (!patientClient) {
  99. log.warn("User is not online, user id: ", message.to);
  100. //发送微信模板消息
  101. self.sendConsultWechatReplyTempMsg(message);
  102. return;
  103. }
  104. groupRepo.getOnGroupMsg(message.msgId, function (err, result) {
  105. if (err) {
  106. modelUtil.emitDbError(self.eventEmitter, "get group msg info failed", err);
  107. }
  108. var msg = result ? result[0] : "";
  109. if (msg) {
  110. patientClient.socketServer.sockets.emit('message', msg);
  111. }
  112. })
  113. };
  114. /**
  115. * 发送微信模板消息给居民
  116. *
  117. * @param message
  118. */
  119. sendConsultWechatReplyTempMsg(message) {
  120. let selt = this;
  121. // 发送微信消息
  122. function sendWxMessage(openid, name, consult) {
  123. var replyContent = message.content;
  124. switch (Number.parseInt(message.contentType)) {
  125. case CONTENT_TYPES.Image:
  126. replyContent = "[图片]";
  127. break;
  128. case CONTENT_TYPES.Audio:
  129. replyContent = "[语音]";
  130. break;
  131. case CONTENT_TYPES.Article:
  132. case CONTENT_TYPES.GoTo:
  133. case CONTENT_TYPES.SessionBegin:
  134. case CONTENT_TYPES.SessionEnd:
  135. return;
  136. default:
  137. break;
  138. }
  139. // 模板消息数据
  140. var msg = {
  141. touser: openid,
  142. template_id: config.wechatConfig.template.consultTemplate,
  143. url: config.wechatConfig.baseUrl + "/wx/html/yszx/html/consulting-doctor.html?openid=" + openid +
  144. "&consult=" + consult.consult + "&toUser=" + message.to,
  145. data: {
  146. first: {value: "您的健康咨询有新的回复", color: "#000000"}
  147. , remark: {value: "", color: "#000000"}
  148. , keyword1: {value: consult.symptoms, color: "#000000"}
  149. , keyword2: {value: replyContent, color: "#000000"}
  150. , keyword3: {value: name, color: "#000000"}
  151. }
  152. };
  153. // 发送模板消息
  154. wechatUtil.sendWxTemplateMessage(msg);
  155. }
  156. // 查询居民openid
  157. patientRepo.getPatientOpenid(message.to, function (err, result) {
  158. if (err) {
  159. modelUtil.emitDbError(self.eventEmitter, "get patient openid failed", err);
  160. return;
  161. }
  162. var openid = result && result.length > 0 ? result[0].openid : "";
  163. if (openid) {
  164. // 查询医生信息
  165. doctorRepo.getDoctorInfo(message.from, function (err, result) {
  166. if (err) {
  167. modelUtil.emitDbError(self.eventEmitter, "get doctor info failed", err);
  168. return;
  169. }
  170. if (result && result.length > 0) {
  171. var name = result[0].name;
  172. if (message.group) {
  173. groupRepo.getGroupConsultInfo(message.group, function (err, result) {
  174. if (err) {
  175. modelUtil.emitDbError(self.eventEmitter, "get patient and doctor consult info failed", err);
  176. return;
  177. }
  178. var consult = result && result.length > 0 ? result[0] : "";
  179. if (consult) {
  180. sendWxMessage(openid, name, consult);
  181. }
  182. });
  183. } else {
  184. // 查询医生与居民对应的咨询信息
  185. patientRepo.getPatientDoctorConsult(message.to, message.from, function (err, result) {
  186. if (err) {
  187. modelUtil.emitDbError(self.eventEmitter, "get patient and doctor consult info failed", err);
  188. return;
  189. }
  190. var consult = result && result.length > 0 ? result[0] : "";
  191. if (consult) {
  192. sendWxMessage(openid, name, consult);
  193. }
  194. });
  195. }
  196. } else {
  197. modelUtil.emitDbError(self.eventEmitter, "can not find doctor info", err);
  198. }
  199. });
  200. } else {
  201. modelUtil.logError("patient does not bind wechat", err);
  202. }
  203. });
  204. };
  205. }
  206. module.exports = Patient;