goeasyimutil.js 6.6 KB

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