chats.endpoint.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * 消息端点。
  3. *
  4. * 此控制器处理点对点,组及消息消息。为三类消息提供发送及查询功能。
  5. */
  6. "use strict";
  7. let express = require('express');
  8. let router = express.Router();
  9. let http = require('http');
  10. let log = require('../util/log.js');
  11. let objectUtil = require("../util/objectUtil.js");
  12. let controllerUtil = require('../util/controllerUtil');
  13. let Patient = new require("../models/patient");
  14. let Doctor = new require('../models/doctor');
  15. let Group = new require('../models/group');
  16. let Search = require('../models/search');
  17. const APIv1 = require('../include/endpoints').APIv1;
  18. const CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
  19. const MAX_INT = require('../include/commons').MAX_INT;
  20. const DEFAULT_PAGE_SIZE = require('../include/commons').DEFAULT_PAGE_SIZE;
  21. //--------------------------------------------------------------//
  22. //----------------------------消息发送----------------------------//
  23. //--------------------------------------------------------------//
  24. /**
  25. * 发送System消息。
  26. *
  27. * 请求URL:
  28. * /chats/sm
  29. *
  30. * 消息格式:
  31. * {
  32. * to: "Rose",
  33. * title: "System Message",
  34. * summary: "You have new job",
  35. * contentType: "1",
  36. * content: "The patient has been followed in the scheduler, please make new follow plan as soon as possible."
  37. * }
  38. *
  39. * @param message
  40. */
  41. router.post(APIv1.Chats.SM, function (req, res) {
  42. // 检查消息体及消息格式是否正确
  43. let message = req.body;
  44. if (!objectUtil.isJsonObject(message)) {
  45. throw {httpStatus: 406, message: 'Problems parsing JSON.'}
  46. }
  47. // 字段判断
  48. let testing = objectUtil.fieldsCheck(message, "to", "title", "summary", "contentType", "content");
  49. if (!testing.pass) {
  50. throw {httpStatus: 406, message: testing.message}
  51. }
  52. // 消息处理
  53. let doctor = new Doctor();
  54. controllerUtil.regModelEventHandler(doctor, res);
  55. doctor.sendSystemMessage(message);
  56. });
  57. /**
  58. * 处理Private消息。处理流程分:
  59. * 1 解析消息,并保存到数据库
  60. * 2 更新消息统计数据
  61. * 3 获取目标的状态并构建通知消息,如果用户在线就推送通知消息
  62. *
  63. * 请求URL:
  64. * /chats/pm
  65. *
  66. * 消息格式:
  67. * {
  68. * from: sand,
  69. * to: Rose,
  70. * contentType: "1,2,3,4",
  71. * content: "Please follow the patient as soon as possible."
  72. * }
  73. *
  74. * @param message
  75. */
  76. router.post(APIv1.Chats.PM, function (req, res) {
  77. // 检查消息体及消息格式是否正确
  78. let message = req.body;
  79. if (!objectUtil.isJsonObject(message)) {
  80. throw {httpStatus: 406, message: 'Problems parsing JSON.'}
  81. }
  82. // 字段判断
  83. let testing = objectUtil.fieldsCheck(message, "from", "to", "contentType", "content");
  84. if (!testing.pass) {
  85. throw {httpStatus: 406, message: testing.message};
  86. }
  87. // 消息处理,患者与医生分开发送
  88. Patient.isPatientCode(message.to,
  89. function () {
  90. let patient = new Patient();
  91. controllerUtil.regModelEventHandler(patient, res);
  92. patient.sendMessage(message);
  93. }, function () {
  94. let doctor = new Doctor();
  95. controllerUtil.regModelEventHandler(doctor, res);
  96. doctor.sendMessage(message);
  97. });
  98. });
  99. /**
  100. * 处理讨论组消息。
  101. *
  102. * 请求URL:
  103. * /chats/gm
  104. *
  105. * 消息格式:
  106. * {
  107. * from: "sand", // 发送者id
  108. * at: "Rose", // @人
  109. * group: "DiscussionGroupId", // 所在组
  110. * groupType: "1 or 2", // 组类型:行政团队或讨论组
  111. * contentType: "1,2,3", // 内容类型
  112. * content: "The patient mess me up" // 内容
  113. * }
  114. *
  115. * @param message
  116. */
  117. router.post(APIv1.Chats.GM, function (req, res) {
  118. // 检查消息体及消息格式是否正确
  119. let message = req.body;
  120. if (!objectUtil.isJsonObject(message)) {
  121. throw {httpStatus: 406, message: 'Problems parsing JSON.'};
  122. }
  123. // 字段判断
  124. let testing = objectUtil.fieldsCheck(message, 'from', 'at', 'group', 'groupType', 'contentType', 'content');
  125. if (!testing.pass) {
  126. throw {httpStatus: 406, message: testing.message}
  127. }
  128. // 消息处理
  129. let group = new Group();
  130. controllerUtil.regModelEventHandler(group, res);
  131. group.sendMessage(message);
  132. });
  133. //--------------------------------------------------------------//
  134. //----------------------------消息提取----------------------------//
  135. //--------------------------------------------------------------//
  136. /**
  137. * 获取参与的聊天列表,包括:点对点,@我,参与的讨论组,系统消息等。
  138. *
  139. * 请求URL:
  140. * /chats/list?user_id=sand
  141. */
  142. router.get(APIv1.Chats.List, function (req, res) {
  143. let userId = req.query.user_id;
  144. if (userId === null) {
  145. throw {httpStatus: 406, message: 'Missing fields.'};
  146. }
  147. let doctor = new Doctor();
  148. controllerUtil.regModelEventHandler(doctor, res);
  149. doctor.getChatList(userId);
  150. });
  151. /**
  152. * 获取与患者的聊天列表,包括:P2P,参与的讨论组和行政团队。
  153. *
  154. * 请求URL:
  155. * /chats/list/patient
  156. */
  157. router.get(APIv1.Chats.ListWithPatient, function (req, res) {
  158. let userId = req.query.user_id;
  159. if (userId === null) {
  160. throw {httpStatus: 406, message: 'Missing fields.'};
  161. }
  162. let doctor = new Doctor();
  163. controllerUtil.regModelEventHandler(doctor, res);
  164. doctor.getChatsListWithPatient(userId);
  165. });
  166. /**
  167. * 获取与医生的聊天列表,包括:点对点,参与的讨论组。
  168. *
  169. * 请求URL:
  170. * /chats/list/doctor
  171. */
  172. router.get(APIv1.Chats.ListWithDoctor, function (req, res) {
  173. let userId = req.query.user_id;
  174. if (userId === null) {
  175. throw {httpStatus: 406, message: 'Missing fields.'};
  176. }
  177. let doctor = new Doctor();
  178. controllerUtil.regModelEventHandler(doctor, res);
  179. doctor.getChatListWithDoctor(userId);
  180. });
  181. /**
  182. * 获取最近聊天对象:包括患者,医生与讨论组。客户端自行根据需要提取患者、医生或讨论组数据。
  183. *
  184. * 请求URL:
  185. * /chats/recent?user_id=0de7295862dd11e69faffa163e8aee56&days=7
  186. *
  187. * 参数:
  188. * user_id: 用户ID
  189. * target_type: 对象类型,1患者,2医生,3讨论组
  190. * days: 最近天数,默认7天
  191. */
  192. router.get(APIv1.Chats.Recent, function (req, res) {
  193. let userId = req.query.user_id;
  194. let days = req.query.days;
  195. if (userId === null) {
  196. throw {httpStatus: 406, message: 'Missing field: user_id'};
  197. }
  198. if (days === null) {
  199. days = 7;
  200. }
  201. let doctor = new Doctor();
  202. controllerUtil.regModelEventHandler(doctor, res);
  203. doctor.getRecentChatList(userId, days);
  204. });
  205. /**
  206. * 获取私信。倒序排列。
  207. *
  208. * 参数:
  209. * user_id 必须,医生ID
  210. * peer_id 必须,对方医生ID
  211. * content_type 必须,消息类型
  212. * message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
  213. * message_end_id 可选,消息的结束ID,如果为空从第一条开始获取
  214. * count 可选,消息数量,如果不指定、小于零或大于50,默认为100条。若message_start_id与message_end_id均不为空,则此参数无效,方法是设置为10000条
  215. * closed_interval 消息范围是否使用闭区间
  216. *
  217. * 请求URL:
  218. * /chats/pm?user_id=sand&peer_id=Rose&content_type=2&message_start_id=10000&message_end_id=0&count=20&closed_interval=false
  219. */
  220. router.get(APIv1.Chats.PM, function (req, res) {
  221. let userId = req.query.user_id;
  222. let peerId = req.query.peer_id;
  223. let contentType = req.query.content_type;
  224. let msgStartId = !req.query.message_start_id ? MAX_INT : parseInt(req.query.message_start_id);
  225. let msgEndId = !req.query.message_end_id ? 0 : parseInt(req.query.message_end_id);
  226. let count = req.query.count === undefined ? DEFAULT_PAGE_SIZE : parseInt(req.query.count);
  227. let closedInterval = (req.query.closed_interval != false && req.query.closed_interval === "true");
  228. if (contentType !== undefined && parseInt(contentType) === CONTENT_TYPES.Image) count = DEFAULT_PAGE_SIZE;
  229. if (req.query.message_start_id && req.query.message_end_id) count = 10000;
  230. if (!userId) {
  231. throw {httpStatus: 400, message: "Missing field: user_id."};
  232. }
  233. let doctor = new Doctor();
  234. controllerUtil.regModelEventHandler(doctor, res);
  235. doctor.getPrivateMessages(userId, peerId, contentType, msgStartId, msgEndId, count, closedInterval);
  236. });
  237. /**
  238. * 获取未读私信。倒序排列。
  239. *
  240. * 参数:
  241. * user_id 必须,医生ID
  242. * peer_id 必须,对方医生ID
  243. *
  244. * 请求URL:
  245. * /chats/pm/unread?user_id=sand&peer_id=Rose
  246. */
  247. router.get(APIv1.Chats.PMUnread, function (req, res) {
  248. let userId = req.query.user_id;
  249. let peerId = req.query.peer_id;
  250. if (userId === undefined) {
  251. throw {httpStatus: 400, message: "Missing field: user_id."};
  252. }
  253. let doctor = new Doctor();
  254. controllerUtil.regModelEventHandler(doctor, res);
  255. doctor.getUnreadPrivateMessages(userId, peerId);
  256. });
  257. /**
  258. * 按时间倒序获取群消息。
  259. *
  260. * 参数:
  261. * user_id 必须,医生ID
  262. * group_id 必须,组ID
  263. * message_start_id 可选,消息的起始ID,如果为空从最新的一条开始获取
  264. * count 可选,消息数量,如果不指定、小于零或大于50,默认为50条
  265. *
  266. * 请求URL:
  267. * /chats/gm?user_id=D2016008240002&group_id=494&content_type=2&message_start_id=0&message_end_id=0&count=20
  268. */
  269. router.get(APIv1.Chats.GM, function (req, res) {
  270. let groupId = req.query.group_id;
  271. let memberId = req.query.user_id;
  272. let contentType = req.query.content_type;
  273. let msgStartId = !req.query.message_start_id ? MAX_INT : parseInt(req.query.message_start_id);
  274. let msgEndId = !req.query.message_end_id ? 0 : parseInt(req.query.message_end_id);
  275. let count = req.query.count === undefined ? DEFAULT_PAGE_SIZE : parseInt(req.query.count);
  276. if (groupId === undefined) {
  277. throw {httpStatus: 400, message: "Missing field: group_id."};
  278. }
  279. if (contentType !== undefined && parseInt(contentType) === CONTENT_TYPES.Image) count = DEFAULT_PAGE_SIZE;
  280. if (req.query.message_start_id && req.query.message_end_id) count = 100000;
  281. let group = new Group();
  282. controllerUtil.regModelEventHandler(group, res);
  283. group.getMessages(groupId, memberId, contentType, msgStartId, msgEndId, count);
  284. });
  285. /**
  286. * 获取未读群消息。
  287. *
  288. * 请求URL:
  289. * /chats/gm/unread?group_id=discussionGroupId&user_id=sand
  290. */
  291. router.get(APIv1.Chats.GMUnread, function (req, res) {
  292. let memberId = req.query.user_id;
  293. let groupId = req.query.group_id;
  294. if (memberId === undefined) {
  295. throw {httpStatus: 400, message: "Missing field: user_id."};
  296. }
  297. if (groupId === undefined) {
  298. throw {httpStatus: 400, message: "Missing field: group_id."};
  299. }
  300. let group = new Group();
  301. controllerUtil.regModelEventHandler(group, res);
  302. group.getUnreadMessages(groupId, memberId);
  303. });
  304. //--------------------------------------------------------------//
  305. //----------------------------消息统计----------------------------//
  306. //--------------------------------------------------------------//
  307. /**
  308. * 获取所有群组未读消息总数。
  309. *
  310. * 请求URL:
  311. * /chats/gm/unread_count?user_id=sand
  312. *
  313. * 参数:
  314. * user_id:医生ID
  315. */
  316. router.get(APIv1.Chats.GMUnreadCount, function (req, res) {
  317. let memberId = req.query.user_id;
  318. if (memberId === null) {
  319. throw {httpStatus: 406, message: 'Missing fields.'};
  320. }
  321. let group = new Group();
  322. controllerUtil.regModelEventHandler(group, res);
  323. group.getUnreadMessageCount(memberId);
  324. });
  325. /**
  326. * 获取特定群组消息统计情况。
  327. *
  328. * /chats/gm/statistic?group_id=GGG&&user_id=sand
  329. *
  330. * 参数:
  331. * user_id:信息所有者id
  332. * group_id:群组id
  333. */
  334. router.get(APIv1.Chats.GMStats, function (req, res) {
  335. let memberId = req.query.user_id;
  336. let groupId = req.query.group_id;
  337. if (memberId === null || groupId === null) {
  338. throw {httpStatus: 406, message: 'Miss fields.'};
  339. }
  340. let group = new Group();
  341. controllerUtil.regModelEventHandler(group, res);
  342. group.getChatSummary(groupId, memberId);
  343. });
  344. /**
  345. * 获取与某人的私信统计。
  346. *
  347. * /chats/pm/statistic?user_id=sand&&peer_id=rose
  348. *
  349. * 参数:
  350. * user_id:信息所有者id
  351. * peer_id:聊天对端id
  352. */
  353. router.get(APIv1.Chats.PMStats, function (req, res) {
  354. let userId = req.query.user_id;
  355. let peerId = req.query.peer_id;
  356. if (userId == null || peerId == null) {
  357. throw {httpStatus: 406, message: "Missing fields."};
  358. }
  359. let doctor = new Doctor();
  360. controllerUtil.regModelEventHandler(doctor, res);
  361. doctor.getChatSummary(userId, peerId);
  362. });
  363. /**
  364. * 获取所有未读私信总数。
  365. *
  366. * /chats/pm/unread_count?user_id=sand
  367. *
  368. * 参数:
  369. * uid:信息所有者id
  370. */
  371. router.get(APIv1.Chats.PMUnreadCount, function (req, res) {
  372. let userId = req.query.user_id;
  373. let doctor = new Doctor();
  374. controllerUtil.regModelEventHandler(doctor, res);
  375. doctor.getUnreadPrivateMessages(userId);
  376. });
  377. /**
  378. * 所有聊天消息未读数。
  379. *
  380. * 请求URL:
  381. * /chats/chats/unread_count?user_id=sand
  382. *
  383. * 参数:
  384. * user_id:信息所有者id
  385. */
  386. router.get(APIv1.Chats.UnreadMsgCount, function (req, res) {
  387. let userId = req.query.user_id;
  388. if (userId === null) {
  389. throw {httpStatus: 406, message: "Missing fields."};
  390. }
  391. let doctor = new Doctor();
  392. controllerUtil.regModelEventHandler(doctor, res);
  393. doctor.getAllUnreadMessageCount(userId);
  394. });
  395. /**
  396. * 搜索患者相关的数据,包括患者信息与相关的私信记录。关键词不支持空格拆分。
  397. *
  398. * 请求URL:
  399. * /search/patient?user_id=3b723bb8699a11e69f7c005056850d66&user_role=1&keyword=庄
  400. *
  401. * 参数:
  402. * keywords: 关键词
  403. */
  404. router.get(APIv1.Chats.SearchAboutPatient, function (req, res) {
  405. let userId = req.query.user_id;
  406. let userRole = req.query.user_role;
  407. let keyword = req.query.keyword;
  408. if (!userId) throw {httpStatus: 406, message: "Missing fields: user_id."};
  409. if (!userRole) throw {httpStatus: 406, message: "Missing fields: user_role."};
  410. if (!keyword) throw {httpStatus: 406, message: "Missing fields: keyword."};
  411. let search = new Search();
  412. controllerUtil.regModelEventHandler(search, res);
  413. search.searchAboutPatient(userId, userRole, keyword);
  414. });
  415. /**
  416. * 搜索医生相关的数据,包括医生信息与相关的聊天记录,包括私信与群信。
  417. *
  418. * 请求URL:
  419. * /search/doctor?user_id=5fa5e88f7a4111e69f7c005056850d66&keyword=丘
  420. *
  421. * 参数:
  422. * keywords: 关键词
  423. */
  424. router.get(APIv1.Chats.SearchAboutDoctor, function (req, res) {
  425. let userId = req.query.user_id;
  426. let keyword = req.query.keyword;
  427. if (!userId) throw {httpStatus: 406, message: "Missing fields: user_id."};
  428. if (!keyword) throw {httpStatus: 406, message: "Missing fields: keyword."};
  429. let search = new Search();
  430. controllerUtil.regModelEventHandler(search, res);
  431. search.searchAboutDoctor(userId, keyword);
  432. });
  433. /**
  434. * 获取单条消息。
  435. *
  436. * URL:
  437. * /chats/message?id=1234&type=1
  438. */
  439. router.get(APIv1.Chats.Message, function (req, res) {
  440. let messageId = req.query.id;
  441. let messageType = req.query.type;
  442. let doctor = new Doctor();
  443. controllerUtil.regModelEventHandler(doctor, res);
  444. doctor.getMessage(messageId, messageType);
  445. });
  446. /**
  447. * 判断当前会话是否已经结束。
  448. *
  449. * 请求URL:
  450. * /chats/pm/finished?user_id=sand&peer_id=rose
  451. */
  452. router.get(APIv1.Chats.PMFinished, function (req, res) {
  453. let doctorId = req.query.doctor_id;
  454. let patientId = req.query.patient_id;
  455. if (!doctorId) {
  456. throw {httpStatus: 406, message: "Missing field: doctor_id"};
  457. }
  458. if (!patientId) {
  459. throw {httpStatus: 406, message: "Missing field: patient_id"};
  460. }
  461. let doctor = new Doctor();
  462. controllerUtil.regModelEventHandler(doctor, res);
  463. doctor.isConsultFinished(doctorId, patientId);
  464. });
  465. module.exports = router;