goeasyimutil.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import GoEasyIM from '@/common/goeasy-im-1.3.1.js';
  2. function Friend(uuid, name, avatar) {
  3. this.uuid = uuid;
  4. this.name = name;
  5. this.avatar = avatar;
  6. }
  7. function CurrentUser(uuid, name, avatar) {
  8. this.uuid = uuid;
  9. this.name = name;
  10. this.avatar = avatar;
  11. }
  12. //初始化
  13. function IMService() {
  14. this.im = GoEasyIM.getInstance({
  15. host: 'hangzhou.goeasy.io', //应用所在的区域地址: [hangzhou.goeasy.io, 新加坡暂不支持IM,敬请期待]
  16. appkey: "BC-eef530bc08134a22b2a58ec8f8da0b25"
  17. // appkey: "BC-f420edb1a2c24b97ad43a55299987a85"
  18. });
  19. console.log(this.im)
  20. //当前“我”
  21. this.currentUser = null;
  22. //我的好友
  23. this.friends = {};
  24. //私聊消息记录,map格式,每个好友对应一个数组
  25. this.privateMessages = {};
  26. //收到一条私聊消息
  27. this.onNewPrivateMessageReceive = function(friendId, message) {};
  28. this.onConversationsUpdate = function(conversations) {};
  29. }
  30. IMService.prototype.login = function(user) {
  31. console.log(user)
  32. if (user) {
  33. //初始化当前用户
  34. this.currentUser = new CurrentUser(user.id, user.nickname, user.avatar);
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. };
  40. IMService.prototype.getPrivateMessages = function(friendId) {
  41. console.log(this.privateMessages)
  42. if (!this.privateMessages[friendId]) {
  43. this.privateMessages[friendId] = {
  44. sentMessages: [],
  45. pendingMessages: []
  46. };
  47. }
  48. return this.privateMessages[friendId]
  49. };
  50. //连接GoEasy
  51. IMService.prototype.connectIM = function() {
  52. this.initialIMListeners();
  53. this.im.connect({
  54. id: this.currentUser.uuid,
  55. data: {
  56. avatar: this.currentUser.avatar,
  57. name: this.currentUser.name
  58. }
  59. }).then(() => {
  60. console.log('connect成功')
  61. }).catch(error => {
  62. console.log('connect失败,请确保网络正常,appkey和host正确,code:' + error.code + " content:" + error.content);
  63. });
  64. };
  65. IMService.prototype.initialIMListeners = function() {
  66. this.im.on(GoEasyIM.EVENT.CONNECTED, () => {
  67. console.log('连接成功.')
  68. });
  69. this.im.on(GoEasyIM.EVENT.DISCONNECTED, () => {
  70. console.log('连接断开.')
  71. });
  72. this.im.on(GoEasyIM.EVENT.CONNECTING, (times) => {
  73. console.log('连接中', times);
  74. });
  75. //监听会话列表
  76. this.im.on(GoEasyIM.EVENT.CONVERSATIONS_UPDATED, (conversations) => {
  77. this.conversations = conversations;
  78. this.onConversationsUpdate(this.conversations)
  79. });
  80. //监听私聊消息
  81. this.im.on(GoEasyIM.EVENT.PRIVATE_MESSAGE_RECEIVED, (message) => {
  82. //更新私聊消息记录
  83. let friendId;
  84. if (this.currentUser.uuid == message.senderId) {
  85. friendId = message.receiverId;
  86. } else {
  87. friendId = message.senderId;
  88. }
  89. removePrivatePendingMessage(this, friendId, message);
  90. let friendMessages = this.getPrivateMessages(friendId);
  91. friendMessages.sentMessages.push(message);
  92. //如果页面传入了相应的listener,执行listener
  93. this.onNewPrivateMessageReceive(friendId, message);
  94. });
  95. };
  96. //加载单聊历史消息
  97. IMService.prototype.loadPrivateHistoryMessage = function(friendId, timeStamp) {
  98. return new Promise((resolve, reject) => {
  99. this.im.history({
  100. friendId: friendId,
  101. lastTimestamp: timeStamp
  102. }).then(result => {
  103. let history = result.content;
  104. let privateMessages = this.getPrivateMessages(friendId);
  105. let friendMessages = privateMessages.sentMessages;
  106. for (let i = history.length - 1; i >= 0; i--) {
  107. friendMessages.unshift(history[i])
  108. }
  109. resolve(friendMessages)
  110. }).catch(error => {
  111. if (error.code == 401) {
  112. console.log("您尚未开通历史消息,请登录GoEasy,查看应用详情里自助启用.");
  113. }
  114. reject(error)
  115. });
  116. })
  117. };
  118. function removePrivatePendingMessage(imService, friendId, message) {
  119. let privateMessages = imService.getPrivateMessages(friendId);
  120. let pendingMessages = privateMessages.pendingMessages;
  121. let pendingMessageIndex = pendingMessages.findIndex(item => item.messageId == message.messageId);
  122. if (pendingMessageIndex > -1) {
  123. pendingMessages.splice(pendingMessageIndex, 1);
  124. }
  125. }
  126. //发送私聊消息
  127. IMService.prototype.sendPrivateTextMessage = function(friendId, text) {
  128. let textMessage = this.im.createTextMessage({
  129. text: text
  130. });
  131. this.sendPrivateMessage(friendId, textMessage);
  132. };
  133. //私聊图片消息
  134. IMService.prototype.sendPrivateImageMessage = function(friendId, imageFile) {
  135. let imageMessage = this.im.createImageMessage({
  136. file: imageFile,
  137. onProgress: function(progress) {
  138. console.log(progress)
  139. }
  140. });
  141. this.sendPrivateMessage(friendId, imageMessage);
  142. };
  143. //私聊视频消息
  144. IMService.prototype.sendPrivateVideoMessage = function(friendId, videoFile) {
  145. let videoMessage = this.im.createVideoMessage({
  146. file: videoFile,
  147. onProgress: function(progress) {
  148. console.log(progress)
  149. }
  150. });
  151. this.sendPrivateMessage(friendId, videoMessage);
  152. };
  153. IMService.prototype.sendPrivateAudioMessage = function(friendId, audiofile) {
  154. let audioMessage = this.im.createAudioMessage({
  155. file: audiofile,
  156. onProgress: function(progress) {
  157. console.log(progress)
  158. }
  159. });
  160. this.sendPrivateMessage(friendId, audioMessage);
  161. };
  162. //发送私聊消息
  163. IMService.prototype.sendPrivateMessage = function(friendId, message) {
  164. console.log(friendId, message)
  165. //添加到消息中
  166. let privateMessages = this.getPrivateMessages(friendId);
  167. privateMessages.pendingMessages.push(message);
  168. //发送
  169. this.im.sendPrivateMessage(friendId, message)
  170. .then((res) => {
  171. console.log(res)
  172. })
  173. .catch(e => {
  174. console.log(e)
  175. })
  176. };
  177. IMService.prototype.markPrivateMessageAsRead = function(friendId) {
  178. this.im.markPrivateMessageAsRead(friendId)
  179. .then(res => {
  180. console.log('标记为已读成功')
  181. })
  182. .catch(e => {
  183. console.log(e)
  184. })
  185. };
  186. IMService.prototype.disconnect = function() {
  187. return this.im.disconnect()
  188. };
  189. IMService.prototype.latestConversations = function() {
  190. let promise = this.im.latestConversations();
  191. promise.then((conversations) => {
  192. this.conversations = conversations
  193. });
  194. return promise
  195. };
  196. IMService.prototype.topPrivateConversation = function(userId, isTop) {
  197. let promise = this.im.topPrivateConversation(userId, isTop);
  198. promise.then(res => {
  199. console.log(res)
  200. }).catch(e => {
  201. console.log(e)
  202. });
  203. return promise
  204. };
  205. IMService.prototype.removePrivateConversation = function(friendId) {
  206. let promise = this.im.removePrivateConversation(friendId);
  207. promise.then(res => {
  208. console.log(res)
  209. }).catch(e => {
  210. console.log(e)
  211. });
  212. return promise
  213. };
  214. IMService.prototype.sendCustomMessage = function(id, type, options) {
  215. let customMessage = this.im.createCustomMessage(options);
  216. let promise = null;
  217. if (type == 'private') {
  218. promise = this.im.sendPrivateMessage(id, customMessage)
  219. } else {
  220. promise = this.im.sendGroupMessage(id, customMessage)
  221. }
  222. return promise
  223. };
  224. export default IMService;