goeasyimutil.js 6.6 KB

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