session.endpoint.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 videoconferencing = req.query.videoconferencing;
  140. if (!page) {
  141. throw {httpStatus: 406, message: 'Missing page.'};
  142. }
  143. if (!size) {
  144. throw {httpStatus: 406, message: 'Missing size.'};
  145. }
  146. if (!userId) {
  147. throw {httpStatus: 406, message: 'Missing user.'};
  148. }
  149. let sessions = new Sessions();
  150. ControllerUtil.regModelEventHandler(sessions, res);
  151. //判断是否是中山医院MDT的请求
  152. if(videoconferencing && videoconferencing == 1){
  153. sessions.conversionAarticipant(userId, (doctorid) => {
  154. //传入参数齐全走过滤方法
  155. if(status&&businessType&&doctorid&&size&&page){
  156. sessions.getUserStatusSessions(doctorid,status,businessType,page,size);
  157. }else{
  158. sessions.getUserSessions(doctorid, page, size, businessType);
  159. }
  160. })
  161. }else{
  162. //传入参数齐全走过滤方法
  163. if(status&&businessType&&userId&&size&&page){
  164. sessions.getUserStatusSessions(userId,status,businessType,page,size);
  165. }else{
  166. sessions.getUserSessions(userId, page, size, businessType);
  167. }
  168. }
  169. });
  170. /**
  171. * 按会话类型获取会话列表
  172. * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
  173. */
  174. router.get(APIv2.Sessions.SessionListByType, function (req, res) {
  175. let page = req.query.page;
  176. let size = req.query.size;
  177. let userId = req.query.user_id;
  178. let type = req.query.type;
  179. let status = req.query.status;
  180. if (!page) {
  181. throw {httpStatus: 406, message: 'Missing page.'};
  182. }
  183. if (!size) {
  184. throw {httpStatus: 406, message: 'Missing size.'};
  185. }
  186. if (!type) {
  187. throw {httpStatus: 406, message: 'Missing type.'};
  188. }
  189. if (!userId) {
  190. throw {httpStatus: 406, message: 'Missing user.'};
  191. }
  192. let sessions = new Sessions();
  193. ControllerUtil.regModelEventHandler(sessions, res);
  194. sessions.getUserSessionsByType(userId,type,page,size,status);
  195. });
  196. /**
  197. * 按会话类型获取会话总数
  198. * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
  199. */
  200. router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
  201. let userId = req.query.user_id;
  202. let type = req.query.type;
  203. let status = req.query.status;
  204. if (!type) {
  205. throw {httpStatus: 406, message: 'Missing type.'};
  206. }
  207. if (!userId) {
  208. throw {httpStatus: 406, message: 'Missing user.'};
  209. }
  210. let sessions = new Sessions();
  211. ControllerUtil.regModelEventHandler(sessions, res);
  212. sessions.getSessionCountByType(userId,type,status);
  213. });
  214. router.get(APIv2.Sessions.Session,function(req,res){
  215. let sessionId = req.params.session_id;
  216. let userId = req.query.user_id;
  217. if (!sessionId) {
  218. throw {httpStatus: 406, message: 'Missing sessionId.'};
  219. }
  220. if (!userId) {
  221. throw {httpStatus: 406, message: 'Missing user.'};
  222. }
  223. let sessions = new Sessions();
  224. ControllerUtil.regModelEventHandler(sessions, res);
  225. sessions.getSession(sessionId,userId);
  226. });
  227. /**
  228. * 最近会话列表。
  229. *
  230. * URL:
  231. * /sessions/recent?user_id=abc&date_span=7
  232. */
  233. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  234. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  235. let userId = req.query.user_id;
  236. let dateSpan = req.query.date_span;
  237. let sessions = new Sessions();
  238. ControllerUtil.regModelEventHandler(sessions, res);
  239. sessions.getRecentSessions(userId, dateSpan);
  240. });
  241. /**
  242. * 判断会话是否存在。
  243. *
  244. * URL:
  245. * /sessions/isExist?session_id=abc
  246. */
  247. router.get(APIv2.Sessions.IsExist, function (req, res) {
  248. let sessionId = req.query.session_id;
  249. if (!sessionId) {
  250. throw {httpStatus: 406, message: 'Missing sessionId.'}
  251. }
  252. let sessions = new Sessions();
  253. ControllerUtil.regModelEventHandler(sessions, res);
  254. sessions.isExist(sessionId);
  255. });
  256. /**
  257. * 某个聊天记录置顶操作
  258. *
  259. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  260. */
  261. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  262. let user = req.query.user;
  263. let sessionId = req.query.sessionId;
  264. if (!user) {
  265. throw {httpStatus: 406, message: 'Missing user.'};
  266. }
  267. if (!sessionId) {
  268. throw {httpStatus: 406, message: 'Missing sessionId.'};
  269. }
  270. let sessions = new Sessions();
  271. ControllerUtil.regModelEventHandler(sessions, res);
  272. sessions.stickSession(sessionId, user);
  273. });
  274. /**
  275. * 某个聊天记录置顶操作
  276. *
  277. * 请求URL /status?status=1&sessionId=testsessionmsg1
  278. */
  279. router.post(APIv2.Sessions.SessionStatus, function (req, res) {
  280. let sessionId = req.query.sessionId;
  281. let status = req.query.status;
  282. if (!sessionId) {
  283. throw {httpStatus: 406, message: 'Missing sessionId.'};
  284. }
  285. if (!status) {
  286. throw {httpStatus: 406, message: 'Missing status.'};
  287. }
  288. let sessions = new Sessions();
  289. ControllerUtil.regModelEventHandler(sessions, res);
  290. sessions.updateSessionStatus(sessionId, status);
  291. });
  292. /**
  293. * 修改会话名称(修改redis和数据库)
  294. *
  295. * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
  296. */
  297. router.post(APIv2.Sessions.SessionName, function (req, res) {
  298. let sessionId = req.body.sessionId;
  299. let name = req.body.name;
  300. if (!sessionId) {
  301. throw {httpStatus: 406, message: 'Missing sessionId.'};
  302. }
  303. if (!name) {
  304. throw {httpStatus: 406, message: 'Missing name.'};
  305. }
  306. let sessions = new Sessions();
  307. ControllerUtil.regModelEventHandler(sessions, res);
  308. sessions.updateSessionName(sessionId, name);
  309. });
  310. /**
  311. * 取消置顶
  312. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  313. */
  314. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  315. let user = req.query.user;
  316. let sessionId = req.query.sessionId;
  317. if (!user) {
  318. throw {httpStatus: 406, message: 'Missing user.'};
  319. }
  320. if (!sessionId) {
  321. throw {httpStatus: 406, message: 'Missing sessionId.'};
  322. }
  323. let sessions = new Sessions();
  324. ControllerUtil.regModelEventHandler(sessions, res);
  325. sessions.cancelStickSession(sessionId, user);
  326. });
  327. /**
  328. * 获取会话成员。
  329. */
  330. router.get(APIv2.Sessions.Participants, function (req, res) {
  331. let sessionId = req.params.session_id;
  332. let participants = new Participants();
  333. ControllerUtil.regModelEventHandler(participants, res);
  334. participants.getParticipants(sessionId);
  335. });
  336. /**
  337. * 获取会话成员头像列表。
  338. */
  339. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  340. let sessionId = req.params.session_id;
  341. let participants = new Participants();
  342. ControllerUtil.regModelEventHandler(participants, res);
  343. participants.getParticipantsAvatar(sessionId);
  344. });
  345. /**
  346. * 增加成员
  347. * user:增加的人员
  348. * sessionId 会话ID
  349. */
  350. router.put(APIv2.Sessions.Participant, function (req, res) {
  351. let user = req.params.participant_id;
  352. if (!user) {
  353. throw {httpStatus: 406, message: 'Missing participant_id.'};
  354. }
  355. let sessionId = req.params.session_id;
  356. if (!sessionId) {
  357. throw {httpStatus: 406, message: 'Missing session_id.'};
  358. }
  359. let participants = new Participants();
  360. ControllerUtil.regModelEventHandler(participants, res);
  361. participants.addUser(sessionId, user);
  362. });
  363. /**
  364. * 增加成员
  365. * user_id:增加的人员
  366. * old_user_id:删除的人员
  367. * session_id 会话ID
  368. */
  369. router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
  370. let payload = req.body;
  371. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  372. if (!testing.pass) {
  373. throw testing.message;
  374. }
  375. let participants = new Participants();
  376. ControllerUtil.regModelEventHandler(participants, res);
  377. participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
  378. });
  379. /**
  380. * 移除成员
  381. * user:移除的人员
  382. * sessionId 会话ID
  383. */
  384. router.delete(APIv2.Sessions.Participant, function (req, res) {
  385. let user = req.params.participant_id;
  386. if (!user) {
  387. throw {httpStatus: 406, message: 'Missing user.'};
  388. }
  389. let sessionId = req.params.session_id;
  390. if (!sessionId) {
  391. throw {httpStatus: 406, message: 'Missing sessionId.'};
  392. }
  393. let participants = new Participants();
  394. ControllerUtil.regModelEventHandler(participants, res);
  395. participants.removeUser(sessionId, user);
  396. });
  397. /**
  398. * 发送消息
  399. *
  400. * 请求URL:
  401. * /sessions/:session_id/messages
  402. *
  403. * 消息体:
  404. * {
  405. * sender_id: bea851fc,
  406. * sender_name: 李毅,
  407. * content_type: 1,
  408. * content: 李医生,你好
  409. * }
  410. */
  411. router.post(APIv2.Sessions.Messages, function (req, res) {
  412. let payload = req.body;
  413. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  414. if (!testing.pass) {
  415. throw {httpStatus: 406, message: testing.message}
  416. }
  417. if(payload.view&&payload.view==1){
  418. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  419. }
  420. // 消息的发送时间由服务端决定
  421. payload.timestamp = new Date((new Date().getTime()));
  422. let sessions = new Sessions();
  423. ControllerUtil.regModelEventHandler(sessions, res);
  424. sessions.saveMessageBySession(req.params.session_id, payload);
  425. });
  426. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  427. let payload = req.body;
  428. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  429. if (!testing.pass) {
  430. throw {httpStatus: 406, message: testing.message}
  431. }
  432. if(payload.view&&payload.view==1){
  433. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  434. }
  435. let sessions = new Sessions();
  436. ControllerUtil.regModelEventHandler(sessions, res);
  437. payload.timestamp = new Date((new Date().getTime()));
  438. sessions.sendTopicMessages(req.params.topic_id, payload);
  439. })
  440. /**
  441. * 获取消息
  442. *
  443. * 请求URL:
  444. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  445. */
  446. router.get(APIv2.Sessions.Messages, function (req, res) {
  447. let startMsgId = req.query.start_msg_id;
  448. let endMsgId = req.query.end_msg_id;
  449. let pagesize = req.query.pagesize;
  450. let page = req.query.page;
  451. let user = req.query.user;
  452. let sessionId = req.query.session_id;
  453. let content_type = req.query.content_type;
  454. let isoffset = req.query.isoffset;
  455. if (!user) {
  456. throw {httpStatus: 406, message: 'Missing user.'};
  457. }
  458. if (!sessionId) {
  459. throw {httpStatus: 406, message: 'Missing session_id.'};
  460. }
  461. if (!pagesize) {
  462. pagesize = 20;
  463. }
  464. if (!page) {
  465. page = 1;
  466. }
  467. if (content_type) {
  468. let messages = new Messages();
  469. ControllerUtil.regModelEventHandler(messages, res);
  470. messages.getMessageByType(sessionId, page, pagesize, content_type)
  471. } else {
  472. let sessions = new Sessions();
  473. ControllerUtil.regModelEventHandler(sessions, res);
  474. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  475. }
  476. });
  477. /**
  478. * 修改单条消息
  479. * session_id 会话ID
  480. * message_id:消息ID
  481. * content 消息内容
  482. */
  483. router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
  484. let payload = req.body;
  485. let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
  486. if (!testing.pass) {
  487. throw testing.message;
  488. }
  489. let messages = new Messages();
  490. ControllerUtil.regModelEventHandler(messages, res);
  491. messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
  492. });
  493. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  494. let sessionId = req.params.session_id;
  495. let userId = req.query.user_id;
  496. let sessions = new Sessions();
  497. ControllerUtil.regModelEventHandler(sessions, res);
  498. sessions.getSessionUnreadMessages(sessionId, userId);
  499. });
  500. /**
  501. * 获取所有会话未读消息数。
  502. *
  503. * 请求URL:/sessions/unread_message_count?user_id=xyz&type=1
  504. */
  505. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  506. let userId = req.query.user_id;
  507. let type = req.query.type;
  508. let sessions = new Sessions();
  509. ControllerUtil.regModelEventHandler(sessions, res);
  510. sessions.getAllSessionsUnreadMessageCount(userId,type);
  511. });
  512. /**
  513. * 获取指定会话未读消息数
  514. *
  515. * 请求URL
  516. * /sessions/:session_id/unread_message_count?user_id=xyz
  517. */
  518. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  519. let sessionId = req.params.session_id;
  520. let userId = req.query.user_id;
  521. let sessions = new Sessions();
  522. ControllerUtil.regModelEventHandler(sessions, res);
  523. sessions.getSessionUnreadMessageCount(sessionId, userId);
  524. });
  525. module.exports = router;