im.client.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
  3. */
  4. var isProd = false; // 连接生产环境
  5. var isDemo = false; // 连接公司演示或开发测试环境
  6. // 服务器列表
  7. var servers = {
  8. dev: "http://172.19.103.76:3000",
  9. demo: "http://ehr.yihu.com",
  10. prod: "http://120.41.253.95:3000"
  11. };
  12. // REST接口相对路径
  13. var restPath = {
  14. login: "/user/login.im",
  15. logout: "/user/logout.im",
  16. updateStatus: "/user/updatestatus.im",
  17. sendGroup: "/group/sendmsg.im",
  18. getGroupMsg: "/group/getmsg.im",
  19. sendOneByOne: "/p2p/sendmsg.im",
  20. getOneByOneMsg: "/p2p/getmsg.im",
  21. getNewMsgCount: "/statistic/getgroupchatinfo.im",
  22. getBadgeNumber: "/statistic/getbadgenum.im"
  23. };
  24. var actualServer;
  25. if (isProd === true) {
  26. actualServer = servers.prod;
  27. } else if (isDemo === true) {
  28. actualServer = servers.demo;
  29. } else {
  30. actualServer = servers.dev;
  31. }
  32. // REST接口实际路径
  33. var endpoints = {
  34. login: actualServer + restPath.login,
  35. logout: actualServer + restPath.logout,
  36. updateStatus: actualServer + restPath.updateStatus,
  37. sendGroup: actualServer + restPath.sendGroupMsg,
  38. getGroupMsg: actualServer + restPath.getGroupMsg,
  39. sendOneByOne: actualServer + restPath.sendPrivateMsg,
  40. getOneByOneMsg: actualServer + restPath.getPrivateMsg,
  41. getNewMsgCount: actualServer + restPath.getNewMsgCount,
  42. getBadgeNumber: actualServer + restPath.getBadgeNumber
  43. };
  44. // 本地临时缓存Key
  45. var plusStorageKey = {
  46. userId: "im.user.id"
  47. };
  48. var im = {
  49. login: function (userId, token, client_id, platform) {
  50. console.log("IM - Login: " + userId);
  51. plus.storage.setItem(plusStorageKey.userId, userId);
  52. $.ajax({
  53. type: "get",
  54. url: endpoints.login,
  55. data: {user_id: userId, token: token, client_id: client_id, platform: platform},
  56. async: true,
  57. dataType: "json",
  58. success: function (data) {
  59. console.log(JSON.stringify(data));
  60. },
  61. error: function (msg) {
  62. }
  63. });
  64. },
  65. logout: function (userId) {
  66. plus.storage.removeItem(plusStorageKey.userId);
  67. $.ajax({
  68. type: "get",
  69. url: endpoints.logout,
  70. data: {user_id: userId},
  71. async: true,
  72. dataType: "json",
  73. success: function (data) {
  74. console.log(JSON.stringify(data));
  75. },
  76. error: function (msg) {
  77. }
  78. });
  79. },
  80. updateStatus: function (userId, status) {
  81. // 更新用户在线状态
  82. $.ajax({
  83. type: "get",
  84. url: endpoints.updateStatus,
  85. data: {user_id: userId, status: status},
  86. async: true,
  87. dataType: "json",
  88. success: function (data) {
  89. console.log(JSON.stringify(data));
  90. },
  91. error: function (msg) {
  92. }
  93. });
  94. },
  95. sendGroupMsg: function (userId, groupId, content, type, handler, group_type) {
  96. function send() {
  97. $.ajax({
  98. type: "get",
  99. url: endpoints.sendGroupMsg,
  100. data: {from_uid: userId, to_gid: groupId, content: content, type: type, group_type: group_type},
  101. async: true,
  102. dataType: "json",
  103. success: function (data) {
  104. console.log(JSON.stringify(data));
  105. handler(data);
  106. },
  107. error: function (msg) {
  108. handler(msg);
  109. }
  110. });
  111. }
  112. checkLogin(send);
  113. },
  114. getGroupMsg: function (uid, groupId, start, count, handler) {
  115. $.ajax({
  116. type: "get",
  117. url: endpoints.getGroupMsg,
  118. data: {uid: uid, gid: groupId, start: start, count: count},
  119. async: true,
  120. dataType: "json",
  121. success: function (data) {
  122. console.log(JSON.stringify(data));
  123. handler(data);
  124. },
  125. error: function (msg) {
  126. handler(msg);
  127. }
  128. });
  129. },
  130. getGroupInfo: function (uid, gid, handler) {
  131. $.ajax({
  132. type: "get",
  133. url: endpoints.getNewMsgCount,
  134. data: {uid: uid, gid: gid},
  135. async: true,
  136. dataType: "json",
  137. success: function (data) {
  138. handler(data);
  139. },
  140. error: function (msg) {
  141. handler(msg);
  142. }
  143. });
  144. },
  145. sendPrivateMsg: function (from_uid, to_uid, content, type) {
  146. function send() {
  147. $.ajax({
  148. type: "get",
  149. url: endpoints.sendPrivateMsg,
  150. data: {from_uid: from_uid, to_uid: to_uid, content: content, type: type},
  151. async: true,
  152. dataType: "json",
  153. success: function (data) {
  154. console.log(JSON.stringify(data));
  155. handler(data);
  156. },
  157. error: function (msg) {
  158. handler(msg);
  159. }
  160. });
  161. }
  162. checkLogin(send);
  163. },
  164. sendPrivateMsgWithCallback: function (from_uid, to_uid, content, type, msgCallback, errCallback) {
  165. function send() {
  166. $.ajax({
  167. type: "get",
  168. url: endpoints.sendPrivateMsg,
  169. data: {from_uid: from_uid, to_uid: to_uid, content: content, type: type},
  170. async: true,
  171. dataType: "json",
  172. success: function (msg) {
  173. if (msgCallback) msgCallback(msg);
  174. },
  175. error: function (err) {
  176. if (errCallback) errCallback(err);
  177. }
  178. });
  179. }
  180. checkLogin(send);
  181. },
  182. getPrivateMsg: function (uid, peer_uid, start, count) {
  183. $.ajax({
  184. type: "get",
  185. url: endpoints.getPrivateMsg,
  186. data: {gid: groupId, start: start, count: count},
  187. async: true,
  188. dataType: "json",
  189. success: function (data) {
  190. console.log(JSON.stringify(data));
  191. handler(data);
  192. },
  193. error: function (msg) {
  194. handler(msg);
  195. }
  196. });
  197. },
  198. getPagedPrivateMsg: function (uid, peer_uid, start, count, handler) {
  199. $.ajax({
  200. type: "get",
  201. url: endpoints.getPrivateMsg,
  202. data: {uid: uid, peer_uid: peer_uid, start: start, count: count},
  203. async: true,
  204. dataType: "json",
  205. success: function (data) {
  206. handler(data);
  207. },
  208. error: function (msg) {
  209. handler(msg);
  210. }
  211. });
  212. },
  213. getBadgeNumber: function (userId, handler) {
  214. $.ajax({
  215. type: "get",
  216. url: endpoints.getBadgeNumber,
  217. data: {uid: userId},
  218. async: true,
  219. dataType: "json",
  220. success: function (data) {
  221. console.log(JSON.stringify(data));
  222. if (handler) handler(data);
  223. },
  224. error: function (msg) {
  225. console.log('error');
  226. if (handler) handler(msg);
  227. }
  228. });
  229. }
  230. };
  231. /*
  232. 执行业务接口前,调用此函数判断当前用户是否在线。
  233. */
  234. function checkLogin(callback) {
  235. sendPost("/doctor/islive", {}, null, function (res) {
  236. if (res.status == 200) {
  237. callback();
  238. }
  239. })
  240. }