Ver código fonte

代码审核

Sand 8 anos atrás
pai
commit
56b35c46bd

+ 1 - 1
src/server/app.js

@ -36,7 +36,7 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(methodOverride(null, {methods: ['GET', 'POST']}));
app.use(methodOverride(null, {methods: ['GET', 'POST', 'PUT', 'DELETE']}));
// web pages and endpoint
UrlInitializer.initWebPages(app);

+ 7 - 7
src/server/models/sessions/sessions.js

@ -22,7 +22,6 @@ let logger = require('../../util/log.js');
let mongoose = require('mongoose');
let async = require("async");
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;
@ -376,7 +375,7 @@ class Sessions extends RedisModel {
                            callback = index, index = 0
                        }
                        let sessionId = sessionIds[index]
                        let sessionId = sessionIds[index];
                        let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
                        let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
                        let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
@ -393,14 +392,15 @@ class Sessions extends RedisModel {
                                let lastFetchTime = res[2];
                                let users = res[3];
                                let sessionName = "";
                                let otheruserId = "";
                                let otherUserId = "";
                                if (session.type == SESSION_TYPES.P2P) {
                                    for (let j in users) {
                                        if (users[j] != userId) {
                                            otheruserId = users[j];
                                            otherUserId = users[j];
                                        }
                                    }
                                }
                                if (!role) role = 0;
                                if (!lastFetchTime) lastFetchTime = new Date().getTime();
@ -408,8 +408,9 @@ class Sessions extends RedisModel {
                                let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
                                redis.zcountAsync(messagesByTimestampKey, lastFetchTime, new Date().getTime())
                                    .then(function (count) {
                                        if (!otheruserId) otheruserId = userId;
                                        ParticipantRepo.findNameById(otheruserId, function (err, res) {
                                        if (!otherUserId) otherUserId = userId;
                                        ParticipantRepo.findNameById(otherUserId, function (err, res) {
                                            if ((res && res.length == 0) || session.type != SESSION_TYPES.P2P) {
                                                sessionName = session.name;
                                            } else {
@ -1022,7 +1023,6 @@ class Sessions extends RedisModel {
            }
        });
    }
}
// Expose class

+ 54 - 60
src/server/repository/mysql/participant.repo.js

@ -1,15 +1,17 @@
/**
 * 搜索功能。
 * 会话成员。
 */
"use strict";
let ImDb = require('../mysql/db/im.db');
let DbUtil = require('../../util/db.util');
let log = require('../../util/log.js');
const DB_TABLES = require('../../include/commons').DB_TABLES;
const SESSION_USER_STATUS = require('../../include/commons').SESSION_USER_STATUS;
const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
const SESSION_BUSINESS_TYPE = require('../../include/commons').SESSION_BUSINESS_TYPE;
class ParticipantRepo {
    constructor() {
    }
@ -21,10 +23,10 @@ class ParticipantRepo {
     * @param handler
     */
    static findAll(sessionId, handler) {
        let sql = "select u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, 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, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, true is_patient from sessions s, participants p, patients u " +
            "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
        let sql = "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, 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, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, true is_patient FROM sessions s, participants p, patients u " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
        ImDb.execQuery({
            "sql": sql,
@ -40,10 +42,10 @@ 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 " +
            "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
        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 " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
        ImDb.execQuery({
            "sql": sql,
@ -59,10 +61,10 @@ class ParticipantRepo {
     * @param handler
     */
    static findAllAvatars(sessionId, handler) {
        let sql = "select u.id, u.avatar 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.avatar from sessions s, participants p, patients u " +
            "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
        let sql = "SELECT u.id, u.avatar 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.avatar FROM sessions s, participants p, patients u " +
            "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
        ImDb.execQuery({
            "sql": sql,
@ -80,7 +82,7 @@ class ParticipantRepo {
     * @param handler
     */
    static updateParticipant(sessionId, participantId, role, handler) {
        let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?";
        let sql = "update participants set participant_role = ? WHERE session_id = ? AND participant_id = ?";
        ImDb.execQuery({
            "sql": sql,
            "args": [role, sessionId, participantId],
@ -100,19 +102,19 @@ class ParticipantRepo {
        handler(null, sessionId);
    }
    static getBusinessType(users,handler){
        let sql ="select count(1) as count from patients p where p.id in ('"+users+"')";
    static getBusinessType(users, handler) {
        let sql = "SELECT count(1) as count FROM patients p WHERE p.id in ('" + users + "')";
        ImDb.execQuery({
            "sql": sql,
            "args": [],
            "handler": function(err,res){
                if(err){
                    console.log("err businessType  : "+err);
                }else{
                    if(res[0].count){
                        handler(err,SESSION_BUSINESS_TYPE.PATIENT);
                    }else{
                        handler(err,SESSION_BUSINESS_TYPE.DOCTOR);
            "handler": function (err, res) {
                if (err) {
                    console.log("err businessType  : " + err);
                } else {
                    if (res[0].count) {
                        handler(err, SESSION_BUSINESS_TYPE.PATIENT);
                    } else {
                        handler(err, SESSION_BUSINESS_TYPE.DOCTOR);
                    }
                }
            }
@ -121,35 +123,37 @@ class ParticipantRepo {
    }
    static findNameById(userId,handler){
        let sql ="select p.name  from patients p where p.id =? union select d.name  from doctors d where d.id =?";
    static findNameById(userId, handler) {
        let sql = "SELECT p.name FROM patients p WHERE p.id =? union SELECT d.name FROM doctors d WHERE d.id =?";
        ImDb.execQuery({
            "sql": sql,
            "args": [userId,userId],
            "handler": function(err,res){
                if(err){
                    console.log("err businessType  : "+err);
                }else{
                    handler(null,res);
            "args": [userId, userId],
            "handler": function (err, res) {
                if (err) {
                    console.log("err businessType  : " + err);
                } else {
                    handler(null, res);
                }
            }
        });
    }
    static findMucSessionIdByUser(users,handler){
        var userTemp = [];//先匹配出在线用户
        for(var j in users){
            if(users[j]==SESSION_USER_STATUS.ONLINE){
                userTemp.push(j);
    static findMucSessionIdByUser(users, handler) {
        //先匹配出在线用户
        let userTemp = [];
        users.forEach(function (user) {
            if (user == SESSION_USER_STATUS.ONLINE) {
                userTemp.push(user);
            }
        }
        let sql ="select DISTINCT s.* from "+DB_TABLES.Participants +" p1,"+DB_TABLES.Participants+ " p2,"+
                DB_TABLES.Sessions+" s  where p1.session_id =  s.id  and p2.session_id = s.id  and s.type =? " +
                "and ((p1.participant_id =? and p2.participant_id = ?) or (p1.participant_id =? and p2.participant_id = ?))"
        });
        let sql = "SELECT DISTINCT s.* FROM " + DB_TABLES.Participants + " p1," + DB_TABLES.Participants + " p2," +
            DB_TABLES.Sessions + " s WHERE p1.session_id =  s.id  AND p2.session_id = s.id  AND s.type =? " +
            "AND ((p1.participant_id =? AND p2.participant_id = ?) or (p1.participant_id =? AND p2.participant_id = ?))";
        ImDb.execQuery({
            "sql": sql,
            "args": [SESSION_TYPES.MUC,userTemp[0], userTemp[1], userTemp[1],userTemp[0]],
            "args": [SESSION_TYPES.MUC, userTemp[0], userTemp[1], userTemp[1], userTemp[0]],
            "handler": handler
        });
    }
@ -163,7 +167,7 @@ class ParticipantRepo {
     * @param handler
     */
    static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) {
        let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? where session_id = ? and participant_id =?";
        let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? WHERE session_id = ? AND participant_id =?";
        ImDb.execQuery({
            "sql": sql,
            "args": [lastMessageTime, sessionId, participantId],
@ -179,7 +183,7 @@ class ParticipantRepo {
     * @param handler
     */
    static existsParticipant(sessionId, userId, handler) {
        let sql = "select case when count(*) > 0 then true else false end exist from participants w where w.session_id =? and w.participant_id = ? ";
        let sql = "SELECT case when count(*) > 0 then true else false end exist FROM participants w WHERE w.session_id =? AND w.participant_id = ? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [sessionId, userId],
@ -195,9 +199,9 @@ class ParticipantRepo {
     * @param handler
     */
    static saveParticipantsToMysql(sessionId, users, handler) {
        let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES "
        let sql = "INSERT INTO " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES ";
        let args = [];
        for (var j in users) {
        for (let j in users) {
            sql += "(?,?,?)";
            let tokens = users[j].split(":");
@ -217,26 +221,16 @@ class ParticipantRepo {
        });
    }
    static deleteUserFromMysql(sessionId, userId,handler) {
        let sql = "delete from " + DB_TABLES.Participants + " where user_id=? and session_id=? ";
    static deleteUserFromMysql(sessionId, userId, handler) {
        let sql = "DELETE FROM " + DB_TABLES.Participants + " WHERE user_id = ? AND session_id = ? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [userId, sessionId],
            "handler": handler||function(){
            "handler": handler || function () {
                log.info("deleteUserFromMysql");
            }
        });
    }
    static getPatientOpenid(code, handler) {
        var sql = "select openid from patients where code = ? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [code],
            "handler": handler
        });
    }
}
module.exports = ParticipantRepo;

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

@ -2,7 +2,7 @@
let ImDb = require('../mysql/db/im.db');
let async = require("async");
var ObjectUtil = require("../../util/object.util.js");
let ObjectUtil = require("../../util/object.util.js");
const DB_TABLES = require('../../include/commons').DB_TABLES;