|
@ -6,16 +6,19 @@
|
|
|
*/
|
|
|
"use strict";
|
|
|
|
|
|
const RedisKeys = require('../../include/commons').REDIS_KEYS;
|
|
|
const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
|
|
|
const PLATFORMS = require('../../include/commons').PLATFORM;
|
|
|
|
|
|
let RedisModel = require('../redis.model');
|
|
|
let Doctor = require('./doctor');
|
|
|
let Patient = require('./patient');
|
|
|
let Sessions = require('../sessions/sessions');
|
|
|
|
|
|
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;
|
|
@ -26,7 +29,7 @@ class Users extends RedisModel {
|
|
|
constructor() {
|
|
|
super();
|
|
|
|
|
|
this._key = RedisKeys.Users;
|
|
|
this._key = REDIS_KEYS.Users;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@ -73,7 +76,7 @@ class Users extends RedisModel {
|
|
|
async.waterfall([
|
|
|
// get from redis
|
|
|
function (callback) {
|
|
|
let userStatusKey = self.makeRedisKey(RedisKeys.UserStatus, userId);
|
|
|
let userStatusKey = self.makeRedisKey(REDIS_KEYS.UserStatus, userId);
|
|
|
redisConn.hgetallAsync(userStatusKey).then(function (res) {
|
|
|
if(res === null){
|
|
|
callback(null); // get from mysql
|
|
@ -134,11 +137,62 @@ class Users extends RedisModel {
|
|
|
/**
|
|
|
* 用户登录。
|
|
|
*
|
|
|
* 用户登录时会加载与之相关的会话列表,会话消息,用户自身信息:App状态与微信状态。
|
|
|
*
|
|
|
* @param userId
|
|
|
* @param outCallback
|
|
|
* @param platform
|
|
|
* @param token
|
|
|
* @param clientId
|
|
|
*
|
|
|
* @return 用户token
|
|
|
*/
|
|
|
login(userId, 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 = self.makeRedisKey(loginFromApp ? REDIS_KEYS.UserWechatStatus : REDIS_KEYS.UserWechatStatus, userId);
|
|
|
let lastLoginTime = new Date();
|
|
|
|
|
|
async.waterfall([
|
|
|
// get user info
|
|
|
function (callback) {
|
|
|
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();*/
|
|
|
}
|
|
|
]);
|
|
|
|
|
|
DoctorRepo.deleteToken(token, function (err, result) {
|
|
|
if (err) {
|
|
|
ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
|