|
@ -11,6 +11,7 @@ let WechatSDK = require('../../util/wechat.sdk');
|
|
|
let PatientRepo = require('../../repository/mysql/patient.repo');
|
|
|
let TopicRepo = require("../../repository/mysql/topics.repo.js");
|
|
|
let ParticipantRepo = require("../../repository/mysql/participant.repo");
|
|
|
let WeChatTokenRepo = require("../../repository/mysql/wechat.token.repo");
|
|
|
let redisConn = RedisClient.redisClient().connection;
|
|
|
let clientCache = require('../socket.io/client.cache').clientCache();
|
|
|
let configFile = require('../../include/commons').CONFIG_FILE;
|
|
@ -65,7 +66,7 @@ class WechatClient extends RedisModel {
|
|
|
message.content_type == CONTENT_TYPES.PrescriptionFollowupContent)) {
|
|
|
let patientClient = clientCache.findById(targetUserId);
|
|
|
let doctorClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);
|
|
|
let pc_doctorClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
|
|
|
// let pc_doctorClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
|
|
|
var count = 0;
|
|
|
if (patientClient) {
|
|
|
log.warn("User's wechat endpoint is online, sending via web socket. User id: ", targetUserId);
|
|
@ -82,17 +83,17 @@ class WechatClient extends RedisModel {
|
|
|
}else{
|
|
|
count++;
|
|
|
}
|
|
|
if(pc_doctorClient){
|
|
|
log.error("doctor sessionid "+pc_doctorClient.sessionId);
|
|
|
log.error("patient sessionid "+patientClient.sessionId);
|
|
|
if(patientClient.sessionId==pc_doctorClient.sessionId){
|
|
|
//用户socket在线,推送给用户后,告知医生此消息已读
|
|
|
WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
WechatClient.sendReadDoctor(pc_doctorClient.socket, message);
|
|
|
}
|
|
|
}else{
|
|
|
count++;
|
|
|
}
|
|
|
// if(pc_doctorClient){
|
|
|
// log.error("doctor sessionid "+pc_doctorClient.sessionId);
|
|
|
// log.error("patient sessionid "+patientClient.sessionId);
|
|
|
// if(patientClient.sessionId==pc_doctorClient.sessionId){
|
|
|
// //用户socket在线,推送给用户后,告知医生此消息已读
|
|
|
// WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
// WechatClient.sendReadDoctor(pc_doctorClient.socket, message);
|
|
|
// }
|
|
|
// }else{
|
|
|
// count++;
|
|
|
// }
|
|
|
|
|
|
if(count==0){
|
|
|
log.error("doctor client not found");
|
|
@ -189,8 +190,8 @@ class WechatClient extends RedisModel {
|
|
|
|
|
|
static sendReadDoctorByDoctorId(doctorId, message) {
|
|
|
let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
|
|
|
let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
|
|
|
if(!doctorClient&&!pc_doctorClient){
|
|
|
// let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
|
|
|
if(!doctorClient){
|
|
|
log.warn("target doctor is not online!");
|
|
|
return;
|
|
|
}
|
|
@ -217,43 +218,36 @@ class WechatClient extends RedisModel {
|
|
|
count++;
|
|
|
}
|
|
|
//发送pc版医生端
|
|
|
if(pc_doctorClient&&sendDoctorClient&&sendDoctorClient.sessionId==pc_doctorClient.sessionId){
|
|
|
WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
pc_doctorClient.socket.emit('message', {
|
|
|
id: message.id,
|
|
|
session_id: message.session_id,
|
|
|
sender_id: message.sender_id,
|
|
|
sender_name: message.sender_name,
|
|
|
content_type: message.content_type,
|
|
|
content: message.content,
|
|
|
timestamp: ObjectUtil.timestampToLong(message.timestamp),
|
|
|
type: message.content_type, // legacy support
|
|
|
name: message.sender_name,
|
|
|
read:"one"
|
|
|
});
|
|
|
}else{
|
|
|
count++;
|
|
|
}
|
|
|
// if(pc_doctorClient&&sendDoctorClient&&sendDoctorClient.sessionId==pc_doctorClient.sessionId){
|
|
|
// WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
// pc_doctorClient.socket.emit('message', {
|
|
|
// id: message.id,
|
|
|
// session_id: message.session_id,
|
|
|
// sender_id: message.sender_id,
|
|
|
// sender_name: message.sender_name,
|
|
|
// content_type: message.content_type,
|
|
|
// content: message.content,
|
|
|
// timestamp: ObjectUtil.timestampToLong(message.timestamp),
|
|
|
// type: message.content_type, // legacy support
|
|
|
// name: message.sender_name,
|
|
|
// read:"one"
|
|
|
// });
|
|
|
// }else{
|
|
|
// count++;
|
|
|
// }
|
|
|
|
|
|
if(count==2){
|
|
|
if(count==1){
|
|
|
log.warn("doctor is not in the same session or is not online");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static sendSocketMessageToDoctor(doctorId, message) {
|
|
|
let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
|
|
|
let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
|
|
|
if(!doctorClient&&!pc_doctorClient){
|
|
|
// let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
|
|
|
if(!doctorClient){
|
|
|
log.warn("target doctor is not online!");
|
|
|
return;
|
|
|
}
|
|
|
// let sendClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);//app医生发送的消息
|
|
|
// if(!sendClient){//pc医生发送的消息
|
|
|
// sendClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
|
|
|
// }
|
|
|
// if(!sendClient){//居民发送的消息
|
|
|
// sendClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.PATIENT);
|
|
|
// }
|
|
|
|
|
|
var count = 0;
|
|
|
if(doctorClient&&message.session_id==doctorClient.sessionId){
|
|
@ -275,24 +269,24 @@ class WechatClient extends RedisModel {
|
|
|
|
|
|
|
|
|
//发送pc端
|
|
|
if(pc_doctorClient&&message.session_id==pc_doctorClient.sessionId){
|
|
|
WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
pc_doctorClient.socket.emit('message', {
|
|
|
id: message.id,
|
|
|
session_id: message.session_id,
|
|
|
sender_id: message.sender_id,
|
|
|
sender_name: message.sender_name,
|
|
|
content_type: message.content_type,
|
|
|
content: message.content,
|
|
|
timestamp: ObjectUtil.timestampToLong(message.timestamp),
|
|
|
type: message.content_type, // legacy support
|
|
|
name: message.sender_name,
|
|
|
});
|
|
|
}else{
|
|
|
count++;
|
|
|
}
|
|
|
// if(pc_doctorClient&&message.session_id==pc_doctorClient.sessionId){
|
|
|
// WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
|
|
|
// pc_doctorClient.socket.emit('message', {
|
|
|
// id: message.id,
|
|
|
// session_id: message.session_id,
|
|
|
// sender_id: message.sender_id,
|
|
|
// sender_name: message.sender_name,
|
|
|
// content_type: message.content_type,
|
|
|
// content: message.content,
|
|
|
// timestamp: ObjectUtil.timestampToLong(message.timestamp),
|
|
|
// type: message.content_type, // legacy support
|
|
|
// name: message.sender_name,
|
|
|
// });
|
|
|
// }else{
|
|
|
// count++;
|
|
|
// }
|
|
|
|
|
|
if(count==2){
|
|
|
if(count==1){
|
|
|
log.warn("doctor is not in the same session or is not online");
|
|
|
}
|
|
|
}
|
|
@ -336,40 +330,25 @@ class WechatClient extends RedisModel {
|
|
|
async.waterfall([
|
|
|
// 获取微信openid
|
|
|
function (callback) {
|
|
|
PatientRepo.findWechatOpenIds(targetUserId, function (err, res) {
|
|
|
PatientRepo.findWechatOpenId(targetUserId, function (err, res) {
|
|
|
if (err) {
|
|
|
ModelUtil.logError("Get wechat openid failed", err);
|
|
|
return;
|
|
|
}
|
|
|
var map = new Map();
|
|
|
res.forEach(function (participant) {
|
|
|
let openid = participant.openid;
|
|
|
if (targetUserId==participant.code) {
|
|
|
if (!openid) {
|
|
|
ModelUtil.logError("User haven't bound with wechat, user id: " + targetUserId);
|
|
|
}
|
|
|
map.set("openid",participant);
|
|
|
}else {
|
|
|
if(!map.has(openid)){
|
|
|
map.set(openid,participant);
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
|
|
|
//
|
|
|
// let openid = result && result.length > 0 ? result[0].openid : null;
|
|
|
// if (!openid) {
|
|
|
// ModelUtil.logError("User haven't bound with wechat, user id: " + targetUserId);
|
|
|
// return;
|
|
|
// }
|
|
|
//
|
|
|
// log.warn("Send via wechat message template, user id: " + targetUserId + ", openid: " + openid);
|
|
|
|
|
|
callback(null, map);
|
|
|
|
|
|
let openid = res;
|
|
|
if (!openid) {
|
|
|
ModelUtil.logError("User haven't bound with wechat, user id: " + targetUserId);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
log.warn("Send via wechat message template, user id: " + targetUserId + ", openid: " + openid);
|
|
|
|
|
|
callback(null, openid);
|
|
|
});
|
|
|
},
|
|
|
// 获取议题信息
|
|
|
function (map, callback) {
|
|
|
function (openid, callback) {
|
|
|
TopicRepo.findLastTopicStatusAndType(message.session_id, function (err, res) {
|
|
|
if (err) {
|
|
|
ModelUtil.logError("Get topic failed", err);
|
|
@ -380,132 +359,56 @@ class WechatClient extends RedisModel {
|
|
|
ModelUtil.logError("Unable to find session last topic");
|
|
|
return;
|
|
|
}
|
|
|
callback(null, map, message.sender_name, res[0]);
|
|
|
callback(null, openid, message.sender_name, res[0]);
|
|
|
});
|
|
|
},
|
|
|
// 发送消息
|
|
|
function (map, senderName, topic,callback) {
|
|
|
let replyContent = message.content;
|
|
|
switch (Number.parseInt(message.content_type)) {
|
|
|
case CONTENT_TYPES.Image:
|
|
|
replyContent = "[图片]";
|
|
|
break;
|
|
|
case CONTENT_TYPES.Audio:
|
|
|
replyContent = "[语音]";
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
function (openid, senderName, topic,callback) {
|
|
|
WeChatTokenRepo.findWxTemplateOne(function (err, res) {
|
|
|
if (err) {
|
|
|
ModelUtil.logError("Get topic failed", err);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var patient = map.get("openid");
|
|
|
map.delete("openid");
|
|
|
let agent = topic.agent;
|
|
|
let consultTitle = topic.type==8?"续方":"健康";
|
|
|
let description = topic.type==8?"续方咨询":topic.description;
|
|
|
let url = config.wechatConfig.baseUrl + (topic.type==8?"/wx/html/yszx/html/prescription-consulting.html":"/wx/html/yszx/html/consulting-doctor.html");
|
|
|
if(agent){//代理人发起的议题
|
|
|
var agentOpenid = "";
|
|
|
if(map.size>0){
|
|
|
for(var key of map.keys()){
|
|
|
var member = map.get(key);
|
|
|
if(agent == member.code){
|
|
|
agentOpenid = key;
|
|
|
var openid = key;
|
|
|
var first = "您的家人("+patient.name+")的"+consultTitle+"咨询有新的回复";
|
|
|
// 发送模板消息
|
|
|
WechatSDK.sendTemplateMessage({
|
|
|
touser: openid,
|
|
|
name: member.name,
|
|
|
patient: member.code,
|
|
|
template_id: config.wechatConfig.template.consultTemplate,
|
|
|
url: url + "?openid=" + openid + "&type="+topic.type+"&doctor="+message.sender_id+
|
|
|
"&consult=" + topic.id + "&toUser=" + member.code + "&toName=" + member.name+"&represented="+patient.code,
|
|
|
data: {
|
|
|
first: {value: first, color: "#000000"}
|
|
|
, remark: {value: "", color: "#000000"}
|
|
|
, keyword1: {value: description, color: "#000000"}
|
|
|
, keyword2: {value: replyContent, color: "#000000"}
|
|
|
, keyword3: {value: senderName, color: "#000000"}
|
|
|
}
|
|
|
}, function (err, res) {
|
|
|
err ? log.error(err) : log.info(res);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
if (!res || res.length == 0) {
|
|
|
ModelUtil.logError("Unable to find session last topic");
|
|
|
return;
|
|
|
}
|
|
|
if(patient.openid&&patient.openid!=agentOpenid){
|
|
|
var first = "您的"+consultTitle+"咨询有新的回复";
|
|
|
// 发送模板消息
|
|
|
WechatSDK.sendTemplateMessage({
|
|
|
touser: patient.openid,
|
|
|
name: targetUserName,
|
|
|
patient: targetUserId,
|
|
|
template_id: config.wechatConfig.template.consultTemplate,
|
|
|
url: url + "?openid=" + patient.openid +"&type="+topic.type+"&doctor="+message.sender_id+
|
|
|
"&consult=" + topic.id + "&toUser=" + targetUserId + "&toName=" + targetUserName+"&represented="+patient.code,
|
|
|
data: {
|
|
|
first: {value: first, color: "#000000"}
|
|
|
, remark: {value: "", color: "#000000"}
|
|
|
, keyword1: {value: description, color: "#000000"}
|
|
|
, keyword2: {value: replyContent, color: "#000000"}
|
|
|
, keyword3: {value: senderName, color: "#000000"}
|
|
|
}
|
|
|
}, function (err, res) {
|
|
|
err ? log.error(err) : log.info(res);
|
|
|
});
|
|
|
let replyContent = message.content;
|
|
|
switch (Number.parseInt(message.content_type)) {
|
|
|
case CONTENT_TYPES.Image:
|
|
|
replyContent = "[图片]";
|
|
|
break;
|
|
|
case CONTENT_TYPES.Audio:
|
|
|
replyContent = "[语音]";
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}else {//自己发起的议题
|
|
|
// 发送模板消息
|
|
|
if(patient.openid){
|
|
|
WechatSDK.sendTemplateMessage({
|
|
|
touser: patient.openid,
|
|
|
name: targetUserName,
|
|
|
patient: targetUserId,
|
|
|
template_id: config.wechatConfig.template.consultTemplate,
|
|
|
url: url + "?openid=" + patient.openid +"&type="+topic.type+"&doctor="+message.sender_id+
|
|
|
"&consult=" + topic.id + "&toUser=" + targetUserId + "&toName=" + targetUserName+"&represented="+patient.code,
|
|
|
data: {
|
|
|
first: {value: "您的"+consultTitle+"咨询有新的回复", color: "#000000"}
|
|
|
, remark: {value: "", color: "#000000"}
|
|
|
, keyword1: {value: description, color: "#000000"}
|
|
|
, keyword2: {value: replyContent, color: "#000000"}
|
|
|
, keyword3: {value: senderName, color: "#000000"}
|
|
|
}
|
|
|
}, function (err, res) {
|
|
|
err ? log.error(err) : log.info(res);
|
|
|
});
|
|
|
}
|
|
|
if(map.size>0){
|
|
|
for(var key of map.keys()){
|
|
|
if(!patient.openid||key!=patient.openid){
|
|
|
var member = map.get(key);
|
|
|
var openid = key;
|
|
|
var first = "您的家人("+patient.name+")的"+consultTitle+"咨询有新的回复";
|
|
|
// 发送模板消息
|
|
|
WechatSDK.sendTemplateMessage({
|
|
|
touser: openid,
|
|
|
name: member.name,
|
|
|
patient: member.code,
|
|
|
template_id: config.wechatConfig.template.consultTemplate,
|
|
|
url: url + "?openid=" + openid +"&type="+topic.type+"&doctor="+message.sender_id+
|
|
|
"&consult=" + topic.id + "&toUser=" + member.code + "&toName=" + member.name+"&represented="+patient.code,
|
|
|
data: {
|
|
|
first: {value: first, color: "#000000"}
|
|
|
, remark: {value: "", color: "#000000"}
|
|
|
, keyword1: {value: description, color: "#000000"}
|
|
|
, keyword2: {value: replyContent, color: "#000000"}
|
|
|
, keyword3: {value: senderName, color: "#000000"}
|
|
|
}
|
|
|
}, function (err, res) {
|
|
|
err ? log.error(err) : log.info(res);
|
|
|
});
|
|
|
}
|
|
|
let url = config.wechatConfig.baseUrl + res.url;
|
|
|
|
|
|
WechatSDK.sendTemplateMessage({
|
|
|
touser: openid,
|
|
|
name: targetUserName,
|
|
|
patient: targetUserId,
|
|
|
template_id: config.wechatConfig.template.consultTemplate,
|
|
|
url: url + "?openid=" + openid +"&type="+topic.type+"&doctor="+message.sender_id+
|
|
|
"&consult=" + topic.id + "&toUser=" + targetUserId + "&toName=" + targetUserName+"&represented="+patient.code,
|
|
|
data: {
|
|
|
first: {value: res.first, color: "#000000"}
|
|
|
, remark: {value: res.remark, color: "#000000"}
|
|
|
, keyword1: {value: topic.description, color: "#000000"}
|
|
|
, keyword2: {value: replyContent, color: "#000000"}
|
|
|
, keyword3: {value: senderName, color: "#000000"}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}, function (err, res) {
|
|
|
err ? log.error(err) : log.info(res);
|
|
|
});
|
|
|
|
|
|
callback(null, null);
|
|
|
});
|
|
|
|
|
|
callback(null, null);
|
|
|
}
|
|
|
],
|
|
|
function (err, res) {
|