app.client.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * App客户端。
  3. */
  4. "use strict";
  5. let RedisClient = require('../../repository/redis/redis.client');
  6. let RedisModel = require('./../redis.model');
  7. let AppStatusRepo = require('../../repository/mysql/app.status.repo');
  8. let ModelUtil = require('../../util/model.util');
  9. let clientCache = require('../socket.io/client.cache').clientCache();
  10. let WlyyAssistantSDK = require("../../util/wlyyAssistant.sdk");
  11. let log = require("../../util/log.js");
  12. let pusher = require('../push/pusher');
  13. let redisConn = RedisClient.redisClient().connection;
  14. const CONTENT_TYPES = require('../../include/commons').CONTENT_TYPES;
  15. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  16. const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
  17. const PLATFORMS = require('../../include/commons').PLATFORM;
  18. class AppClient extends RedisModel {
  19. constructor() {
  20. super();
  21. }
  22. /**
  23. * 更新客户端App状态。
  24. *
  25. * @param userId
  26. * @param appInBg
  27. * @param handler
  28. */
  29. updateAppStatus(userId, appInBg, handler) {
  30. let self = this;
  31. let userStatusKey = RedisModel.makeRedisKey(REDIS_KEYS.UserAppStatus, userId);
  32. redisConn.hgetAsync(userStatusKey, 'app_in_bg').then(function (res) {
  33. if (res !== null) {
  34. redisConn.hsetAsync(userStatusKey, 'app_in_bg', appInBg ? 1 : 0).then(function (res) {
  35. if (handler) {
  36. handler(null, true);
  37. } else {
  38. ModelUtil.emitOK(self.eventEmitter, {});
  39. }
  40. });
  41. } else {
  42. if (handler) {
  43. handler(null, true);
  44. } else {
  45. ModelUtil.emitDataNotFound(self.eventEmitter, {"message": "User is offline, unable to update app status."});
  46. }
  47. }
  48. });
  49. }
  50. /**
  51. * 获取用户App端状态。若Redis中找不到,则从MySQL中查找。
  52. *
  53. * @param userId
  54. * @param handler
  55. */
  56. static getAppStatus(userId, handler) {
  57. let userStatusKey = RedisModel.makeRedisKey(REDIS_KEYS.UserAppStatus, userId);
  58. redisConn.hgetallAsync(userStatusKey)
  59. .then(function (res) {
  60. if (res) {
  61. handler(null, res);
  62. } else {
  63. AppStatusRepo.findOne(userId, function (err, res) {
  64. if (err) throw err;
  65. let userStatus = null;
  66. if (res.length > 0) {
  67. userStatus = {
  68. platform: res[0].platform,
  69. device_token: res[0].device_token,
  70. client_id: res[0].client_id,
  71. app_in_bg: res[0].app_in_bg == 1,
  72. last_login_time: res[0].last_login_time
  73. }
  74. }
  75. handler(null, userStatus);
  76. });
  77. }
  78. })
  79. .catch(function (err) {
  80. handler(err, null);
  81. });
  82. }
  83. /**
  84. * 向App端推送消息。指令性消息不推。
  85. *
  86. * @param targetId
  87. * @param message
  88. * @param sessionType
  89. */
  90. static sendNotification(targetId, message, sessionType,badge) {
  91. if (message.content_type == CONTENT_TYPES.PlainText ||
  92. message.content_type == CONTENT_TYPES.Image ||
  93. message.content_type == CONTENT_TYPES.Audio||
  94. message.content_type == CONTENT_TYPES.Video||
  95. message.content_type == CONTENT_TYPES.GoTo||
  96. sessionType==SESSION_TYPES.SYSTEM) {
  97. AppClient.getAppStatus(targetId, function (err, userStatus) {
  98. if (err) {
  99. ModelUtil.logError("Get user app status failed", err);
  100. return;
  101. }
  102. // let pc_doctorClient = clientCache.findByIdAndType("pc_"+targetId,SOCKET_TYPES.PC_DOCTOR);
  103. // if(pc_doctorClient){
  104. // log.warn("User's pc is online, user id: " + targetId + ", we cannot send getui.");
  105. // return;
  106. // }
  107. //调用接口推送医生助手微信模板
  108. let params = 'sessionId='+(message.session_id||'')+'&sessionType='+sessionType+"&from="+(message.sender_id|| '')+'&content='+message.content+'&businessType='+(message.business_type || 1)+'&doctorCode='+targetId;
  109. WlyyAssistantSDK.request('admin', '0a5c5258-8863-4b07-a3f9-88c768528ab4', '', 'admin_imei', '/doctor/feldsher/sendDoctorTemplate?' + params, 'GET', function (err, res) {
  110. if(err){
  111. log.error(err);
  112. }else {
  113. log.info(res);
  114. }
  115. });
  116. if (!userStatus) {
  117. log.warn("User's app status is not found, user id: " + targetId + ", maybe user never login yet or logout?");
  118. return;
  119. }
  120. let tipMessage = CONTENT_TYPES.typeToDescription(parseInt(message.content_type), "您有一条新消息") || message.content;
  121. let customData = {
  122. session_id: message.session_id||'',
  123. session_type: sessionType,
  124. from: message.sender_id|| '',
  125. data: message.content,
  126. business_type: message.business_type || 1
  127. };
  128. if (userStatus.platform == PLATFORMS.iOS) {
  129. pusher.pushToSingleViaAPN(tipMessage, customData, message.content_type, userStatus.device_token,badge, function (err, res) {
  130. if (err) {
  131. ModelUtil.logError("Send notification via APN failed:"+userStatus.device_token, err);
  132. } else {
  133. log.info("Send notification via APN succeed: ", JSON.stringify(res));
  134. }
  135. });
  136. } else if (userStatus.platform == PLATFORMS.Android) {
  137. let title = '新消息';
  138. pusher.pushToSingleViaAndroid(title, tipMessage, customData, userStatus.client_id, userStatus.app_in_bg, function (err, res) {
  139. if (err) {
  140. ModelUtil.logError("Send notification via Android failed", err);
  141. } else {
  142. log.info("Send notification via Android succeed: ", JSON.stringify(res));
  143. }
  144. });
  145. }
  146. });
  147. }
  148. }
  149. }
  150. // Expose class
  151. module.exports = AppClient;