wechat.client.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * 用户微信客户端。
  3. */
  4. "use strict";
  5. let RedisClient = require('../../repository/redis/redis.client');
  6. let RedisModel = require('../redis.model');
  7. let ObjectUtil = require("../../util/object.util.js");
  8. let ModelUtil = require('../../util/model.util');
  9. let DoctorRepo = require('../../repository/mysql/doctor.repo');
  10. let WechatSDK = require('../../util/wechat.sdk');
  11. let PatientRepo = require('../../repository/mysql/patient.repo');
  12. let redisConn = RedisClient.redisClient().connection;
  13. let clientCache = require('../socket.io/client.cache').clientCache();
  14. let configFile = require('../../include/commons').CONFIG_FILE;
  15. let config = require('../../resources/config/' + configFile);
  16. let log = require("../../util/log.js");
  17. let https = require('https');
  18. const CONTENT_TYPES = require('../../include/commons').CONTENT_TYPES;
  19. const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
  20. class WechatClient extends RedisModel {
  21. constructor() {
  22. super();
  23. }
  24. /**
  25. * 取得用户微信端状态。若Redis中找不到,则从MySQL中查找。
  26. *
  27. * @param userId
  28. * @param handler
  29. */
  30. static getWechatStatus(userId, handler) {
  31. redisConn.hgetallAsync(RedisModel.makeRedisKey(REDIS_KEYS.UserWechatStatus, userId))
  32. .then(function (status) {
  33. if (status == null) {
  34. PatientRepo.findWechatOpenId(userId, handler);
  35. } else {
  36. handler(null, {open_id: status.open_id});
  37. }
  38. })
  39. .catch(function (err) {
  40. handler(err, null);
  41. });
  42. }
  43. /**
  44. * 向微信端用户发送消息。若用户微信端在线,通过Web Socket推送给患者,如果不在线则通过微信的模板消息。
  45. *
  46. * @param message 消息
  47. */
  48. static sendMessage(message) {
  49. let patientClient = clientCache.findById(message.to);
  50. if (patientClient) {
  51. WechatClient.sendViaWebSocket(patientClient.socket, message);
  52. } else {
  53. log.info("User of wechat endpoint is not online, user id: ", message.to, ", sending via wechat template message.");
  54. WechatClient.sendViaTemplateMessage(message);
  55. }
  56. };
  57. static sendViaWebSocket(socket, message){
  58. message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
  59. socket.emit('message', message);
  60. }
  61. /**
  62. * 发送微信模板消息给居民
  63. *
  64. * @param message
  65. */
  66. static sendViaTemplateMessage(message) {
  67. function sendWxMessage(openid, name, topic) {
  68. let replyContent = message.content;
  69. switch (Number.parseInt(message.contentType)) {
  70. case CONTENT_TYPES.Image:
  71. replyContent = "[图片]";
  72. break;
  73. case CONTENT_TYPES.Audio:
  74. replyContent = "[语音]";
  75. break;
  76. case 0:
  77. case CONTENT_TYPES.Article:
  78. case CONTENT_TYPES.GoTo:
  79. case CONTENT_TYPES.TopicBegin:
  80. case CONTENT_TYPES.TopicEnd:
  81. return;
  82. default:
  83. break;
  84. }
  85. // 发送模板消息
  86. WechatSDK.sendTemplateMessage({
  87. touser: openid,
  88. template_id: config.wechatConfig.template.consultTemplate,
  89. url: config.wechatConfig.baseUrl + "/wx/html/yszx/html/consulting-doctor.html?openid=" + openid +
  90. "&consult=" + topic.name + "&toUser=" + message.to,
  91. data: {
  92. first: {value: "您的健康咨询有新的回复", color: "#000000"}
  93. , remark: {value: "", color: "#000000"}
  94. , keyword1: {value: topic.description, color: "#000000"}
  95. , keyword2: {value: replyContent, color: "#000000"}
  96. , keyword3: {value: name, color: "#000000"}
  97. }
  98. });
  99. }
  100. // 查询微信OpenId及医生信息,用于构建微信模板消息
  101. PatientRepo.findWechatOpenId(message.to, function (err, result) {
  102. if (err) {
  103. ModelUtil.logError("Get wechat openid failed", err);
  104. return;
  105. }
  106. let openid = result && result.length > 0 ? result[0].openid : "";
  107. if (openid) {
  108. DoctorRepo.findOne(message.from, function (err, result) {
  109. if (err) {
  110. ModelUtil.logError("Get doctor info failed", err);
  111. return;
  112. }
  113. if (result && result.length > 0) {
  114. let name = result[0].name;
  115. let topic = result && result.length > 0 ? result[0] : "";
  116. if (topic) {
  117. sendWxMessage(openid, name, topic);
  118. }
  119. } else {
  120. ModelUtil.logError("Can not find user info", err);
  121. }
  122. });
  123. } else {
  124. ModelUtil.logError("User haven't bound with wechat, user id: " + message.to, err);
  125. }
  126. });
  127. };
  128. }
  129. module.exports = WechatClient;