Ver código fonte

修复微信websocket无法显示消息的问题

Sand 8 anos atrás
pai
commit
129718edb5

+ 43 - 38
src/server/models/client/app.client.js

@ -87,53 +87,58 @@ class AppClient extends RedisModel {
    }
    /**
     * 向App端推送消息。
     * 向App端推送消息。指令性消息不推。
     *
     * @param targetId
     * @param message
     * @param sessionType
     */
    static sendNotification(targetId, message, sessionType) {
        AppClient.getAppStatus(targetId, function (err, userStatus) {
            if (err) {
                ModelUtil.logError("Get user app status failed", err);
                return;
            }
        if (message.content_type == CONTENT_TYPES.PlainText ||
            message.content_type == CONTENT_TYPES.Image ||
            message.content_type == CONTENT_TYPES.Audio) {
            if (!userStatus) {
                log.warn("User's app status is not found, user id: " + targetId + ", MAYBE user never login yet?");
                return;
            }
            AppClient.getAppStatus(targetId, function (err, userStatus) {
                if (err) {
                    ModelUtil.logError("Get user app status failed", err);
                    return;
                }
            let tipMessage = CONTENT_TYPES.typeToDescription(parseInt(message.content_type), "您有一条新消息") || message.content;
            let customData = {
                session_id: '' || message.session_id,
                session_type: sessionType,
                topic_id: '' || message.topic_id,
                from: '' || message.sender_id,
                data: message.content,
                business_type: message.business_type || 1
            };
                if (!userStatus) {
                    log.warn("User's app status is not found, user id: " + targetId + ", MAYBE user never login yet?");
                    return;
                }
            if (userStatus.platform == PLATFORMS.iOS) {
                pusher.pushToSingleViaAPN(tipMessage, customData, message.contentType, userStatus.device_token, function (err, res) {
                    if (err) {
                        ModelUtil.logError("Send notification via APN failed", err);
                    } else {
                        log.info("Send notification via APN succeed: ", JSON.stringify(res));
                    }
                });
            } else if (userStatus.platform == PLATFORMS.Android) {
                let title = '新消息';
                pusher.pushToSingleViaAndroid(title, tipMessage, customData, userStatus.client_id, userStatus.app_in_bg, function (err, res) {
                    if (err) {
                        ModelUtil.logError("Send notification via Android failed", err);
                    } else {
                        log.info("Send notification via Android succeed: ", JSON.stringify(res));
                    }
                });
            }
        });
                let tipMessage = CONTENT_TYPES.typeToDescription(parseInt(message.content_type), "您有一条新消息") || message.content;
                let customData = {
                    session_id: '' || message.session_id,
                    session_type: sessionType,
                    topic_id: '' || message.topic_id,
                    from: '' || message.sender_id,
                    data: message.content,
                    business_type: message.business_type || 1
                };
                if (userStatus.platform == PLATFORMS.iOS) {
                    pusher.pushToSingleViaAPN(tipMessage, customData, message.contentType, userStatus.device_token, function (err, res) {
                        if (err) {
                            ModelUtil.logError("Send notification via APN failed", err);
                        } else {
                            log.info("Send notification via APN succeed: ", JSON.stringify(res));
                        }
                    });
                } else if (userStatus.platform == PLATFORMS.Android) {
                    let title = '新消息';
                    pusher.pushToSingleViaAndroid(title, tipMessage, customData, userStatus.client_id, userStatus.app_in_bg, function (err, res) {
                        if (err) {
                            ModelUtil.logError("Send notification via Android failed", err);
                        } else {
                            log.info("Send notification via Android succeed: ", JSON.stringify(res));
                        }
                    });
                }
            });
        }
    }
}

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

@ -62,6 +62,8 @@ class WechatClient extends RedisModel {
            let patientClient = clientCache.findById(targetUserId);
            if (patientClient) {
                log.warn("User's wechat endpoint is online, sending via web socket. User id: ", targetUserId);
                WechatClient.sendViaWebSocket(patientClient.socket, message);
            } else {
                log.info("User's wechat endpoint is not online, sending via wechat template message. User id: ", targetUserId);
@ -79,7 +81,10 @@ class WechatClient extends RedisModel {
            sender_name: message.sender_name,
            content_type: message.content_type,
            content: message.content,
            timestamp: ObjectUtil.timestampToLong(message.timestamp)
            timestamp: ObjectUtil.timestampToLong(message.timestamp),
            type: message.content_type,          // legacy support
            name: message.sender_name
        });
    }
@ -105,6 +110,8 @@ class WechatClient extends RedisModel {
                            return;
                        }
                        log.warn("Send via wechat message template, user id: " + targetUserId + ", openid: " + openid);
                        callback(null, openid);
                    });
                },

+ 4 - 4
src/server/models/push/pusher.js

