|  | @ -3,14 +3,14 @@
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 此控制器处理点对点,组及消息消息。为三类消息提供发送及查询功能。
 |  |  * 此控制器处理点对点,组及消息消息。为三类消息提供发送及查询功能。
 | 
												
													
														
															|  |  */
 |  |  */
 | 
												
													
														
															|  | 
 |  | var express = require('express');
 | 
												
													
														
															|  | 
 |  | var router = express.Router();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | var http = require('http');
 |  | var http = require('http');
 | 
												
													
														
															|  | var getui = require('getui');
 |  | var getui = require('getui');
 | 
												
													
														
															|  | var log = require('../util/log.js');
 |  | var log = require('../util/log.js');
 | 
												
													
														
															|  | var objectUtil = require('../util/objectUtil');
 |  | var objectUtil = require('../util/objectUtil');
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | var express = require('express');
 |  | 
 | 
												
													
														
															|  | var router = express.Router();
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | var systemMsg = require("../models/msg.system");
 |  | var systemMsg = require("../models/msg.system");
 | 
												
													
														
															|  | var privateMsg = require('../models/msg.private');
 |  | var privateMsg = require('../models/msg.private');
 | 
												
													
														
															|  | var groupMsg = require("../models/msg.group");
 |  | var groupMsg = require("../models/msg.group");
 | 
												
											
												
													
														
															|  | @ -18,13 +18,18 @@ var notifyMsg = require("../models/msg.notify");
 | 
												
													
														
															|  | var msgStats = require("../models/msg.stat");
 |  | var msgStats = require("../models/msg.stat");
 | 
												
													
														
															|  | var user = require("../models/user");
 |  | var user = require("../models/user");
 | 
												
													
														
															|  | var group = require("../models/group");
 |  | var group = require("../models/group");
 | 
												
													
														
															|  | 
 |  | var search = require('../models/search');
 | 
												
													
														
															|  | 
 |  | var dbUtils = require("../util/objectUtil.js");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | var APIv1 = require('../include/endpoints').APIv1;
 |  | var APIv1 = require('../include/endpoints').APIv1;
 | 
												
													
														
															|  | var CHANNELS = require('../include/commons').CHANNELS;
 |  | 
 | 
												
													
														
															|  | 
 |  | var CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
 | 
												
													
														
															|  | 
 |  | var GROUP_TYPE = require('../include/commons').GROUP_TYPE;
 | 
												
													
														
															|  | var PLATFORMS = require('../include/commons').PLATFORM;
 |  | var PLATFORMS = require('../include/commons').PLATFORM;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | var MAX_INT = 9007199254740992;
 |  | var MAX_INT = 9007199254740992;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | var DEFAULT_PAGE_SIZE = 100;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | //--------------------------------------------------------------//
 | 
												
													
														
															|  | //----------------------------消息发送----------------------------//
 |  | //----------------------------消息发送----------------------------//
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | //--------------------------------------------------------------//
 | 
												
											
												
													
														
															|  | @ -32,6 +37,9 @@ var MAX_INT = 9007199254740992;
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 发送System消息。
 |  |  * 发送System消息。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/sm
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  |  * 消息格式:
 |  |  * 消息格式:
 | 
												
													
														
															|  |  *  {
 |  |  *  {
 | 
												
													
														
															|  |  *      to: "Rose",
 |  |  *      to: "Rose",
 | 
												
											
												
													
														
															|  | @ -138,11 +146,14 @@ router.post(APIv1.Chats.SM, function (req, res) {
 | 
												
													
														
															|  |  * 2 更新消息统计数据
 |  |  * 2 更新消息统计数据
 | 
												
													
														
															|  |  * 3 获取目标的状态并构建通知消息,如果用户在线就推送通知消息
 |  |  * 3 获取目标的状态并构建通知消息,如果用户在线就推送通知消息
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/pm
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  |  * 消息格式:
 |  |  * 消息格式:
 | 
												
													
														
															|  |  *  {
 |  |  *  {
 | 
												
													
														
															|  |  *      from: sand,
 |  |  *      from: sand,
 | 
												
													
														
															|  |  *      to: Rose,
 |  |  *      to: Rose,
 | 
												
													
														
															|  |  *      contentType: "1",
 |  | 
 | 
												
													
														
															|  | 
 |  |  *      contentType: "1,2,3,4",
 | 
												
													
														
															|  |  *      content: "Please follow the patient as soon as possible."
 |  |  *      content: "Please follow the patient as soon as possible."
 | 
												
													
														
															|  |  *  }
 |  |  *  }
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
											
												
													
														
															|  | @ -176,22 +187,30 @@ router.post(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         // 保存消息
 |  |         // 保存消息
 | 
												
													
														
															|  |         privateMsg.save(message.to, message.from, message.contentType, message.content, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |         var tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
 | 
												
													
														
															|  | 
 |  |         privateMsg.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
 | 
												
													
														
															|  |             if (err) {
 |  |             if (err) {
 | 
												
													
														
															|  |                 res.status(500).send({message: 'Save private message failed.'});
 |  |                 res.status(500).send({message: 'Save private message failed.'});
 | 
												
													
														
															|  |                 return;
 |  |                 return;
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             // 结束网络连接,后续操作继续执行
 |  |             // 结束网络连接,后续操作继续执行
 | 
												
													
														
															|  |             res.status(200).send();
 |  | 
 | 
												
													
														
															|  | 
 |  |             privateMsg.findOneMessage(result.insertId, function (err, msg) {
 | 
												
													
														
															|  | 
 |  |                 if (err) {
 | 
												
													
														
															|  | 
 |  |                     res.status(500).send("Save private message success, but return last message failed.");
 | 
												
													
														
															|  | 
 |  |                 } else {
 | 
												
													
														
															|  | 
 |  |                     var message = fillMessages(msg);
 | 
												
													
														
															|  | 
 |  |                     res.status(200).send(message);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             // 更新自身的聊天统计信息
 |  |             // 更新自身的聊天统计信息
 | 
												
													
														
															|  |             msgStats.updateP2PChatInfo(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |             msgStats.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
 | 
												
													
														
															|  |                 if (err) log.error(err);
 |  |                 if (err) log.error(err);
 | 
												
													
														
															|  |             });
 |  |             });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             // 更新对端的聊天统计信息
 |  |             // 更新对端的聊天统计信息
 | 
												
													
														
															|  |             msgStats.updateP2PChatInfo(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |             msgStats.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
 | 
												
													
														
															|  |                 if (err) log.error(err);
 |  |                 if (err) log.error(err);
 | 
												
													
														
															|  |             });
 |  |             });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -205,11 +224,11 @@ router.post(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  |                 // 构建通知消息
 |  |                 // 构建通知消息
 | 
												
													
														
															|  |                 var title = '新消息';
 |  |                 var title = '新消息';
 | 
												
													
														
															|  |                 var content = '';
 |  |                 var content = '';
 | 
												
													
														
															|  |                 if (message.contentType == 1) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                 if (message.contentType === CONTENT_TYPES.PlainText) {
 | 
												
													
														
															|  |                     content = message.content;
 |  |                     content = message.content;
 | 
												
													
														
															|  |                 } else if (message.contentType == 2) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                 } else if (message.contentType === CONTENT_TYPES.Image) {
 | 
												
													
														
															|  |                     content = '[图片]';
 |  |                     content = '[图片]';
 | 
												
													
														
															|  |                 } else if (message.contentType == 3) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                 } else if (message.contentType === CONTENT_TYPES.Audio) {
 | 
												
													
														
															|  |                     content = '[语音]';
 |  |                     content = '[语音]';
 | 
												
													
														
															|  |                 } else {
 |  |                 } else {
 | 
												
													
														
															|  |                     content = '接收到一条新消息';
 |  |                     content = '接收到一条新消息';
 | 
												
											
												
													
														
															|  | @ -247,7 +266,7 @@ router.post(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  |                                         }
 |  |                                         }
 | 
												
													
														
															|  |                                     });
 |  |                                     });
 | 
												
													
														
															|  |                             } else if (target.platform === PLATFORMS.Android) {
 |  |                             } else if (target.platform === PLATFORMS.Android) {
 | 
												
													
														
															|  |                                 getui.pushAndroid(data.client_id,
 |  | 
 | 
												
													
														
															|  | 
 |  |                                 getui.pushAndroid(target.client_id,
 | 
												
													
														
															|  |                                     message.contentType,
 |  |                                     message.contentType,
 | 
												
													
														
															|  |                                     title,
 |  |                                     title,
 | 
												
													
														
															|  |                                     content,
 |  |                                     content,
 | 
												
											
												
													
														
															|  | @ -272,6 +291,9 @@ router.post(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 处理讨论组消息。
 |  |  * 处理讨论组消息。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/gm
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  |  * 消息格式:
 |  |  * 消息格式:
 | 
												
													
														
															|  |  *  {
 |  |  *  {
 | 
												
													
														
															|  |  *      from: "sand",                       // 发送者id
 |  |  *      from: "sand",                       // 发送者id
 | 
												
											
												
													
														
															|  | @ -320,11 +342,18 @@ router.post(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  |                 return;
 |  |                 return;
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             // 关闭网络连接后执行后续操作
 |  | 
 | 
												
													
														
															|  |             res.status(200).send();
 |  | 
 | 
												
													
														
															|  | 
 |  |             groupMsg.findOneMessage(result.insertId, function (err, groupMsg) {
 | 
												
													
														
															|  | 
 |  |                 if (err) {
 | 
												
													
														
															|  | 
 |  |                     log.error("Save group message success, but return this message failed.");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     res.status(500).send({message: "Save group message success, but return this message failed."});
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |                 // 关闭网络连接后执行后续操作
 | 
												
													
														
															|  | 
 |  |                 res.status(200).send(fillMessages(groupMsg));
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             // 更新组内统计信息
 |  |             // 更新组内统计信息
 | 
												
													
														
															|  |             msgStats.updateGroupChatInfo(message.from, message.group, message.from, 0, message.contentType, message.content, false, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |             msgStats.updateGroupChatSummary(message.from, message.group, message.from, 0, message.contentType, message.content, false, function (err, result) {
 | 
												
													
														
															|  |                 if (err) log.error(err);
 |  |                 if (err) log.error(err);
 | 
												
													
														
															|  |             });
 |  |             });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -343,8 +372,8 @@ router.post(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  |                 }
 |  |                 }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |                 // 逐个推送通知
 |  |                 // 逐个推送通知
 | 
												
													
														
															|  |                 for (var nIndex = 0; nIndex < result.length; nIndex++) {
 |  | 
 | 
												
													
														
															|  |                     var member = result[nIndex];
 |  | 
 | 
												
													
														
															|  | 
 |  |                 for (var i = 0; i < result.length; i++) {
 | 
												
													
														
															|  | 
 |  |                     var member = result[i];
 | 
												
													
														
															|  |                     if (member.user_id === message.from) continue;
 |  |                     if (member.user_id === message.from) continue;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |                     user.getUserStatus(member.user_id, function (err, result) {
 |  |                     user.getUserStatus(member.user_id, function (err, result) {
 | 
												
											
												
													
														
															|  | @ -355,13 +384,13 @@ router.post(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |                         var title = '';
 |  |                         var title = '';
 | 
												
													
														
															|  |                         var content = '';
 |  |                         var content = '';
 | 
												
													
														
															|  |                         if (message.contentType === 1) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                         if (message.contentType === CONTENT_TYPES.PlainText) {
 | 
												
													
														
															|  |                             title = '群组消息';
 |  |                             title = '群组消息';
 | 
												
													
														
															|  |                             content = message.content;
 |  |                             content = message.content;
 | 
												
													
														
															|  |                         } else if (message.contentType === 2) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                         } else if (message.contentType === CONTENT_TYPES.Image) {
 | 
												
													
														
															|  |                             title = '群组消息';
 |  |                             title = '群组消息';
 | 
												
													
														
															|  |                             content = '[图片]';
 |  |                             content = '[图片]';
 | 
												
													
														
															|  |                         } else if (message.contentType === 3) {
 |  | 
 | 
												
													
														
															|  | 
 |  |                         } else if (message.contentType === CONTENT_TYPES.Audio) {
 | 
												
													
														
															|  |                             title = '群组消息';
 |  |                             title = '群组消息';
 | 
												
													
														
															|  |                             content = '[语音]';
 |  |                             content = '[语音]';
 | 
												
													
														
															|  |                         } else {
 |  |                         } else {
 | 
												
											
												
													
														
															|  | @ -449,7 +478,7 @@ router.post(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  |                     var at = 0;
 |  |                     var at = 0;
 | 
												
													
														
															|  |                     if (message.at == member.user_id) at = 1;
 |  |                     if (message.at == member.user_id) at = 1;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |                     msgStats.updateGroupChatInfo(member.user_id,
 |  | 
 | 
												
													
														
															|  | 
 |  |                     msgStats.updateGroupChatSummary(member.user_id,
 | 
												
													
														
															|  |                         message.group,
 |  |                         message.group,
 | 
												
													
														
															|  |                         message.from,
 |  |                         message.from,
 | 
												
													
														
															|  |                         at,
 |  |                         at,
 | 
												
											
												
													
														
															|  | @ -466,54 +495,338 @@ router.post(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | //--------------------------------------------------------------//
 | 
												
													
														
															|  | //----------------------------消息获取----------------------------//
 |  | 
 | 
												
													
														
															|  | 
 |  | //----------------------------消息提取----------------------------//
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | //--------------------------------------------------------------//
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 按时间倒序获取私信。
 |  | 
 | 
												
													
														
															|  | 
 |  |  * 获取参与的聊天列表,包括:点对点,@我,参与的讨论组,系统消息等。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/list?user_id=sand
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.List, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     if (userId === null) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 406, message: 'Missing fields.'};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getChatList(userId, function (err, rows) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error('Get user chat list failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get user chat list failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (rows.length == 0) {
 | 
												
													
														
															|  | 
 |  |             res.status(200).send([]);
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var chats = new Array(rows.length);
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < rows.length; i++) {
 | 
												
													
														
															|  | 
 |  |             var row = rows[i];
 | 
												
													
														
															|  | 
 |  |             chats[i] = {
 | 
												
													
														
															|  | 
 |  |                 userId: row.uid,
 | 
												
													
														
															|  | 
 |  |                 peerId: row.from_uid,
 | 
												
													
														
															|  | 
 |  |                 groupId: row.from_gid === undefined ? "" : row.from_gid,
 | 
												
													
														
															|  | 
 |  |                 atMe: row.at_me === 1,
 | 
												
													
														
															|  | 
 |  |                 messageType: row.msg_type,
 | 
												
													
														
															|  | 
 |  |                 lastContentType: row.last_content_type,
 | 
												
													
														
															|  | 
 |  |                 lastContent: row.last_content,
 | 
												
													
														
															|  | 
 |  |                 newMessageCount: row.new_msg_count,
 | 
												
													
														
															|  | 
 |  |                 timestamp: objectUtil.timestampToLong(row.timestamp)
 | 
												
													
														
															|  | 
 |  |             };
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(200).send(chats);
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 获取与患者的聊天列表,包括:P2P,参与的讨论组和行政团队。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/list/patient
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.ListWithPatient, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     if (userId === null) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 406, message: 'Missing fields.'};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     privateMsg.findAllP2PWithPatient(userId, function (err, patients) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error('Get chat list with patient failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get chat list with patient failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var chats = {patients: [], groups: []};
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < patients.length; i++) {
 | 
												
													
														
															|  | 
 |  |             var patient = patients[i];
 | 
												
													
														
															|  | 
 |  |             chats.patients.push({
 | 
												
													
														
															|  | 
 |  |                 code: patient.code,
 | 
												
													
														
															|  | 
 |  |                 name: patient.name,
 | 
												
													
														
															|  | 
 |  |                 birthday: patient.birthday,
 | 
												
													
														
															|  | 
 |  |                 sex: patient.sex,
 | 
												
													
														
															|  | 
 |  |                 avatar: patient.photo == null ? "" : patient.photo,
 | 
												
													
														
															|  | 
 |  |                 newMessageCount: patient.new_msg_count,
 | 
												
													
														
															|  | 
 |  |                 lastContentType: patient.last_content_type,
 | 
												
													
														
															|  | 
 |  |                 lastContent: patient.last_content,
 | 
												
													
														
															|  | 
 |  |                 timestamp: objectUtil.timestampToLong(patient.timestamp)
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         groupMsg.findAllGroupsWithPatient(userId, function (err, groups) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error('Get group list with patient failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: 'Get group list with patient failed.'});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             for (var i = 0; i < groups.length; i++) {
 | 
												
													
														
															|  | 
 |  |                 var group = groups[i];
 | 
												
													
														
															|  | 
 |  |                 chats.groups.push({
 | 
												
													
														
															|  | 
 |  |                     code: group.code,
 | 
												
													
														
															|  | 
 |  |                     name: group.name,
 | 
												
													
														
															|  | 
 |  |                     groupType: group.msg_type,
 | 
												
													
														
															|  | 
 |  |                     newMessageCount: group.new_msg_count,
 | 
												
													
														
															|  | 
 |  |                     lastContentType: group.last_content_type,
 | 
												
													
														
															|  | 
 |  |                     lastContent: group.last_content,
 | 
												
													
														
															|  | 
 |  |                     timestamp: objectUtil.timestampToLong(group.timestamp)
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(chats);
 | 
												
													
														
															|  | 
 |  |         })
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 获取与医生的聊天列表,包括:点对点,参与的讨论组。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/list/doctor
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.ListWithDoctor, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     if (userId === null) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 406, message: 'Missing fields.'};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     // 先获取医生间的私聊
 | 
												
													
														
															|  | 
 |  |     privateMsg.findAllP2PWithDoctor(userId, function (err, doctors) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error('Get chat list with doctor failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get chat list with doctor failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var chats = {doctors: [], groups: []};
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < doctors.length; i++) {
 | 
												
													
														
															|  | 
 |  |             var doctor = doctors[i];
 | 
												
													
														
															|  | 
 |  |             chats.doctors.push({
 | 
												
													
														
															|  | 
 |  |                 code: doctor.code,
 | 
												
													
														
															|  | 
 |  |                 name: doctor.name,
 | 
												
													
														
															|  | 
 |  |                 sex: doctor.sex,
 | 
												
													
														
															|  | 
 |  |                 avatar: doctor.photo === null ? "" : doctor.photo,
 | 
												
													
														
															|  | 
 |  |                 newMessageCount: doctor.new_msg_count,
 | 
												
													
														
															|  | 
 |  |                 lastContentType: doctor.last_content_type,
 | 
												
													
														
															|  | 
 |  |                 lastContent: doctor.last_content,
 | 
												
													
														
															|  | 
 |  |                 timestamp: objectUtil.timestampToLong(doctor.timestamp)
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         // 再获取医生间的组
 | 
												
													
														
															|  | 
 |  |         groupMsg.findAllGroupsWithDoctor(userId, function (err, groups) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error('Get group list with doctor failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: 'Get group list with doctor failed.'});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             for (var i = 0; i < groups.length; i++) {
 | 
												
													
														
															|  | 
 |  |                 var group = groups[i];
 | 
												
													
														
															|  | 
 |  |                 chats.groups.push({
 | 
												
													
														
															|  | 
 |  |                     code: group.code,
 | 
												
													
														
															|  | 
 |  |                     name: group.name,
 | 
												
													
														
															|  | 
 |  |                     groupType: group.type,
 | 
												
													
														
															|  | 
 |  |                     newMessageCount: group.new_msg_count,
 | 
												
													
														
															|  | 
 |  |                     lastContentType: group.last_content_type,
 | 
												
													
														
															|  | 
 |  |                     lastContent: group.last_content,
 | 
												
													
														
															|  | 
 |  |                     timestamp: objectUtil.timestampToLong(group.timestamp)
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(chats);
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 获取最近聊天对象:包括患者,医生与讨论组。客户端自行根据需要提取患者、落花生或讨论组数据。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/recent?user_id=0de7295862dd11e69faffa163e8aee56&days=7
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 参数:
 | 
												
													
														
															|  | 
 |  |  *  user_id: 用户ID
 | 
												
													
														
															|  | 
 |  |  *  target_type: 对象类型,1患者,2医生,3讨论组
 | 
												
													
														
															|  | 
 |  |  *  days: 最近天数
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.Recent, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     var days = req.query.days;
 | 
												
													
														
															|  | 
 |  |     if (userId === null) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 406, message: 'Missing field: user_id'};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (days === null) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 406, message: 'Missing field: days'};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getRecentChats(userId, days, function (err, result) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error('Get recent chat objects failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get recent chat objects failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var data = {patients: [], doctors: [], groups: []};
 | 
												
													
														
															|  | 
 |  |         if (result.length === 0) {
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(data);
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < result.length; ++i) {
 | 
												
													
														
															|  | 
 |  |             var row = result[i];
 | 
												
													
														
															|  | 
 |  |             if (row.type.indexOf('patient') > -1) {
 | 
												
													
														
															|  | 
 |  |                 data.patients.push({
 | 
												
													
														
															|  | 
 |  |                     code: row.code,
 | 
												
													
														
															|  | 
 |  |                     name: row.name,
 | 
												
													
														
															|  | 
 |  |                     birthday: row.birthday === null ? "" : row.birthday,
 | 
												
													
														
															|  | 
 |  |                     sex: row.sex,
 | 
												
													
														
															|  | 
 |  |                     avatar: row.photo === null ? "" : row.photo
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             } else if (row.type.indexOf('doctor') > -1) {
 | 
												
													
														
															|  | 
 |  |                 data.doctors.push({
 | 
												
													
														
															|  | 
 |  |                     code: row.code,
 | 
												
													
														
															|  | 
 |  |                     name: row.name,
 | 
												
													
														
															|  | 
 |  |                     birthday: row.birthday === null ? "" : row.birthday,
 | 
												
													
														
															|  | 
 |  |                     sex: row.sex,
 | 
												
													
														
															|  | 
 |  |                     avatar: row.photo === null ? "" : row.photo
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             } else if (row.type.indexOf('group') > -1) {
 | 
												
													
														
															|  | 
 |  |                 data.groups.push({
 | 
												
													
														
															|  | 
 |  |                     code: row.code,
 | 
												
													
														
															|  | 
 |  |                     name: row.name
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(200).send(data);
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 获取私信。倒序排列。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  user_id 必须,医生ID
 |  |  *  user_id 必须,医生ID
 | 
												
													
														
															|  |  *  peer_id 必须,对方医生ID
 |  |  *  peer_id 必须,对方医生ID
 | 
												
													
														
															|  | 
 |  |  *  content_type 必须,消息类型
 | 
												
													
														
															|  |  *  message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
 |  |  *  message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
 | 
												
													
														
															|  |  *  count 可选,消息数量,如果不指定、小于零或大于50,默认为50条
 |  | 
 | 
												
													
														
															|  | 
 |  |  *  message_end_id 可选,消息的结束ID,如果为空从第一条开始获取
 | 
												
													
														
															|  | 
 |  |  *  count 可选,消息数量,如果不指定、小于零或大于50,默认为100条。若message_start_id与message_end_id均不为空,则此参数无效,方法是设置为10000条。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 请求URL:
 |  |  * 请求URL:
 | 
												
													
														
															|  |  *  /pm?user_id=sand&peer_id=Rose&message_start_id=0&count=20
 |  | 
 | 
												
													
														
															|  | 
 |  |  *  /chats/pm?user_id=sand&peer_id=Rose&content_type=2&message_start_id=10000&message_end_id=0&count=20
 | 
												
													
														
															|  |  */
 |  |  */
 | 
												
													
														
															|  | router.get(APIv1.Chats.PM, function (req, res) {
 |  | router.get(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  |     var userId = req.query.user_id;
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  |     var peerId = req.query.peer_id;
 |  |     var peerId = req.query.peer_id;
 | 
												
													
														
															|  |     var msgStartId = req.query.message_start_id === null ? MAX_INT : parseInt(req.query.message_start_id);
 |  | 
 | 
												
													
														
															|  |     var count = req.query.count == null ? 50 : parsetInt(req.query.count);
 |  | 
 | 
												
													
														
															|  | 
 |  |     var contentType = req.query.content_type;
 | 
												
													
														
															|  | 
 |  |     var msgStartId = !req.query.message_start_id ? MAX_INT : parseInt(req.query.message_start_id);
 | 
												
													
														
															|  | 
 |  |     var msgEndId = !req.query.message_end_id ? 0 : parseInt(req.query.message_end_id);
 | 
												
													
														
															|  | 
 |  |     var count = req.query.count === undefined ? DEFAULT_PAGE_SIZE : parseInt(req.query.count);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     if (userId == null) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (contentType !== undefined && parseInt(contentType) === CONTENT_TYPES.Image) count = DEFAULT_PAGE_SIZE;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (req.query.message_start_id && req.query.message_end_id) count = 10000;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (userId === undefined) {
 | 
												
													
														
															|  |         throw {httpStatus: 400, message: "Missing field."};
 |  |         throw {httpStatus: 400, message: "Missing field."};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     privateMsg.findByGroupId(userId, peerId, msgStartId, count, function (err, rows) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     privateMsg.findAllMessages(userId, peerId, contentType === undefined ? "1,2,3,5,6" : contentType, msgStartId, msgEndId, count, function (err, rows) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             log.error("Get private message failed, ", err);
 |  |             log.error("Get private message failed, ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             throw {httpStatus: 500, message: "Get private messages failed."};
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         var messages = {};
 |  | 
 | 
												
													
														
															|  |         messages.startId = msgStartId;
 |  | 
 | 
												
													
														
															|  |         messages.count = rows.length;
 |  | 
 | 
												
													
														
															|  |         messages.records = [];
 |  | 
 | 
												
													
														
															|  |         for (var nIndex = 0; nIndex < rows.length; nIndex++) {
 |  | 
 | 
												
													
														
															|  |             rows[nIndex].timestamp = Date.parse(new Date(rows[nIndex].timestamp));
 |  | 
 | 
												
													
														
															|  |             messages.records.notifyMsg(rows[nIndex]);
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Get private messages failed."});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         res.writeHead(200);
 |  | 
 | 
												
													
														
															|  |         res.write(messages);
 |  | 
 | 
												
													
														
															|  | 
 |  |         var messages = fillMessages(rows);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         // 清空统计信息
 |  |         // 清空统计信息
 | 
												
													
														
															|  |         msgStats.clearP2PChatInfo(userId, peerId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |         msgStats.clearPrivateChatSummary(userId, peerId, function (err, result) {
 | 
												
													
														
															|  |             if (err) console.log(err);
 |  |             if (err) console.log(err);
 | 
												
													
														
															|  |         });
 |  |         });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(200).send(messages);
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 获取未读私信。倒序排列。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 参数:
 | 
												
													
														
															|  | 
 |  |  *  user_id 必须,医生ID
 | 
												
													
														
															|  | 
 |  |  *  peer_id 必须,对方医生ID
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/pm/unread?user_id=sand&peer_id=Rose
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.PMUnread, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     var peerId = req.query.peer_id;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (userId === undefined) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 400, message: "Missing field: user_id."};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getPrivateChatSummary(userId, peerId, function (err, summary) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error("Get unread private messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Get unread private messages failed."});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         // 没有未读消息,直接返回
 | 
												
													
														
															|  | 
 |  |         if (summary.length == 0 || summary[0].new_msg_count === 0) {
 | 
												
													
														
															|  | 
 |  |             res.status(200).send({startId: 0, count: 0, records: []});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         privateMsg.findUnread(peerId, userId, MAX_INT, summary[0].new_msg_count, function (err, rows) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error("Get unread private messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: "Get unread private messages failed."});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             var messages = fillMessages(rows);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(messages);
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -521,108 +834,134 @@ router.get(APIv1.Chats.PM, function (req, res) {
 | 
												
													
														
															|  |  * 按时间倒序获取群消息。
 |  |  * 按时间倒序获取群消息。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  group_id 必须,组ID
 |  | 
 | 
												
													
														
															|  |  *  user_id 必须,医生ID
 |  |  *  user_id 必须,医生ID
 | 
												
													
														
															|  | 
 |  |  *  group_id 必须,组ID
 | 
												
													
														
															|  |  *  message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
 |  |  *  message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
 | 
												
													
														
															|  |  *  count 可选,消息数量,如果不指定、小于零或大于50,默认为50条
 |  |  *  count 可选,消息数量,如果不指定、小于零或大于50,默认为50条
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 请求URL:
 |  |  * 请求URL:
 | 
												
													
														
															|  |  *  /gm?user_id=sand&group_id=discussionGroup&message_start_id=0&count=20
 |  | 
 | 
												
													
														
															|  | 
 |  |  *  /chats/gm?user_id=D2016008240002&group_id=494&content_type=2&message_start_id=0&message_end_id=0&count=20
 | 
												
													
														
															|  |  */
 |  |  */
 | 
												
													
														
															|  | router.get(APIv1.Chats.GM, function (req, res) {
 |  | router.get(APIv1.Chats.GM, function (req, res) {
 | 
												
													
														
															|  |     var groupId = req.query.group_id;
 |  |     var groupId = req.query.group_id;
 | 
												
													
														
															|  |     var userId = req.query.user_id;
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  |     var msgStartId = req.query.message_start_id === null ? MAX_INT : parseInt(req.query.message_start_id);
 |  | 
 | 
												
													
														
															|  |     var count = req.query.count == null ? 50 : parsetInt(req.query.count);
 |  | 
 | 
												
													
														
															|  | 
 |  |     var contentType = req.query.content_type;
 | 
												
													
														
															|  | 
 |  |     var msgStartId = !req.query.message_start_id ? MAX_INT : parseInt(req.query.message_start_id);
 | 
												
													
														
															|  | 
 |  |     var msgEndId = !req.query.message_end_id ? 0 : parseInt(req.query.message_end_id);
 | 
												
													
														
															|  | 
 |  |     var count = req.query.count === undefined ? DEFAULT_PAGE_SIZE : parseInt(req.query.count);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     if (groupId == null) {
 |  | 
 | 
												
													
														
															|  |         throw {httpStatus: 400, message: "Missing field."};
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (groupId === undefined) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 400, message: "Missing field: group_id."};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     groupMsg.findByGroupId(groupId, msgStartId, count, function (err, rows) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (contentType !== undefined && parseInt(contentType) === CONTENT_TYPES.Image) count = DEFAULT_PAGE_SIZE;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (req.query.message_start_id && req.query.message_end_id) count = 100000;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     groupMsg.findAllMessages(groupId, !contentType ? "1,2,3,5,6" : contentType, msgStartId, msgEndId, count, function (err, rows) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.log('Get group message failed: ', err);
 |  |             console.log('Get group message failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             throw {httpStatus: 500, message: 'Get group message failed.'};
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         var messages = {};
 |  | 
 | 
												
													
														
															|  |         messages.startId = msgStartId;
 |  | 
 | 
												
													
														
															|  |         messages.count = rows.length;
 |  | 
 | 
												
													
														
															|  |         messages.records = [];
 |  | 
 | 
												
													
														
															|  |         for (var nIndex = 0; nIndex < rows.length; nIndex++) {
 |  | 
 | 
												
													
														
															|  |             rows[nIndex].timestamp = Date.parse(new Date(rows[nIndex].timestamp));
 |  | 
 | 
												
													
														
															|  |             messages.records.push(rows[nIndex]);
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get group message failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         res.writeHead(200);
 |  | 
 | 
												
													
														
															|  |         res.write(messages);
 |  | 
 | 
												
													
														
															|  | 
 |  |         var messages = fillMessages(rows);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         // 清空统计信息
 |  |         // 清空统计信息
 | 
												
													
														
															|  |         msgStats.clearGroupChatInfo(req.query.uid,
 |  | 
 | 
												
													
														
															|  |             userId,
 |  | 
 | 
												
													
														
															|  |             function (err, result) {
 |  | 
 | 
												
													
														
															|  |                 if (err) console.log(err);
 |  | 
 | 
												
													
														
															|  |             });
 |  | 
 | 
												
													
														
															|  | 
 |  |         msgStats.clearGroupChatSummary(req.query.uid, userId, function (err, result) {
 | 
												
													
														
															|  | 
 |  |             if (err) console.log(err);
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(200).send(messages);
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | 
 | 
												
													
														
															|  | //----------------------------消息统计----------------------------//
 |  | 
 | 
												
													
														
															|  | //--------------------------------------------------------------//
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 获取参与的聊天列表,包括:点对点,@我,参与的讨论组。
 |  | 
 | 
												
													
														
															|  | 
 |  |  * 获取未读群消息。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 请求URL:
 |  | 
 | 
												
													
														
															|  |  *  /api/v1/chats?user_id=sand
 |  | 
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /chats/gm/unread?group_id=discussionGroupId&user_id=sand
 | 
												
													
														
															|  |  */
 |  |  */
 | 
												
													
														
															|  | router.get(APIv1.Chats.List, function (req, res) {
 |  | 
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.GMUnread, function (req, res) {
 | 
												
													
														
															|  |     var userId = req.query.user_id;
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  |     if (userId === null) {
 |  | 
 | 
												
													
														
															|  |         throw {httpStatus: 406, message: 'Missing fields.'};
 |  | 
 | 
												
													
														
															|  | 
 |  |     var groupId = req.query.group_id;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (userId === undefined) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 400, message: "Missing field: user_id."};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getChatList(userId, function (err, rows) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (groupId === undefined) {
 | 
												
													
														
															|  | 
 |  |         throw {httpStatus: 400, message: "Missing field: group_id."};
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getGroupChatSummary(userId, groupId, function (err, summary) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             log.error('Get users chat list failed: ', err);
 |  | 
 | 
												
													
														
															|  | 
 |  |             log.error("Get unread group messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             res.status(500).send({message: 'Get users chat list failed.'});
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Get unread group messages failed."});
 | 
												
													
														
															|  |             return;
 |  |             return;
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         if (rows.length == 0) {
 |  | 
 | 
												
													
														
															|  |             res.status(200).send([]);
 |  | 
 | 
												
													
														
															|  | 
 |  |         var messages = {startId: 0, count: 0, records: []};
 | 
												
													
														
															|  | 
 |  |         if (summary.length == 0 || summary[0].new_msg_count === 0) {
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(messages);
 | 
												
													
														
															|  |             return;
 |  |             return;
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         var chats = new Array(rows.length);
 |  | 
 | 
												
													
														
															|  |         for (var i = 0; i < rows.length; i++) {
 |  | 
 | 
												
													
														
															|  |             var row = rows[i];
 |  | 
 | 
												
													
														
															|  |             var element = {
 |  | 
 | 
												
													
														
															|  |                 userId: row.uid,
 |  | 
 | 
												
													
														
															|  |                 peerId: row.from_uid,
 |  | 
 | 
												
													
														
															|  |                 groupId: row.from_gid === undefined ? "" : row.from_gid,
 |  | 
 | 
												
													
														
															|  |                 atMe: row.at_me === 1,
 |  | 
 | 
												
													
														
															|  |                 messageType: row.msg_type,
 |  | 
 | 
												
													
														
															|  |                 lastContentType: row.last_content_type,
 |  | 
 | 
												
													
														
															|  |                 lastContent: row.last_content,
 |  | 
 | 
												
													
														
															|  |                 newMessageCount: row.new_msg_count,
 |  | 
 | 
												
													
														
															|  |                 timestamp: Date.parse(new Date(row.timestamp))
 |  | 
 | 
												
													
														
															|  |             };
 |  | 
 | 
												
													
														
															|  | 
 |  |         messages.count = summary[0].new_msg_count;
 | 
												
													
														
															|  | 
 |  |         groupMsg.findUnread(groupId, MAX_INT, messages.count, function (err, rows) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error("Get unread group messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             chats[i] = element;
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: "Get unread group messages failed."});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         res.status(200).send(chats);
 |  | 
 | 
												
													
														
															|  | 
 |  |             var messages = fillMessages(rows);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(messages);
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 将消息的返回结果合并成JSON。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * @param rows
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * @returns {startId: 0, count: 0, records: []}
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | function fillMessages(rows) {
 | 
												
													
														
															|  | 
 |  |     var messages = {startId: rows.length > 0 ? rows[0].msg_id : '', count: rows.length, records: []};
 | 
												
													
														
															|  | 
 |  |     for (var i = 0; i < rows.length; i++) {
 | 
												
													
														
															|  | 
 |  |         var row = rows[i];
 | 
												
													
														
															|  | 
 |  |         var record = {
 | 
												
													
														
															|  | 
 |  |             id: row.msg_id,
 | 
												
													
														
															|  | 
 |  |             from: row.from_uid,
 | 
												
													
														
															|  | 
 |  |             contentType: row.type,
 | 
												
													
														
															|  | 
 |  |             content: row.content,
 | 
												
													
														
															|  | 
 |  |             timestamp: objectUtil.timestampToLong(row.timestamp)
 | 
												
													
														
															|  | 
 |  |         };
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (row.to_uid !== undefined) record.to = row.to_uid;
 | 
												
													
														
															|  | 
 |  |         if (row.at_uid !== undefined) record.at = row.at_uid;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         messages.records.push(record);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     return messages;
 | 
												
													
														
															|  | 
 |  | }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | //--------------------------------------------------------------//
 | 
												
													
														
															|  | 
 |  | //----------------------------消息统计----------------------------//
 | 
												
													
														
															|  | 
 |  | //--------------------------------------------------------------//
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 获取所有群组未读消息总数。
 |  |  * 获取所有群组未读消息总数。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 请求URL:
 |  |  * 请求URL:
 | 
												
													
														
															|  |  *  /gm/unread_count?user_id=sand
 |  | 
 | 
												
													
														
															|  | 
 |  |  *  /chats/gm/unread_count?user_id=sand
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  user_id:医生ID
 |  |  *  user_id:医生ID
 | 
												
											
												
													
														
															|  | @ -633,7 +972,7 @@ router.get(APIv1.Chats.GMUnreadCount, function (req, res) {
 | 
												
													
														
															|  |         throw {httpStatus: 406, message: 'Missing fields.'};
 |  |         throw {httpStatus: 406, message: 'Missing fields.'};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getGroupChatAllUnRead(userId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getGroupChatAllUnReadCount(userId, function (err, result) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.log('Get all unread messages failed: ', err);
 |  |             console.log('Get all unread messages failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -659,7 +998,7 @@ router.get(APIv1.Chats.GMUnreadCount, function (req, res) {
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 获取特定群组消息统计情况。
 |  |  * 获取特定群组消息统计情况。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * /gm/statistic?group_id=GGG&&user_id=sand
 |  | 
 | 
												
													
														
															|  | 
 |  |  * /chats/gm/statistic?group_id=GGG&&user_id=sand
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  user_id:信息所有者id
 |  |  *  user_id:信息所有者id
 | 
												
											
												
													
														
															|  | @ -672,38 +1011,44 @@ router.get(APIv1.Chats.GMStats, function (req, res) {
 | 
												
													
														
															|  |         throw {httpStatus: 406, message: 'Miss fields.'};
 |  |         throw {httpStatus: 406, message: 'Miss fields.'};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getGroupChatInfo(userId, groupId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getGroupChatSummary(userId, groupId, function (err, result) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.log('Get group stats failed: ', err);
 |  |             console.log('Get group stats failed: ', err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             throw {httpStatus: 500, message: 'Get group stats failed.'};
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: 'Get group stats failed.'});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         if (result.length == 0) {
 |  | 
 | 
												
													
														
															|  |             var data = {
 |  | 
 | 
												
													
														
															|  |                 "userId": userId,
 |  | 
 | 
												
													
														
															|  |                 "from": "",
 |  | 
 | 
												
													
														
															|  |                 "groupId": groupId,
 |  | 
 | 
												
													
														
															|  |                 "atMe": 0,
 |  | 
 | 
												
													
														
															|  |                 "lastContentType": 1,
 |  | 
 | 
												
													
														
															|  |                 "lastContent": "",
 |  | 
 | 
												
													
														
															|  |                 "newMessageCount": 0,
 |  | 
 | 
												
													
														
															|  |                 "timestamp": 0
 |  | 
 | 
												
													
														
															|  |             };
 |  | 
 | 
												
													
														
															|  | 
 |  |         var data = {
 | 
												
													
														
															|  | 
 |  |             userId: userId,
 | 
												
													
														
															|  | 
 |  |             from: "",
 | 
												
													
														
															|  | 
 |  |             groupId: groupId,
 | 
												
													
														
															|  | 
 |  |             atMe: 0,
 | 
												
													
														
															|  | 
 |  |             lastContentType: 1,
 | 
												
													
														
															|  | 
 |  |             lastContent: "",
 | 
												
													
														
															|  | 
 |  |             newMessageCount: 0,
 | 
												
													
														
															|  | 
 |  |             timestamp: 0
 | 
												
													
														
															|  | 
 |  |         };
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             res.write(data);
 |  | 
 | 
												
													
														
															|  |             return;
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (result.length > 0) {
 | 
												
													
														
															|  | 
 |  |             var row = result[0];
 | 
												
													
														
															|  | 
 |  |             data.from = row.from_uid;
 | 
												
													
														
															|  | 
 |  |             data.groupId = row.gid;
 | 
												
													
														
															|  | 
 |  |             data.atMe = row.at_me;
 | 
												
													
														
															|  | 
 |  |             data.lastContentType = row.last_content_type;
 | 
												
													
														
															|  | 
 |  |             data.lastContent = row.lastContent;
 | 
												
													
														
															|  | 
 |  |             data.newMessageCount = row.new_msg_count;
 | 
												
													
														
															|  | 
 |  |             data.timestamp = objectUtil.timestampToLong(row.timestamp)
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         result[0].timestamp = Date.parse(new Date(result[0].timestamp));
 |  | 
 | 
												
													
														
															|  |         res.write(result[0]);
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(200).send(data);
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 获取与某人的私信统计。
 |  |  * 获取与某人的私信统计。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * /pm/statistic?user_id=sand&&peer_id=rose
 |  | 
 | 
												
													
														
															|  | 
 |  |  * /chats/pm/statistic?user_id=sand&&peer_id=rose
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  user_id:信息所有者id
 |  |  *  user_id:信息所有者id
 | 
												
											
												
													
														
															|  | @ -716,35 +1061,41 @@ router.get(APIv1.Chats.PMStats, function (req, res) {
 | 
												
													
														
															|  |         throw {httpStatus: 406, message: "Missing fields."};
 |  |         throw {httpStatus: 406, message: "Missing fields."};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getP2PChatInfo(userId, peerId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getPrivateChatSummary(userId, peerId, function (err, result) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.log("Get private messages stats failed: ", err);
 |  |             console.log("Get private messages stats failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |             throw {httpStatus: 500, message: "Get private messages stats failed."};
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Get private messages stats failed."});
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         if (result.length == 0) {
 |  | 
 | 
												
													
														
															|  |             var data = {
 |  | 
 | 
												
													
														
															|  |                 "userId": userId,
 |  | 
 | 
												
													
														
															|  |                 "peerId": peerId,
 |  | 
 | 
												
													
														
															|  |                 "lastCContentType": 1,
 |  | 
 | 
												
													
														
															|  |                 "lastContent": "",
 |  | 
 | 
												
													
														
															|  |                 "newMessageCount": 0,
 |  | 
 | 
												
													
														
															|  |                 "timestamp": 0
 |  | 
 | 
												
													
														
															|  |             };
 |  | 
 | 
												
													
														
															|  |             res.write(data);
 |  | 
 | 
												
													
														
															|  |             return;
 |  | 
 | 
												
													
														
															|  | 
 |  |         var data = {
 | 
												
													
														
															|  | 
 |  |             userId: userId,
 | 
												
													
														
															|  | 
 |  |             peerId: peerId,
 | 
												
													
														
															|  | 
 |  |             lastCContentType: 1,
 | 
												
													
														
															|  | 
 |  |             lastContent: "",
 | 
												
													
														
															|  | 
 |  |             newMessageCount: 0,
 | 
												
													
														
															|  | 
 |  |             timestamp: 0
 | 
												
													
														
															|  | 
 |  |         };
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (result.length > 0) {
 | 
												
													
														
															|  | 
 |  |             var row = result[0];
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             data.userId = row.uid;
 | 
												
													
														
															|  | 
 |  |             data.peerId = row.from_uid;
 | 
												
													
														
															|  | 
 |  |             data.lastContentType = row.last_content_type;
 | 
												
													
														
															|  | 
 |  |             data.lastContent = row.last_content;
 | 
												
													
														
															|  | 
 |  |             data.newMessageCount = row.new_msg_count;
 | 
												
													
														
															|  | 
 |  |             data.timestamp = objectUtil.timestampToLong(row.timestamp)
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         result[0].timestamp = Date.parse(new Date(result[0].timestamp));
 |  | 
 | 
												
													
														
															|  |         res.write(result[0]);
 |  | 
 | 
												
													
														
															|  | 
 |  |         res.status(0).send(data);
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | /**
 |  | /**
 | 
												
													
														
															|  |  * 获取所有未读私信总数。
 |  |  * 获取所有未读私信总数。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * /pm/unread_count?user_id=sand
 |  | 
 | 
												
													
														
															|  | 
 |  |  * /chats/pm/unread_count?user_id=sand
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  * uid:信息所有者id
 |  |  * uid:信息所有者id
 | 
												
											
												
													
														
															|  | @ -752,7 +1103,7 @@ router.get(APIv1.Chats.PMStats, function (req, res) {
 | 
												
													
														
															|  | router.get(APIv1.Chats.PMUnreadCount, function (req, res) {
 |  | router.get(APIv1.Chats.PMUnreadCount, function (req, res) {
 | 
												
													
														
															|  |     var userId = req.query.user_id;
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getP2PChatAllUnRead(userId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getPrivateChatAllUnReadCount(userId, function (err, result) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.log("Get unread private message count failed: ", err);
 |  |             console.log("Get unread private message count failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -774,7 +1125,7 @@ router.get(APIv1.Chats.PMUnreadCount, function (req, res) {
 | 
												
													
														
															|  |  * 所有聊天消息未读数。
 |  |  * 所有聊天消息未读数。
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 请求URL:
 |  |  * 请求URL:
 | 
												
													
														
															|  |  *  /chats/unread_count?user_id=sand
 |  | 
 | 
												
													
														
															|  | 
 |  |  *  /chats/chats/unread_count?user_id=sand
 | 
												
													
														
															|  |  *
 |  |  *
 | 
												
													
														
															|  |  * 参数:
 |  |  * 参数:
 | 
												
													
														
															|  |  *  user_id:信息所有者id
 |  |  *  user_id:信息所有者id
 | 
												
											
												
													
														
															|  | @ -785,7 +1136,7 @@ router.get(APIv1.Chats.UnreadMsgCount, function (req, res) {
 | 
												
													
														
															|  |         throw {httpStatus: 406, message: "Missing fields."};
 |  |         throw {httpStatus: 406, message: "Missing fields."};
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     msgStats.getChatAllUnRead(userId, function (err, result) {
 |  | 
 | 
												
													
														
															|  | 
 |  |     msgStats.getChatAllUnReadCount(userId, function (err, result) {
 | 
												
													
														
															|  |         if (err) {
 |  |         if (err) {
 | 
												
													
														
															|  |             console.error("Get all unread message count failed: ", err);
 |  |             console.error("Get all unread message count failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -803,4 +1154,213 @@ router.get(APIv1.Chats.UnreadMsgCount, function (req, res) {
 | 
												
													
														
															|  |     });
 |  |     });
 | 
												
													
														
															|  | });
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 搜索患者相关的数据,包括患者信息与相关的私信记录。关键词不支持空格拆分。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /search/patient?user_id=3b723bb8699a11e69f7c005056850d66&user_role=1&keyword=庄
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 参数:
 | 
												
													
														
															|  | 
 |  |  *  keywords: 关键词
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.SearchAboutPatient, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     var userRole = req.query.user_role;
 | 
												
													
														
															|  | 
 |  |     var keyword = req.query.keyword;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (!userId) throw {httpStatus: 406, message: "Missing fields: user_id."};
 | 
												
													
														
															|  | 
 |  |     if (!userRole) throw {httpStatus: 406, message: "Missing fields: user_role."};
 | 
												
													
														
															|  | 
 |  |     if (!keyword) throw {httpStatus: 406, message: "Missing fields: keyword."};
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     search.searchPatients(userId, userRole, keyword, function (err, patients) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error("Search patient on basic information failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Search patient on basic information failed."});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var data = {patients: [], chats: []};
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < patients.length; ++i) {
 | 
												
													
														
															|  | 
 |  |             var patient = patients[i];
 | 
												
													
														
															|  | 
 |  |             data.patients.push({
 | 
												
													
														
															|  | 
 |  |                 code: patient.code,
 | 
												
													
														
															|  | 
 |  |                 name: patient.name,
 | 
												
													
														
															|  | 
 |  |                 sex: patient.sex,
 | 
												
													
														
															|  | 
 |  |                 birthday: dbUtils.timestampToLong(patient.birthday),
 | 
												
													
														
															|  | 
 |  |                 avatar: patient.photo === null ? "" : patient.photo
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         search.searchPatientPM(userId, keyword, function (err, chats) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error("Search patient on private messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: "Search patient on private messages failed."});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             var lastPatientCode = '';
 | 
												
													
														
															|  | 
 |  |             var lastPatient = {code: '', name: '', sex: '', avatar: '', messages: []};
 | 
												
													
														
															|  | 
 |  |             for (var i = 0; i < chats.length; ++i) {
 | 
												
													
														
															|  | 
 |  |                 var chat = chats[i];
 | 
												
													
														
															|  | 
 |  |                 if (lastPatientCode !== chat.code) {
 | 
												
													
														
															|  | 
 |  |                     lastPatientCode = chat.code;
 | 
												
													
														
															|  | 
 |  |                     lastPatient.code = chat.code;
 | 
												
													
														
															|  | 
 |  |                     lastPatient.name = chat.name;
 | 
												
													
														
															|  | 
 |  |                     lastPatient.sex = chat.sex;
 | 
												
													
														
															|  | 
 |  |                     lastPatient.birthday = dbUtils.timestampToLong(patient.birthday);
 | 
												
													
														
															|  | 
 |  |                     lastPatient.avatar = chat.photo === null ? "" : chat.photo;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     data.chats.push(lastPatient);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 lastPatient.messages.push({
 | 
												
													
														
															|  | 
 |  |                     id: chat.msg_id,
 | 
												
													
														
															|  | 
 |  |                     content: chat.content
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(200).send(data);
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 搜索医生相关的数据,包括医生信息与相关的聊天记录,包括私信与群信。
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 请求URL:
 | 
												
													
														
															|  | 
 |  |  *  /search/doctor?user_id=5fa5e88f7a4111e69f7c005056850d66&keyword=丘
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * 参数:
 | 
												
													
														
															|  | 
 |  |  *  keywords: 关键词
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | router.get(APIv1.Chats.SearchAboutDoctor, function (req, res) {
 | 
												
													
														
															|  | 
 |  |     var userId = req.query.user_id;
 | 
												
													
														
															|  | 
 |  |     var keyword = req.query.keyword;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     if (!userId) throw {httpStatus: 406, message: "Missing fields: user_id."};
 | 
												
													
														
															|  | 
 |  |     if (!keyword) throw {httpStatus: 406, message: "Missing fields: keyword."};
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     // 搜索医生
 | 
												
													
														
															|  | 
 |  |     search.searchDoctors(userId, keyword, function (err, doctors) {
 | 
												
													
														
															|  | 
 |  |         if (err) {
 | 
												
													
														
															|  | 
 |  |             log.error("Search doctor on basic information failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             res.status(500).send({message: "Search doctor on basic information failed."});
 | 
												
													
														
															|  | 
 |  |             return;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         var data = {doctors: [], groups: [], chats: {doctors: [], groups: []}};
 | 
												
													
														
															|  | 
 |  |         for (var i = 0; i < doctors.length; ++i) {
 | 
												
													
														
															|  | 
 |  |             var doctor = doctors[i];
 | 
												
													
														
															|  | 
 |  |             data.doctors.push({
 | 
												
													
														
															|  | 
 |  |                 code: doctor.code,
 | 
												
													
														
															|  | 
 |  |                 name: doctor.name,
 | 
												
													
														
															|  | 
 |  |                 sex: doctor.sex,
 | 
												
													
														
															|  | 
 |  |                 avatar: doctor.photo === null ? "" : doctor.photo
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         // 搜索讨论组名称及成员名称
 | 
												
													
														
															|  | 
 |  |         search.searchGroups(userId, keyword, function (err, groups) {
 | 
												
													
														
															|  | 
 |  |             if (err) {
 | 
												
													
														
															|  | 
 |  |                 log.error("Search talk group failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 res.status(500).send({message: "Search talk group failed."});
 | 
												
													
														
															|  | 
 |  |                 return;
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             var lastGroupCode = '';
 | 
												
													
														
															|  | 
 |  |             var lastGroup = null;
 | 
												
													
														
															|  | 
 |  |             for (var i = 0; i < groups.length; ++i) {
 | 
												
													
														
															|  | 
 |  |                 var group = groups[i];
 | 
												
													
														
															|  | 
 |  |                 if (lastGroupCode !== group.code) {
 | 
												
													
														
															|  | 
 |  |                     lastGroupCode = group.code;
 | 
												
													
														
															|  | 
 |  |                     lastGroup = {
 | 
												
													
														
															|  | 
 |  |                         code: group.code,
 | 
												
													
														
															|  | 
 |  |                         name: group.name,
 | 
												
													
														
															|  | 
 |  |                         type: GROUP_TYPE.DiscussionGroup,
 | 
												
													
														
															|  | 
 |  |                         members: []
 | 
												
													
														
															|  | 
 |  |                     };
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     data.groups.push(lastGroup);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 lastGroup.members.push({
 | 
												
													
														
															|  | 
 |  |                     code: group.member_code,
 | 
												
													
														
															|  | 
 |  |                     name: group.member_name
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             // 搜索医生间的私信
 | 
												
													
														
															|  | 
 |  |             search.searchDoctorMessages(userId, keyword, function (err, messages) {
 | 
												
													
														
															|  | 
 |  |                 if (err) {
 | 
												
													
														
															|  | 
 |  |                     log.error("Search doctor private messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     res.status(500).send({message: "Search doctor private messages failed."});
 | 
												
													
														
															|  | 
 |  |                     return;
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 var lastDoctor;
 | 
												
													
														
															|  | 
 |  |                 var lastDoctorCode = '';
 | 
												
													
														
															|  | 
 |  |                 for (var i = 0; i < messages.length; ++i) {
 | 
												
													
														
															|  | 
 |  |                     var message = messages[i];
 | 
												
													
														
															|  | 
 |  |                     if (lastDoctorCode !== message.code) {
 | 
												
													
														
															|  | 
 |  |                         lastDoctorCode = message.code;
 | 
												
													
														
															|  | 
 |  |                         lastDoctor = {
 | 
												
													
														
															|  | 
 |  |                             code: message.code,
 | 
												
													
														
															|  | 
 |  |                             name: message.name,
 | 
												
													
														
															|  | 
 |  |                             photo: message.photo === null ? "" : message.photo,
 | 
												
													
														
															|  | 
 |  |                             messages: []
 | 
												
													
														
															|  | 
 |  |                         };
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                         data.chats.doctors.push(lastDoctor);
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     lastDoctor.messages.push({
 | 
												
													
														
															|  | 
 |  |                         id: message.msg_id,
 | 
												
													
														
															|  | 
 |  |                         content: message.content
 | 
												
													
														
															|  | 
 |  |                     });
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                 // 搜索医生间的讨论组消息
 | 
												
													
														
															|  | 
 |  |                 search.searchGroupMessages(userId, keyword, function (err, messages) {
 | 
												
													
														
															|  | 
 |  |                     if (err) {
 | 
												
													
														
															|  | 
 |  |                         log.error("Search doctor group messages failed: ", err);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                         res.status(500).send({message: "Search doctor group messages failed."});
 | 
												
													
														
															|  | 
 |  |                         return;
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     // g.code, g.name, gm.msg_id, gm.content
 | 
												
													
														
															|  | 
 |  |                     var lastGroup;
 | 
												
													
														
															|  | 
 |  |                     var lastGroupCode = '';
 | 
												
													
														
															|  | 
 |  |                     for (var i = 0; i < messages.length; ++i) {
 | 
												
													
														
															|  | 
 |  |                         var message = messages[i];
 | 
												
													
														
															|  | 
 |  |                         if (lastGroupCode !== message.code) {
 | 
												
													
														
															|  | 
 |  |                             lastGroupCode = message.code;
 | 
												
													
														
															|  | 
 |  |                             lastGroup = {
 | 
												
													
														
															|  | 
 |  |                                 code: message.code,
 | 
												
													
														
															|  | 
 |  |                                 name: message.name,
 | 
												
													
														
															|  | 
 |  |                                 messages: []
 | 
												
													
														
															|  | 
 |  |                             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                             data.chats.groups.push(lastGroup);
 | 
												
													
														
															|  | 
 |  |                         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                         lastGroup.messages.push({
 | 
												
													
														
															|  | 
 |  |                             id: message.msg_id,
 | 
												
													
														
															|  | 
 |  |                             content: message.content
 | 
												
													
														
															|  | 
 |  |                         });
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |                     res.status(200).send(data);
 | 
												
													
														
															|  | 
 |  |                 });
 | 
												
													
														
															|  | 
 |  |             });
 | 
												
													
														
															|  | 
 |  |         });
 | 
												
													
														
															|  | 
 |  |     });
 | 
												
													
														
															|  | 
 |  | });
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | module.exports = router;
 |  | module.exports = router;
 |