|
@ -121,8 +121,12 @@ class Sessions extends RedisModel {
|
|
|
}
|
|
|
} else {
|
|
|
if (!sessionId) {
|
|
|
handler("MUC OR GROUP session sessionId is not allow null .", null);
|
|
|
return;
|
|
|
if(type == SESSION_TYPES.DISCUSSION){
|
|
|
sessionId = messageId;
|
|
|
}else {
|
|
|
handler("MUC OR GROUP session sessionId is not allow null .", null);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
callBusinessType(sessionId);
|
|
|
}
|
|
@ -130,6 +134,8 @@ class Sessions extends RedisModel {
|
|
|
function callBusinessType(sessionId) {
|
|
|
if(type == SESSION_TYPES.MUC||type == SESSION_TYPES.PRESCRIPTION){
|
|
|
callCreate(sessionId, SESSION_BUSINESS_TYPE.PATIENT);
|
|
|
}else if(type==SESSION_TYPES.DISCUSSION||type==SESSION_TYPES.GROUP){
|
|
|
callCreate(sessionId, SESSION_BUSINESS_TYPE.DOCTOR);
|
|
|
}else {
|
|
|
ParticipantRepo.getBusinessType(participantIdArray, function (err, businessType) {
|
|
|
callCreate(sessionId, businessType);
|
|
@ -186,6 +192,9 @@ class Sessions extends RedisModel {
|
|
|
sessionId = res;
|
|
|
callBusinessType();
|
|
|
});
|
|
|
} else if(type == SESSION_TYPES.DISCUSSION){
|
|
|
sessionId = messageId;
|
|
|
callBusinessType();
|
|
|
} else {
|
|
|
return handler("MUC模式和团队模式,不允许sessionId为空!", null);
|
|
|
}
|
|
@ -197,7 +206,7 @@ class Sessions extends RedisModel {
|
|
|
function callBusinessType() {
|
|
|
if(type==SESSION_TYPES.MUC||type==SESSION_TYPES.PRESCRIPTION){
|
|
|
callCreateSession(SESSION_BUSINESS_TYPE.PATIENT);
|
|
|
}else if(type==SESSION_TYPES.SPECIALISTGROUP){
|
|
|
}else if(type==SESSION_TYPES.SPECIALISTGROUP||type==SESSION_TYPES.DISCUSSION){
|
|
|
callCreateSession(SESSION_BUSINESS_TYPE.DOCTOR);
|
|
|
}else{
|
|
|
ParticipantRepo.getBusinessType(participantIdArray, function (err, businessType) {
|
|
@ -661,6 +670,149 @@ class Sessions extends RedisModel {
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找会话数
|
|
|
* @param userId
|
|
|
* @param type
|
|
|
* @param handler
|
|
|
*/
|
|
|
getSessionCountByType(userId,type,handler){
|
|
|
let self = this;
|
|
|
SessionRepo.findSessionCountByType(userId,type,function (err,res) {
|
|
|
if(res){
|
|
|
ModelUtil.emitOK(self.eventEmitter,{status:200,count:res[0].count});
|
|
|
return;
|
|
|
}else {
|
|
|
ModelUtil.emitOK(self.eventEmitter,{status:200,count:0});
|
|
|
return;
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户类型获取用户的session列表
|
|
|
* @param userId
|
|
|
* @param page
|
|
|
* @param size
|
|
|
* @param businessType
|
|
|
*/
|
|
|
getUserSessionsByType(userId,type,page, size) {
|
|
|
let userSessionKey = RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, userId);
|
|
|
let self = this;
|
|
|
async.waterfall([
|
|
|
// 获取会话ID列表
|
|
|
function (callback) {
|
|
|
SessionRepo.findListByType(userId,type,page,size,function(err,res){
|
|
|
if (res.length == 0) {
|
|
|
ModelUtil.emitOK(self.eventEmitter, []);
|
|
|
return;
|
|
|
}
|
|
|
callback(null,res);
|
|
|
})
|
|
|
},
|
|
|
// 遍历会话
|
|
|
function (sessionIds) {
|
|
|
let sessionList = [];
|
|
|
let functionList = [];
|
|
|
for (let j = 0; j < sessionIds.length; j++) {
|
|
|
let fun = function (index, callback) {
|
|
|
if (!callback) {
|
|
|
callback = index, index = 0
|
|
|
}
|
|
|
|
|
|
let mysqlSession = sessionIds[index];
|
|
|
let sessionId = mysqlSession.id;
|
|
|
let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
|
|
|
let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
|
|
|
redis.multi()
|
|
|
.hgetall(sessionKey) // 会话实体
|
|
|
.zscore(sessionParticipantsKey, userId) // 用户在此会话中最后一次获取未读消息的时间
|
|
|
.zrange(sessionParticipantsKey, 0, -1)
|
|
|
.zrange(sessionParticipantsKey, 0,-1,'withscores') // 所有用户在此会话中最后一次获取未读消息的时间
|
|
|
.execAsync()
|
|
|
.then(function (res) {
|
|
|
let session = res[0];
|
|
|
let lastFetchTime = res[1];
|
|
|
let users = res[2];
|
|
|
let participantsTimeArray = res[3];
|
|
|
let participantsTime = [];
|
|
|
for(var j = 0 ;j<participantsTimeArray.length;j++){
|
|
|
if(j%2!=0)continue;
|
|
|
let participantsTimeJson = {};
|
|
|
participantsTimeJson[participantsTimeArray[j]] = participantsTimeArray[j+1];
|
|
|
participantsTime.push(participantsTimeJson);
|
|
|
}
|
|
|
let sessionName = "";
|
|
|
let otherUserId = "";
|
|
|
if (session.type == SESSION_TYPES.P2P) {
|
|
|
for (let j in users) {
|
|
|
if (users[j] != userId) {
|
|
|
otherUserId = users[j];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!lastFetchTime) lastFetchTime = new Date().getTime();
|
|
|
|
|
|
// 计算未读消息数
|
|
|
let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
|
|
|
redis.zcountAsync(messagesByTimestampKey, parseInt(lastFetchTime)+1, new Date().getTime())
|
|
|
.then(function (count) {
|
|
|
if (!otherUserId) otherUserId = userId;
|
|
|
ParticipantRepo.findNameById(otherUserId, function (err, res) {
|
|
|
if ((res && res.length == 0) || session.type != SESSION_TYPES.P2P) {
|
|
|
sessionName = session.name;
|
|
|
} else {
|
|
|
sessionName = res[0].name;
|
|
|
}
|
|
|
var bir = new Date().getTime();
|
|
|
if (res.length != 0 && res[0].birthdate) {
|
|
|
bir = res[0].birthdate.getTime();
|
|
|
}
|
|
|
var sex = 1;
|
|
|
if (res.length != 0 && res[0].sex) {
|
|
|
sex = res[0].sex;
|
|
|
}
|
|
|
|
|
|
sessionList.push({
|
|
|
id: sessionId,
|
|
|
name: sessionName,
|
|
|
create_date: new Date(mysqlSession.last_message_time).getTime(),
|
|
|
last_content_type: session.last_content_type,
|
|
|
last_content: session.last_content,
|
|
|
sender_id: session.last_sender_id,
|
|
|
type: session.type,
|
|
|
sender_name: session.last_sender_name,
|
|
|
unread_count: count,
|
|
|
business_type: session.business_type,
|
|
|
sender_sex: sex,
|
|
|
sender_birthday: bir,
|
|
|
participantsTimeArray:participantsTime,
|
|
|
status:session.status,
|
|
|
});
|
|
|
|
|
|
index = (parseInt(index) + 1);
|
|
|
if (index == sessionIds.length) {
|
|
|
ModelUtil.emitOK(self.eventEmitter, sessionList);
|
|
|
} else {
|
|
|
callback(null, index);
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
.catch(function (err) {
|
|
|
logger.error("Get sessions:"+sessionId+" failed: ", err);
|
|
|
});
|
|
|
};
|
|
|
|
|
|
functionList.push(fun);
|
|
|
}
|
|
|
|
|
|
async.waterfall(functionList);
|
|
|
}
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户ID获取用户已经结束咨询的session列表
|
|
|
* @param userId
|
|
@ -1305,6 +1457,7 @@ class Sessions extends RedisModel {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 消息保存到Redis,并更新会话最后状态、用户最后消息获取时间
|
|
|
messages.saveMessageToRedis(sessionId, sessionType, messageId, message);
|
|
|
Messages.updateLastContent(sessionKey, sessionType, sessionName, message);
|
|
@ -1351,6 +1504,10 @@ class Sessions extends RedisModel {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static getMessagesBySessionId(){
|
|
|
|
|
|
}
|
|
|
|
|
|
sendTopicMessages(topicId, message) {
|
|
|
let self = this;
|
|
|
TopicRepo.findAllByTopicId(topicId, function (err, res) {
|
|
@ -1629,20 +1786,46 @@ class Sessions extends RedisModel {
|
|
|
//告知医生新消息
|
|
|
WechatClient.sendSocketMessageToDoctor(targetUserId,message);
|
|
|
|
|
|
WlyySDK.request(targetUserId, '', '', '', '/im/common/message/messages', 'POST', function (err, res) {
|
|
|
let count = 0;
|
|
|
res = JSON.parse(res)
|
|
|
if (res.status == 200) {
|
|
|
let data = res.data;
|
|
|
count = parseInt(JSON.parse(data.imMsgCount).count) + parseInt(data.system.amount) + parseInt(data.healthIndex.amount) + parseInt(data.sign.amount);
|
|
|
let count = 0;
|
|
|
//系统消息
|
|
|
MessageRepo.getWlyyMessageCount(targetUserId,function (err,res) {
|
|
|
if(res){
|
|
|
count = res[0].count;
|
|
|
}
|
|
|
if(config.environment!='local'){//pc版不推送个推,通过redis的publish
|
|
|
AppClient.sendNotification(targetUserId, message,sessionType,count);
|
|
|
});
|
|
|
//im消息
|
|
|
let sessions = new Sessions();
|
|
|
sessions.getAllSessionsUnreadMessageCount(targetUserId,function (err,res) {
|
|
|
if(res){
|
|
|
count += res;
|
|
|
}
|
|
|
//外网pcim通过socket推送
|
|
|
WechatClient.sendPcImSocket(targetUserId,message,sessionType);
|
|
|
});
|
|
|
|
|
|
if(config.environment!='local'){//pc版不推送个推,通过redis的publish
|
|
|
AppClient.sendNotification(targetUserId, message,sessionType,count);
|
|
|
}
|
|
|
//外网pcim通过socket推送
|
|
|
WechatClient.sendPcImSocket(targetUserId,message,sessionType);
|
|
|
|
|
|
// WlyySDK.request(targetUserId, '', '', '', '/im/common/message/messages', 'POST', function (err, res) {
|
|
|
// let count = 0;
|
|
|
// if(err){
|
|
|
// logger.error(err);
|
|
|
// }else {
|
|
|
// logger.error(res);
|
|
|
// res = JSON.parse(res)
|
|
|
// if (res.status == 200) {
|
|
|
// let data = res.data;
|
|
|
// count = parseInt(JSON.parse(data.imMsgCount).count) + parseInt(data.system.amount) + parseInt(data.healthIndex.amount) + parseInt(data.sign.amount);
|
|
|
// }
|
|
|
// }
|
|
|
// if(config.environment!='local'){//pc版不推送个推,通过redis的publish
|
|
|
// AppClient.sendNotification(targetUserId, message,sessionType,count);
|
|
|
// }
|
|
|
// //外网pcim通过socket推送
|
|
|
// WechatClient.sendPcImSocket(targetUserId,message,sessionType);
|
|
|
// });
|
|
|
|
|
|
message.targetUserId = targetUserId;
|
|
|
message.targetUserName = targetUserName;
|
|
|
message.sessionType = sessionType;
|