im.client.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // 服务器
  2. var publish_version = true;
  3. // 本地临时缓存Key
  4. var LocalStorageKey = {
  5. userId: "im_userid"
  6. };
  7. var imServer = "",
  8. socketUrl = "";
  9. var imTimeOut = 20000;
  10. if(publish_version){
  11. imServer = "http://125.73.45.32:3000/api/v2";
  12. socketUrl = "http://125.73.45.32:3000";
  13. }else{
  14. imServer = "http://172.19.103.72:3000/api/v2";
  15. socketUrl = "http://172.19.103.72:3000";
  16. }
  17. /**
  18. * IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
  19. */
  20. // Node.js模拟jQuery及ajax请求所需要的环境:document及XMLHttpRequest。
  21. // 这些环境仅用于模拟,客户端在使用时候不需要真正引入
  22. if (typeof process !== 'undefined') {
  23. module.exports = imClient;
  24. var jsdom = require('jsdom').jsdom;
  25. var document = jsdom('<html></html>', {});
  26. var window = document.defaultView;
  27. var jQuery = require('jquery');
  28. var $ = jQuery(window);
  29. $.support.cors = true;
  30. XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
  31. $.ajaxSettings.xhr = function () {
  32. return new XMLHttpRequest();
  33. };
  34. }
  35. // 资源实体在URL中的占位符
  36. var UserPath = ":user_id";
  37. var SessionPath = ":session_id";
  38. var TopicPath = ":topic_id";
  39. var ParticipantPath = ":participant_id";
  40. // REST API
  41. var ENDPOINTS = {
  42. Application: {
  43. BadgeNo: '/application/badge_no'
  44. },
  45. Management: {
  46. Health: '/management/health',
  47. DbStatus: '/management/db'
  48. },
  49. Users: {
  50. Login: '/users/login',
  51. Logout: '/users/logout',
  52. User: '/users/:user_id',
  53. UserStatus: '/users/:user_id/status'
  54. },
  55. Sessions: {
  56. Sessions: '/sessions',
  57. getSessionsInfo: '/sessions/:session_id/session',
  58. Session: '/sessions/:session_id',
  59. SessionSticky: '/sessions/:session_id/sticky',
  60. RecentSessions: '/sessions/recent',
  61. Topics: '/sessions/:session_id/topics',
  62. Topic: '/sessions/:session_id/topics/:topic_id',
  63. TopicEnded: '/sessions/:session_id/topics/:topic_id/ended',
  64. Messages: '/sessions/:session_id/messages',
  65. MessagesByTopic: '/sessions/:session_id/topics/:topic_id/messages',
  66. Message: '/sessions/:session_id/messages/:message_id',
  67. SessionsUnreadMessageCount: '/sessions/unread_message_count',
  68. SessionUnreadMessageCount: '/sessions/:session_id/unread_message_count',
  69. SessionUnreadMessages: '/sessions/:session_id/messages/unread',
  70. Participants: '/sessions/:session_id/participants',
  71. ParticipantsAvatar: '/sessions/:session_id/participants/avatars',
  72. Participant: '/sessions/:session_id/participants/:participant_id',
  73. ParticipantAvatar: '/session/:session_id/participants/:participant_id/avatars'
  74. },
  75. Search: {
  76. Search: "/search"
  77. },
  78. Consult :{
  79. getHealthConsult: '/sessions/topics',
  80. updateConsultStatus: '/sessions/:session_id/topics'
  81. }
  82. };
  83. var httpClient = {
  84. get: function (endpoint, data, successCallback, errorCallback) {
  85. $.ajax({
  86. type: "get",
  87. url: imServer + endpoint,
  88. data: data,
  89. async: true,
  90. dataType: "json",
  91. timeout: imTimeOut,
  92. success: function (data) {
  93. successCallback?successCallback(data):"";
  94. },
  95. error: function (xhr, status, error) {
  96. errorCallback ? errorCallback(xhr, status, error) : "";
  97. }
  98. });
  99. },
  100. post: function (endpoint, data, successCallback, errorCallback) {
  101. $.ajax({
  102. type: "post",
  103. url: imServer + endpoint,
  104. data: data,
  105. async: true,
  106. dataType: "json",
  107. timeout: imTimeOut,
  108. success: function (data) {
  109. successCallback?successCallback(data):"";
  110. },
  111. error: function (xhr, status, error) {
  112. errorCallback?errorCallback(xhr, status, error):"";
  113. }
  114. });
  115. },
  116. put: function (endpoint, data, successCallback, errorCallback) {
  117. $.ajax({
  118. type: "post",
  119. url: imServer + endpoint,
  120. data: data,
  121. async: true,
  122. dataType: "json",
  123. timeout: imTimeOut,
  124. headers: {
  125. "X-HTTP-Method-Override": "PUT"
  126. },
  127. success: function (data) {
  128. successCallback? successCallback(data) : "";
  129. },
  130. error: function (xhr, status, error) {
  131. errorCallback? errorCallback(xhr, status, error): "";
  132. }
  133. });
  134. },
  135. delete: function (endpoint, data, successCallback, errorCallback) {
  136. $.ajax({
  137. type: "get",
  138. url: imServer + endpoint,
  139. data: data,
  140. async: true,
  141. dataType: "json",
  142. timeout: imTimeOut,
  143. headers: {
  144. "X-HTTP-Method-Override": "DELETE"
  145. },
  146. success: function (data) {
  147. successCallback?successCallback(data): "";
  148. },
  149. error: function (xhr, status, error) {
  150. errorCallback? errorCallback(xhr, status, error) : "";
  151. }
  152. });
  153. },
  154. // 执行业务接口前,调用此函数判断当前用户是否在线。
  155. isOnline: function (callback, failure) {
  156. httpClient.get(ENDPOINTS.Users.UserStatus, {}, null, function (res) {
  157. if (res.status == 200) {
  158. callback();
  159. } else {
  160. failure();
  161. }
  162. });
  163. }
  164. };
  165. var imClient = {
  166. Application: {
  167. // 获取应用角标数
  168. getBadgeNo: function (userId, success, failure) {
  169. httpClient.get(ENDPOINTS.Application.BadgeNo,
  170. {user_id: userId},
  171. success,
  172. failure);
  173. }
  174. },
  175. Management: {
  176. getDbStatus: function (success, failure) {
  177. httpClient.get(ENDPOINTS.Management.DbStatus,
  178. {},
  179. success,
  180. failure);
  181. }
  182. },
  183. Users: {
  184. // 登录
  185. login: function (userId, token, client_id, platform, success, failure) {
  186. if (typeof plus !== 'undefined') plus.storage.setItem(LocalStorageKey.userId, userId);
  187. httpClient.post(ENDPOINTS.Users.Login,
  188. {user_id: userId, token: token, client_id: client_id, platform: platform},
  189. success,
  190. failure);
  191. },
  192. // 退出
  193. logout: function (userId, success, failure) {
  194. if (typeof plus !== 'undefined') plus.storage.removeItem(LocalStorageKey.userId);
  195. httpClient.delete(ENDPOINTS.Users.Logout,
  196. {user_id: userId},
  197. success,
  198. failure);
  199. },
  200. // 更新用户状态
  201. updateStatus: function (userId, appInBg, success, failure) {
  202. httpClient.put(ENDPOINTS.Users.UserStatus.replace(UserPath, userId),
  203. {app_in_bg: appInBg},
  204. success,
  205. failure);
  206. },
  207. },
  208. Sessions: {
  209. // 创建MUC会话
  210. createMucSession: function (userId, peerId, sessionName, success, failure) {
  211. var p = {};
  212. p[userId] = 0;
  213. p[peerId] = 0;
  214. httpClient.post(ENDPOINTS.Sessions.Sessions,
  215. {session_id: userId, session_type: 1, session_name: sessionName,
  216. participants: JSON.stringify(p)},
  217. success,
  218. failure);
  219. },
  220. // 创建P2P会话
  221. createP2pSession: function (userId, peerId, sessionName, success, failure) {
  222. var p = {};
  223. p[userId] = 0;
  224. p[peerId] = 0;
  225. httpClient.post(ENDPOINTS.Sessions.Sessions,
  226. {session_type: 2, session_name: sessionName, participants: JSON.stringify(p) },
  227. success,
  228. failure);
  229. },
  230. createGroupSession: function (teamId, sessionName, participants, success, failure) {
  231. var p = {};
  232. for(var k in participants){
  233. p[k] = 0;
  234. }
  235. httpClient.post(ENDPOINTS.Sessions.Sessions,
  236. {session_id: teamId, session_type: 3, session_name: sessionName, participants: JSON.stringify(p)},
  237. success,
  238. failure);
  239. },
  240. // 获取与患者发生的会话,实际上这些是MUC会话
  241. getSessionsWithPatient: function (userId, page, size, status , success, failure) {
  242. httpClient.get(ENDPOINTS.Sessions.Sessions,
  243. {user_id: userId, business_type: "2", status: status, page: page, size: size},
  244. success,
  245. failure);
  246. },
  247. // 获取与医生相关的会话,实际上这些是P2P,群聊和讨论组
  248. getSessionsWithDoctor: function (userId, page, size, success, failure) {
  249. httpClient.get(ENDPOINTS.Sessions.Sessions,
  250. {user_id: userId, business_type: "1", page: page, size: size},
  251. success,
  252. failure);
  253. },
  254. // 获取最近7天内的会话列表
  255. getRecentSessions: function (userId, success, failure) {
  256. httpClient.get(ENDPOINTS.Sessions.RecentSessions,
  257. {user_id: userId, date_span: 7},
  258. success,
  259. failure);
  260. },
  261. // 获取与患者的最近会话
  262. getRecentSessionsWithPatient: function (userId, success, failure) {
  263. httpClient.get(ENDPOINTS.Sessions.RecentSessions,
  264. {session_type: 1, user_id: userId},
  265. success,
  266. failure);
  267. },
  268. // 获取与医生的最近会话,包括P2P、群聊与讨论组
  269. getRecentSessionsWithDoctor: function (userId, success, failure) {
  270. httpClient.get(ENDPOINTS.Sessions.RecentSessions,
  271. {session_type: "2, 3", userId: userId},
  272. success,
  273. failure);
  274. },
  275. // 置顶会话
  276. stickSession: function (userId, sessionId, success, failure) {
  277. httpClient.put(ENDPOINTS.Sessions.SessionSticky.replace(SessionPath, sessionId),
  278. {user_id: userId},
  279. success,
  280. failure);
  281. },
  282. // 取消会话置顶
  283. unstickSession: function (userId, sessionId, success, failure) {
  284. httpClient.put(ENDPOINTS.Sessions.SessionSticky.replace(SessionPath, sessionId),
  285. {user_id: userId},
  286. success,
  287. failure);
  288. },
  289. // 获取所有咨询
  290. getTopics: function (sessionId, success, failure) {
  291. httpClient.get(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
  292. {},
  293. success,
  294. failure);
  295. },
  296. // 创建咨询
  297. createTopic: function (sessionId, userId, success, failure) {
  298. httpClient.post(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
  299. {user_id: userId},
  300. success,
  301. failure);
  302. },
  303. // 结束咨询
  304. endTopic: function (sessionId, userId, success, failure) {
  305. httpClient.put(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
  306. {user_id: userId},
  307. success,
  308. failure);
  309. },
  310. // 咨询是否已结束
  311. isTopicEnded: function (sessionId, topicId, success, failure) {
  312. httpClient.get(ENDPOINTS.Sessions.TopicEnded.replace(SessionPath, sessionId).replace(TopicPath, topicId),
  313. {session_id: sessionId, topic_id: topicId},
  314. success,
  315. failure);
  316. },
  317. // 发送消息,不论是何种会话,均不需要指定会话对象是谁,只需要向会话本身发送消息即可
  318. sendMessage: function (sessionId, userId, userName, content, contentType, success, failure) {
  319. var userAgent = plus.storage.getItem("userAgent");
  320. var view = 0; //记录观察者模式
  321. if(JSON.parse(userAgent).observer == "1"){
  322. view = 1;
  323. }
  324. httpClient.post(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId),
  325. {sender_id: userId, sender_name: userName, content_type: contentType, content: content, view: view},
  326. success,
  327. failure);
  328. },
  329. // 按会话获取消息
  330. getMessagesBySession: function (sessionId, userId, page, pagesize, startMsgId, endMsgId, success, failure, content_type, isoffset) {
  331. httpClient.get(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId),
  332. {
  333. page: page || 1,
  334. pagesize: pagesize || 20,
  335. end_msg_id: endMsgId || "",
  336. start_msg_id: startMsgId || "",
  337. user: userId,
  338. session_id :sessionId,
  339. content_type: content_type || "",
  340. isoffset: isoffset || ""
  341. },
  342. success,
  343. failure);
  344. },
  345. // 按议题获取消息
  346. getMessagesByTopic: function (sessionId, topicId, startMessageId, count, success, failure) {
  347. httpClient.get(ENDPOINTS.Sessions.MessagesByTopic.replace(SessionPath, sessionId),
  348. {start_message_id: startMessageId, count: count},
  349. success,
  350. failure);
  351. },
  352. // 获取所有会话的未读消息数
  353. getAllSessionUnreadMessageCount: function (userId, success, failure) {
  354. httpClient.get(ENDPOINTS.Sessions.SessionsUnreadMessageCount,
  355. {user_id: userId},
  356. success,
  357. failure);
  358. },
  359. // 获取指定会话的未读消息数
  360. getSessionUnreadMessageCount: function (sessionId, userId, success, failure) {
  361. httpClient.get(ENDPOINTS.Sessions.SessionUnreadMessageCount.replace(SessionPath, sessionId),
  362. {user_id: userId},
  363. success,
  364. failure);
  365. },
  366. // 获取指定会话的未读消息
  367. getSessionUnreadMessages: function (sessionId, userId, success, failure) {
  368. httpClient.get(ENDPOINTS.Sessions.SessionUnreadMessages.replace(SessionPath, sessionId),
  369. {user_id: userId},
  370. success,
  371. failure);
  372. },
  373. // 添加会话成员
  374. addParticipant: function (sessionId, participantId, success, failure) {
  375. httpClient.put(ENDPOINTS.Sessions.Participant.replace(SessionPath, sessionId).replace(ParticipantPath, participantId),
  376. {participant_id: participantId},
  377. success,
  378. failure);
  379. },
  380. // 获取会话成员列表
  381. getParticipants: function (sessionId, success, failure) {
  382. httpClient.get(ENDPOINTS.Sessions.Participants.replace(SessionPath, sessionId),
  383. {},
  384. success,
  385. failure);
  386. },
  387. // 移除会话成员
  388. removeParticipant: function (sessionId, participantId, success, failure) {
  389. httpClient.delete(ENDPOINTS.Sessions.Participant.replace(SessionPath, sessionId).replace(ParticipantPath, participantId),
  390. {},
  391. success,
  392. failure);
  393. },
  394. // 获取会话成员头像
  395. getParticipantAvatar: function (sessionId, participantId, success, failure) {
  396. httpClient.get(ENDPOINTS.Sessions.ParticipantAvatar.replace(SessionPath, sessionId).replace(ParticipantPath, participantId),
  397. {},
  398. success,
  399. failure);
  400. },
  401. // 获取会话成员头像
  402. getParticipantsAvatars: function (sessionId, success, failure) {
  403. httpClient.get(ENDPOINTS.Sessions.ParticipantsAvatar.replace(SessionPath, sessionId),
  404. {},
  405. success,
  406. failure);
  407. },
  408. //根据session_id获取会话信息
  409. getSessionsInfo:function(sessionId,userId,success, failure){
  410. httpClient.get(ENDPOINTS.Sessions.getSessionsInfo.replace(SessionPath, sessionId),
  411. {user_id:userId},
  412. success,
  413. failure);
  414. }
  415. },
  416. Search: {
  417. Doctor: {
  418. // 搜索所有对象:医生,会话及消息
  419. searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) {
  420. httpClient.get(ENDPOINTS.Search.Search,
  421. {user_id: userId, target_role: 'doctor', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions},
  422. success,
  423. failure);
  424. },
  425. // 搜索医生
  426. searchDoctors: function (userId, keyword,excludeTopicEndedSessions, page, size, success, failure) {
  427. httpClient.get(ENDPOINTS.Search.Search,
  428. {user_id: userId, target_role: 'doctor', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
  429. success,
  430. failure);
  431. },
  432. // 搜索会话
  433. searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) {
  434. httpClient.get(ENDPOINTS.Search.Search,
  435. {user_id: userId, target_role: 'doctor', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
  436. success,
  437. failure);
  438. },
  439. // 搜索会话消息
  440. searchMessages: function (userId, keyword, page, size, success, failure) {
  441. httpClient.get(ENDPOINTS.Search.Search,
  442. {user_id: userId, target_role: 'doctor', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size},
  443. success,
  444. failure);
  445. }
  446. },
  447. Patient: {
  448. // 搜索所有对象:患者,会话及消息
  449. searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) {
  450. httpClient.get(ENDPOINTS.Search.Search,
  451. {user_id: userId, target_role: 'patient', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions},
  452. success,
  453. failure);
  454. },
  455. // 搜索患者
  456. searchPatient: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) {
  457. httpClient.get(ENDPOINTS.Search.Search,
  458. {user_id: userId, target_role: 'patient', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
  459. success,
  460. failure);
  461. },
  462. // 搜索会话
  463. searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) {
  464. httpClient.get(ENDPOINTS.Search.Search,
  465. {user_id: userId, target_role: 'patient', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
  466. success,
  467. failure);
  468. },
  469. // 搜索会话消息
  470. searchMessages: function (userId, keyword, page, size, success, failure) {
  471. httpClient.get(ENDPOINTS.Search.Search,
  472. {user_id: userId, target_role: 'patient', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size},
  473. success,
  474. failure);
  475. }
  476. }
  477. },
  478. Consult:{
  479. //获取健康咨询列表
  480. getHealthConsult : function(user, status, page, pagesize, success, failure){
  481. httpClient.get(ENDPOINTS.Consult.getHealthConsult,
  482. {user: user,status:status,page:page,pagesize:pagesize},
  483. success,
  484. failure);
  485. },
  486. //更新咨询状态
  487. updateConsultStatus : function(sessionId, id, status, success, failure){
  488. httpClient.put(ENDPOINTS.Consult.updateConsultStatus.replace(SessionPath, sessionId),
  489. {topic_id:id,data:JSON.stringify({status:status})},
  490. success,
  491. failure);
  492. }
  493. }
  494. };