|  | @ -18,6 +18,7 @@ let ImDb = require('../../repository/mysql/db/im.db');
 | 
	
		
			
				|  |  | let DoctorRepo = require('../../repository/mysql/doctor.repo');
 | 
	
		
			
				|  |  | let PatientRepo = require('../../repository/mysql/patient.repo');
 | 
	
		
			
				|  |  | let AppStatusRepo = require('../../repository/mysql/app.status.repo');
 | 
	
		
			
				|  |  | let ModelUtil = require('../../util/model.util');
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | let RedisClient = require('../../repository/redis/redis.client');
 | 
	
		
			
				|  |  | let redisConn = RedisClient.redisClient().connection;
 | 
	
	
		
			
				|  | @ -142,28 +143,56 @@ class Users extends RedisModel {
 | 
	
		
			
				|  |  |      * @param platform
 | 
	
		
			
				|  |  |      * @param token
 | 
	
		
			
				|  |  |      * @param clientId
 | 
	
		
			
				|  |  |      * @param outCallback
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return 用户token
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     login(userId, platform, token, clientId, outCallback){
 | 
	
		
			
				|  |  |     login(userId, platform, token, clientId){
 | 
	
		
			
				|  |  |         let self = this;
 | 
	
		
			
				|  |  |         let loginFromApp = platform === PLATFORMS.Wechat;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         let usersKey = REDIS_KEYS.Users;
 | 
	
		
			
				|  |  |         let userKey = self.makeRedisKey(REDIS_KEYS.User, userId);
 | 
	
		
			
				|  |  |         let userStatusKey = platform === PLATFORMS.Wechat ? REDIS_KEYS.UserWechatStatus : REDIS_KEYS.UserWechatStatus;
 | 
	
		
			
				|  |  |         userStatusKey = self.makeRedisKey(userStatusKey, userId);
 | 
	
		
			
				|  |  |         let userStatusKey = self.makeRedisKey(loginFromApp ? REDIS_KEYS.UserWechatStatus : REDIS_KEYS.UserWechatStatus, userId);
 | 
	
		
			
				|  |  |         let lastLoginTime = new Date();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         async.waterfall([
 | 
	
		
			
				|  |  |             // save user info and app status
 | 
	
		
			
				|  |  |             // get user info
 | 
	
		
			
				|  |  |             function (callback) {
 | 
	
		
			
				|  |  |                 let
 | 
	
		
			
				|  |  |                 self.getUser(userId, function (err, userInfo) {
 | 
	
		
			
				|  |  |                     if(userInfo === null){
 | 
	
		
			
				|  |  |                         ModelUtil.emitDataNotFound(self, 'User not exists.');
 | 
	
		
			
				|  |  |                         return;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     callback(null, userInfo);
 | 
	
		
			
				|  |  |                 })
 | 
	
		
			
				|  |  |             },
 | 
	
		
			
				|  |  |             // save user info and app status/wechat status
 | 
	
		
			
				|  |  |             function (userInfo, callback) {
 | 
	
		
			
				|  |  |                 let multi = redisConn.multi()
 | 
	
		
			
				|  |  |                     .zadd(usersKey, lastLoginTime.getMilliseconds(), userId)
 | 
	
		
			
				|  |  |                     .hmset(userKey, 'avatar', userInfo.avatar, 'birthdate', userInfo.birthdate,
 | 
	
		
			
				|  |  |                         'name', userInfo.name, 'role', loginFromApp ? 'doctor' : 'patient');
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 if(loginFromApp){
 | 
	
		
			
				|  |  |                     // app status
 | 
	
		
			
				|  |  |                     multi = multi.hmset(userStatusKey, 'platform', platform, 'app_in_bg', false, 'client_id', clientId,
 | 
	
		
			
				|  |  |                         'token', token, 'last_login_time', lastLoginTime);
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     // wechat status
 | 
	
		
			
				|  |  |                     multi = multi.hmset(userKey, 'open_id', userInfo.open_id);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 multi.execAsnyc().then(function (res) {
 | 
	
		
			
				|  |  |                         callback(null);
 | 
	
		
			
				|  |  |                     });
 | 
	
		
			
				|  |  |             },
 | 
	
		
			
				|  |  |             // load sessions
 | 
	
		
			
				|  |  |             function (callback) {
 | 
	
		
			
				|  |  |                 let sessions = new Sessions();
 | 
	
		
			
				|  |  |                 sessions.getUserSessionsFromMysql();
 | 
	
		
			
				|  |  |                 /*let sessions = new Sessions();
 | 
	
		
			
				|  |  |                 sessions.getUserSessionsFromMysql();*/
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         ]);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         DoctorRepo.deleteToken(token, function (err, result) {
 | 
	
		
			
				|  |  |             if (err) {
 | 
	
		
			
				|  |  |                 ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
 |