Browse Source

在线离线改变居民和医生的在线状态

huangwenjie 5 years ago
parent
commit
6ccf406515

+ 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);
                }

+ 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);

+ 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是否属于患者。