@ -77,15 +77,15 @@ class Pusher extends RedisModel {
     * @param title
     * @param message
     * @param customData
     * @param clientid
     * @param clientId
     * @param appInBg
     * @param handler
     */
    pushToSingleViaAndroid(title, message, customData, clientid, appInBg, handler) {
    pushToSingleViaAndroid(title, message, customData, clientId, appInBg, handler) {
        if (appInBg == 1) {
            this._pushAndroidNotify(clientid, title, message, customData, handler);
            this._pushAndroidNotify(clientId, title, message, customData, handler);
        } else {
            this._pushAndroidTransmission(clientid, customData, handler);
            this._pushAndroidTransmission(clientId, customData, handler);
        }
    }

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

@ -21,11 +21,12 @@ let redis = RedisClient.redisClient().connection;
let logger = require('../../util/log.js');
let mongoose = require('mongoose');
let async = require("async");
var log = require("../../util/log.js");
let log = require("../../util/log.js");
const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
const STICKY_SESSION_BASE_SCORE = require('../../include/commons').STICKY_SESSION_BASE_SCORE;
const PARTICIPANT_ROLES = require('../../include/commons').PARTICIPANT_ROLES;
class Sessions extends RedisModel {
    constructor() {
@ -132,7 +133,7 @@ class Sessions extends RedisModel {
                timestamp: createDate,
                id: messageId
            };
            if(type == SESSION_TYPES.MUC){
            if (type == SESSION_TYPES.MUC) {
                businessType = 2;
            }
            let session = {
@ -202,7 +203,7 @@ class Sessions extends RedisModel {
            //查找该sessionId是否存在存在则直接返回实例
            SessionRepo.findOne(sessionId, function (err, res) {
                if (res.length > 0) {//已经存在
                     //更新成员
                    //更新成员
                    Participants.saveParticipantsToMysql(sessionId, participantArray, function (err, update) {
                        handler(null, res[0]);
                        return;
@ -236,7 +237,7 @@ class Sessions extends RedisModel {
                if (err) {
                    handler(err, null);
                    return;
                }else{
                } else {
                    handler(null, session);
                    return;
                }
@ -385,12 +386,12 @@ class Sessions extends RedisModel {
                                                sessionName = res[0].name;
                                            }
                                            var bir = new Date().getTime();
                                            if(res.length!=0&&res[0].birthdate){
                                            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;
                                            if (res.length != 0 && res[0].sex) {
                                                sex = res[0].sex;
                                            }
                                            sessionList.push({
@ -405,8 +406,8 @@ class Sessions extends RedisModel {
                                                unread_count: count,
                                                business_type: session.business_type,
                                                my_role: role,
                                                sender_sex:sex,
                                                sender_birthday:bir,
                                                sender_sex: sex,
                                                sender_birthday: bir,
                                            });
                                            index = (parseInt(index) + 1);
@ -591,7 +592,7 @@ class Sessions extends RedisModel {
                        let startMsgScore = res[1];
                        let endMsgScore = res[0];
                        if (startMsgScore == null || endMsgScore == null || (startMsgScore == endMsgScore&&isoffset==1)) {
                        if (startMsgScore == null || endMsgScore == null || (startMsgScore == endMsgScore && isoffset == 1)) {
                            handler(null, []);
                            return;
                        }
@ -657,13 +658,13 @@ class Sessions extends RedisModel {
                }
                count = count + con;
                if(session.type ==2){
                    patientCount = patientCount+con;
                }else{
                    doctorCount = doctorCount+con;
                if (session.type == 2) {
                    patientCount = patientCount + con;
                } else {
                    doctorCount = doctorCount + con;
                }
                if (j == res.length - 1) {
                    ModelUtil.emitOK(self.eventEmitter, {count: count,patient:patientCount,doctor:doctorCount});
                    ModelUtil.emitOK(self.eventEmitter, {count: count, patient: patientCount, doctor: doctorCount});
                }
            })
        }
@ -819,11 +820,9 @@ class Sessions extends RedisModel {
                        } else {
                            message.session_id = sessionId;
                            log.info("Candidate to push: " + participants);
                            res.forEach(function (participant) {
                                if (participant.id !== message.sender_id) {
                                    log.warn('Push message to ' + participant.id);
                                if (participant.id !== message.sender_id &&
                                    participant.participant_role == PARTICIPANT_ROLES.HOST) {
                                    Sessions.pushNotification(participant.id, message);
                                }
                            });

+ 10 - 8
src/server/repository/mysql/participant.repo.js

@ -42,9 +42,11 @@ class ParticipantRepo {
     * @param handler
     */
    static findIds(sessionId, handler) {
        let sql = "SELECT u.id, false is_patient 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, true is_patient FROM sessions s, participants p, patients u " +
        let sql =
            "SELECT u.id, false is_patient, p.participant_role 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, true is_patient, p.participant_role FROM sessions s, participants p, patients u " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
        ImDb.execQuery({
@ -111,7 +113,7 @@ class ParticipantRepo {
                if (err) {
                    console.log("err businessType  : " + err);
                } else {
                    if (res[0].count>0) {
                    if (res[0].count > 0) {
                        handler(err, SESSION_BUSINESS_TYPE.PATIENT);
                    } else {
                        handler(err, SESSION_BUSINESS_TYPE.DOCTOR);
@ -221,23 +223,23 @@ class ParticipantRepo {
        });
    }
    static deleteUserFromMysql(sessionId, userId,handler) {
    static deleteUserFromMysql(sessionId, userId, handler) {
        let sql = "delete from " + DB_TABLES.Participants + " where participant_id=? and session_id=? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [userId, sessionId],
            "handler": handler||function(err,res){
            "handler": handler || function (err, res) {
                log.info("deleteUserFromMysql");
            }
        });
    }
    static deleteAllUser(sessionId,handler) {
    static deleteAllUser(sessionId, handler) {
        let sql = "delete from " + DB_TABLES.Participants + " where session_id=? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [sessionId],
            "handler": handler||function(err,res){
            "handler": handler || function (err, res) {
                log.info("deleteUserFromMysql");
            }
        });