imservice.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import restApi from './restapi';
  2. function Friend(uuid, name, avatar) {
  3. this.uuid = uuid;
  4. this.name = name;
  5. this.avatar = avatar;
  6. }
  7. function Group(uuid, name, avatar) {
  8. this.uuid = uuid;
  9. this.name = name;
  10. this.avatar = avatar;
  11. }
  12. function IMService(goEasy, GoEasy) {
  13. //当前用户
  14. this.currentUser = null;
  15. //我的群
  16. this.groups = {};
  17. // GoEasy 模块
  18. this.GoEasy = GoEasy;
  19. //goEasy 实例对象
  20. this.goEasy = goEasy;
  21. //私聊消息记录,map格式,每个好友对应一个数组
  22. this.privateMessages = {};
  23. //群聊消息记录,map格式,每个群对应一个数组
  24. this.groupMessages = {};
  25. /*
  26. * 监听器们
  27. *
  28. * 可以在页面里,根据需求,重写以下监听器,
  29. * 便于当各种事件触发时,页面能够执行对应的响应
  30. *
  31. */
  32. //收到一条私聊消息
  33. this.onNewPrivateMessageReceive = function(friendId, message) {};
  34. //收到一条群聊消息
  35. this.onNewGroupMessageReceive = function(groupId, message) {};
  36. }
  37. //获取群成员
  38. IMService.prototype.getGroupMembers = function(groupId) {
  39. let members = restApi.findGroupMembers(groupId);
  40. let membersMap = {};
  41. members.map(item => {
  42. membersMap[item.uuid] = item
  43. });
  44. return membersMap;
  45. };
  46. IMService.prototype.findGroupById = function(groupId) {
  47. let group = restApi.findGroupById(groupId);
  48. return new Group(group.uuid, group.name, group.avatar);
  49. };
  50. IMService.prototype.findFriendById = function(userId) {
  51. let user = restApi.findUserById(userId);
  52. return new Friend(user.uuid, user.name, user.avatar);
  53. };
  54. IMService.prototype.getGroupMessages = function(groupId) {
  55. if (!this.groupMessages[groupId]) {
  56. this.groupMessages[groupId] = [];
  57. }
  58. return this.groupMessages[groupId]
  59. };
  60. IMService.prototype.getPrivateMessages = function(friendId) {
  61. if (!this.privateMessages[friendId]) {
  62. this.privateMessages[friendId] = [];
  63. }
  64. return this.privateMessages[friendId];
  65. };
  66. //连接GoEasy
  67. IMService.prototype.connect = function(currentUser) {
  68. this.currentUser = currentUser;
  69. let userData = {
  70. name: this.currentUser.name,
  71. avatar: this.currentUser.avatar
  72. }
  73. //初始化相关的监听器
  74. this.initialListeners();
  75. this.goEasy.connect({
  76. id: this.currentUser.uuid,
  77. data: userData,
  78. onSuccess: function() {
  79. //连接成功
  80. console.log("GoEasy connect successfully.")
  81. },
  82. onFailed: function(error) {
  83. //连接失败
  84. console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
  85. },
  86. onProgress: function(attempts) { //连接或自动重连中
  87. console.log("GoEasy is connecting", attempts);
  88. }
  89. });
  90. // this.subscribeGroupMessage(currentUser);
  91. };
  92. IMService.prototype.subscribeGroupMessage = function() {
  93. let groups = restApi.findGroups(this.currentUser);
  94. let groupIds = groups.map(item => item.uuid);
  95. this.goEasy.im.subscribeGroup({
  96. groupIds: groupIds,
  97. onSuccess: function() {
  98. //订阅成功
  99. console.log('订阅群消息成功');
  100. },
  101. onFailed: function(error) {
  102. //订阅失败
  103. console.log('订阅群消息失败')
  104. }
  105. });
  106. }
  107. //初始化监听器
  108. IMService.prototype.initialListeners = function() {
  109. //监听私聊消息
  110. this.goEasy.im.on(this.GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, (message) => {
  111. //更新私聊消息记录
  112. let friendId;
  113. if (this.currentUser.uuid === message.senderId) {
  114. friendId = message.receiverId;
  115. } else {
  116. friendId = message.senderId;
  117. }
  118. let friendMessages = this.getPrivateMessages(friendId);
  119. friendMessages.push(message);
  120. //如果页面传入了相应的listener,执行listener
  121. this.onNewPrivateMessageReceive(friendId, message);
  122. });
  123. //监听群聊消息
  124. this.goEasy.im.on(this.GoEasy.IM_EVENT.GROUP_MESSAGE_RECEIVED, (message) => {
  125. let groupId = message.groupId;
  126. //更新群聊消息记录
  127. let groupMessages = this.getGroupMessages(groupId);
  128. groupMessages.push(message);
  129. //如果页面传入了相应的listener,执行listener
  130. this.onNewGroupMessageReceive(groupId, message);
  131. })
  132. };
  133. export default IMService;