Forráskód Böngészése

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

huangwenjie 5 éve
szülő
commit
86032650c9

+ 25 - 3
src/server/handlers/socket.handler.js

@ -18,6 +18,7 @@ let Users = require('../models/user/users');
let ModelUtil = require('../util/model.util.js');
let pusher = require('../models/push/pusher.js');
let AppClient = require('../models/client/app.client.js');
let Participants = require('../models/sessions/participants');
let sessions = new Sessions();
let users = new Users();
@ -101,7 +102,8 @@ class SocketHandler {
                            clientCache.addClient(patientClient);
                            users.login(data.userId, 10, '', '');
                            //修改居民在线状态
                            users.changUserRedisLoginStatus(original_login_userid,data.clientType,1);
                            let participants = new Participants();
                            participants.changUserRedisLoginStatus(original_login_userid,data.clientType,1,patientClient.sessionId);
                            socket.emit('ack', {});
                        }else if("pc_patient"===data.clientType || "pc_patient_system"===data.clientType){////新增居民PC外层-登陆类型-20191012-huangnwenjie
                            //用于pcpatient 消息通知
@ -113,7 +115,8 @@ class SocketHandler {
                            clientCache.addClient(pcpatientClient);
                            users.login(data.userId, 10, '', '');
                            //修改居民在线状态
                            users.changUserRedisLoginStatus(original_login_userid,data.clientType,1);
                            let participants = new Participants();
                            participants.changUserRedisLoginStatus(original_login_userid,data.clientType,1,pcpatientClient.sessionId);
                            socket.emit('ack', {});
                        //MDT 登陆类型已迁移到上面的分支-20191012-huangnwenjie
                        // }else if("pcim_doctor"===data.clientType){   登
@ -133,7 +136,8 @@ class SocketHandler {
                            doctorClient.sessionId =  data.sessionId||"";
                            clientCache.addClient(doctorClient);
                            //修改医生在线状态
                            users.changUserRedisLoginStatus(original_login_userid,data.clientType,1);
                            let participants = new Participants();
                            participants.changUserRedisLoginStatus(original_login_userid,data.clientType,1,doctorClient.sessionId);
                            socket.emit('ack', {});
                        }
                    }
@ -310,6 +314,12 @@ class SocketHandler {
                if (client) {
                    //修改居民在线状态
                    // users.changUserRedisLoginStatus(original_login_userid,client.clientType,0);
                    //通知会话的其他成员离线消息
                    if(client.sessionId !=  "system"){
                        let participants = new Participants();
                        participants.emitSessionUsers(client.sessionId,client.userId,"offline");
                    }
                }
                log.info(JSON.stringify(errMsg));
                socket.emit(errMsg);
@ -321,6 +331,12 @@ class SocketHandler {
                if (client) {
                    //修改居民在线状态
                    // users.changUserRedisLoginStatus(original_login_userid,client.clientType,0);
                    //通知会话的其他成员离线消息
                    if(client.sessionId !=  "system"){
                        let participants = new Participants();
                        participants.emitSessionUsers(client.sessionId,client.userId,"offline");
                    }
                    log.info('User logout: ' + client.userId);
                    clientCache.removeByUserId(client.userId);
                }
@ -332,6 +348,12 @@ class SocketHandler {
                if (patientClient) {
                    //修改居民在线状态
                    // users.changUserRedisLoginStatus(original_login_userid,client.clientType,0);
                    //通知会话的其他成员离线消息
                    if(patientClient.sessionId !=  "system"){
                        let participants = new Participants();
                        participants.emitSessionUsers(patientClient.sessionId,patientClient.userId,"offline");
                    }
                    log.info("User disconnect: ", patientClient.userId);
                    clientCache.removeByUserSocket(socket);
                }

+ 107 - 3
src/server/models/client/wechat.client.js

@ -73,8 +73,11 @@ class WechatClient extends RedisModel {
            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);
            let pc_patient_system_Client = clientCache.findByIdAndType("pcpatient_system_"+targetUserId,SOCKET_TYPES.PC_PATIENT_SYSTEM);
            var count = 0;
            if (patientClient || pc_patientClient) {
            if (patientClient || pc_patientClient || pc_patient_system_Client) {
                if(patientClient){
                    log.warn("User's wechat endpoint is online, sending via web socket. User id: ", targetUserId);
@ -86,8 +89,12 @@ class WechatClient extends RedisModel {
                    WechatClient.sendViaWebSocket(pc_patientClient.socket, message);
                }
                if(pc_patient_system_Client){
                    log.warn("User's pc systme endpoint is online, sending via web socket. User id: ", targetUserId);
                    WechatClient.sendViaWebSocket(pc_patient_system_Client.socket, message);
                }
                if(doctorClient){
                if(doctorClient && (patientClient || pc_patientClient) ){
                    log.error("doctor sessionid "+doctorClient.sessionId);
                    if(patientClient && patientClient.sessionId==doctorClient.sessionId){
@ -356,6 +363,58 @@ class WechatClient extends RedisModel {
        }
    }
    /**
     * 推送MDT,医生外层新消息
     * @param doctorId
     * @param message
     */
    static sendMDTSocketMessageToDoctor(doctorId, message) {
        // let doctorMDTClient = clientCache.findByIdAndType("pcim_"+doctorId,SOCKET_TYPES.PCIM_DOCTOR);
        let doctorSYSTEMClient = clientCache.findByIdAndType("pc_system_"+doctorId,SOCKET_TYPES.PC_DOCTOR_SYSTEM);
        let doctorAPPSYSTEMClient = clientCache.findByIdAndType("doctor_system_"+doctorId,SOCKET_TYPES.DOCTOR_SYSTEM);
        //外层
        if(!doctorSYSTEMClient){
            log.warn("target system doctor is not online!");
        }else{
            doctorSYSTEMClient.socket.emit('message', {
                id: message.id,
                session_id: message.session_id,
                sender_id: message.sender_id,
                sender_name: message.sender_name,
                content_type: 1,
                content: {"socket_sms_type":4},
                timestamp: ObjectUtil.timestampToLong(message.timestamp),
                type: message.content_type,          // legacy support
                name: message.sender_name,
                patient_name:message.patient_name,
                patient_age:message.patient_age,
                patient_sex:message.patient_sex
            });
        }
        if(!doctorAPPSYSTEMClient){
            log.warn("target system app doctor is not online!");
        }else{
            doctorAPPSYSTEMClient.socket.emit('message', {
                id: message.id,
                session_id: message.session_id,
                sender_id: message.sender_id,
                sender_name: message.sender_name,
                content_type: 1,
                content: {"socket_sms_type":4},
                timestamp: ObjectUtil.timestampToLong(message.timestamp),
                type: message.content_type,          // legacy support
                name: message.sender_name,
                patient_name:message.patient_name,
                patient_age:message.patient_age,
                patient_sex:message.patient_sex
            });
        }
    }
    static sendPcImSocket(targetId, message, sessionType) {
        if (message.content_type == CONTENT_TYPES.PlainText ||
            message.content_type == CONTENT_TYPES.Image ||
@ -378,9 +437,19 @@ class WechatClient extends RedisModel {
                    session_type: sessionType,
                    from: message.sender_id|| '',
                    data: message.content,
                    business_type: message.business_type || 1
                    business_type: message.business_type || 1,
                    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,
                    patient_name:message.patient_name,
                    patient_age:message.patient_age,
                    patient_sex:message.patient_sex
                });
            }
        }
    }
@ -548,6 +617,41 @@ class WechatClient extends RedisModel {
        }
    }
    //推送托盘消息
    static sendMDTSystemSocketMessage(targetUserId, message) {
        //居民外层消息推送
        let mdtDcotorClient = clientCache.findByIdAndType("pcim_"+targetUserId,SOCKET_TYPES.PCIM_DOCTOR);
        if(!mdtDcotorClient){
            log.warn("target  mdt doctor is not online!");
        }
        var mdtDoctorCount = 0;
        if(mdtDcotorClient&&message.session_id==mdtDcotorClient.sessionId){
            WechatClient.updateParticipantLastFetchTime(mdtDcotorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
            mdtDcotorClient.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{
            mdtDoctorCount++;
        }
        if(mdtDoctorCount==2){
            log.warn("doctor system is not in the same session or is not online");
        }
    }
    /**
     *
     * 发送微信模板消息给居民

+ 4 - 1
src/server/models/messages/messages.js

@ -192,7 +192,10 @@ class Messages extends RedisModel {
                WechatClient.sendSocketMessageToPatient(targetUserId,message,sessionType);
                //推送居民、居民PC、医生、医生PC外层推送
                WechatClient.sendSystemSocketMessage(targetUserId,message,sessionType)
                WechatClient.sendSystemSocketMessage(targetUserId,message,sessionType);
                //推送托盘消息
                // WechatClient.sendMDTSystemSocketMessage(targetUserId,message,sessionType);
                //redis发布消息
                if(config.pubSubSwitch) {//接收订阅消息处理开关,本地运行和测试库单独运行时防止用户接收消息2次

+ 70 - 0
src/server/models/sessions/participants.js

@ -6,12 +6,15 @@
let RedisModel = require('./../redis.model.js');
let ModelUtil = require('../../util/model.util');
let RedisClient = require('../../repository/redis/redis.client.js');
let clientCache = require('../socket.io/client.cache').clientCache();
let ParticipantRepo = require('../../repository/mysql/participant.repo');
let SessionRepo = require('../../repository/mysql/session.repo');
let log = require('../../util/log.js');
let redis = RedisClient.redisClient().connection;
let Users = require('../user/users');
const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
const SOCKET_TYPES = require('../../include/commons').SOCKET_TYPES;
class Participants extends RedisModel {
    constructor() {
@ -253,6 +256,73 @@ class Participants extends RedisModel {
        ParticipantRepo.deleteUserFromMysql(sessionId, user);
    }
    /**
     * 改变登陆者的在线状态
     * @param userid 居民 patient_userid, 医生 doctor_userid
     * @param status 0离线 1在线
     */
    changUserRedisLoginStatus(userid,clientType,status,sessionId){
        let participants = new Participants();
        log.info("changUserRedisLoginStatus,userid:"+userid)
        log.info("changUserRedisLoginStatus,userid:"+userid)
        log.info("changUserRedisLoginStatus,userid:"+userid)
        if(sessionId != "system"){
            this.emitSessionUsers(sessionId,userid,"online");
        }
    }
    /**
     * 通知会话的其他成员离线,上线消息
     * @param sessionId 会话
     * @param sender_id 发送人
     * @param status online,offline
     */
    emitSessionUsers(sessionId,sender_id,status) {
        log.info("emitSessionUsers:sessionId:"+sessionId);
        log.info("emitSessionUsers:sender_id:"+sender_id);
        log.info("emitSessionUsers:status:"+status);
        // 推送消息
        ParticipantRepo.findIds(sessionId, function (err, res) {
            if (err) {
                log.error("Push message from session: get participant's id list failed: ", err);
                return;
            } else {
                res.forEach(function (participant) {
                    Users.isPatientId(participant.id, function (err, isPatient) {
                        let message = {
                            sender_id:sender_id,
                            session_id:sessionId,
                            status:status,
                            is_online_emit:1
                        };
                        if (isPatient) {
                            log.info("emitSessionUsers:isPatient"+participant.id);
                            let patientClient = clientCache.findById(participant.id);
                            if(patientClient){
                                patientClient.socket.emit('message', message);
                            }
                            let pc_patientClient = clientCache.findById("pcpatient_"+participant.id);
                            if(pc_patientClient){
                                pc_patientClient.socket.emit('message', message);
                            }
                        } else {
                            log.info("emitSessionUsers:isdoctor"+participant.id);
                            let doctorClient = clientCache.findByIdAndType(participant.id,SOCKET_TYPES.DOCTOR);
                            if(doctorClient){
                                doctorClient.socket.emit('message', message);
                            }
                            let pc_doctorClient = clientCache.findByIdAndType("pc_"+participant.id,SOCKET_TYPES.PC_DOCTOR);
                            if(pc_doctorClient){
                                pc_doctorClient.socket.emit('message',message);
                            }
                        }
                    });
                })
            }
        })
    }
    updateSessionUser(userId,oldUserId,sessionId){
        let self = this;
        let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);

+ 3 - 0
src/server/models/sessions/sessions.js

@ -1985,6 +1985,9 @@ class Sessions extends RedisModel {
                message.targetUserName = targetUserName;
                message.sessionType = sessionType;
                message.targetType = 'doctor';
                //推送MDT,医生外层新消息
                WechatClient.sendMDTSocketMessageToDoctor(targetUserId,message);
            }
            //redis发布消息
            if(config.pubSubSwitch) {//接收订阅消息处理开关,本地运行和测试库单独运行时防止用户接收消息2次

+ 1 - 8
src/server/models/user/users.js

@ -20,6 +20,7 @@ let ModelUtil = require('../../util/model.util');
let ObjectUtil = require("../../util/object.util.js");
let Patient = require('./patient');
let Doctor = require('./doctor');
let Participants = require('../../models/sessions/participants');
let redisConn = RedisClient.redisClient().connection;
let async = require('async');
@ -344,14 +345,6 @@ class Users extends RedisModel {
        );
    }
    /**
     * 改变登陆者的在线状态
     * @param userid 居民 patient_userid, 医生 doctor_userid
     * @param status 0离线 1在线
     */
    changUserRedisLoginStatus(userid,clientType,status){
    }
    /**
     * 用户ID是否属于患者。

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

@ -23,9 +23,9 @@ class ParticipantRepo {
     * @param handler
     */
    static findAll(sessionId, handler) {
        let sql = "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, false is_patient,p.last_fetch_time,u.level FROM sessions s, participants p, doctors u " +
        let sql = "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, false is_patient,p.last_fetch_time,u.level,u.mobile FROM sessions s, participants p, doctors u " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id union " +
            "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, true is_patient,p.last_fetch_time,0 as level FROM sessions s, participants p, patients u " +
            "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar,u.hospital_name, p.participant_role role, true is_patient,p.last_fetch_time,0 as level,u.mobile FROM sessions s, participants p, patients u " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
        ImDb.execQuery({