im_new.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
  3. */
  4. var isProd = true; // 连接生产环境
  5. var isDemo = false; // 连接公司演示或开发测试环境
  6. // 服务器列表
  7. var servers = {
  8. dev: "http://192.168.131.109:3008",
  9. prod: "http://27.154.233.186:3030",
  10. demo: "http://ehr.yihu.com",
  11. //prod: "http://120.41.253.95:3000"
  12. // prod:"http://192.168.131.24:3000"
  13. };
  14. // REST接口相对路径
  15. var restPath = {
  16. login: "/api/v1/users/login",
  17. logout: "/api/v1/users/logout",
  18. status: "/api/v1/users/:user_id/status",
  19. sendGroupMsg: "/api/v1/chats/gm",
  20. getGroupMsg: "/api/v1/chats/gm",
  21. getGroupUnreadMsg: "/api/v1/chats/gm/unread",
  22. getGroupImgMsg: "/api/v1/chats/gm/images",
  23. sendOneByOne: "/api/v1/chats/pm",
  24. getOneByOneMsg: "/api/v1/chats/pm",
  25. getOneByOneUnreadMsg: "/api/v1/chats/pm/unread",
  26. getGroupStatistic: "/api/v1/chats/gm/statistic",
  27. getBadgeNumber: "/api/v1/application/badge_no",
  28. getRecentContacts: "/api/v1/chats/recent",
  29. getDDoctorImInfo: "/api/v1/chats/list/doctor",//搜索消息页的医生im列表
  30. getDPatientImInfo: "/api/v1/chats/list/patient",//搜索消息页的居民im列表
  31. getGroupsMemberAvatars: "/api/v1/groups/member/avatars",//搜索讨论组的组员头像
  32. getSearchPatient: "/api/v1/chats/search/patient",//搜索消息页居民im-搜索接口
  33. getSearchDoctor: "/api/v1/chats/search/doctor",//搜索消息页的居民im-搜索接口
  34. getSearchZKList:"/api/v1/chats/list",//搜索专科医生的Im列表
  35. getSearchPatientList: "/api/v1/chats/search/patient/list",//搜索居民im的详情列表
  36. getSearchPatientAll: "/api/v1/chats/search/patient/all",//搜索居民im的查看更多列表( type参数:1、医生和患者 2、聊天记录 3、群聊)
  37. getSearchDoctorList: "/api/v1/chats/search/doctor/content/list",//搜索医生im的详情列表
  38. getSearchDoctorAll: "/api/v1/chats/search/doctor/list",//搜索医生im的查看更多列表(type参数: type = 1 查询聊过的医生; type = 2 查询群组标题和人员包含的群聊 ; type = 3 查询聊天关键字)
  39. getWeiDuMessageCount: "/api/v1/chats/msg/amount",//获取医生im和居民im的未读消息数
  40. consultFinish: "/api/v1/chats/pm/finished",
  41. getHealthConsult: "/api/v2/sessions/topics",
  42. updateConsultStatus :"/api/v2/sessions/sessionId/topics"
  43. };
  44. //api/v1/groups/member/avatars?groups
  45. var actualServer;
  46. if (isProd === true) {
  47. actualServer = servers.prod;
  48. } else if (isDemo === true) {
  49. actualServer = servers.demo;
  50. } else {
  51. actualServer = servers.dev;
  52. }
  53. // REST接口实际路径
  54. var endpoints = {
  55. login: actualServer + restPath.login,
  56. logout: actualServer + restPath.logout,
  57. updateStatus: actualServer + restPath.status,
  58. sendGroupMsg: actualServer + restPath.sendGroupMsg,
  59. getGroupMsg: actualServer + restPath.getGroupMsg,
  60. getGroupUnreadMsg: actualServer + restPath.getGroupUnreadMsg,
  61. getGroupImgMsg: actualServer + restPath.getGroupImgMsg,
  62. getGroupStatistic: actualServer + restPath.getGroupStatistic,
  63. getBadgeNumber: actualServer + restPath.getBadgeNumber,
  64. getPrivateMsg: actualServer + restPath.getOneByOneMsg,
  65. getPrivateUnreadMsg: actualServer + restPath.getOneByOneUnreadMsg,
  66. sendPrivateMsg: actualServer + restPath.sendOneByOne,
  67. getRecentContacts: actualServer + restPath.getRecentContacts,
  68. getDDoctorImInfo: actualServer + restPath.getDDoctorImInfo,
  69. getDPatientImInfo: actualServer + restPath.getDPatientImInfo,
  70. getGroupsMemberAvatars :actualServer + restPath.getGroupsMemberAvatars,
  71. getSearchPatient: actualServer + restPath.getSearchPatient,
  72. getSearchDoctor: actualServer + restPath.getSearchDoctor,
  73. getSearchZKList: actualServer + restPath.getSearchZKList,
  74. getSearchPatientList: actualServer + restPath.getSearchPatientList,
  75. getSearchPatientAll: actualServer + restPath.getSearchPatientAll,
  76. getSearchDoctorList: actualServer + restPath.getSearchDoctorList,
  77. getSearchDoctorAll: actualServer + restPath.getSearchDoctorAll,
  78. getWeiDuMessageCount: actualServer + restPath.getWeiDuMessageCount,
  79. consultFinish: actualServer + restPath.consultFinish,
  80. getHealthConsult: actualServer + restPath.getHealthConsult,
  81. updateConsultStatus: actualServer + restPath.updateConsultStatus
  82. };
  83. // 本地临时缓存Key
  84. var plusStorageKey = {
  85. userId: "im_userid"
  86. };
  87. var im = {
  88. // 登录
  89. login: function (userId, token, client_id, platform) {
  90. console.log("IM - Login: " + userId);
  91. console.log('endpoints.login'+endpoints.login);
  92. console.log(JSON.stringify({user_id: userId, token: token, client_id: client_id, platform: platform}));
  93. plus.storage.setItem(plusStorageKey.userId, userId);
  94. $.ajax({
  95. type: "get",
  96. url: endpoints.login,
  97. data: {user_id: userId, token: token, client_id: client_id, platform: platform},
  98. async: true,
  99. dataType: "json",
  100. success: function (data) {
  101. console.log(JSON.stringify(data));
  102. },
  103. error: function (msg) {
  104. console.log(JSON.stringify(msg));
  105. }
  106. });
  107. },
  108. // 退出
  109. logout: function (userId) {
  110. plus.storage.removeItem(plusStorageKey.userId);
  111. $.ajax({
  112. type: "get",
  113. url: endpoints.logout,
  114. data: {user_id: userId},
  115. async: true,
  116. dataType: "json",
  117. success: function (data) {
  118. console.log(JSON.stringify(data));
  119. },
  120. error: function (msg) {
  121. }
  122. });
  123. },
  124. // 更新用户在线状态
  125. updateStatus: function (userId, status) {
  126. //alert(endpoints.updateStatus.replace(':user_id', userId));
  127. if(userId == undefined || userId == "")return;
  128. $.ajax({
  129. type: "post",
  130. url: endpoints.updateStatus.replace(':user_id', userId),
  131. data: {user_id: userId, status: status},
  132. async: true,
  133. dataType: "json",
  134. success: function (data) {
  135. console.log("logout:"+JSON.stringify(data));
  136. },
  137. error: function (msg) {
  138. console.log("logout:"+JSON.stringify(msg));
  139. }
  140. });
  141. },
  142. // 发送群组消息
  143. sendGroupMsg: function (userId, groupId, content, type, handler, group_type, err) {
  144. function send() {
  145. $.ajax({
  146. type: "post",
  147. url: endpoints.sendGroupMsg,
  148. data: {at: "", from: userId, group: groupId, groupType: group_type, contentType: type, content: content},
  149. async: true,
  150. dataType: "json",
  151. success: function (data) {
  152. if(handler) handler(data);
  153. },
  154. error: function (msg) {
  155. console.log(JSON.stringify(msg));
  156. if(err)
  157. err(msg);
  158. else{
  159. mui.toast("发送失败")
  160. }
  161. }
  162. });
  163. }
  164. checkLogin(send);
  165. },
  166. // 获取组消息
  167. getGroupMsg: function (uid, groupId, start, count, handler, contentType, end) {
  168. var p = {user_id: uid, group_id: groupId, count: count}
  169. if(start>0)
  170. p.message_start_id = start;
  171. if(end)
  172. p.message_end_id = end;
  173. if(contentType)
  174. p.content_type = contentType;
  175. $.ajax({
  176. type: "get",
  177. url: endpoints.getGroupMsg,
  178. data: p,
  179. async: true,
  180. dataType: "json",
  181. success: function (data) {
  182. if(handler) handler(data);
  183. },
  184. error: function (msg) {
  185. console.log("getGroupMsg:"+ JSON.stringify(msg));
  186. mui.toast('获取消息失败');
  187. }
  188. });
  189. },
  190. // 获取组未读消息
  191. getGroupUnreadMsg: function (uid, groupId, handler) {
  192. $.ajax({
  193. type: "get",
  194. url: endpoints.getGroupUnreadMsg,
  195. data: {user_id: uid, group_id: groupId},
  196. async: true,
  197. dataType: "json",
  198. success: function (data) {
  199. if(handler) handler(data);
  200. },
  201. error: function (msg) {
  202. console.log("getGroupUnreadMsg:"+ JSON.stringify(msg));
  203. mui.toast("获取消息失败");
  204. }
  205. });
  206. },
  207. // 获取组图片消息
  208. getGroupImgMsg: function (uid, groupId, handler) {
  209. $.ajax({
  210. type: "get",
  211. url: endpoints.getGroupImgMsg,
  212. data: {user_id: uid, group_id: groupId},
  213. async: true,
  214. dataType: "json",
  215. success: function (data) {
  216. if(handler) handler(data);
  217. },
  218. error: function (msg) {
  219. console.log(JSON.stringify(msg));
  220. mui.toast("获取消息失败");
  221. }
  222. });
  223. },
  224. // 获取组消息数
  225. getGroupInfo: function (uid, gid, handler) {
  226. $.ajax({
  227. type: "get",
  228. url: endpoints.getGroupStatistic,
  229. data: {user_id: uid, group_id: gid},
  230. async: true,
  231. dataType: "json",
  232. success: function (data) {
  233. handler(data);
  234. },
  235. error: function (msg) {
  236. handler(msg);
  237. }
  238. });
  239. },
  240. // 发送私信
  241. sendPrivateMsg: function (from_uid, to_uid, content, type, success,error) {
  242. function send() {
  243. $.ajax({
  244. type: "post",
  245. url: endpoints.sendPrivateMsg,
  246. data: {from: from_uid, to: to_uid, content: content, contentType: type},
  247. async: true,
  248. dataType: "json",
  249. success: function (data) {
  250. if(success) success(data);
  251. },
  252. error: function (data) {
  253. console.log(JSON.stringify(data));
  254. if(error) error(data);
  255. mui.toast("发送失败")
  256. }
  257. });
  258. }
  259. checkLogin(send);
  260. },
  261. // 获取私信
  262. getPrivateMsg: function (uid, peer_uid, start, count, handler, type, end) {
  263. var p = {user_id: uid, peer_id: peer_uid, count: count}
  264. if(start>0)
  265. p.message_start_id = start;
  266. if(end)
  267. p.message_end_id = end;
  268. if(type)
  269. p.content_type = type;
  270. $.ajax({
  271. type: "get",
  272. url: endpoints.getPrivateMsg,
  273. data: p,
  274. async: true,
  275. dataType: "json",
  276. success: function (data) {
  277. if(handler) handler(data);
  278. },
  279. error: function (msg) {
  280. mui.toast("获取聊天信息失败")
  281. }
  282. });
  283. },
  284. getPrivateUnreadMsg: function (uid, peer_uid, handler) {
  285. $.ajax({
  286. type: "get",
  287. url: endpoints.getPrivateUnreadMsg,
  288. data: {user_id: uid, peer_id: peer_uid},
  289. async: true,
  290. dataType: "json",
  291. success: function (data) {
  292. if(handler) handler(data);
  293. },
  294. error: function (msg) {
  295. if(handler) handler(msg);
  296. }
  297. });
  298. },
  299. // 获取私信
  300. getPrivateMsgWithCallback: function (uid, peer_uid, start, count, handler) {
  301. $.ajax({
  302. type: "get",
  303. url: endpoints.getPrivateMsg,
  304. data: {user_id: uid, peer_id: peer_uid, message_start_id: start, count: count},
  305. async: true,
  306. dataType: "json",
  307. success: function (data) {
  308. handler(data);
  309. },
  310. error: function (msg) {
  311. handler(msg);
  312. }
  313. });
  314. },
  315. // 获取应用角标数
  316. getBadgeNumber: function (userId, handler) {
  317. $.ajax({
  318. type: "get",
  319. url: endpoints.getBadgeNumber,
  320. data: {user_id: userId},
  321. async: true,
  322. dataType: "json",
  323. success: function (data) {
  324. console.log(JSON.stringify(data));
  325. if (handler) handler(data);
  326. },
  327. error: function (msg) {
  328. console.log('error');
  329. if (handler) handler(msg);
  330. }
  331. });
  332. },
  333. // 获取最近联系人
  334. getRecentContacts: function(userId, days,success,error) {
  335. $.ajax({
  336. type: "get",
  337. url: endpoints.getRecentContacts,
  338. data: {user_id: userId,days: days || 7},
  339. async: true,
  340. dataType: "json",
  341. success: function (data) {
  342. console.log(JSON.stringify(data));
  343. if (success) success(data);
  344. },
  345. error: function (msg) {
  346. console.log('error');
  347. if (error) error(msg);
  348. }
  349. });
  350. },
  351. // 获取医生与居民的IM记录
  352. getDPatientInfo: function (userId, handler, errHandle) {
  353. $.ajax({
  354. type: "get",
  355. url: endpoints.getDPatientImInfo,
  356. data: {user_id: userId},
  357. async: true,
  358. dataType: "json",
  359. timeout: 5000,
  360. success: function (data) {
  361. console.log(JSON.stringify(data));
  362. if (handler) handler(data);
  363. },
  364. error: function (msg) {
  365. console.log('error');
  366. if (errHandle) errHandle(msg);
  367. }
  368. });
  369. },
  370. // 获取医生与医生的IM记录
  371. getDDoctorImInfo: function (userId, handler, errHandle) {
  372. $.ajax({
  373. type: "get",
  374. url: endpoints.getDDoctorImInfo,
  375. data: {user_id: userId},
  376. async: true,
  377. dataType: "json",
  378. timeout: 5000,
  379. success: function (data) {
  380. console.log(JSON.stringify(data));
  381. if (handler) handler(data);
  382. },
  383. error: function (msg) {
  384. console.log('error');
  385. if (errHandle) errHandle(msg);
  386. }
  387. });
  388. },
  389. getGroupsMemberAvatars: function(groups,handler, errHandle){
  390. $.ajax({
  391. type: "get",
  392. url: endpoints.getGroupsMemberAvatars,
  393. data: {groups: groups},
  394. async: true,
  395. dataType: "json",
  396. timeout: 5000,
  397. success: function (data) {
  398. console.log(JSON.stringify(data));
  399. if (handler) handler(data);
  400. },
  401. error: function (msg) {
  402. console.log('error');
  403. if (errHandle) errHandle(msg);
  404. }
  405. });
  406. },
  407. getSearchPatient: function(userId,userRole,keyWord,handler){
  408. $.ajax({
  409. type: "get",
  410. url: endpoints.getSearchPatient,
  411. data: {user_id: userId,user_role:userRole,keyword:keyWord},
  412. async: true,
  413. dataType: "json",
  414. success: function (data) {
  415. console.log(JSON.stringify(data));
  416. if (handler) handler(data);
  417. },
  418. error: function (msg) {
  419. console.log('error');
  420. if (handler) handler(msg);
  421. }
  422. });
  423. },
  424. getSearchDoctor: function(userId,keyWord,success,error){
  425. $.ajax({
  426. type: "get",
  427. url: endpoints.getSearchDoctor,
  428. data: {user_id: userId,keyword:keyWord},
  429. async: true,
  430. dataType: "json",
  431. success: function (data) {
  432. console.log(JSON.stringify(data));
  433. if (success) success(data);
  434. },
  435. error: function (msg) {
  436. console.log('error');
  437. if (error) error(msg);
  438. }
  439. });
  440. },
  441. consultFinish: function(user_id, peer_id, success, error){
  442. $.ajax({
  443. type: "get",
  444. url: endpoints.consultFinish,
  445. data: {doctor_id: user_id, patient_id:peer_id},
  446. async: true,
  447. dataType: "json",
  448. success: function (data) {
  449. if (success) success(data);
  450. },
  451. error: function (msg) {
  452. console.log(msg);
  453. if (error) error(msg);
  454. }
  455. });
  456. },
  457. getSearchZKList:function(userId,success,error){
  458. $.ajax({
  459. type: "get",
  460. url: endpoints.getSearchZKList,
  461. data: {user_id: userId},
  462. async: true,
  463. dataType: "json",
  464. success: function (data) {
  465. console.log(JSON.stringify(data));
  466. if (success) success(data);
  467. },
  468. error: function (msg) {
  469. console.log('error');
  470. if (error) error(msg);
  471. }
  472. });
  473. },
  474. getSearchPatientList: function(userId,keyWord,type,userRole,groupId,success,error){
  475. $.ajax({
  476. type: "get",
  477. url: endpoints.getSearchPatientList,
  478. data: {user_id: userId,keyword:keyWord,type:type,user_role:userRole,group_id:groupId},
  479. async: true,
  480. dataType: "json",
  481. success: function (data) {
  482. console.log(JSON.stringify(data));
  483. if (success) success(data);
  484. },
  485. error: function (msg) {
  486. console.log('error');
  487. if (error) error(msg);
  488. }
  489. });
  490. },
  491. getSearchPatientAll: function(userId,keyWord,type,userRole,success,error){
  492. $.ajax({
  493. type: "get",
  494. url: endpoints.getSearchPatientAll,
  495. data: {user_id: userId,keyword:keyWord,type:type,user_role:userRole},
  496. async: true,
  497. dataType: "json",
  498. success: function (data) {
  499. console.log(JSON.stringify(data));
  500. if (success) success(data);
  501. },
  502. error: function (msg) {
  503. console.log('error');
  504. if (error) error(msg);
  505. }
  506. });
  507. },
  508. getSearchDoctorList: function(userId,keyWord,type,groupCode,success,error){
  509. $.ajax({
  510. type: "get",
  511. url: endpoints.getSearchDoctorList,
  512. data: {user_id: userId,keyword:keyWord,type:type,groupcode:groupCode},
  513. async: true,
  514. dataType: "json",
  515. success: function (data) {
  516. console.log(JSON.stringify(data));
  517. if (success) success(data);
  518. },
  519. error: function (msg) {
  520. console.log('error');
  521. if (error) error(msg);
  522. }
  523. });
  524. },
  525. getSearchDoctorAll: function(userId,keyWord,type,success,error){
  526. $.ajax({
  527. type: "get",
  528. url: endpoints.getSearchDoctorAll,
  529. data: {user_id: userId,keyword:keyWord,type:type},
  530. async: true,
  531. dataType: "json",
  532. success: function (data) {
  533. console.log(JSON.stringify(data));
  534. if (success) success(data);
  535. },
  536. error: function (msg) {
  537. console.log('error');
  538. if (error) error(msg);
  539. }
  540. });
  541. },
  542. getWeiDuMessageCount :function(userId,success,error){
  543. $.ajax({
  544. type: "get",
  545. url: endpoints.getWeiDuMessageCount,
  546. data: {user_id: userId},
  547. async: true,
  548. dataType: "json",
  549. success: function (data) {
  550. console.log(JSON.stringify(data));
  551. if (success) success(data);
  552. },
  553. error: function (msg) {
  554. console.log('error');
  555. if (error) error(msg);
  556. }
  557. });
  558. },
  559. getHealthConsult :function(user,status,page,pagesize,success,error){
  560. $.ajax({
  561. type: "get",
  562. url: endpoints.getHealthConsult,
  563. data: {user: user,status:status,page:page,pagesize:pagesize},
  564. async: true,
  565. dataType: "json",
  566. success: function (data) {
  567. console.log(JSON.stringify(data));
  568. if (success) success(data);
  569. },
  570. error: function (msg) {
  571. console.log('error');
  572. if (error) error(msg);
  573. }
  574. });
  575. },
  576. updateConsultStatus :function(sessionId,id,status,success,error){
  577. endpoints.updateConsultStatus = endpoints.updateConsultStatus.replace("sessionId",sessionId);
  578. $.ajax({
  579. type: "put",
  580. url: endpoints.updateConsultStatus,
  581. data: {topic_id:id,data:JSON.stringify({status:status})},
  582. async: true,
  583. dataType: "json",
  584. success: function (data) {
  585. console.log(JSON.stringify(data));
  586. if (success) success(data);
  587. },
  588. error: function (msg) {
  589. console.log('error');
  590. if (error) error(msg);
  591. }
  592. });
  593. },
  594. };
  595. /*
  596. 执行业务接口前,调用此函数判断当前用户是否在线。
  597. */
  598. function checkLogin(callback) {
  599. /*sendPost("/doctor/islive", {}, null, function (res) {
  600. if (res.status == 200) {
  601. callback();
  602. }
  603. })*/
  604. callback();
  605. }
  606. im.sendGroup = im.sendGroupMsg;
  607. im.getGroup = im.getGroupMsg;
  608. im.getgroupchatinfo = im.getGroupInfo
  609. im.sendOneByOne = im.sendPrivateMsg
  610. im.getOneByOneMsg = im.getPrivateMsg
  611. im.getBadgeNumber = im.getBadgeNumber
  612. im.getDPatientInfo = im.getDPatientInfo
  613. im.getDDoctorImInfo = im.getDDoctorImInfo
  614. im.getGroupsMemberAvatars = im.getGroupsMemberAvatars
  615. im.getSearchPatient = im.getSearchPatient
  616. im.getSearchDoctor = im.getSearchDoctor
  617. im.getSearchZKList = im.getSearchZKList
  618. im.getSearchPatientList = im.getSearchPatientList
  619. im.getSearchPatientAll = im.getSearchPatientAll
  620. im.getSearchDoctorList = im.getSearchDoctorList
  621. im.getSearchDoctorAll = im.getSearchDoctorAll
  622. im.getWeiDuMessageCount = im.getWeiDuMessageCount
  623. im.getHealthConsult = im.getHealthConsult
  624. im.updateConsultStatus = im.updateConsultStatus