contacts.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { emContacts, emUserInfos, emPresence } from "@/EaseIM/emApis";
  2. import Vue from "vue";
  3. import { handlePresence } from "@/EaseIM/utils";
  4. import { EMClient } from "@/EaseIM";
  5. const { fetchContactsListFromServer, getBlocklistFromServer } = emContacts();
  6. const { fetchOtherInfoFromServer } = emUserInfos();
  7. const { subscribePresence, unsubscribePresence } = emPresence();
  8. const ContactsStore = {
  9. state: {
  10. friendList: [],
  11. friendUserInfoCollection: {}, //用对象替换好友属性集合
  12. blockUserList: [],
  13. subscribeStatusInfoMap: {}, //好友用户在线状态
  14. loginUserPresenceInfoMap: {}, //自己的用户状态变更
  15. },
  16. mutations: {
  17. RESET_CONTACTS_STORE: (state) => {
  18. state.friendList = [];
  19. state.friendUserInfoCollection = {};
  20. state.blockUserList = [];
  21. },
  22. SET_FROEND_LIST: (state, payload) => {
  23. if (state.friendList.length === 0) {
  24. if (Array.isArray(payload)) {
  25. Vue.set(state, "friendList", [...payload]);
  26. } else {
  27. Vue.set(state, "friendList", [payload]);
  28. }
  29. } else {
  30. if (Array.isArray(payload)) {
  31. Vue.set(state, "friendList", [...state.friendList, ...payload]);
  32. } else {
  33. Vue.set(state, "friendList", [...state.friendList, payload]);
  34. }
  35. }
  36. },
  37. DELETE_FRIEND_ITEM: (state, friendId) => {
  38. state.friendList.map((item, idnex) => {
  39. if (item.userId === friendId) {
  40. state.friendList.splice(idnex, 1);
  41. }
  42. });
  43. },
  44. SET_FRIEND_USER_INFO_COLLECTION: (state, friendProfiles) => {
  45. if (Object.keys(friendProfiles).length) {
  46. for (const key in friendProfiles) {
  47. if (Object.hasOwnProperty.call(friendProfiles, key)) {
  48. const values = friendProfiles[key];
  49. Object.values(values).length &&
  50. Vue.set(state.friendUserInfoCollection, key, values);
  51. }
  52. }
  53. }
  54. },
  55. SET_BLOCK_USER_LIST: (state, userList) => {
  56. state.blockUserList = [...userList];
  57. },
  58. UPDATE_FRIEND_USER_REMARK: (state, { contactId, remark }) => {
  59. state.friendList.map((item) => {
  60. if (item.userId === contactId) {
  61. Vue.set(item, "remark", remark);
  62. }
  63. });
  64. // Vue.set(state.friendUserInfoCollection[contactId], 'remark', remark);
  65. },
  66. // 好友在线状态
  67. SET_FRIENDSTATUS_STATUS_INFO: (state, subscribeStatus) => {
  68. subscribeStatus.map((item) => {
  69. // console.log("用户在线状态", handlePresence(item));
  70. const commonStatus = handlePresence(item);
  71. const matchingFriend = state.friendList.find(
  72. (friend) => friend.userId === commonStatus.uid
  73. );
  74. if (matchingFriend) {
  75. Vue.set(
  76. state.subscribeStatusInfoMap,
  77. matchingFriend.userId,
  78. commonStatus
  79. );
  80. } else {
  81. Vue.set(state.subscribeStatusInfoMap, commonStatus.uid, commonStatus);
  82. }
  83. });
  84. console.log("用户在线状态", state.subscribeStatusInfoMap);
  85. },
  86. // 自己状态变更
  87. SET_SUBSCRIBE_STATUS_INFO_LOGIN_USER: (state, subscribeStatus) => {
  88. if (Object.keys(subscribeStatus).length) {
  89. for (const key in subscribeStatus) {
  90. if (Object.hasOwnProperty.call(subscribeStatus, key)) {
  91. const values = subscribeStatus[key];
  92. Object.values(values).length &&
  93. Vue.set(state.loginUserPresenceInfoMap, key, values);
  94. }
  95. }
  96. }
  97. console.log("自己的状态变更", state.loginUserPresenceInfoMap);
  98. },
  99. // 取消订阅删除对应的好友状态
  100. DELETE_PRESENCE_ITEM: (state, friendId) => {
  101. for (const key in state.subscribeStatusInfoMap) {
  102. if (
  103. Object.values(state.subscribeStatusInfoMap[key]).includes(friendId)
  104. ) {
  105. Vue.delete(state.subscribeStatusInfoMap, key);
  106. }
  107. }
  108. },
  109. },
  110. actions: {
  111. async fetchFriendList({ commit }) {
  112. try {
  113. const friendList = await fetchContactsListFromServer();
  114. commit("SET_FROEND_LIST", friendList);
  115. } catch (error) {
  116. console.log(">>>>好友列表获取失败", error);
  117. }
  118. },
  119. //批量获取好友列表对应的用户属性
  120. async fetchFriendUserInfoCollection({ state, commit }) {
  121. try {
  122. const friendProfiles = await fetchOtherInfoFromServer(
  123. state.friendList.map((item) => item.userId)
  124. );
  125. console.log("friendProfiles", friendProfiles);
  126. commit("SET_FRIEND_USER_INFO_COLLECTION", friendProfiles);
  127. } catch (error) {
  128. console.log(">>>>获取好友属性失败", error);
  129. }
  130. },
  131. //获取单个用户的用户属性
  132. async fetchFriendUserInfo({ state, commit }, userId) {
  133. try {
  134. const friendProfile = await fetchOtherInfoFromServer([userId]);
  135. console.log(">>>>获取单个用户属性执行", friendProfile);
  136. commit("SET_FRIEND_USER_INFO_COLLECTION", friendProfile);
  137. } catch (error) {
  138. console.log(">>>>获取好友属性失败", error);
  139. }
  140. },
  141. async fetchBlockUserList({ commit }) {
  142. try {
  143. const userList = await getBlocklistFromServer();
  144. console.log(">>>>getBlocklistFromServer", userList);
  145. commit("SET_BLOCK_USER_LIST", userList);
  146. } catch (error) {
  147. console.log(">>>>获取好友黑名单失败", error);
  148. }
  149. },
  150. // 订阅用户的在线状态
  151. async subscribePresenceStatus({ state, commit }) {
  152. try {
  153. const subscribeStatus = await subscribePresence(
  154. state.friendList.map((item) => item.userId)
  155. );
  156. commit("SET_FRIENDSTATUS_STATUS_INFO", subscribeStatus);
  157. console.log(">>>>订阅用户在线状态", subscribeStatus);
  158. } catch (error) {
  159. console.log(">>>>订阅用户在线状态失败", error);
  160. }
  161. },
  162. // 取消订阅
  163. async unsubscribePresenceStatus({ state, commit }, userId) {
  164. try {
  165. const subscribeStatus = await unsubscribePresence(userId);
  166. } catch (error) {
  167. console.log(">>>>取消订阅用户在线状态失败", error);
  168. }
  169. },
  170. // 处理状态变更
  171. async handlePresenceChanges({ state, commit }, status) {
  172. const { userId } = status || {};
  173. if (userId === EMClient.user) {
  174. commit("SET_SUBSCRIBE_STATUS_INFO_LOGIN_USER", status);
  175. } else {
  176. commit("SET_FRIENDSTATUS_STATUS_INFO", [{ ...status }]);
  177. }
  178. },
  179. },
  180. getters: {
  181. friendUserInfoCollection(state) {
  182. return state.friendUserInfoCollection;
  183. },
  184. friendList(state) {
  185. return state.friendList;
  186. },
  187. blockUserList(state) {
  188. return state.blockUserList;
  189. },
  190. subscribeStatusInfoMap(state) {
  191. return state.subscribeStatusInfoMap;
  192. },
  193. },
  194. };
  195. export default ContactsStore;