wechat.client.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 WechatSDK = require('../../util/wechat.sdk');
  10. let PatientRepo = require('../../repository/mysql/patient.repo');
  11. let TopicRepo = require("../../repository/mysql/topics.repo.js");
  12. let ParticipantRepo = require("../../repository/mysql/participant.repo");
  13. let redisConn = RedisClient.redisClient().connection;
  14. let clientCache = require('../socket.io/client.cache').clientCache();
  15. let configFile = require('../../include/commons').CONFIG_FILE;
  16. let config = require('../../resources/config/' + configFile);
  17. let log = require("../../util/log.js");
  18. let https = require('https');
  19. let async = require('async');
  20. const CONTENT_TYPES = require('../../include/commons').CONTENT_TYPES;
  21. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  22. const SOCKET_TYPES = require('../../include/commons').SOCKET_TYPES;
  23. const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
  24. class WechatClient extends RedisModel {
  25. constructor() {
  26. super();
  27. }
  28. /**
  29. * 取得用户微信端状态。若Redis中找不到,则从MySQL中查找。
  30. *
  31. * @param userId
  32. * @param handler
  33. */
  34. static getWechatStatus(userId, handler) {
  35. redisConn.hgetallAsync(RedisModel.makeRedisKey(REDIS_KEYS.UserWechatStatus, userId))
  36. .then(function (status) {
  37. if (status == null) {
  38. PatientRepo.findWechatOpenId(userId, handler);
  39. } else {
  40. handler(null, {openid: status.openid});
  41. }
  42. })
  43. .catch(function (err) {
  44. handler(err, null);
  45. });
  46. }
  47. /**
  48. * 向微信端用户发送消息。若用户微信端在线,通过Web Socket推送给患者,如果不在线则通过微信的模板消息。
  49. *
  50. * 只推送文本、图片及语音消息
  51. *
  52. * @param targetUserId
  53. * @param message 消息体
  54. */
  55. static sendMessage(targetUserId, targetUserName, message) {
  56. if (message&&(message.content_type == CONTENT_TYPES.PlainText ||
  57. message.content_type == CONTENT_TYPES.Image ||
  58. message.content_type == CONTENT_TYPES.Audio||
  59. message.content_type == CONTENT_TYPES.PrescriptionBloodStatus ||
  60. message.content_type == CONTENT_TYPES.PrescriptionFollowupContent ||
  61. message.content_type == CONTENT_TYPES.ChangeDoorCardInfo ||
  62. message.content_type == CONTENT_TYPES.ChangeDoorDoctor ||
  63. message.content_type == CONTENT_TYPES.ChangeDoorPackageItems)) {
  64. let patientClient = clientCache.findById(targetUserId);
  65. let pc_patientClient = clientCache.findById("pcpatient_"+targetUserId);
  66. let doctorClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);
  67. let pc_doctorClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
  68. var count = 0;
  69. if (patientClient || pc_patientClient) {
  70. if(patientClient){
  71. log.warn("User's wechat endpoint is online, sending via web socket. User id: ", targetUserId);
  72. WechatClient.sendViaWebSocket(patientClient.socket, message);
  73. }
  74. if(pc_patientClient){
  75. log.warn("User's pc endpoint is online, sending via web socket. User id: ", targetUserId);
  76. WechatClient.sendViaWebSocket(pc_patientClient.socket, message);
  77. }
  78. if(doctorClient){
  79. log.error("doctor sessionid "+doctorClient.sessionId);
  80. if(patientClient && patientClient.sessionId==doctorClient.sessionId){
  81. log.error("patient sessionid "+patientClient.sessionId);
  82. //用户socket在线,推送给用户后,告知医生此消息已读
  83. WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
  84. WechatClient.sendReadDoctor(doctorClient.socket, message);
  85. }
  86. if(pc_patientClient && pc_patientClient.sessionId==doctorClient.sessionId){
  87. log.error("pc_patient sessionid "+pc_patientClient.sessionId);
  88. //用户socket在线,推送给用户后,告知医生此消息已读
  89. WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
  90. WechatClient.sendReadDoctor(doctorClient.socket, message);
  91. }
  92. }else{
  93. count++;
  94. }
  95. if(pc_doctorClient){
  96. log.error("doctor sessionid "+pc_doctorClient.sessionId);
  97. if(patientClient){
  98. log.error("patient sessionid "+patientClient.sessionId);
  99. if(patientClient.sessionId==pc_doctorClient.sessionId){
  100. //用户socket在线,推送给用户后,告知医生此消息已读
  101. WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
  102. WechatClient.sendReadDoctor(pc_doctorClient.socket, message);
  103. }
  104. }
  105. if(pc_patientClient){
  106. log.error("pc_patient sessionid "+pc_patientClient.sessionId);
  107. if(pc_patientClient.sessionId==pc_doctorClient.sessionId){
  108. //用户socket在线,推送给用户后,告知医生此消息已读
  109. WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, targetUserId, ObjectUtil.timestampToLong(message.timestamp));
  110. WechatClient.sendReadDoctor(pc_doctorClient.socket, message);
  111. }
  112. }
  113. }else{
  114. count++;
  115. }
  116. if(count==0){
  117. log.error("doctor client not found");
  118. }
  119. } else {
  120. log.info("User's wechat and pc endpoint is not online, sending via wechat template message. User id: ", targetUserId);
  121. var isSendWXTem = true;//是否发送微信模板
  122. if(message.content_type == CONTENT_TYPES.PrescriptionBloodStatus||message.content_type == CONTENT_TYPES.PrescriptionFollowupContent){
  123. var content = JSON.parse(message.content);
  124. if(content.isSendWxTemplate){
  125. message.content = content.text;
  126. }else {
  127. isSendWXTem = false;
  128. }
  129. }
  130. if(isSendWXTem){
  131. WechatClient.sendViaMessageTemplate(targetUserId, targetUserName, message);
  132. }
  133. }
  134. } else if(message.content_type == CONTENT_TYPES.TopicEnd){
  135. let patientClient = clientCache.findById(targetUserId);
  136. if(patientClient){//结束咨询的告知患者
  137. WechatClient.sendViaWebSocket(patientClient.socket, message);
  138. }
  139. let pc_patientClient = clientCache.findById("pcpatient_"+targetUserId);
  140. if(pc_patientClient)//结束咨询的告知患者
  141. {
  142. WechatClient.sendViaWebSocket(pc_patientClient.socket, message);
  143. }
  144. }
  145. };
  146. static sendViaWebSocket(socket, message) {
  147. socket.emit('message', {
  148. id: message.id,
  149. session_id: message.session_id,
  150. sender_id: message.sender_id,
  151. sender_name: message.sender_name,
  152. content_type: message.content_type,
  153. content: message.content,
  154. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  155. type: message.content_type, // legacy support
  156. name: message.sender_name,
  157. sender_img : message.sender_img
  158. });
  159. }
  160. static sendAllRead(doctorId,sessionId){
  161. let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
  162. let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
  163. if(doctorClient){
  164. if(doctorClient.sessionId==sessionId){
  165. doctorClient.socket.emit('message',{ read:"all"});
  166. }else{
  167. log.warn(" doctor not in the same session ");
  168. }
  169. }else{
  170. if(pc_doctorClient && pc_doctorClient.sessionId == sessionId){
  171. pc_doctorClient.socket.emit('message',{ read:"all"});
  172. }else{
  173. log.warn(doctorId+" target doctor is not online!");
  174. }
  175. }
  176. }
  177. static sendMucAllRead(doctorId,loginUserId,sessionId){
  178. let loginClinet = clientCache.findByIdAndType(loginUserId,SOCKET_TYPES.DOCTOR);
  179. if(loginClinet){
  180. //muc是医生来获取数据不能更新成已读
  181. log.warn("type is muc login is doctor not send all read to other doctor!")
  182. return;
  183. }
  184. let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
  185. let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
  186. if(doctorClient){
  187. if(doctorClient.sessionId==sessionId){
  188. doctorClient.socket.emit('message',{ read:"all"});
  189. }else{
  190. log.warn(" doctor not in the same session ");
  191. }
  192. }else{
  193. if(pc_doctorClient && pc_doctorClient.sessionId == sessionId){
  194. pc_doctorClient.socket.emit('message',{ read:"all"});
  195. }else{
  196. log.warn(doctorId+" target doctor is not online!");
  197. }
  198. }
  199. }
  200. static sendReadDoctor(socket, message) {
  201. socket.emit('message', {
  202. id: message.id,
  203. session_id: message.session_id,
  204. sender_id: message.sender_id,
  205. sender_name: message.sender_name,
  206. content_type: message.content_type,
  207. content: message.content,
  208. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  209. type: message.content_type, // legacy support
  210. name: message.sender_name,
  211. read:"one"
  212. });
  213. }
  214. static sendReadDoctorByDoctorId(doctorId, message) {
  215. let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
  216. let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
  217. if(!doctorClient&&!pc_doctorClient){
  218. log.warn("target doctor is not online!");
  219. return;
  220. }
  221. let sendDoctorClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);
  222. if(!sendDoctorClient){
  223. sendDoctorClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
  224. }
  225. var count = 0;
  226. if(doctorClient&&sendDoctorClient&&sendDoctorClient.sessionId==doctorClient.sessionId){
  227. WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
  228. sendDoctorClient.socket.emit('message', {
  229. id: message.id,
  230. session_id: message.session_id,
  231. sender_id: message.sender_id,
  232. sender_name: message.sender_name,
  233. content_type: message.content_type,
  234. content: message.content,
  235. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  236. type: message.content_type, // legacy support
  237. name: message.sender_name,
  238. read:"one"
  239. });
  240. }else{
  241. count++;
  242. }
  243. //发送pc版医生端
  244. if(pc_doctorClient&&sendDoctorClient&&sendDoctorClient.sessionId==pc_doctorClient.sessionId){
  245. WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
  246. pc_doctorClient.socket.emit('message', {
  247. id: message.id,
  248. session_id: message.session_id,
  249. sender_id: message.sender_id,
  250. sender_name: message.sender_name,
  251. content_type: message.content_type,
  252. content: message.content,
  253. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  254. type: message.content_type, // legacy support
  255. name: message.sender_name,
  256. read:"one"
  257. });
  258. }else{
  259. count++;
  260. }
  261. if(count==2){
  262. log.warn("doctor is not in the same session or is not online");
  263. }
  264. }
  265. static sendSocketMessageToDoctor(doctorId, message) {
  266. let doctorClient = clientCache.findByIdAndType(doctorId,SOCKET_TYPES.DOCTOR);
  267. let pc_doctorClient = clientCache.findByIdAndType("pc_"+doctorId,SOCKET_TYPES.PC_DOCTOR);
  268. if(!doctorClient&&!pc_doctorClient){
  269. log.warn("target doctor is not online!");
  270. return;
  271. }
  272. // let sendClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.DOCTOR);//app医生发送的消息
  273. // if(!sendClient){//pc医生发送的消息
  274. // sendClient = clientCache.findByIdAndType("pc_"+message.sender_id,SOCKET_TYPES.PC_DOCTOR);
  275. // }
  276. // if(!sendClient){//居民发送的消息
  277. // sendClient = clientCache.findByIdAndType(message.sender_id,SOCKET_TYPES.PATIENT);
  278. // }
  279. var count = 0;
  280. if(doctorClient&&message.session_id==doctorClient.sessionId){
  281. WechatClient.updateParticipantLastFetchTime(doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
  282. doctorClient.socket.emit('message', {
  283. id: message.id,
  284. session_id: message.session_id,
  285. sender_id: message.sender_id,
  286. sender_name: message.sender_name,
  287. content_type: message.content_type,
  288. content: message.content,
  289. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  290. type: message.content_type, // legacy support
  291. name: message.sender_name,
  292. });
  293. }else{
  294. count++;
  295. }
  296. //发送pc端
  297. if(pc_doctorClient&&message.session_id==pc_doctorClient.sessionId){
  298. WechatClient.updateParticipantLastFetchTime(pc_doctorClient.sessionId, doctorId, ObjectUtil.timestampToLong(message.timestamp));
  299. pc_doctorClient.socket.emit('message', {
  300. id: message.id,
  301. session_id: message.session_id,
  302. sender_id: message.sender_id,
  303. sender_name: message.sender_name,
  304. content_type: message.content_type,
  305. content: message.content,
  306. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  307. type: message.content_type, // legacy support
  308. name: message.sender_name,
  309. });
  310. }else{
  311. count++;
  312. }
  313. if(count==2){
  314. log.warn("doctor is not in the same session or is not online");
  315. }
  316. }
  317. static sendPcImSocket(targetId, message, sessionType) {
  318. if (message.content_type == CONTENT_TYPES.PlainText ||
  319. message.content_type == CONTENT_TYPES.Image ||
  320. message.content_type == CONTENT_TYPES.Audio||
  321. message.content_type == CONTENT_TYPES.Video||
  322. message.content_type == CONTENT_TYPES.GoTo||
  323. sessionType==SESSION_TYPES.SYSTEM) {
  324. let pcim_doctorClient = clientCache.findByIdAndType("pcim_"+targetId,SOCKET_TYPES.PCIM_DOCTOR);
  325. if(pcim_doctorClient) {
  326. let customData = {
  327. session_id: message.session_id||'',
  328. session_type: sessionType,
  329. from: message.sender_id|| '',
  330. data: message.content,
  331. business_type: message.business_type || 1
  332. };
  333. pcim_doctorClient.socket.emit('message', {
  334. session_id: message.session_id||'',
  335. session_type: sessionType,
  336. from: message.sender_id|| '',
  337. data: message.content,
  338. business_type: message.business_type || 1
  339. });
  340. }
  341. }
  342. }
  343. /**
  344. *
  345. * 发送微信模板消息给居民
  346. *
  347. * @param targetUserId
  348. * @param message
  349. */
  350. static sendViaMessageTemplate(targetUserId, targetUserName, message) {
  351. async.waterfall([
  352. // 获取微信openid
  353. function (callback) {
  354. PatientRepo.findWechatOpenIds(targetUserId, function (err, res) {
  355. if (err) {
  356. ModelUtil.logError("Get wechat openid failed", err);
  357. return;
  358. }
  359. var map = new Map();
  360. res.forEach(function (participant) {
  361. let openid = participant.openid;
  362. if (targetUserId==participant.code) {
  363. if (!openid) {
  364. ModelUtil.logError("User haven't bound with wechat, user id: " + targetUserId);
  365. }
  366. map.set("openid",participant);
  367. }else {
  368. if(!map.has(openid)){
  369. map.set(openid,participant);
  370. }
  371. }
  372. })
  373. //
  374. // let openid = result && result.length > 0 ? result[0].openid : null;
  375. // if (!openid) {
  376. // ModelUtil.logError("User haven't bound with wechat, user id: " + targetUserId);
  377. // return;
  378. // }
  379. //
  380. // log.warn("Send via wechat message template, user id: " + targetUserId + ", openid: " + openid);
  381. callback(null, map);
  382. });
  383. },
  384. // 获取议题信息
  385. function (map, callback) {
  386. TopicRepo.findLastTopicStatusAndType(message.session_id, function (err, res) {
  387. if (err) {
  388. ModelUtil.logError("Get topic failed", err);
  389. return;
  390. }
  391. if (!res || res.length == 0) {
  392. ModelUtil.logError("Unable to find session last topic");
  393. return;
  394. }
  395. callback(null, map, message.sender_name, res[0]);
  396. });
  397. },
  398. // 发送消息
  399. function (map, senderName, topic,callback) {
  400. let replyContent = message.content;
  401. switch (Number.parseInt(message.content_type)) {
  402. case CONTENT_TYPES.Image:
  403. replyContent = "[图片]";
  404. break;
  405. case CONTENT_TYPES.Audio:
  406. replyContent = "[语音]";
  407. break;
  408. default:
  409. break;
  410. }
  411. var patient = map.get("openid");
  412. map.delete("openid");
  413. let agent = topic.agent;
  414. let consultTitle = null;
  415. let description = null;
  416. let url = config.wechatConfig.baseUrl;
  417. switch (topic.type) {
  418. case 8:
  419. consultTitle = "续方";
  420. description = "续方咨询";
  421. url = url + "/wx/html/yszx/html/prescription-consulting.html";
  422. case 11:
  423. consultTitle = "上门服务";
  424. description = "上门服务咨询";
  425. url = url + "wx/html/appoint_service/html/appoint-serviceDetail.html";
  426. default:
  427. consultTitle = "健康";
  428. description = topic.description;
  429. url = url + "/wx/html/yszx/html/consulting-doctor.html";
  430. }
  431. if(agent){//代理人发起的议题
  432. var agentOpenid = "";
  433. if(map.size>0){
  434. for(var key of map.keys()){
  435. var member = map.get(key);
  436. if(agent == member.code){
  437. agentOpenid = key;
  438. var openid = key;
  439. var first = "您的家人("+patient.name+")的"+consultTitle+"咨询有新的回复";
  440. // 发送模板消息
  441. WechatSDK.sendTemplateMessage({
  442. touser: openid,
  443. name: member.name,
  444. patient: member.code,
  445. template_id: config.wechatConfig.template.consultTemplate,
  446. url: url + "?openid=" + openid + "&type="+topic.type+"&doctor="+message.sender_id+
  447. "&consult=" + topic.id + "&toUser=" + member.code + "&toName=" + member.name+"&represented="+patient.code,
  448. data: {
  449. first: {value: first, color: "#000000"}
  450. , remark: {value: "", color: "#000000"}
  451. , keyword1: {value: description, color: "#000000"}
  452. , keyword2: {value: replyContent, color: "#000000"}
  453. , keyword3: {value: senderName, color: "#000000"}
  454. }
  455. }, function (err, res) {
  456. err ? log.error(err) : log.info(res);
  457. });
  458. }
  459. }
  460. }
  461. if(patient.openid&&patient.openid!=agentOpenid){
  462. var first = "您的"+consultTitle+"咨询有新的回复";
  463. // 发送模板消息
  464. WechatSDK.sendTemplateMessage({
  465. touser: patient.openid,
  466. name: targetUserName,
  467. patient: targetUserId,
  468. template_id: config.wechatConfig.template.consultTemplate,
  469. url: url + "?openid=" + patient.openid +"&type="+topic.type+"&doctor="+message.sender_id+
  470. "&consult=" + topic.id + "&toUser=" + targetUserId + "&toName=" + targetUserName+"&represented="+patient.code,
  471. data: {
  472. first: {value: first, color: "#000000"}
  473. , remark: {value: "", color: "#000000"}
  474. , keyword1: {value: description, color: "#000000"}
  475. , keyword2: {value: replyContent, color: "#000000"}
  476. , keyword3: {value: senderName, color: "#000000"}
  477. }
  478. }, function (err, res) {
  479. err ? log.error(err) : log.info(res);
  480. });
  481. }
  482. }else {//自己发起的议题
  483. // 发送模板消息
  484. if(patient.openid){
  485. WechatSDK.sendTemplateMessage({
  486. touser: patient.openid,
  487. name: targetUserName,
  488. patient: targetUserId,
  489. template_id: config.wechatConfig.template.consultTemplate,
  490. url: url + "?openid=" + patient.openid +"&type="+topic.type+"&doctor="+message.sender_id+
  491. "&consult=" + topic.id + "&toUser=" + targetUserId + "&toName=" + targetUserName+"&represented="+patient.code,
  492. data: {
  493. first: {value: "您的"+consultTitle+"咨询有新的回复", color: "#000000"}
  494. , remark: {value: "", color: "#000000"}
  495. , keyword1: {value: description, color: "#000000"}
  496. , keyword2: {value: replyContent, color: "#000000"}
  497. , keyword3: {value: senderName, color: "#000000"}
  498. }
  499. }, function (err, res) {
  500. err ? log.error(err) : log.info(res);
  501. });
  502. }
  503. if(map.size>0){
  504. for(var key of map.keys()){
  505. if(!patient.openid||key!=patient.openid){
  506. var member = map.get(key);
  507. var openid = key;
  508. var first = "您的家人("+patient.name+")的"+consultTitle+"咨询有新的回复";
  509. // 发送模板消息
  510. WechatSDK.sendTemplateMessage({
  511. touser: openid,
  512. name: member.name,
  513. patient: member.code,
  514. template_id: config.wechatConfig.template.consultTemplate,
  515. url: url + "?openid=" + openid +"&type="+topic.type+"&doctor="+message.sender_id+
  516. "&consult=" + topic.id + "&toUser=" + member.code + "&toName=" + member.name+"&represented="+patient.code,
  517. data: {
  518. first: {value: first, color: "#000000"}
  519. , remark: {value: "", color: "#000000"}
  520. , keyword1: {value: description, color: "#000000"}
  521. , keyword2: {value: replyContent, color: "#000000"}
  522. , keyword3: {value: senderName, color: "#000000"}
  523. }
  524. }, function (err, res) {
  525. err ? log.error(err) : log.info(res);
  526. });
  527. }
  528. }
  529. }
  530. }
  531. callback(null, null);
  532. }
  533. ],
  534. function (err, res) {
  535. if (!err) {
  536. log.info("Send via wechat template message, DONE!");
  537. }
  538. });
  539. };
  540. static updateParticipantLastFetchTime(sessionId, userId, score) {
  541. score = score + 1000;
  542. let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
  543. redisConn.zaddAsync(participantsKey, score, userId)
  544. .then(function (res) {
  545. ParticipantRepo.updateLastFetchTime(new Date(score), sessionId, userId, function (err, res) {
  546. if (err) {
  547. log.error("Update participant last fetch time failed: ", err);
  548. }
  549. });
  550. })
  551. .catch(function (err) {
  552. log.error("Update participant last fetch time failed: ", err);
  553. });
  554. }
  555. }
  556. module.exports = WechatClient;