浏览代码

Merge branch 'im-internet-hospital' of huangwenjie/im.doctor into im-internet-hospital

huangwenjie 6 年之前
父节点
当前提交
cc54642b26

+ 13 - 0
src/server/handlers/socket.handler.js

@ -8,6 +8,7 @@ let log = require("../util/log.js");
let clientCache = require('../models/socket.io/client.cache').clientCache();
let PatientClient = require('./../models/socket.io/patient.client');
let PcPatientClient = require('./../models/socket.io/pcPatient.client');
let PcDoctorClient  = require('./../models/socket.io/pcDoctor.client');
let DoctorClient  = require('./../models/socket.io/doctor.client');
let RtcClient  = require('../models/socket.io/rtc.client.js');
@ -45,6 +46,8 @@ class SocketHandler {
                        data.userId = "pc_"+data.userId;
                    }else if("pcim_doctor"===data.clientType){//用于pcim 消息通知
                        data.userId = "pcim_"+data.userId;
                    }else if("pc_patient"===data.clientType){//新增居民PC登陆类型
                        data.userId= "pcpatient_"+data.userId;
                    }
                    if(clientCache.removeByUserId(data.userId)){
@ -62,6 +65,16 @@ class SocketHandler {
                        clientCache.addClient(patientClient);
                        users.login(data.userId, 10, '', '');
                        socket.emit('ack', {});
                    }else if("pc_patient"===data.clientType){
                        //用于pcpatient 消息通知
                        let pcpatientClient = new PcPatientClient(socket, socketServer);
                        pcpatientClient.userId = data.userId;
                        pcpatientClient.password = data.password;
                        pcpatientClient.clientType = data.clientType;
                        pcpatientClient.sessionId = data.sessionId||"";
                        clientCache.addClient(pcpatientClient);
                        users.login(data.userId, 10, '', '');
                        socket.emit('ack', {});
                    }else if("pcim_doctor"===data.clientType){
                        //用于pcim 消息通知
                        let pcdoctorClient = new PcDoctorClient(socket, socketServer);

+ 1 - 0
src/server/include/commons.js

@ -119,6 +119,7 @@ exports.CONTENT_TYPES = CONTENT_TYPES;
exports.SOCKET_TYPES={
    PATIENT:"patient",
    PC_PATIENT:"pc_patient",
    DOCTOR:"doctor",
    PC_DOCTOR:"pc_doctor",
    PCIM_DOCTOR:"pcim_doctor",

+ 1 - 1
src/server/models/client/app.client.js

@ -75,7 +75,7 @@ class AppClient extends RedisModel {
                        if (err) throw err;
                        let userStatus = null;
                        if (res.length > 0) {
                        if (res && res.length > 0) {
                            userStatus = {
                                platform: res[0].platform,
                                device_token: res[0].device_token,

+ 52 - 12
src/server/models/client/wechat.client.js

@ -67,32 +67,64 @@ class WechatClient extends RedisModel {
            message.content_type == CONTENT_TYPES.ChangeDoorDoctor ||
            message.content_type == CONTENT_TYPES.ChangeDoorPackageItems)) {
            let patientClient = clientCache.findById(targetUserId);
            let pc_patientClient = clientCache.findById("pcpatient_"+targetUserId);
            let doctorClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.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);
                WechatClient.sendViaWebSocket(patientClient.socket, message);
            if (patientClient || pc_patientClient) {
                if(patientClient){
                    log.warn("User's wechat endpoint is online, sending via web socket. User id: ", targetUserId);
                    WechatClient.sendViaWebSocket(patientClient.socket, message);
                }
                if(pc_patientClient){
                    log.warn("User's pc endpoint is online, sending via web socket. User id: ", targetUserId);
                    WechatClient.sendViaWebSocket(pc_patientClient.socket, message);
                }
                if(doctorClient){
                    log.error("doctor sessionid "+doctorClient.sessionId);
                    log.error("patient sessionid "+patientClient.sessionId);
                    if(patientClient.sessionId==doctorClient.sessionId){
                    if(patientClient && patientClient.sessionId==doctorClient.sessionId){
                        log.error("patient sessionid "+patientClient.sessionId);
                    //用户socket在线,推送给用户后,告知医生此消息已读
                        WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
                        WechatClient.sendReadDoctor(doctorClient.socket, message);
                    }
                    if(pc_patientClient && pc_patientClient.sessionId==doctorClient.sessionId){
                        log.error("pc_patient sessionid "+pc_patientClient.sessionId);
                        //用户socket在线,推送给用户后,告知医生此消息已读
                        WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
                        WechatClient.sendReadDoctor(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);
                    if(patientClient){
                        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);
                        }
                    }
                    if(pc_patientClient){
                        log.error("pc_patient sessionid "+pc_patientClient.sessionId);
                        if(pc_patientClient.sessionId==pc_doctorClient.sessionId){
                            //用户socket在线,推送给用户后,告知医生此消息已读
                            WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
                            WechatClient.sendReadDoctor(pc_doctorClient.socket, message);
                        }
                    }
                }else{
                    count++;
                }
@ -102,7 +134,7 @@ class WechatClient extends RedisModel {
                }
            } else {
                log.info("User's wechat endpoint is not online, sending via wechat template message. User id: ", targetUserId);
                log.info("User's wechat and pc  endpoint is not online, sending via wechat template message. User id: ", targetUserId);
                var isSendWXTem = true;//是否发送微信模板
                if(message.content_type == CONTENT_TYPES.PrescriptionBloodStatus||message.content_type == CONTENT_TYPES.PrescriptionFollowupContent){
                    var content = JSON.parse(message.content);
@ -120,8 +152,16 @@ class WechatClient extends RedisModel {
            }
        } else if(message.content_type == CONTENT_TYPES.TopicEnd){
            let patientClient = clientCache.findById(targetUserId);
            if(patientClient)//结束咨询的告知患者
            if(patientClient){//结束咨询的告知患者
                WechatClient.sendViaWebSocket(patientClient.socket, message);
            }
            let pc_patientClient = clientCache.findById("pcpatient_"+targetUserId);
            if(pc_patientClient)//结束咨询的告知患者
            {
                WechatClient.sendViaWebSocket(pc_patientClient.socket, message);
            }
        }
    };

+ 1 - 1
src/server/models/migration/migration.redis.js

@ -270,7 +270,7 @@ class MigrateRedis extends RedisModel{
                let repoProto = isPatientId ? PatientRepo : DoctorRepo;
                repoProto.findOne(userId, function (err, res) {
                    let user = isPatientId ? new Patient() : new Doctor();
                    if (res.length > 0) {
                    if (res && res.length > 0) {
                        user.name = res[0].name;
                        user.sex = res[0].sex;
                        user.birthdate = res[0].birthdate;

+ 1 - 1
src/server/models/sessions/participants.js

@ -261,7 +261,7 @@ class Participants extends RedisModel {
                ModelUtil.emitOK(self.eventEmitter, {status:-1,message: "会话查询失败!"});
                return;
            }
            if(res.length!=1){
            if(res && res.length!=1){
                ModelUtil.emitOK(self.eventEmitter, {status:200,message: "用户未创建咨询!"});
                return;
            }else{

+ 22 - 18
src/server/models/sessions/sessions.js

@ -223,7 +223,7 @@ class Sessions extends RedisModel {
        function callCreateSession(businessType) {
            //查找该sessionId是否存在存在则直接返回实例
            SessionRepo.findOne(sessionId, function (err, res) {
                if (res.length > 0) {//已经存在
                if (res && res.length > 0) {//已经存在
                    //已存在的会话不修改名称
                    name = res[0].name;
                    //更新成员
@ -415,7 +415,7 @@ class Sessions extends RedisModel {
            // },
            function (callback) {
                SessionRepo.findAllByType(userId,businessType,page,size,function(err,res){
                    if (res.length == 0) {
                    if (res && res.length == 0) {
                        ModelUtil.emitOK(self.eventEmitter, []);
                        return;
                    }
@ -625,11 +625,11 @@ class Sessions extends RedisModel {
                                                sessionName = res[0].name;
                                            }
                                            var bir = new Date().getTime();
                                            if (res.length != 0 && res[0].birthdate) {
                                            if (res && res.length != 0 && res[0].birthdate) {
                                                bir = res[0].birthdate.getTime();
                                            }
                                            var sex = 1;
                                            if (res.length != 0 && res[0].sex) {
                                            if (res && res.length != 0 && res[0].sex) {
                                                sex = res[0].sex;
                                            }
                                            //end
@ -700,13 +700,16 @@ class Sessions extends RedisModel {
     * @param status 0:进行中的会话1:结束的会话
     */
    getUserSessionsByType(userId,type,page, size,status) {
        logger.info("根据用户类型获取用户的session列表1: ");
        let userSessionKey = RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, userId);
        let self = this;
        logger.info("根据用户类型获取用户的session列表2: ");
        async.waterfall([
            // 获取会话ID列表
            function (callback) {
                SessionRepo.findListByType(userId,type,page,size,status,function(err,res){
                    if (res.length == 0) {
                    logger.info("根据用户类型获取用户的session列表: res :"+res);
                    if (res == null || res.length == 0) {
                        ModelUtil.emitOK(self.eventEmitter, []);
                        return;
                    }
@ -717,6 +720,8 @@ class Sessions extends RedisModel {
            function (sessionIds) {
                let sessionList = [];
                let functionList = [];
                for (let j = 0; j < sessionIds.length; j++) {
                    let fun = function (index, callback) {
                        if (!callback) {
@ -756,7 +761,6 @@ class Sessions extends RedisModel {
                                }
                                if (!lastFetchTime) lastFetchTime = new Date().getTime();
                                // 计算未读消息数
                                let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
                                redis.zcountAsync(messagesByTimestampKey, parseInt(lastFetchTime)+1, new Date().getTime())
@ -769,11 +773,11 @@ class Sessions extends RedisModel {
                                                sessionName = res[0].name;
                                            }
                                            var bir = new Date().getTime();
                                            if (res.length != 0 && res[0].birthdate) {
                                            if (res && res.length != 0 && res[0].birthdate) {
                                                bir = res[0].birthdate.getTime();
                                            }
                                            var sex = 1;
                                            if (res.length != 0 && res[0].sex) {
                                            if (res && res.length != 0 && res[0].sex) {
                                                sex = res[0].sex;
                                            }
@ -831,7 +835,7 @@ class Sessions extends RedisModel {
            // 获取会话ID列表
            function (callback) {
                SessionRepo.findAllByTypeAndStatus(userId,businessType,status,page,size,function(err,res){
                    if (res.length == 0) {
                    if (res && res.length == 0) {
                        ModelUtil.emitOK(self.eventEmitter, []);
                        return;
                    }
@ -911,7 +915,7 @@ class Sessions extends RedisModel {
                                                sessionName = res[0].name;
                                            }
                                            var bir = new Date().getTime();
                                            if (res.length != 0 && res[0].birthdate) {
                                            if (res && res.length != 0 && res[0].birthdate) {
                                                bir = res[0].birthdate.getTime();
                                            }
                                            var sex = 1;
@ -981,8 +985,8 @@ class Sessions extends RedisModel {
        if (!start_msg_id && !end_msg_id) {
            redis.zrevrangeAsync(message_timestamp_key, 0, 0).then(function (res) {
                logger.info("redis return res-----"+res);
                log.info("session.js--830---getMessages  res.length: " + res.length);
                if (res.length == 0) {
                // log.info("session.js--830---getMessages  res.length: " + res.length);
                if (res && res.length == 0) {
                    //修复应redis没有缓冲聊天记录导致会话列表加载不出来
                    // cache messages
                    let messagesKey = RedisModel.makeRedisKey(REDIS_KEYS.Messages, sessionId);
@ -1026,7 +1030,7 @@ class Sessions extends RedisModel {
                }
                start_msg_id = res[0];
                redis.zrangeAsync(message_timestamp_key, 0, 0).then(function (res) {
                    if (res.length == 0) {
                    if (res && res.length == 0) {
                        if (handler) {
                            handler(null, res);
                            return;
@ -1055,7 +1059,7 @@ class Sessions extends RedisModel {
            })
        } else if (!start_msg_id) {
            redis.zrevrangeAsync(message_timestamp_key, 0, 0).then(function (res) {
                if (res.length == 0) {
                if (res && res.length == 0) {
                    if (handler) {
                        handler(null, res);
                        return;
@ -1083,7 +1087,7 @@ class Sessions extends RedisModel {
            })
        } else if (!end_msg_id) {
            redis.zrangeAsync(message_timestamp_key, 0, 0).then(function (res) {
                if (res.length == 0) {
                if (res && res.length == 0) {
                    ModelUtil.emitOK(self.eventEmitter, res);
                    return;
                }
@ -1176,7 +1180,7 @@ class Sessions extends RedisModel {
                        if(endMsgScore>startMsgScore){
                            redis.zrangebyscoreAsync(messagesTimestampKey, startMsgScore, endMsgScore, "limit", offset, count)
                                .then(function (res) {
                                    if (res.length == 0) {
                                    if (res && res.length == 0) {
                                        handler(null, []);
                                        return;
                                    }
@ -1208,7 +1212,7 @@ class Sessions extends RedisModel {
                            // 从消息时间表中过滤出要获取的消息ID列表,倒序取出消息
                            redis.zrevrangebyscoreAsync(messagesTimestampKey, startMsgScore, endMsgScore, "limit", offset, count)
                                .then(function (res) {
                                    if (res.length == 0) {
                                    if (res && res.length == 0) {
                                        handler(null, []);
                                        return;
                                    }
@ -1263,7 +1267,7 @@ class Sessions extends RedisModel {
                return;
            }
            if (res.length == 0) {
            if (res && res.length == 0) {
                if(handler)
                {
                    handler(err,count);

+ 19 - 0
src/server/models/socket.io/pcPatient.client.js

@ -0,0 +1,19 @@
/**
 * 患者端socket.
 *
 * author: huangwenjie
 * since: 2019/07/01
 */
"use strict";
var util = require('util');
var Client = require('./client');
class pcPatientClient extends Client{
    constructor(socket, socketServer){
        super(socket, socketServer);
    }
}
module.exports = pcPatientClient;

+ 3 - 3
src/server/models/user/users.js

@ -55,7 +55,7 @@ class Users extends RedisModel {
                let repoProto = isPatientId ? PatientRepo : DoctorRepo;
                repoProto.findOne(userId, function (err, res) {
                    let user = isPatientId ? new Patient() : new Doctor();
                    if (res.length > 0) {
                    if (res && res.length > 0) {
                        user.name = res[0].name;
                        user.sex = res[0].sex;
                        user.birthdate = res[0].birthdate;
@ -322,7 +322,7 @@ class Users extends RedisModel {
                        .del(userStatusKey)
                        .execAsync()
                        .then(function (res) {
                            if (res.length > 0 && res[0] === 0) {
                            if (res && res.length > 0 && res[0] === 0) {
                                ModelUtil.emitDataNotFound(self.eventEmitter, {message: "User not found."});
                            } else {
                                ModelUtil.emitOK(self.eventEmitter, {});
@ -366,7 +366,7 @@ class Users extends RedisModel {
                    });
                },
                function (res, callback) {
                    if (res.length === 0) return false;
                    if (res && res.length === 0) return false;
                    callback(null, res[0].is_patient);
                }

+ 1 - 1
src/server/repository/mysql/message.repo.js

@ -30,7 +30,7 @@ class MessageRepo {
        SessionRepo.findOne(sessionId, function (err, res) {
            if (!err) {
                if (res.length == 0) {
                if (res && res.length == 0) {
                    handler(null, null);
                    return;
                }

+ 1 - 1
src/server/repository/mysql/session.repo.js

@ -151,7 +151,7 @@ class SessionRepo {
            + DB_TABLES.Sessions + " s where s.id in(" + sql + ") and s.type=? ";
        if(status != null && status != ""){
            sessionSQL += " s.id="+status
            sessionSQL += " and s.status="+status
        }
        sessionSQL +=  " order by s.last_message_time desc limit "+page+","+pagesize;

+ 2 - 2
src/server/repository/mysql/topics.repo.js

@ -37,7 +37,7 @@ class TopicRepo {
            sql: sql,
            args: [sessionId],
            handler: function (err, res) {
                if (res.length == 0) {
                if (res && res.length == 0) {
                    handler(null, null);
                } else {
                    TopicRepo.findTopicStatus(res[0].id, handler);
@ -281,7 +281,7 @@ class TopicRepo {
            sql: sql,
            args: [sessionId],
            handler: function (err, res) {
                if (res.length == 0) {
                if (res && res.length == 0) {
                    handler(null, null);
                } else {
                    TopicRepo.findTopicStatusAndType(res[0].id, handler);

+ 1 - 1
src/server/resources/config/config.test.js

@ -4,7 +4,7 @@ let imDbConfig = {
    host: '172.26.0.104',
    user: 'ssgg',
    password: 'ssgg@jkzl2019',
    database: 'im_internt_hospital',
    database: 'im_internet_hospital',
    connectionLimit: '50',
    charset: 'utf8mb4'
};