users.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * 用户集合。管理Redis中的用户列表。
  3. *
  4. * author: Sand
  5. * since: 12/13/2016
  6. */
  7. "use strict";
  8. const RedisKeys = require('../../include/commons').REDIS_KEYS;
  9. let RedisModel = require('../redis.model');
  10. let Doctor = require('./doctor');
  11. let Patient = require('./patient');
  12. let ImDb = require('../../repository/mysql/db/im.db');
  13. let DoctorRepo = require('../../repository/mysql/doctor.repo');
  14. let PatientRepo = require('../../repository/mysql/patient.repo');
  15. let AppStatusRepo = require('../../repository/mysql/app.status.repo');
  16. let RedisClient = require('../../repository/redis/redis.client');
  17. let redisConn = RedisClient.redisClient().connection;
  18. let async = require('async');
  19. let log = require('../../util/log');
  20. class Users extends RedisModel {
  21. constructor() {
  22. super();
  23. this._key = RedisKeys.Users;
  24. }
  25. /**
  26. * 获取用户,直接从MYSQL获取,缓存是否有在不能确定。
  27. *
  28. * @param userId
  29. * @param outCallback
  30. */
  31. getUser(userId, outCallback) {
  32. let self = this;
  33. async.waterfall([
  34. // determine user type
  35. function (callback) {
  36. self.isPatientId(userId, function (err, isPatient) {
  37. callback(null, isPatient);
  38. });
  39. },
  40. // get from mysql
  41. function (isPatientId) {
  42. let repoProto = isPatientId ? PatientRepo : DoctorRepo;
  43. repoProto.findOne(userId, function (err, res) {
  44. let user = isPatientId ? new Doctor() : new Patient();
  45. if(res.length > 0){
  46. user.name = res[0].name;
  47. user.sex = res[0].sex;
  48. user.birthdate = res[0].birthdate;
  49. user.avatar = res[0].avatar;
  50. }
  51. outCallback(null, user);
  52. });
  53. }
  54. ]);
  55. }
  56. /**
  57. * 获取客户端App状态。
  58. *
  59. * @param userId
  60. * @param outCallback
  61. */
  62. getAppStatus(userId, outCallback){
  63. let self = this;
  64. async.waterfall([
  65. // get from redis
  66. function (callback) {
  67. let userStatusKey = self.makeRedisKey(RedisKeys.UserStatus, userId);
  68. redisConn.hgetallAsync(userStatusKey).then(function (res) {
  69. if(res === null){
  70. callback(null); // get from mysql
  71. } else {
  72. outCallback(null, res);
  73. }
  74. });
  75. },
  76. // get from MySQL
  77. function () {
  78. AppStatusRepo.findOne(userId, function (err, res) {
  79. let userStatus = null;
  80. if(res.length > 0){
  81. userStatus = {};
  82. userStatus.platform = res[0].platform;
  83. userStatus.token = res[0].token;
  84. userStatus.client_id = res[0].client_id;
  85. userStatus.app_in_bg = res[0].app_in_bg;
  86. userStatus.last_login_time = res[0].last_login_time;
  87. }
  88. outCallback(null, userStatus);
  89. });
  90. }
  91. ]);
  92. }
  93. /**
  94. * 更新客户端App状态。
  95. *
  96. * @param userId
  97. * @param appInBg
  98. * @param outCallback
  99. */
  100. updateAppStatus(userId, appInBg, outCallback){
  101. let self = this;
  102. DoctorRepo.updateStatus(userId, status,
  103. function (err, result) {
  104. if (err) {
  105. ModelUtil.emitDbError(self.eventEmitter, 'Update user status failed', err);
  106. return;
  107. }
  108. ModelUtil.emitData(self.eventEmitter, {});
  109. });
  110. }
  111. /**
  112. * 取得用户微信端状态。
  113. *
  114. * @param userId
  115. * @param outCallback
  116. */
  117. getWechatStatus(userId, outCallback){
  118. let self = this;
  119. }
  120. /**
  121. * 用户登录。
  122. *
  123. * @param userId
  124. * @param outCallback
  125. */
  126. login(userId, outCallback){
  127. let self = this;
  128. DoctorRepo.deleteToken(token, function (err, result) {
  129. if (err) {
  130. ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
  131. } else {
  132. DoctorRepo.login(userId, token, clientId, platform,
  133. function (err, result) {
  134. if (err) {
  135. ModelUtil.emitDbError(self.eventEmitter, 'Error occurs when user login and delete token', err);
  136. }
  137. let token = new Token(userId, clientId, platform);
  138. ModelUtil.emitData(self.eventEmitter, {token: token.value});
  139. });
  140. }
  141. });
  142. }
  143. logout(userId, outCallback){
  144. let self = this;
  145. DoctorRepo.logout(userId,
  146. function (err, result) {
  147. if (err) {
  148. ModelUtil.emitDbError(self.eventEmitter, 'Logout failed', err);
  149. return;
  150. }
  151. ModelUtil.emitData(self.eventEmitter, {});
  152. });
  153. }
  154. /**
  155. * 用户ID是否属于患者。
  156. *
  157. * @param userId
  158. * @param callback
  159. */
  160. isPatientId(userId, callback) {
  161. async.waterfall([
  162. function (callback) {
  163. var sql = "select case when count(*) > 0 then true else false end 'is_patient' from patients where id = ?";
  164. ImDb.execQuery({
  165. "sql": sql,
  166. "args": [userId],
  167. "handler": function (err, res) {
  168. if (err) callback(err, res);
  169. callback(null, res);
  170. }
  171. });
  172. },
  173. function (res, callback) {
  174. if (res.length === 0) return false;
  175. callback(null, res[0].is_patient);
  176. }
  177. ],
  178. function (err, res) {
  179. if (err) {
  180. log.error("User id check failed: ", err);
  181. callback(null, false);
  182. return;
  183. }
  184. callback(null, res !== 0);
  185. });
  186. }
  187. }
  188. let Promises = require('bluebird');
  189. Promises.promisifyAll(Users.prototype);
  190. module.exports = Users;