You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 regels
6.4 KiB

  1. (function ($, learun) {
  2. "use strict";
  3. var msgList = {};
  4. var imChat;
  5. var isLoaded = -1;
  6. var loadingMsg = {};
  7. var loadingMsg2 = {};
  8. var _im = {
  9. init: function () {
  10. _im.registerServer();
  11. _im.connect();
  12. }
  13. // 连接服务端
  14. , connect: function () {
  15. var loginInfo = learun.clientdata.get(['userinfo']);
  16. if (loginInfo.imOpen != 'true') {
  17. return;
  18. }
  19. $.ajax({
  20. url: loginInfo.imUrl + "/hubs",
  21. type: "get",
  22. dataType: "text",
  23. success: function (data) {
  24. eval(data);
  25. //Set the hubs URL for the connection
  26. $.connection.hub.url = loginInfo.imUrl;
  27. $.connection.hub.qs = { "userId": loginInfo.userId };
  28. // Declare a proxy to reference the hub.
  29. imChat = $.connection.ChatsHub;
  30. _im.registerClient();
  31. // 连接成功后注册服务器方法
  32. $.connection.hub.start().done(function () {
  33. _im.afterSuccess();
  34. });
  35. //断开连接后
  36. $.connection.hub.disconnected(function () {
  37. _im.disconnected();
  38. });
  39. },
  40. error: function (XMLHttpRequest, textStatus, errorThrown) {
  41. isLoaded = 0;
  42. },
  43. });
  44. }
  45. // 连接成功后执行方法
  46. , afterSuccess: function () {
  47. isLoaded = 1;
  48. $('.lr-im-bell').show();
  49. }
  50. // 断开连接后执行
  51. , disconnected: function () {
  52. isLoaded = 0;
  53. }
  54. // 注册客户端方法
  55. , registerClient: function () {
  56. if (imChat) {
  57. //接收消息
  58. imChat.client.revMsg = function (userId, msg, dateTime, isSystem) {
  59. if (!loadingMsg2[userId]) {
  60. var point = { userId: userId, content: msg, time: dateTime, isSystem: isSystem || 0 };
  61. addMsgList(userId, point);
  62. learun.im.revMsg && learun.im.revMsg(userId, msg, dateTime, isSystem || 0);
  63. }
  64. }
  65. }
  66. }
  67. // 注册服务端方法
  68. ,registerServer: function () {
  69. // 发送信息
  70. _im.sendMsg = function (userId, msg) {
  71. if (isLoaded == 1) {
  72. imChat.server.sendMsg(userId, msg, 0);
  73. }
  74. else if (isLoaded == -1) {
  75. setTimeout(function () {
  76. _im.sendMsg(userId, msg);
  77. }, 100);
  78. }
  79. };
  80. }
  81. };
  82. function addMsgList(userId, item) {
  83. msgList[userId] = msgList[userId] || [];
  84. if (loadingMsg[userId]) {
  85. setTimeout(function () {
  86. addMsgList(userId, item);
  87. }, 100);
  88. }
  89. else {
  90. msgList[userId].push(item);
  91. }
  92. }
  93. learun.im = {
  94. addContacts: function (userId) {// 添加联系人
  95. learun.httpAsync('Post', top.$.rootUrl + '/LR_IM/IMMsg/AddContact', { otherUserId: userId }, function (data) {});
  96. },
  97. removeContacts: function (userId) {// 移除联系人
  98. learun.httpAsync('Post', top.$.rootUrl + '/LR_IM/IMMsg/RemoveContact', { otherUserId: userId }, function (data) { });
  99. },
  100. getContacts: function (callback) {// 获取最近的联系人列表
  101. setTimeout(function () {
  102. learun.httpAsync('GET', top.$.rootUrl + '/LR_IM/IMMsg/GetContactsList', {}, function (res) {
  103. if (res) {
  104. _im.init();
  105. callback(res.data || [], res.sysUserList || []);
  106. }
  107. });
  108. }, 1000);
  109. },
  110. updateContacts: function (userId) {
  111. learun.httpAsync('Post', top.$.rootUrl + '/LR_IM/IMMsg/UpdateContactState', { otherUserId: userId}, function (data) {
  112. });
  113. },
  114. sendMsg: function (userId, msg) {// 发送消息
  115. var time = "";
  116. var loginInfo = learun.clientdata.get(['userinfo']);
  117. var point = { userId: loginInfo.userId, content: msg, time: learun.getDate('yyyy-MM-dd hh:mm:ss'), isSystem: 0 };
  118. addMsgList(userId, point);
  119. learun.httpAsync('Post', top.$.rootUrl + '/LR_IM/IMMsg/SendMsg', { userId: userId, content: msg }, function (data) {
  120. _im.sendMsg(userId, msg);// 发送给即时通讯服务
  121. });
  122. if (msgList[userId].length > 1) {
  123. if (learun.parseDate(point.time).DateDiff('s', msgList[userId][msgList[userId].length - 2].time) > 60) {
  124. time = point.time;
  125. }
  126. }
  127. else {
  128. time = point.time;
  129. }
  130. return time;
  131. },
  132. getMsgList: function (userId, callback,isGetMsgList) {
  133. msgList[userId] = msgList[userId] || [];
  134. loadingMsg[userId] = true;
  135. if (msgList[userId].length == 0 && isGetMsgList) {// 如果没有信息,获取最近10条的聊天记录
  136. loadingMsg2[userId] = true;
  137. learun.httpAsync('GET', top.$.rootUrl + '/LR_IM/IMMsg/GetMsgList', { userId: userId }, function (data) {
  138. msgList[userId] = msgList[userId] || [];
  139. data = data || [];
  140. var len = data;
  141. if (len > 0) {
  142. for (var i = len - 1; i >= 0; i--) {
  143. var item = data[i];
  144. var point = { userId: _item.F_SendUserId, content: _item.F_Content, time: _item.F_CreateDate, isSystem: _item.F_IsSystem || 0 };
  145. msgList[userId].push(point);
  146. }
  147. }
  148. callback(msgList[userId]);
  149. loadingMsg[userId] = false;
  150. loadingMsg2[userId] = false;
  151. });
  152. }
  153. else {
  154. callback(msgList[userId]);
  155. loadingMsg[userId] = false;
  156. }
  157. },
  158. registerRevMsg: function (callback) {// 获取消息记录
  159. learun.im.revMsg = callback;
  160. }
  161. };
  162. })(jQuery, top.learun);