|  | @ -16,7 +16,6 @@ let MessageRepo = require('../../repository/mysql/message.repo');
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | let WechatClient = require("../client/wechat.client.js");
 | 
	
		
			
				|  |  | let AppClient = require("../client/app.client.js");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | let configFile = require('../../include/commons').CONFIG_FILE;
 | 
	
		
			
				|  |  | let config = require('../../resources/config/' + configFile);
 | 
	
		
			
				|  |  | let redis = RedisClient.redisClient().connection;
 | 
	
	
		
			
				|  | @ -28,7 +27,6 @@ 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;
 | 
	
		
			
				|  |  | const SESSION_BUSINESS_TYPE = require('../../include/commons').SESSION_BUSINESS_TYPE;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |     constructor() {
 | 
	
	
		
			
				|  | @ -338,69 +336,79 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |                     })
 | 
	
		
			
				|  |  |             },
 | 
	
		
			
				|  |  |             // 遍历会话
 | 
	
		
			
				|  |  |             function (sessionIds, callback) {
 | 
	
		
			
				|  |  |             function (sessionIds) {
 | 
	
		
			
				|  |  |                 let sessionList = [];
 | 
	
		
			
				|  |  |                 sessionIds.forEach(function (sessionId) {
 | 
	
		
			
				|  |  |                     let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
 | 
	
		
			
				|  |  |                     let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
 | 
	
		
			
				|  |  |                     let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
 | 
	
		
			
				|  |  |                     let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants,sessionId);
 | 
	
		
			
				|  |  |                     redis.multi()
 | 
	
		
			
				|  |  |                         .hgetall(sessionKey)                       // 会话实体
 | 
	
		
			
				|  |  |                         .hget(participantsRoleKey, userId)         // 用户在此会话中的角色
 | 
	
		
			
				|  |  |                         .zscore(sessionParticipantsKey, userId)    // 用户在此会话中最后一次获取未读消息的时间
 | 
	
		
			
				|  |  |                         .zrange(participantsKey,0,-1)
 | 
	
		
			
				|  |  |                         .execAsync()
 | 
	
		
			
				|  |  |                         .then(function (res) {
 | 
	
		
			
				|  |  |                             let session = res[0];
 | 
	
		
			
				|  |  |                             let role = res[1];
 | 
	
		
			
				|  |  |                             let lastFetchTime = res[2];
 | 
	
		
			
				|  |  |                             let users = res[3];
 | 
	
		
			
				|  |  |                             let sessionName="";
 | 
	
		
			
				|  |  |                             let otheruserId ="";
 | 
	
		
			
				|  |  |                             if(session.type==SESSION_TYPES.P2P){
 | 
	
		
			
				|  |  |                                 for(var j in users){
 | 
	
		
			
				|  |  |                                     if(users[j]!=userId){
 | 
	
		
			
				|  |  |                                         otheruserId = users[j];
 | 
	
		
			
				|  |  |                                     }
 | 
	
		
			
				|  |  |                                 }
 | 
	
		
			
				|  |  |                             }
 | 
	
		
			
				|  |  |                             if(!role)role =0;
 | 
	
		
			
				|  |  |                             if(!lastFetchTime)lastFetchTime=new Date().getTime();
 | 
	
		
			
				|  |  |                                 // 计算未读消息数
 | 
	
		
			
				|  |  |                                 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((res&&res.length==0)||session.type!=SESSION_TYPES.P2P){
 | 
	
		
			
				|  |  |                                                 sessionName = session.name;
 | 
	
		
			
				|  |  |                                             }else{
 | 
	
		
			
				|  |  |                                                 sessionName = res[0].name;
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                             sessionList.push({
 | 
	
		
			
				|  |  |                                                 id: sessionId,
 | 
	
		
			
				|  |  |                                                 name: sessionName,
 | 
	
		
			
				|  |  |                                                 create_date: session.create_date,
 | 
	
		
			
				|  |  |                                                 last_content_type: session.last_content_type,
 | 
	
		
			
				|  |  |                                                 last_content: session.last_content,
 | 
	
		
			
				|  |  |                                                 sender_id: session.sender_id,
 | 
	
		
			
				|  |  |                                                 type: session.type,
 | 
	
		
			
				|  |  |                                                 sender_name: session.sender_name,
 | 
	
		
			
				|  |  |                                                 unread_count: count,
 | 
	
		
			
				|  |  |                                                 business_type: session.business_type,
 | 
	
		
			
				|  |  |                                                 my_role: role
 | 
	
		
			
				|  |  |                                             });
 | 
	
		
			
				|  |  |                                             if (sessionId === sessionIds[sessionIds.length - 1]) {
 | 
	
		
			
				|  |  |                                                 ModelUtil.emitOK(self.eventEmitter, sessionList);
 | 
	
		
			
				|  |  |                 let functionList =[];
 | 
	
		
			
				|  |  |                 for(var j = 0 ;j<sessionIds.length;j++){
 | 
	
		
			
				|  |  |                     let fun = function(index,callback){
 | 
	
		
			
				|  |  |                             if(!callback) {callback =index,index=0};
 | 
	
		
			
				|  |  |                             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);
 | 
	
		
			
				|  |  |                             let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants,sessionId);
 | 
	
		
			
				|  |  |                             redis.multi()
 | 
	
		
			
				|  |  |                                 .hgetall(sessionKey)                       // 会话实体
 | 
	
		
			
				|  |  |                                 .hget(participantsRoleKey, userId)         // 用户在此会话中的角色
 | 
	
		
			
				|  |  |                                 .zscore(sessionParticipantsKey, userId)    // 用户在此会话中最后一次获取未读消息的时间
 | 
	
		
			
				|  |  |                                 .zrange(participantsKey,0,-1)
 | 
	
		
			
				|  |  |                                 .execAsync()
 | 
	
		
			
				|  |  |                                 .then(function (res) {
 | 
	
		
			
				|  |  |                                     let session = res[0];
 | 
	
		
			
				|  |  |                                     let role = res[1];
 | 
	
		
			
				|  |  |                                     let lastFetchTime = res[2];
 | 
	
		
			
				|  |  |                                     let users = res[3];
 | 
	
		
			
				|  |  |                                     let sessionName="";
 | 
	
		
			
				|  |  |                                     let otheruserId ="";
 | 
	
		
			
				|  |  |                                     if(session.type==SESSION_TYPES.P2P){
 | 
	
		
			
				|  |  |                                         for(var j in users){
 | 
	
		
			
				|  |  |                                             if(users[j]!=userId){
 | 
	
		
			
				|  |  |                                                 otheruserId = users[j];
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                         }
 | 
	
		
			
				|  |  |                                     }
 | 
	
		
			
				|  |  |                                     if(!role)role =0;
 | 
	
		
			
				|  |  |                                     if(!lastFetchTime)lastFetchTime=new Date().getTime();
 | 
	
		
			
				|  |  |                                     // 计算未读消息数
 | 
	
		
			
				|  |  |                                     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((res&&res.length==0)||session.type!=SESSION_TYPES.P2P){
 | 
	
		
			
				|  |  |                                                     sessionName = session.name;
 | 
	
		
			
				|  |  |                                                 }else{
 | 
	
		
			
				|  |  |                                                     sessionName = res[0].name;
 | 
	
		
			
				|  |  |                                                 }
 | 
	
		
			
				|  |  |                                                 sessionList.push({
 | 
	
		
			
				|  |  |                                                     id: sessionId,
 | 
	
		
			
				|  |  |                                                     name: sessionName,
 | 
	
		
			
				|  |  |                                                     create_date: session.create_date,
 | 
	
		
			
				|  |  |                                                     last_content_type: session.last_content_type,
 | 
	
		
			
				|  |  |                                                     last_content: session.last_content,
 | 
	
		
			
				|  |  |                                                     sender_id: session.sender_id,
 | 
	
		
			
				|  |  |                                                     type: session.type,
 | 
	
		
			
				|  |  |                                                     sender_name: session.sender_name,
 | 
	
		
			
				|  |  |                                                     unread_count: count,
 | 
	
		
			
				|  |  |                                                     business_type: session.business_type,
 | 
	
		
			
				|  |  |                                                     my_role: role
 | 
	
		
			
				|  |  |                                                 });
 | 
	
		
			
				|  |  |                                                 index=(parseInt(index)+1);
 | 
	
		
			
				|  |  |                                                 if(index==sessionIds.length){
 | 
	
		
			
				|  |  |                                                     ModelUtil.emitOK(self.eventEmitter, sessionList);
 | 
	
		
			
				|  |  |                                                 }else{
 | 
	
		
			
				|  |  |                                                     callback(null,index);
 | 
	
		
			
				|  |  |                                                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                                             })
 | 
	
		
			
				|  |  |                                         })
 | 
	
		
			
				|  |  |                                     })
 | 
	
		
			
				|  |  |                                 }).catch(function (err) {
 | 
	
		
			
				|  |  |                                     logger.error("Get sessions failed: ", ex);
 | 
	
		
			
				|  |  |                                     ModelUtil.emitError(self.eventEmitter, "Get sessions failed: " + err);
 | 
	
		
			
				|  |  |                                 });
 | 
	
		
			
				|  |  |                 });
 | 
	
		
			
				|  |  |                                 logger.error("Get sessions failed: ", err);
 | 
	
		
			
				|  |  |                             });
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     functionList.push(fun);
 | 
	
		
			
				|  |  |                 };
 | 
	
		
			
				|  |  |                 async.waterfall(functionList);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         ]);
 | 
	
		
			
				|  |  |     }
 |