123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /**
- * 患者模型。
- */
- "use strict";
- let configFile = require('../include/commons').CONFIG_FILE;
- let config = require('../resources/config/' + configFile);
- let log = require("../util/log.js");
- let BaseModel = require('./base.model');
- let patientRepo = require('../repository/patient.repo');
- let statsRepo = require("../repository/stats.msg.repo.js");
- let pmRepo = require('../repository/private.msg.repo');
- let objectUtil = require("../util/objectUtil.js");
- let modelUtil = require('../util/modelUtil');
- let Doctor = require('../models/doctor');
- const CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
- let clientCache = require('./socket.io/client.cache').clientCache();
- let doctorRepo = require('../repository/doctor.repo');
- let groupRepo = require('../repository/group.repo');
- let wechatUtil = require('../util/wechatUtil');
- class Patient extends BaseModel {
- constructor() {
- super();
- }
- /**
- * 是否为患者代码。
- *
- * @param code
- * @param passedCallback 测试通过回调
- * @param failedCallback 测试失败回调
- */
- static isPatientCode(code, passedCallback, failedCallback) {
- patientRepo.isPatientCode(code, function (err, result) {
- if (err) {
- log.error('Send message to patient failed: ', err);
- return;
- }
- if (result[0].c > 0) {
- passedCallback();
- } else {
- failedCallback();
- }
- });
- }
- /**
- * 向患者发送消息。
- *
- * 注意:患者消息的保存发送与医生的实现不同。
- *
- * @param message 消息
- */
- sendMessage(message) {
- // 保存消息
- let self = this;
- let tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
- pmRepo.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, 'Save private message failed', err);
- return;
- }
- // 结束网络连接,后续操作继续执行
- pmRepo.findOnePatientMessage(result.insertId, function (err, msg) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, 'Save private message success, but return last message failed', err);
- return;
- }
- modelUtil.emitData(self.eventEmitter, Doctor.fillMessages(msg));
- // 通过Web Socket推送给患者
- let patientClient = clientCache.findById(message.to);
- if (!patientClient) {
- log.warn("User is not online, user id: ", message.to);
- //发送微信模板消息
- self.sendConsultWechatReplyTempMsg(message);
- return;
- }
- let row = msg[0];
- row.timestamp = objectUtil.timestampToLong(row.timestamp);
- patientClient.socketServer.sockets.emit('message', row);
- });
- // 更新自身的聊天统计信息
- statsRepo.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
- if (err) log.error(err);
- });
- // 更新对端的聊天统计信息
- statsRepo.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
- if (err) log.error(err);
- });
- });
- };
- /**
- * 推送群组消息给居民
- *
- * @param message
- */
- pushGroupMessage(message) {
- let self = this;
- // 通过Web Socket推送给患者
- let patientClient = clientCache.findById(message.to);
- if (!patientClient) {
- log.warn("User is not online, user id: ", message.to);
- //发送微信模板消息
- self.sendConsultWechatReplyTempMsg(message);
- return;
- }
- groupRepo.getOnGroupMsg(message.msgId, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, "get group msg info failed", err);
- }
- var msg = result ? result[0] : "";
- if (msg) {
- patientClient.socketServer.sockets.emit('message', msg);
- }
- })
- };
- /**
- * 发送微信模板消息给居民
- *
- * @param message
- */
- sendConsultWechatReplyTempMsg(message) {
- let selt = this;
- // 发送微信消息
- function sendWxMessage(openid, name, consult) {
- var replyContent = message.content;
- switch (Number.parseInt(message.contentType)) {
- case CONTENT_TYPES.Image:
- replyContent = "[图片]";
- break;
- case CONTENT_TYPES.Audio:
- replyContent = "[语音]";
- break;
- case CONTENT_TYPES.Article:
- case CONTENT_TYPES.GoTo:
- case CONTENT_TYPES.SessionBegin:
- case CONTENT_TYPES.SessionEnd:
- return;
- default:
- break;
- }
- // 模板消息数据
- var msg = {
- touser: openid,
- template_id: config.wechatConfig.template.consultTemplate,
- url: config.wechatConfig.baseUrl + "/wx/html/yszx/html/consulting-doctor.html?openid=" + openid +
- "&consult=" + consult.consult + "&toUser=" + message.to,
- data: {
- first: {value: "您的健康咨询有新的回复", color: "#000000"}
- , remark: {value: "", color: "#000000"}
- , keyword1: {value: consult.symptoms, color: "#000000"}
- , keyword2: {value: replyContent, color: "#000000"}
- , keyword3: {value: name, color: "#000000"}
- }
- };
- // 发送模板消息
- wechatUtil.sendWxTemplateMessage(msg);
- }
- // 查询居民openid
- patientRepo.getPatientOpenid(message.to, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, "get patient openid failed", err);
- return;
- }
- var openid = result && result.length > 0 ? result[0].openid : "";
- if (openid) {
- // 查询医生信息
- doctorRepo.getDoctorInfo(message.from, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, "get doctor info failed", err);
- return;
- }
- if (result && result.length > 0) {
- var name = result[0].name;
- if (message.group) {
- groupRepo.getGroupConsultInfo(message.group, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, "get patient and doctor consult info failed", err);
- return;
- }
- var consult = result && result.length > 0 ? result[0] : "";
- if (consult) {
- sendWxMessage(openid, name, consult);
- }
- });
- } else {
- // 查询医生与居民对应的咨询信息
- patientRepo.getPatientDoctorConsult(message.to, message.from, function (err, result) {
- if (err) {
- modelUtil.emitDbError(self.eventEmitter, "get patient and doctor consult info failed", err);
- return;
- }
- var consult = result && result.length > 0 ? result[0] : "";
- if (consult) {
- sendWxMessage(openid, name, consult);
- }
- });
- }
- } else {
- modelUtil.emitDbError(self.eventEmitter, "can not find doctor info", err);
- }
- });
- } else {
- modelUtil.logError("patient does not bind wechat", err);
- }
- });
- };
- }
- module.exports = Patient;
|