message.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import Vue from 'vue';
  2. import { emMessages } from '@/EaseIM/emApis';
  3. import { MESSAGE_STATUS, MESSAGE_TYPE } from '@/EaseIM/constant';
  4. import ConversationStore from './conversation';
  5. import { EVENT_BUS_NAME } from '@/constant';
  6. const { fetchHistoryMessagesFromServer } = emMessages();
  7. const MessageStore = {
  8. state: {
  9. messageCollection: {},
  10. messageStatusCollection: {}, //readed、unread、recalled
  11. },
  12. mutations: {
  13. RESET_MESSAGE_STORE: (state) => {
  14. state.messageCollection = {};
  15. state.messageStatusCollection = {};
  16. },
  17. UPDATE_MESSAGE_COLLECTION(state, payload) {
  18. const { key, message } = payload;
  19. if (!state.messageCollection[key]) {
  20. /**
  21. * Vue 无法检测 property 的添加或移除。由于 Vue 会在初始化实例时对 property 执行 getter/setter 转化,
  22. * 因此使用Vue.set()方法,新增属性引起视图变化。
  23. */
  24. Vue.set(state.messageCollection, key, []);
  25. }
  26. //如果当前会话中Id为消息from,则发布消息列表更新事件。
  27. if (ConversationStore.state.chattingId === key) {
  28. uni.$emit(EVENT_BUS_NAME.EASEIM_MESSAGE_COLLECTION_UPDATE, {
  29. msgBody: message,
  30. });
  31. }
  32. state.messageCollection[key].unshift(message);
  33. },
  34. DELETE_MESSAGE_FROM_COLLECTION(state, payload) {
  35. const { key, mid } = payload;
  36. //!现有逻辑如果传了mid,则删除单条,否则置空key。
  37. if (mid) {
  38. if (state.messageCollection[key]) {
  39. const _index = state.messageCollection[key].findIndex(
  40. (item) => item.id == mid
  41. );
  42. console.log('>>>>>>找到对应的下标', _index);
  43. if (_index >= 0) {
  44. state.messageCollection[key].splice(_index, 1);
  45. //如果当前会话中Id为消息from,则发布消息列表更新事件。
  46. if (ConversationStore.state.chattingId === key) {
  47. uni.$emit(EVENT_BUS_NAME.EASEIM_MESSAGE_COLLECTION_DELETE, {
  48. mid: mid,
  49. });
  50. }
  51. }
  52. }
  53. } else {
  54. if (state.messageCollection[key]) {
  55. Vue.set(state.messageCollection, key, []);
  56. }
  57. }
  58. },
  59. MODIFY_MESSAGE_FROM_COLLECTION(state, payload) {
  60. const { key, mid, message } = payload;
  61. if (state.messageCollection[key]) {
  62. const _index = state.messageCollection[key].findIndex(
  63. (o) => o.id === mid
  64. );
  65. _index >= 0 &&
  66. state.messageCollection[key].splice(_index, 1, { ...message });
  67. console.log('>>>>>找到要修改修改后的消息', _index);
  68. if (ConversationStore.state.chattingId === key) {
  69. uni.$emit(EVENT_BUS_NAME.EASEIM_MESSAGE_COLLECTION_MODIFY, message);
  70. }
  71. // Object.assign(res, payload?.message);
  72. }
  73. },
  74. UPDATE_MESSAGE_FROM_SERVER(state, payload) {
  75. const { key, messageList } = payload;
  76. if (state.messageCollection[key]) {
  77. state.messageCollection[key].push(...messageList);
  78. } else {
  79. Vue.set(state.messageCollection, key, []);
  80. state.messageCollection[key].push(...messageList);
  81. }
  82. },
  83. UPDATE_MESSAGE_COLLECTION_STATUS(state, payload) {
  84. //key 为消息serverMsgId
  85. const { key, status } = payload;
  86. if (state.messageStatusCollection[key]) {
  87. state.messageStatusCollection[key] = status;
  88. } else {
  89. Vue.set(state.messageStatusCollection, key, status);
  90. }
  91. },
  92. ADD_NEW_GRAY_INFORM_MESSAGE(state, payload) {
  93. const { key, message } = payload;
  94. if (!state.messageCollection[key]) {
  95. Vue.set(state.messageCollection, key, []);
  96. }
  97. //如果当前会话中Id为消息from,则发布消息列表更新事件。
  98. if (ConversationStore.state.chattingId === key) {
  99. uni.$emit(EVENT_BUS_NAME.EASEIM_MESSAGE_COLLECTION_UPDATE, {
  100. msgBody: message,
  101. });
  102. }
  103. state.messageCollection[key].unshift(message);
  104. },
  105. },
  106. actions: {
  107. async fetchHistroyMessageListFromServer({ state, commit }, params) {
  108. const { targetId, chatType } = params;
  109. let cursorMsgId = '';
  110. const messages = state.messageCollection[targetId];
  111. //查找消息列表数组中的最后一条消息且不为灰色通知消息最为漫游cursorMsgId
  112. if (messages?.length) {
  113. const reverseMessages = Object.assign([], messages).reverse();
  114. const lastMsgBody = reverseMessages.find((message) => {
  115. return message.type !== MESSAGE_TYPE.GRAY_INFORM;
  116. });
  117. cursorMsgId = lastMsgBody?.id;
  118. }
  119. return new Promise((resolve, reject) => {
  120. fetchHistoryMessagesFromServer({
  121. targetId,
  122. chatType,
  123. cursor: cursorMsgId,
  124. })
  125. .then((res) => {
  126. const result = Object.assign({}, res);
  127. if (result.messages.length) {
  128. const filterMessages = result.messages.filter((message) => {
  129. if (message) {
  130. return message.type !== MESSAGE_TYPE.CMD;
  131. }
  132. });
  133. commit('UPDATE_MESSAGE_FROM_SERVER', {
  134. key: targetId,
  135. messageList: filterMessages,
  136. });
  137. result.messages = filterMessages;
  138. }
  139. resolve(result);
  140. })
  141. .catch((error) => {
  142. reject(error);
  143. });
  144. });
  145. },
  146. },
  147. getters: {
  148. messageCollection(state) {
  149. return state.messageCollection;
  150. },
  151. messageStatusCollection(state) {
  152. return state.messageStatusCollection;
  153. },
  154. },
  155. };
  156. export default MessageStore;