| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /**
- * 用户集合。管理Redis中的用户列表。
- *
- * author: Sand
- * since: 12/13/2016
- */
- "use strict";
- const RedisKeys = require('../../include/commons').REDIS_KEYS;
- let RedisModel = require('../redis.model');
- let Doctor = require('./doctor');
- let Patient = require('./patient');
- 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 RedisClient = require('../../repository/redis/redis.client');
- let redisConn = RedisClient.redisClient().connection;
- let async = require('async');
- let log = require('../../util/log');
- class Users extends RedisModel {
- constructor() {
- super();
- this._key = RedisKeys.Users;
- }
- /**
- * 获取用户,直接从MYSQL获取,缓存是否有在不能确定。
- *
- * @param userId
- * @param outCallback
- */
- getUser(userId, outCallback) {
- let self = this;
- async.waterfall([
- // determine user type
- function (callback) {
- self.isPatientId(userId, function (err, isPatient) {
- callback(null, isPatient);
- });
- },
- // get from mysql
- function (isPatientId) {
- let repoProto = isPatientId ? PatientRepo : DoctorRepo;
- repoProto.findOne(userId, function (err, res) {
- let user = isPatientId ? new Doctor() : new Patient();
- if(res.length > 0){
- user.name = res[0].name;
- user.sex = res[0].sex;
- user.birthdate = res[0].birthdate;
- user.avatar = res[0].avatar;
- }
- outCallback(null, user);
- });
- }
- ]);
- }
- /**
- * 获取客户端App状态。
- *
- * @param userId
- * @param outCallback
- */
- getAppStatus(userId, outCallback){
- let self = this;
- async.waterfall([
- // get from redis
- function (callback) {
- let userStatusKey = self.makeRedisKey(RedisKeys.UserStatus, userId);
- redisConn.hgetallAsync(userStatusKey).then(function (res) {
- if(res === null){
- callback(null); // get from mysql
- } else {
- outCallback(null, res);
- }
- });
- },
- // get from MySQL
- function () {
- AppStatusRepo.findOne(userId, function (err, res) {
- let userStatus = null;
- if(res.length > 0){
- userStatus = {};
- userStatus.platform = res[0].platform;
- userStatus.token = res[0].token;
- userStatus.client_id = res[0].client_id;
- userStatus.app_in_bg = res[0].app_in_bg;
- userStatus.last_login_time = res[0].last_login_time;
- }
- outCallback(null, userStatus);
- });
- }
- ]);
- }
- /**
- * 更新客户端App状态。
- *
- * @param userId
- * @param appInBg
- * @param outCallback
- */
- updateAppStatus(userId, appInBg, outCallback){
- let self = this;
- DoctorRepo.updateStatus(userId, status,
- function (err, result) {
- if (err) {
- ModelUtil.emitDbError(self.eventEmitter, 'Update user status failed', err);
- return;
- }
- ModelUtil.emitData(self.eventEmitter, {});
- });
- }
- /**
- * 取得用户微信端状态。
- *
- * @param userId
- * @param outCallback
- */
- getWechatStatus(userId, outCallback){
- let self = this;
- }
- /**
- * 用户登录。
- *
- * @param userId
- * @param outCallback
- */
- login(userId, outCallback){
- let self = this;
- DoctorRepo.deleteToken(token, function (err, result) {
- if (err) {
- ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
- } else {
- DoctorRepo.login(userId, token, clientId, platform,
- function (err, result) {
- if (err) {
- ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
- }
- let token = new Token(userId, clientId, platform);
- ModelUtil.emitData(self.eventEmitter, {token: token.value});
- });
- }
- });
- }
- logout(userId, outCallback){
- let self = this;
- DoctorRepo.logout(userId,
- function (err, result) {
- if (err) {
- ModelUtil.emitDbError(self.eventEmitter, 'Logout failed', err);
- return;
- }
- ModelUtil.emitData(self.eventEmitter, {});
- });
- }
- /**
- * 用户ID是否属于患者。
- *
- * @param userId
- * @param callback
- */
- isPatientId(userId, callback) {
- async.waterfall([
- function (callback) {
- var sql = "select case when count(*) > 0 then true else false end 'is_patient' from patients where id = ?";
- ImDb.execQuery({
- "sql": sql,
- "args": [userId],
- "handler": function (err, res) {
- if (err) callback(err, res);
- callback(null, res);
- }
- });
- },
- function (res, callback) {
- if (res.length === 0) return false;
- callback(null, res[0].is_patient);
- }
- ],
- function (err, res) {
- if (err) {
- log.error("User id check failed: ", err);
- callback(null, false);
- return;
- }
- callback(null, res !== 0);
- });
- }
- }
- let Promises = require('bluebird');
- Promises.promisifyAll(Users.prototype);
- module.exports = Users;
|