session.endpoint.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /**
  2. * 会话接口。
  3. *
  4. * author: Sand
  5. * since: 12/15/2016
  6. */
  7. "use strict";
  8. let express = require('express');
  9. let router = express.Router();
  10. let ObjectUtil = require("../../util/object.util.js");
  11. let ControllerUtil = require('../../util/controller.util');
  12. let Sessions = require('../../models/sessions/sessions');
  13. let Messages = require('../../models/messages/messages');
  14. let Participants = require('../../models/sessions/participants');
  15. let log = require("../../util/log.js");
  16. let clientCache = require('../../models/socket.io/client.cache').clientCache();
  17. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  18. const APIv2 = require('../../include/endpoints').APIv2;
  19. /**
  20. * 创建会话。对MUC会话而言,需要创建的是议题,如果是第一次创建议题,那应该附带创建会话,而不是直接创建会话。
  21. *
  22. * session_type:1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
  23. * (咨询类型:1三师咨询,2视频咨询,3图文咨询,4公共咨询,5病友圈 6、患者名医咨询 7医生名医咨询 8续方咨询 9居民直接咨询专科 10医生发起的求助 11上门服务咨询)
  24. * users 讨论组包含的用户标示
  25. * sessionId 会话ID
  26. *
  27. * 请求URL: /sessions
  28. * 参数:
  29. * {
  30. * session_id: 会话ID,非必须,仅对群消息使用。
  31. * session_type: 会话类型,必须。
  32. * session_name: 会话名称,
  33. * participants: 此会话的成员列表,格式:["userId1:role", "userId2:role"],用户的ID及角色。
  34. * }
  35. */
  36. router.post("/", function (req, res) {
  37. let payload = req.body;
  38. let testing = ObjectUtil.fieldsCheck(payload, 'participants', 'session_name', 'session_type');
  39. if (!testing.pass) {
  40. throw testing.message;
  41. }
  42. let participants = payload.participants;
  43. let sessionName = payload.session_name;
  44. let sessionType = payload.session_type;
  45. let sessionId = payload.session_id;
  46. let sessions = new Sessions();
  47. let participantArray = [];
  48. log.info("participants: "+participants);
  49. participants = JSON.parse(participants);
  50. //获取传入的参与者数组长度
  51. let participantLength = 0;
  52. for (let i in participants) {
  53. participantLength ++;
  54. }
  55. for (let j in participants) {
  56. log.info("aaaa:"+j + ":" + participants[j]);
  57. if (payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  58. sessions.conversionAarticipant(j, (doctorid) => {
  59. participantArray.push(doctorid+":"+participants[j])
  60. //判断数组长度等于传入的长度,则执行创建会话的请求
  61. if(participantArray.length == participantLength){
  62. ControllerUtil.regModelEventHandler(sessions, res);
  63. console.log(sessionId, sessionName, sessionType, participantArray);
  64. sessions.createSession(sessionId, sessionName, sessionType, participantArray);
  65. ///是否视频会议的会话
  66. let videoconferencing = false;
  67. // participants = JSON.parse(participants);
  68. // for (let j in participants) {
  69. // participantArray.push(j + ":" + participants[j]);
  70. // }
  71. //视频会议的会话 szx add 20181121
  72. if (payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  73. let access_token = "";
  74. if(payload.hasOwnProperty("mobile"))access_token = payload.access_token;
  75. videoconferencing = true;
  76. //发送广播,给相关的与会人员
  77. for (let k in participants) {
  78. sessions.conversionAarticipant(k, (doctorid) => {
  79. let userId = "pcim_"+doctorid;
  80. let pc_doctorClient = clientCache.findById(userId);
  81. if(pc_doctorClient){
  82. log.info("videoconferencing send to "+userId);
  83. pc_doctorClient.socket.emit("startVideoconference",{"sessionId":sessionId,"user_id":j});
  84. }
  85. else{
  86. log.info("videoconferencing not find "+userId);
  87. }
  88. })
  89. }
  90. }
  91. }
  92. })
  93. }else{
  94. participantArray.push(j + ":" + participants[j]);
  95. //判断数组长度等于传入的长度,则执行创建会话的请求
  96. if(participantArray.length == participantLength){
  97. ControllerUtil.regModelEventHandler(sessions, res);
  98. console.log(sessionId, sessionName, sessionType, participantArray);
  99. sessions.createSession(sessionId, sessionName, sessionType, participantArray);
  100. ///是否视频会议的会话
  101. let videoconferencing = false;
  102. // participants = JSON.parse(participants);
  103. // for (let j in participants) {
  104. // participantArray.push(j + ":" + participants[j]);
  105. // }
  106. //视频会议的会话 szx add 20181121
  107. if (payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  108. let access_token = "";
  109. if(payload.hasOwnProperty("mobile"))access_token = payload.access_token;
  110. videoconferencing = true;
  111. //发送广播,给相关的与会人员
  112. for (let j in participants) {
  113. let userId = "pcim_"+j;
  114. let pc_doctorClient = clientCache.findById(userId);
  115. if(pc_doctorClient){
  116. log.info("videoconferencing send to "+userId);
  117. pc_doctorClient.socket.emit("startVideoconference",{"sessionId":sessionId,"user_id":j});
  118. }
  119. else{
  120. log.info("videoconferencing not find "+userId);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. });
  128. /**
  129. * 获取会话列表
  130. *
  131. * 请求URL /sessions?user_id=3121&page=0&size=10
  132. */
  133. router.get("/", function (req, res) {
  134. let page = req.query.page;
  135. let size = req.query.size;
  136. let userId = req.query.user_id;
  137. let businessType = req.query.business_type;
  138. let status = req.query.status;
  139. let name = req.query.name;
  140. let search_type = req.query.search_type;//搜索类型 1医生 2患者 3会话名称
  141. let videoconferencing = req.query.videoconferencing;
  142. if (!page) {
  143. throw {httpStatus: 406, message: 'Missing page.'};
  144. }
  145. if (!size) {
  146. throw {httpStatus: 406, message: 'Missing size.'};
  147. }
  148. if (!userId) {
  149. throw {httpStatus: 406, message: 'Missing user.'};
  150. }
  151. let sessions = new Sessions();
  152. ControllerUtil.regModelEventHandler(sessions, res);
  153. //判断是否是中山医院MDT的请求
  154. if(videoconferencing && videoconferencing == 1){
  155. sessions.conversionAarticipant(userId, (doctorid) => {
  156. //传入参数齐全走过滤方法
  157. if(status&&businessType&&doctorid&&size&&page){
  158. sessions.getUserStatusSessions(doctorid,status,businessType,name,search_type,page,size);
  159. }else{
  160. sessions.getUserSessions(doctorid,name,search_type, page, size, businessType);
  161. }
  162. })
  163. }else{
  164. //传入参数齐全走过滤方法
  165. if(status&&businessType&&userId&&size&&page){
  166. sessions.getUserStatusSessions(userId,status,businessType,name,search_type,page,size);
  167. }else{
  168. sessions.getUserSessions(userId,name,search_type, page, size, businessType);
  169. }
  170. }
  171. });
  172. /**
  173. * 按会话类型获取会话列表
  174. * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
  175. */
  176. router.get(APIv2.Sessions.SessionListByType, function (req, res) {
  177. let page = req.query.page;
  178. let size = req.query.size;
  179. let userId = req.query.user_id;
  180. let type = req.query.type;
  181. let status = req.query.status;
  182. let name = req.query.name;
  183. if (!page) {
  184. throw {httpStatus: 406, message: 'Missing page.'};
  185. }
  186. if (!size) {
  187. throw {httpStatus: 406, message: 'Missing size.'};
  188. }
  189. if (!type) {
  190. throw {httpStatus: 406, message: 'Missing type.'};
  191. }
  192. if (!userId) {
  193. throw {httpStatus: 406, message: 'Missing user.'};
  194. }
  195. let sessions = new Sessions();
  196. ControllerUtil.regModelEventHandler(sessions, res);
  197. sessions.getUserSessionsByType(userId,type,page,size,status,name);
  198. });
  199. router.get(APIv2.Sessions.SessionListByNoParticipant, function (req, res) {
  200. let page = req.query.page;
  201. let size = req.query.size;
  202. let userId = req.query.user_id;
  203. let type = req.query.type;
  204. let status = req.query.status;
  205. if (!page) {
  206. throw {httpStatus: 406, message: 'Missing page.'};
  207. }
  208. if (!size) {
  209. throw {httpStatus: 406, message: 'Missing size.'};
  210. }
  211. if (!type) {
  212. throw {httpStatus: 406, message: 'Missing type.'};
  213. }
  214. if (!userId) {
  215. throw {httpStatus: 406, message: 'Missing user.'};
  216. }
  217. let sessions = new Sessions();
  218. ControllerUtil.regModelEventHandler(sessions, res);
  219. sessions.getUserSessionsByTypeAndNoParticipant(userId,type,page,size,status);
  220. });
  221. /**
  222. * 按会话类型获取会话总数
  223. * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
  224. */
  225. router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
  226. let userId = req.query.user_id;
  227. let type = req.query.type;
  228. let status = req.query.status;
  229. if (!type) {
  230. throw {httpStatus: 406, message: 'Missing type.'};
  231. }
  232. if (!userId) {
  233. throw {httpStatus: 406, message: 'Missing user.'};
  234. }
  235. let sessions = new Sessions();
  236. ControllerUtil.regModelEventHandler(sessions, res);
  237. sessions.getSessionCountByType(userId,type,status);
  238. });
  239. router.get(APIv2.Sessions.Session,function(req,res){
  240. let sessionId = req.params.session_id;
  241. let userId = req.query.user_id;
  242. if (!sessionId) {
  243. throw {httpStatus: 406, message: 'Missing sessionId.'};
  244. }
  245. if (!userId) {
  246. throw {httpStatus: 406, message: 'Missing user.'};
  247. }
  248. let sessions = new Sessions();
  249. ControllerUtil.regModelEventHandler(sessions, res);
  250. sessions.getSession(sessionId,userId);
  251. });
  252. /**
  253. * 最近会话列表。
  254. *
  255. * URL:
  256. * /sessions/recent?user_id=abc&date_span=7
  257. */
  258. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  259. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  260. let userId = req.query.user_id;
  261. let dateSpan = req.query.date_span;
  262. let sessions = new Sessions();
  263. ControllerUtil.regModelEventHandler(sessions, res);
  264. sessions.getRecentSessions(userId, dateSpan);
  265. });
  266. /**
  267. * 判断会话是否存在。
  268. *
  269. * URL:
  270. * /sessions/isExist?session_id=abc
  271. */
  272. router.get(APIv2.Sessions.IsExist, function (req, res) {
  273. let sessionId = req.query.session_id;
  274. if (!sessionId) {
  275. throw {httpStatus: 406, message: 'Missing sessionId.'}
  276. }
  277. let sessions = new Sessions();
  278. ControllerUtil.regModelEventHandler(sessions, res);
  279. sessions.isExist(sessionId);
  280. });
  281. /**
  282. * 某个聊天记录置顶操作
  283. *
  284. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  285. */
  286. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  287. let user = req.query.user;
  288. let sessionId = req.query.sessionId;
  289. if (!user) {
  290. throw {httpStatus: 406, message: 'Missing user.'};
  291. }
  292. if (!sessionId) {
  293. throw {httpStatus: 406, message: 'Missing sessionId.'};
  294. }
  295. let sessions = new Sessions();
  296. ControllerUtil.regModelEventHandler(sessions, res);
  297. sessions.stickSession(sessionId, user);
  298. });
  299. /**
  300. * 某个聊天记录置顶操作
  301. *
  302. * 请求URL /status?status=1&sessionId=testsessionmsg1
  303. */
  304. router.post(APIv2.Sessions.SessionStatus, function (req, res) {
  305. let sessionId = req.query.sessionId;
  306. let status = req.query.status;
  307. if (!sessionId) {
  308. throw {httpStatus: 406, message: 'Missing sessionId.'};
  309. }
  310. if (!status) {
  311. throw {httpStatus: 406, message: 'Missing status.'};
  312. }
  313. let sessions = new Sessions();
  314. ControllerUtil.regModelEventHandler(sessions, res);
  315. sessions.updateSessionStatus(sessionId, status);
  316. });
  317. /**
  318. * 修改会话名称(修改redis和数据库)
  319. *
  320. * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
  321. */
  322. router.post(APIv2.Sessions.SessionName, function (req, res) {
  323. let sessionId = req.body.sessionId;
  324. let name = req.body.name;
  325. if (!sessionId) {
  326. throw {httpStatus: 406, message: 'Missing sessionId.'};
  327. }
  328. if (!name) {
  329. throw {httpStatus: 406, message: 'Missing name.'};
  330. }
  331. let sessions = new Sessions();
  332. ControllerUtil.regModelEventHandler(sessions, res);
  333. sessions.updateSessionName(sessionId, name);
  334. });
  335. /**
  336. * 取消置顶
  337. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  338. */
  339. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  340. let user = req.query.user;
  341. let sessionId = req.query.sessionId;
  342. if (!user) {
  343. throw {httpStatus: 406, message: 'Missing user.'};
  344. }
  345. if (!sessionId) {
  346. throw {httpStatus: 406, message: 'Missing sessionId.'};
  347. }
  348. let sessions = new Sessions();
  349. ControllerUtil.regModelEventHandler(sessions, res);
  350. sessions.cancelStickSession(sessionId, user);
  351. });
  352. /**
  353. * 获取会话成员。
  354. */
  355. router.get(APIv2.Sessions.Participants, function (req, res) {
  356. let sessionId = req.params.session_id;
  357. let participants = new Participants();
  358. ControllerUtil.regModelEventHandler(participants, res);
  359. participants.getParticipants(sessionId);
  360. });
  361. /**
  362. * 获取会话成员头像列表。
  363. */
  364. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  365. let sessionId = req.params.session_id;
  366. let participants = new Participants();
  367. ControllerUtil.regModelEventHandler(participants, res);
  368. participants.getParticipantsAvatar(sessionId);
  369. });
  370. /**
  371. * 增加成员
  372. * user:增加的人员
  373. * sessionId 会话ID
  374. */
  375. router.put(APIv2.Sessions.Participant, function (req, res) {
  376. let user = req.params.participant_id;
  377. if (!user) {
  378. throw {httpStatus: 406, message: 'Missing participant_id.'};
  379. }
  380. let sessionId = req.params.session_id;
  381. if (!sessionId) {
  382. throw {httpStatus: 406, message: 'Missing session_id.'};
  383. }
  384. let participants = new Participants();
  385. ControllerUtil.regModelEventHandler(participants, res);
  386. participants.addUser(sessionId, user);
  387. });
  388. /**
  389. * 增加成员
  390. * user_id:增加的人员
  391. * old_user_id:删除的人员
  392. * session_id 会话ID
  393. */
  394. router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
  395. let payload = req.body;
  396. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  397. if (!testing.pass) {
  398. throw testing.message;
  399. }
  400. let participants = new Participants();
  401. ControllerUtil.regModelEventHandler(participants, res);
  402. participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
  403. });
  404. /**
  405. * 增加活跃成员
  406. * user_id:增加的人员
  407. * old_user_id:删除的人员
  408. * session_id 会话ID
  409. */
  410. router.post(APIv2.Sessions.ParticipantUpdateNew, function (req, res) {
  411. let payload = req.body;
  412. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  413. if (!testing.pass) {
  414. throw testing.message;
  415. }
  416. let participants = new Participants();
  417. ControllerUtil.regModelEventHandler(participants, res);
  418. participants.updateSessionUser0(payload.user_id, payload.old_user_id, payload.session_id);
  419. });
  420. /**
  421. * 移除成员
  422. * user:移除的人员
  423. * sessionId 会话ID
  424. */
  425. router.delete(APIv2.Sessions.Participant, function (req, res) {
  426. let user = req.params.participant_id;
  427. if (!user) {
  428. throw {httpStatus: 406, message: 'Missing user.'};
  429. }
  430. let sessionId = req.params.session_id;
  431. if (!sessionId) {
  432. throw {httpStatus: 406, message: 'Missing sessionId.'};
  433. }
  434. let participants = new Participants();
  435. ControllerUtil.regModelEventHandler(participants, res);
  436. participants.removeUser(sessionId, user);
  437. });
  438. /**
  439. * 发送消息
  440. *
  441. * 请求URL:
  442. * /sessions/:session_id/messages
  443. *
  444. * 消息体:
  445. * {
  446. * sender_id: bea851fc,
  447. * sender_name: 李毅,
  448. * content_type: 1,
  449. * content: 李医生,你好
  450. * }
  451. */
  452. router.post(APIv2.Sessions.Messages, function (req, res) {
  453. let payload = req.body;
  454. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  455. if (!testing.pass) {
  456. throw {httpStatus: 406, message: testing.message}
  457. }
  458. if(payload.view&&payload.view==1){
  459. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  460. }
  461. // 消息的发送时间由服务端决定
  462. payload.timestamp = new Date((new Date().getTime()));
  463. let sessions = new Sessions();
  464. ControllerUtil.regModelEventHandler(sessions, res);
  465. log.info("入参"+payload);
  466. sessions.saveMessageBySession(req.params.session_id, payload);
  467. });
  468. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  469. let payload = req.body;
  470. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  471. if (!testing.pass) {
  472. throw {httpStatus: 406, message: testing.message}
  473. }
  474. if(payload.view&&payload.view==1){
  475. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  476. }
  477. let sessions = new Sessions();
  478. ControllerUtil.regModelEventHandler(sessions, res);
  479. payload.timestamp = new Date((new Date().getTime()));
  480. sessions.sendTopicMessages(req.params.topic_id, payload);
  481. })
  482. /**
  483. * 获取消息
  484. *
  485. * 请求URL:
  486. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  487. */
  488. router.get(APIv2.Sessions.Messages, function (req, res) {
  489. let startMsgId = req.query.start_msg_id;
  490. let endMsgId = req.query.end_msg_id;
  491. let pagesize = req.query.pagesize;
  492. let page = req.query.page;
  493. let user = req.query.user;
  494. let sessionId = req.query.session_id;
  495. let content_type = req.query.content_type;
  496. let isoffset = req.query.isoffset;
  497. if (!user) {
  498. throw {httpStatus: 406, message: 'Missing user.'};
  499. }
  500. if (!sessionId) {
  501. throw {httpStatus: 406, message: 'Missing session_id.'};
  502. }
  503. if (!pagesize) {
  504. pagesize = 20;
  505. }
  506. if (!page) {
  507. page = 1;
  508. }
  509. if (content_type) {
  510. let messages = new Messages();
  511. ControllerUtil.regModelEventHandler(messages, res);
  512. messages.getMessageByType(sessionId, page, pagesize, content_type)
  513. } else {
  514. let sessions = new Sessions();
  515. ControllerUtil.regModelEventHandler(sessions, res);
  516. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  517. }
  518. });
  519. /**
  520. * 修改单条消息
  521. * session_id 会话ID
  522. * message_id:消息ID
  523. * content 消息内容
  524. */
  525. router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
  526. let payload = req.body;
  527. let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
  528. if (!testing.pass) {
  529. throw testing.message;
  530. }
  531. let messages = new Messages();
  532. ControllerUtil.regModelEventHandler(messages, res);
  533. messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
  534. });
  535. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  536. let sessionId = req.params.session_id;
  537. let userId = req.query.user_id;
  538. let sessions = new Sessions();
  539. ControllerUtil.regModelEventHandler(sessions, res);
  540. sessions.getSessionUnreadMessages(sessionId, userId);
  541. });
  542. /**
  543. * 获取所有会话未读消息数。
  544. *
  545. * 请求URL:/sessions/unread_message_count?user_id=xyz&type=1
  546. */
  547. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  548. let userId = req.query.user_id;
  549. let type = req.query.type;
  550. let sessions = new Sessions();
  551. ControllerUtil.regModelEventHandler(sessions, res);
  552. sessions.getAllSessionsUnreadMessageCount(userId,type);
  553. });
  554. /**
  555. * 获取指定会话未读消息数
  556. *
  557. * 请求URL
  558. * /sessions/:session_id/unread_message_count?user_id=xyz
  559. */
  560. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  561. let sessionId = req.params.session_id;
  562. let userId = req.query.user_id;
  563. let sessions = new Sessions();
  564. ControllerUtil.regModelEventHandler(sessions, res);
  565. sessions.getSessionUnreadMessageCount(sessionId, userId);
  566. });
  567. module.exports = router;