im.client.js 7.9 KB

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