123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /**
- * 用户微信客户端。
- */
- "use strict";
- let RedisClient = require('../../repository/redis/redis.client');
- let RedisModel = require('../redis.model');
- let ObjectUtil = require("../../util/object.util.js");
- let ModelUtil = require('../../util/model.util');
- let DoctorRepo = require('../../repository/mysql/doctor.repo');
- let WechatSDK = require('../../util/wechat.sdk');
- let PatientRepo = require('../../repository/mysql/patient.repo');
- let redisConn = RedisClient.redisClient().connection;
- let clientCache = require('../socket.io/client.cache').clientCache();
- let configFile = require('../../include/commons').CONFIG_FILE;
- let config = require('../../resources/config/' + configFile);
- let log = require("../../util/log.js");
- let https = require('https');
- const CONTENT_TYPES = require('../../include/commons').CONTENT_TYPES;
- const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
- class WechatClient extends RedisModel {
- constructor() {
- super();
- }
- /**
- * 取得用户微信端状态。若Redis中找不到,则从MySQL中查找。
- *
- * @param userId
- * @param handler
- */
- static getWechatStatus(userId, handler) {
- redisConn.hgetallAsync(RedisModel.makeRedisKey(REDIS_KEYS.UserWechatStatus, userId))
- .then(function (status) {
- if (status == null) {
- PatientRepo.findWechatOpenId(userId, handler);
- } else {
- handler(null, {open_id: status.open_id});
- }
- })
- .catch(function (err) {
- handler(err, null);
- });
- }
- /**
- * 向微信端用户发送消息。若用户微信端在线,通过Web Socket推送给患者,如果不在线则通过微信的模板消息。
- *
- * @param message 消息
- */
- static sendMessage(message) {
- let patientClient = clientCache.findById(message.to);
- if (patientClient) {
- WechatClient.sendViaWebSocket(patientClient.socket, message);
- } else {
- log.info("User of wechat endpoint is not online, user id: ", message.to, ", sending via wechat template message.");
- WechatClient.sendViaTemplateMessage(message);
- }
- };
- static sendViaWebSocket(socket, message){
- message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
- socket.emit('message', message);
- }
- /**
- * 发送微信模板消息给居民
- *
- * @param message
- */
- static sendViaTemplateMessage(message) {
- function sendWxMessage(openid, name, topic) {
- let replyContent = message.content;
- switch (Number.parseInt(message.contentType)) {
- case CONTENT_TYPES.Image:
- replyContent = "[图片]";
- break;
- case CONTENT_TYPES.Audio:
- replyContent = "[语音]";
- break;
- case 0:
- case CONTENT_TYPES.Article:
- case CONTENT_TYPES.GoTo:
- case CONTENT_TYPES.TopicBegin:
- case CONTENT_TYPES.TopicEnd:
- return;
- default:
- break;
- }
- // 发送模板消息
- WechatSDK.sendTemplateMessage({
- touser: openid,
- template_id: config.wechatConfig.template.consultTemplate,
- url: config.wechatConfig.baseUrl + "/wx/html/yszx/html/consulting-doctor.html?openid=" + openid +
- "&consult=" + topic.name + "&toUser=" + message.to,
- data: {
- first: {value: "您的健康咨询有新的回复", color: "#000000"}
- , remark: {value: "", color: "#000000"}
- , keyword1: {value: topic.description, color: "#000000"}
- , keyword2: {value: replyContent, color: "#000000"}
- , keyword3: {value: name, color: "#000000"}
- }
- });
- }
- // 查询微信OpenId及医生信息,用于构建微信模板消息
- PatientRepo.findWechatOpenId(message.to, function (err, result) {
- if (err) {
- ModelUtil.logError("Get wechat openid failed", err);
- return;
- }
- let openid = result && result.length > 0 ? result[0].openid : "";
- if (openid) {
- DoctorRepo.findOne(message.from, function (err, result) {
- if (err) {
- ModelUtil.logError("Get doctor info failed", err);
- return;
- }
- if (result && result.length > 0) {
- let name = result[0].name;
- let topic = result && result.length > 0 ? result[0] : "";
- if (topic) {
- sendWxMessage(openid, name, topic);
- }
- } else {
- ModelUtil.logError("Can not find user info", err);
- }
- });
- } else {
- ModelUtil.logError("User haven't bound with wechat, user id: " + message.to, err);
- }
- });
- };
- }
- module.exports = WechatClient;
|