session.endpoint.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. *MDT 医生数据转换,根据手机号查询医生CODE
  52. */
  53. if (payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  54. participants = sessions.conversionAarticipant(participants);
  55. }
  56. for (let j in participants) {
  57. log.info("aaaa:"+j + ":" + participants[j]);
  58. participantArray.push(j + ":" + participants[j]);
  59. }
  60. ControllerUtil.regModelEventHandler(sessions, res);
  61. console.log(sessionId, sessionName, sessionType, participantArray);
  62. sessions.createSession(sessionId, sessionName, sessionType, participantArray);
  63. ///是否视频会议的会话
  64. let videoconferencing = false;
  65. // participants = JSON.parse(participants);
  66. // for (let j in participants) {
  67. // participantArray.push(j + ":" + participants[j]);
  68. // }
  69. //视频会议的会话 szx add 20181121
  70. if (payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  71. let access_token = "";
  72. if(payload.hasOwnProperty("mobile"))access_token = payload.access_token;
  73. videoconferencing = true;
  74. //发送广播,给相关的与会人员
  75. for (let j in participants) {
  76. let userId = "pcim_"+j;
  77. let pc_doctorClient = clientCache.findById(userId);
  78. if(pc_doctorClient){
  79. log.info("videoconferencing send to "+userId);
  80. pc_doctorClient.socket.emit("startVideoconference",{"sessionId":sessionId,"user_id":j});
  81. }
  82. else{
  83. log.info("videoconferencing not find "+userId);
  84. }
  85. }
  86. }
  87. });
  88. /**
  89. * 获取会话列表
  90. *
  91. * 请求URL /sessions?user_id=3121&page=0&size=10
  92. */
  93. router.get("/", function (req, res) {
  94. let page = req.query.page;
  95. let size = req.query.size;
  96. let userId = req.query.user_id;
  97. let businessType = req.query.business_type;
  98. let status = req.query.status;
  99. if (!page) {
  100. throw {httpStatus: 406, message: 'Missing page.'};
  101. }
  102. if (!size) {
  103. throw {httpStatus: 406, message: 'Missing size.'};
  104. }
  105. if (!userId) {
  106. throw {httpStatus: 406, message: 'Missing user.'};
  107. }
  108. let sessions = new Sessions();
  109. ControllerUtil.regModelEventHandler(sessions, res);
  110. //传入参数齐全走过滤方法
  111. if(status&&businessType&&userId&&size&&page){
  112. sessions.getUserStatusSessions(userId,status,businessType,page,size);
  113. }else{
  114. sessions.getUserSessions(userId, page, size, businessType);
  115. }
  116. });
  117. /**
  118. * 按会话类型获取会话列表
  119. * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
  120. */
  121. router.get(APIv2.Sessions.SessionListByType, function (req, res) {
  122. let page = req.query.page;
  123. let size = req.query.size;
  124. let userId = req.query.user_id;
  125. let type = req.query.type;
  126. let status = req.query.status;
  127. if (!page) {
  128. throw {httpStatus: 406, message: 'Missing page.'};
  129. }
  130. if (!size) {
  131. throw {httpStatus: 406, message: 'Missing size.'};
  132. }
  133. if (!type) {
  134. throw {httpStatus: 406, message: 'Missing type.'};
  135. }
  136. if (!userId) {
  137. throw {httpStatus: 406, message: 'Missing user.'};
  138. }
  139. let sessions = new Sessions();
  140. ControllerUtil.regModelEventHandler(sessions, res);
  141. sessions.getUserSessionsByType(userId,type,page,size,status);
  142. });
  143. /**
  144. * 按会话类型获取会话总数
  145. * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
  146. */
  147. router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
  148. let userId = req.query.user_id;
  149. let type = req.query.type;
  150. let status = req.query.status;
  151. if (!type) {
  152. throw {httpStatus: 406, message: 'Missing type.'};
  153. }
  154. if (!userId) {
  155. throw {httpStatus: 406, message: 'Missing user.'};
  156. }
  157. let sessions = new Sessions();
  158. ControllerUtil.regModelEventHandler(sessions, res);
  159. sessions.getSessionCountByType(userId,type,status);
  160. });
  161. router.get(APIv2.Sessions.Session,function(req,res){
  162. let sessionId = req.params.session_id;
  163. let userId = req.query.user_id;
  164. if (!sessionId) {
  165. throw {httpStatus: 406, message: 'Missing sessionId.'};
  166. }
  167. if (!userId) {
  168. throw {httpStatus: 406, message: 'Missing user.'};
  169. }
  170. let sessions = new Sessions();
  171. ControllerUtil.regModelEventHandler(sessions, res);
  172. sessions.getSession(sessionId,userId);
  173. });
  174. /**
  175. * 最近会话列表。
  176. *
  177. * URL:
  178. * /sessions/recent?user_id=abc&date_span=7
  179. */
  180. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  181. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  182. let userId = req.query.user_id;
  183. let dateSpan = req.query.date_span;
  184. let sessions = new Sessions();
  185. ControllerUtil.regModelEventHandler(sessions, res);
  186. sessions.getRecentSessions(userId, dateSpan);
  187. });
  188. /**
  189. * 判断会话是否存在。
  190. *
  191. * URL:
  192. * /sessions/isExist?session_id=abc
  193. */
  194. router.get(APIv2.Sessions.IsExist, function (req, res) {
  195. let sessionId = req.query.session_id;
  196. if (!sessionId) {
  197. throw {httpStatus: 406, message: 'Missing sessionId.'}
  198. }
  199. let sessions = new Sessions();
  200. ControllerUtil.regModelEventHandler(sessions, res);
  201. sessions.isExist(sessionId);
  202. });
  203. /**
  204. * 某个聊天记录置顶操作
  205. *
  206. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  207. */
  208. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  209. let user = req.query.user;
  210. let sessionId = req.query.sessionId;
  211. if (!user) {
  212. throw {httpStatus: 406, message: 'Missing user.'};
  213. }
  214. if (!sessionId) {
  215. throw {httpStatus: 406, message: 'Missing sessionId.'};
  216. }
  217. let sessions = new Sessions();
  218. ControllerUtil.regModelEventHandler(sessions, res);
  219. sessions.stickSession(sessionId, user);
  220. });
  221. /**
  222. * 某个聊天记录置顶操作
  223. *
  224. * 请求URL /status?status=1&sessionId=testsessionmsg1
  225. */
  226. router.post(APIv2.Sessions.SessionStatus, function (req, res) {
  227. let sessionId = req.query.sessionId;
  228. let status = req.query.status;
  229. if (!sessionId) {
  230. throw {httpStatus: 406, message: 'Missing sessionId.'};
  231. }
  232. if (!status) {
  233. throw {httpStatus: 406, message: 'Missing status.'};
  234. }
  235. let sessions = new Sessions();
  236. ControllerUtil.regModelEventHandler(sessions, res);
  237. sessions.updateSessionStatus(sessionId, status);
  238. });
  239. /**
  240. * 修改会话名称(修改redis和数据库)
  241. *
  242. * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
  243. */
  244. router.post(APIv2.Sessions.SessionName, function (req, res) {
  245. let sessionId = req.body.sessionId;
  246. let name = req.body.name;
  247. if (!sessionId) {
  248. throw {httpStatus: 406, message: 'Missing sessionId.'};
  249. }
  250. if (!name) {
  251. throw {httpStatus: 406, message: 'Missing name.'};
  252. }
  253. let sessions = new Sessions();
  254. ControllerUtil.regModelEventHandler(sessions, res);
  255. sessions.updateSessionName(sessionId, name);
  256. });
  257. /**
  258. * 取消置顶
  259. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  260. */
  261. router.delete(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.cancelStickSession(sessionId, user);
  273. });
  274. /**
  275. * 获取会话成员。
  276. */
  277. router.get(APIv2.Sessions.Participants, function (req, res) {
  278. let sessionId = req.params.session_id;
  279. let participants = new Participants();
  280. ControllerUtil.regModelEventHandler(participants, res);
  281. participants.getParticipants(sessionId);
  282. });
  283. /**
  284. * 获取会话成员头像列表。
  285. */
  286. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  287. let sessionId = req.params.session_id;
  288. let participants = new Participants();
  289. ControllerUtil.regModelEventHandler(participants, res);
  290. participants.getParticipantsAvatar(sessionId);
  291. });
  292. /**
  293. * 增加成员
  294. * user:增加的人员
  295. * sessionId 会话ID
  296. */
  297. router.put(APIv2.Sessions.Participant, function (req, res) {
  298. let user = req.params.participant_id;
  299. if (!user) {
  300. throw {httpStatus: 406, message: 'Missing participant_id.'};
  301. }
  302. let sessionId = req.params.session_id;
  303. if (!sessionId) {
  304. throw {httpStatus: 406, message: 'Missing session_id.'};
  305. }
  306. let participants = new Participants();
  307. ControllerUtil.regModelEventHandler(participants, res);
  308. participants.addUser(sessionId, user);
  309. });
  310. /**
  311. * 增加成员
  312. * user_id:增加的人员
  313. * old_user_id:删除的人员
  314. * session_id 会话ID
  315. */
  316. router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
  317. let payload = req.body;
  318. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  319. if (!testing.pass) {
  320. throw testing.message;
  321. }
  322. let participants = new Participants();
  323. ControllerUtil.regModelEventHandler(participants, res);
  324. participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
  325. });
  326. /**
  327. * 移除成员
  328. * user:移除的人员
  329. * sessionId 会话ID
  330. */
  331. router.delete(APIv2.Sessions.Participant, function (req, res) {
  332. let user = req.params.participant_id;
  333. if (!user) {
  334. throw {httpStatus: 406, message: 'Missing user.'};
  335. }
  336. let sessionId = req.params.session_id;
  337. if (!sessionId) {
  338. throw {httpStatus: 406, message: 'Missing sessionId.'};
  339. }
  340. let participants = new Participants();
  341. ControllerUtil.regModelEventHandler(participants, res);
  342. participants.removeUser(sessionId, user);
  343. });
  344. /**
  345. * 发送消息
  346. *
  347. * 请求URL:
  348. * /sessions/:session_id/messages
  349. *
  350. * 消息体:
  351. * {
  352. * sender_id: bea851fc,
  353. * sender_name: 李毅,
  354. * content_type: 1,
  355. * content: 李医生,你好
  356. * }
  357. */
  358. router.post(APIv2.Sessions.Messages, function (req, res) {
  359. let payload = req.body;
  360. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  361. if (!testing.pass) {
  362. throw {httpStatus: 406, message: testing.message}
  363. }
  364. if(payload.view&&payload.view==1){
  365. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  366. }
  367. // 消息的发送时间由服务端决定
  368. payload.timestamp = new Date((new Date().getTime()));
  369. let sessions = new Sessions();
  370. ControllerUtil.regModelEventHandler(sessions, res);
  371. sessions.saveMessageBySession(req.params.session_id, payload);
  372. });
  373. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  374. let payload = req.body;
  375. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  376. if (!testing.pass) {
  377. throw {httpStatus: 406, message: testing.message}
  378. }
  379. if(payload.view&&payload.view==1){
  380. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  381. }
  382. let sessions = new Sessions();
  383. ControllerUtil.regModelEventHandler(sessions, res);
  384. payload.timestamp = new Date((new Date().getTime()));
  385. sessions.sendTopicMessages(req.params.topic_id, payload);
  386. })
  387. /**
  388. * 获取消息
  389. *
  390. * 请求URL:
  391. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  392. */
  393. router.get(APIv2.Sessions.Messages, function (req, res) {
  394. let startMsgId = req.query.start_msg_id;
  395. let endMsgId = req.query.end_msg_id;
  396. let pagesize = req.query.pagesize;
  397. let page = req.query.page;
  398. let user = req.query.user;
  399. let sessionId = req.query.session_id;
  400. let content_type = req.query.content_type;
  401. let isoffset = req.query.isoffset;
  402. if (!user) {
  403. throw {httpStatus: 406, message: 'Missing user.'};
  404. }
  405. if (!sessionId) {
  406. throw {httpStatus: 406, message: 'Missing session_id.'};
  407. }
  408. if (!pagesize) {
  409. pagesize = 20;
  410. }
  411. if (!page) {
  412. page = 1;
  413. }
  414. if (content_type) {
  415. let messages = new Messages();
  416. ControllerUtil.regModelEventHandler(messages, res);
  417. messages.getMessageByType(sessionId, page, pagesize, content_type)
  418. } else {
  419. let sessions = new Sessions();
  420. ControllerUtil.regModelEventHandler(sessions, res);
  421. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  422. }
  423. });
  424. /**
  425. * 修改单条消息
  426. * session_id 会话ID
  427. * message_id:消息ID
  428. * content 消息内容
  429. */
  430. router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
  431. let payload = req.body;
  432. let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
  433. if (!testing.pass) {
  434. throw testing.message;
  435. }
  436. let messages = new Messages();
  437. ControllerUtil.regModelEventHandler(messages, res);
  438. messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
  439. });
  440. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  441. let sessionId = req.params.session_id;
  442. let userId = req.query.user_id;
  443. let sessions = new Sessions();
  444. ControllerUtil.regModelEventHandler(sessions, res);
  445. sessions.getSessionUnreadMessages(sessionId, userId);
  446. });
  447. /**
  448. * 获取所有会话未读消息数。
  449. *
  450. * 请求URL:/sessions/unread_message_count?user_id=xyz&type=1
  451. */
  452. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  453. let userId = req.query.user_id;
  454. let type = req.query.type;
  455. let sessions = new Sessions();
  456. ControllerUtil.regModelEventHandler(sessions, res);
  457. sessions.getAllSessionsUnreadMessageCount(userId,type);
  458. });
  459. /**
  460. * 获取指定会话未读消息数
  461. *
  462. * 请求URL
  463. * /sessions/:session_id/unread_message_count?user_id=xyz
  464. */
  465. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  466. let sessionId = req.params.session_id;
  467. let userId = req.query.user_id;
  468. let sessions = new Sessions();
  469. ControllerUtil.regModelEventHandler(sessions, res);
  470. sessions.getSessionUnreadMessageCount(sessionId, userId);
  471. });
  472. module.exports = router;