im.client.js 20 KB

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