瀏覽代碼

pc端医生同时登陆修改

yeshijie 8 年之前
父節點
當前提交
8a7aae8be2

+ 1 - 1
src/server/endpoints/v2/user.endpoint.js

@ -87,7 +87,7 @@ router.delete(APIv2.Users.Logout, function (req, res) {
    let userStatus = new Users();
    ControllerUtil.regModelEventHandler(userStatus, res);
    userStatus.logout(userId);
    userStatus.logout(userId,req.query.platform);
});
module.exports = router;

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

@ -31,10 +31,14 @@ class SocketHandler {
                if (!data.userId) {
                    socketServer.sockets.emit('error', {message: 'Missing fields(s): userId.'});
                } else {
                    if(clientCache.removeByUserId(data.userId)){
                        log.info("User " + data.userId + " already login");
                        return;
                    if("pc_doctor"===data.clientType){//新增pc端医生登录
                        data.userId = "pc_"+data.userId;
                    }
                    clientCache.removeByUserId(data.userId);
                    // if(clientCache.removeByUserId(data.userId)){
                    //     log.info("User " + data.userId + " already login");
                    //     return;
                    // }
                    log.error('User ' + data.userId + ' login');
                    if(!data.clientType||data.clientType=="patient"){

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

@ -105,6 +105,7 @@ exports.CONTENT_TYPES = CONTENT_TYPES;
exports.SOCKET_TYPES={
    PATIENT:"patient",
    DOCTOR:"doctor",
    PC_DOCTOR:"pc_doctor",
    DEMO:"demo"
}
@ -114,6 +115,7 @@ exports.SOCKET_TYPES={
exports.PLATFORM = {
    iOS: 0,
    Android: 1,
    PC: 3,
    Wechat: 10
};
@ -169,6 +171,7 @@ exports.REDIS_KEYS = {
    User: "users:" + REDIS_KEY_REPLACER,
    UserAppStatus: "users:" + REDIS_KEY_REPLACER + ":app_status",
    UserPcStatus: "users:" + REDIS_KEY_REPLACER + ":pc_status",
    UserWechatStatus: "users:" + REDIS_KEY_REPLACER + ":wechat_status",
    UserSessions: "users:" + REDIS_KEY_REPLACER + ":sessions",

+ 7 - 0
src/server/models/client/app.client.js

@ -7,6 +7,7 @@ let RedisClient = require('../../repository/redis/redis.client');
let RedisModel = require('./../redis.model');
let AppStatusRepo = require('../../repository/mysql/app.status.repo');
let ModelUtil = require('../../util/model.util');
let clientCache = require('../socket.io/client.cache').clientCache();
let log = require("../../util/log.js");
let pusher = require('../push/pusher');
@ -108,6 +109,12 @@ class AppClient extends RedisModel {
                    return;
                }
                let pc_doctorClient = clientCache.findByIdAndType("pc_"+targetId,SOCKET_TYPES.PC_DOCTOR);
                if(pc_doctorClient){
                    log.warn("User's pc is online, user id: " + targetId + ", we cannot send getui.");
                    return;
                }
                if (!userStatus) {
                    log.warn("User's app status is not found, user id: " + targetId + ", maybe user never login yet or logout?");
                    return;

+ 51 - 8
src/server/models/client/wechat.client.js

@ -156,12 +156,14 @@ class WechatClient extends RedisModel {
    static sendReadDoctorByDoctorId(doctorId, message) {
        let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
        if(!doctorClient){
        let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
        if(!doctorClient&&!pc_doctorClient){
            log.warn("target doctor is not online!");
            return;
        }
        let sendDoctorClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);
        if(sendDoctorClient&&sendDoctorClient.sessionId==doctorClient.sessionId){
        let pc_sendDoctorClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
        if(doctorClient&&sendDoctorClient&&sendDoctorClient.sessionId==doctorClient.sessionId){
            WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
            sendDoctorClient.socket.emit('message', {
                id: message.id,
@ -176,22 +178,45 @@ class WechatClient extends RedisModel {
                read:"one"
            });
        }else{
            log.warn("doctor is not in the same session");
            log.warn("doctor is not in the same session or not online");
        }
        //发送pc版医生端
        if(pc_doctorClient&&pc_sendDoctorClient&&pc_sendDoctorClient.sessionId==pc_doctorClient.sessionId){
            WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
            pc_sendDoctorClient.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{
            log.warn("doctor is not in the same session or not online");
        }
    }
    static sendSocketMessageToDoctor(doctorId, message) {
        let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
        if(!doctorClient){
        let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
        if(!doctorClient&&!pc_doctorClient){
            log.warn("target doctor is not online!");
            return;
        }
        let sendClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);
        if(!sendClient){
        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);
        }
        if(sendClient&&sendClient.sessionId==doctorClient.sessionId){
        if(doctorClient&&sendClient&&sendClient.sessionId==doctorClient.sessionId){
            WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
            doctorClient.socket.emit('message', {
                id: message.id,
@ -205,7 +230,25 @@ class WechatClient extends RedisModel {
                name: message.sender_name,
            });
        }else{
            log.warn("doctor is not in the same session");
            log.warn("doctor is not in the same session or is not online");
        }
        //发送pc端
        if(pc_doctorClient&&sendClient&&sendClient.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,
            });
        }else{
            log.warn("doctor is not in the same session or is not online");
        }
    }
    /**

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

@ -87,10 +87,11 @@ class Users extends RedisModel {
    login(userId, platform, deviceToken, clientId) {
        let self = this;
        let loginFromApp = platform !== PLATFORMS.Wechat;
        let loginFromPc = platform !== PLATFORMS.PC;
        log.error(userId+"  "+ platform+"  "+deviceToken+"  "+clientId);
        let usersKey = REDIS_KEYS.Users;
        let userKey = RedisModel.makeRedisKey(REDIS_KEYS.User, userId);
        let userStatusKey = RedisModel.makeRedisKey(loginFromApp ? REDIS_KEYS.UserAppStatus : REDIS_KEYS.UserWechatStatus, userId);
        let userStatusKey = RedisModel.makeRedisKey(loginFromApp ? REDIS_KEYS.UserAppStatus : (loginFromPc?REDIS_KEYS.UserPcStatus:REDIS_KEYS.UserWechatStatus), userId);
        let lastLoginTime = new Date();
        async.waterfall([
@ -126,7 +127,12 @@ class Users extends RedisModel {
                            'device_token', deviceToken,
                            'last_login_time', lastLoginTime.getTime(),
                            'platform', platform);
                    } else {
                    } else if(loginFromPc){
                        // cache pc status
                        multi = multi.hmset(userStatusKey,
                            'last_login_time', lastLoginTime.getTime(),
                            'platform', platform);
                    }else {
                        // cache wechat status
                        multi = multi.hmset(userStatusKey,
                            'last_login_time', lastLoginTime.getTime(),
@ -287,7 +293,7 @@ class Users extends RedisModel {
            });
    }
    logout(userId) {
    logout(userId,platform) {
        let self = this;
        async.waterfall([
                function (callback) {
@ -297,7 +303,7 @@ class Users extends RedisModel {
                },
                function (isPatient, callback) {
                    let usersKey = REDIS_KEYS.Users;
                    let userStatusKey = RedisModel.makeRedisKey(isPatient ? REDIS_KEYS.UserWechatStatus : REDIS_KEYS.UserAppStatus, userId);
                    let userStatusKey = RedisModel.makeRedisKey(isPatient ? REDIS_KEYS.UserWechatStatus : (platform==PLATFORM.PC?REDIS_KEYS.UserPcStatus:REDIS_KEYS.UserAppStatus), userId);
                    redisConn.multi()
                        .zrem(usersKey, userId)
                        .del(userStatusKey)
@ -313,10 +319,11 @@ class Users extends RedisModel {
                            log.error("Logout failed: ", ex);
                        });
                    AppStatusRepo.destroy(userId, function (err, res) {
                        if(err) log.error("Delete user status failed: " + err);
                    });
                    if(!platform||platform!=PLATFORM.PC){
                        AppStatusRepo.destroy(userId, function (err, res) {
                            if(err) log.error("Delete user status failed: " + err);
                        });
                    }
                    callback(null, null);
                }],
            function (err, res) {