message.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import IMSDK, { MessageStatus, MessageType } from "openim-uniapp-polyfill";
  2. import { v4 as uuidv4 } from "uuid";
  3. import { UpdateMessageTypes, FileMessageTypes } from "@/pages_im/constant";
  4. const state = {
  5. historyMessageList: [],
  6. previewImageList: [],
  7. hasMoreMessage: true,
  8. quoteMessage: undefined,
  9. };
  10. const mutations = {
  11. SET_HISTORY_MESSAGE_LIST(state, list) {
  12. state.historyMessageList = [...list];
  13. },
  14. SET_PREVIEW_IMAGE_LIST(state, list) {
  15. state.previewImageList = [...list];
  16. },
  17. SET_HAS_MORE_MESSAGE(state, hasMore) {
  18. state.hasMoreMessage = hasMore;
  19. },
  20. SET_QUOTE_MESSAGE(state, message) {
  21. state.quoteMessage = message;
  22. },
  23. };
  24. const actions = {
  25. async getHistoryMesageList({ commit, state }, params) {
  26. let emptyFlag = true;
  27. try {
  28. console.log("getHistoryMesageList:::");
  29. console.log(params);
  30. const { data } = await IMSDK.asyncApi(
  31. IMSDK.IMMethods.GetAdvancedHistoryMessageList,
  32. uuidv4(),
  33. params,
  34. );
  35. console.log(data);
  36. const isFistPage = !params.startClientMsgID;
  37. const messages = data.messageList ?? [];
  38. if (!params.conversationID.includes("sn_")) {
  39. const imageList = filterPreviewImage([...messages]);
  40. commit("SET_PREVIEW_IMAGE_LIST", [
  41. ...imageList,
  42. ...(isFistPage ? [] : state.previewImageList),
  43. ]);
  44. }
  45. emptyFlag = messages.length === 0;
  46. commit("SET_HISTORY_MESSAGE_LIST", [
  47. ...messages,
  48. ...(isFistPage ? [] : state.historyMessageList),
  49. ]);
  50. commit("SET_HAS_MORE_MESSAGE", !data.isEnd && messages.length === 20);
  51. } catch (e) {
  52. commit("SET_HISTORY_MESSAGE_LIST", []);
  53. }
  54. return {
  55. emptyFlag,
  56. };
  57. },
  58. pushNewPreviewImage({ state, commit }, url) {
  59. commit("SET_PREVIEW_IMAGE_LIST", [...state.previewImageList, url]);
  60. },
  61. pushNewMessage({ commit, state }, message) {
  62. if (
  63. message.contentType === MessageType.PictureMessage &&
  64. message.status === MessageStatus.Succeed
  65. ) {
  66. const imageList = filterPreviewImage([message]);
  67. if (imageList.length > 0) {
  68. commit("SET_PREVIEW_IMAGE_LIST", [
  69. ...state.previewImageList,
  70. ...imageList,
  71. ]);
  72. }
  73. }
  74. commit("SET_HISTORY_MESSAGE_LIST", [...state.historyMessageList, message]);
  75. },
  76. updateOneMessage(
  77. { commit, dispatch, state },
  78. {
  79. message,
  80. type = UpdateMessageTypes.Overall,
  81. keyWords = [],
  82. isSuccess = false,
  83. },
  84. ) {
  85. const tmpList = state.historyMessageList;
  86. const idx = tmpList.findIndex(
  87. (msg) => msg.clientMsgID === message.clientMsgID,
  88. );
  89. if (idx !== -1) {
  90. if (type === UpdateMessageTypes.Overall) {
  91. if (FileMessageTypes.includes(message.contentType) && isSuccess){
  92. let filePath = "";
  93. switch (message.contentType) {
  94. case MessageType.FileMessage:
  95. filePath = message.fileElem.filePath;
  96. break;
  97. case MessageType.PictureMessage:
  98. filePath = message.pictureElem.sourcePath;
  99. break;
  100. case MessageType.VideoMessage:
  101. filePath = message.videoElem.videoPath;
  102. break;
  103. case MessageType.VoiceMessage:
  104. filePath = message.soundElem.soundPath;
  105. break;
  106. default:
  107. break;
  108. }
  109. if (filePath){
  110. dispatch("user/addLocalPathToCache", {
  111. key: message.clientMsgID,
  112. path: filePath,
  113. })
  114. }
  115. }
  116. if (message.contentType === MessageType.PictureMessage && isSuccess) {
  117. const imageList = filterPreviewImage([message]);
  118. if (imageList.length > 0) {
  119. commit("SET_PREVIEW_IMAGE_LIST", [
  120. ...state.previewImageList,
  121. ...imageList,
  122. ]);
  123. }
  124. }
  125. tmpList[idx] = {
  126. ...message,
  127. };
  128. } else if (type === UpdateMessageTypes.KeyWords) {
  129. const updateFields = Array.isArray(keyWords) ? keyWords : [keyWords];
  130. updateFields.forEach(
  131. (field) => (tmpList[idx][field.key] = field.value),
  132. );
  133. }
  134. commit("SET_HISTORY_MESSAGE_LIST", tmpList);
  135. }
  136. },
  137. deleteMessages({ commit, state }, messages) {
  138. const tmpList = [...state.historyMessageList];
  139. messages.map((message) => {
  140. const idx = tmpList.findIndex(
  141. (msg) => msg.clientMsgID === message.clientMsgID,
  142. );
  143. if (idx !== -1) {
  144. tmpList.splice(idx, 1);
  145. }
  146. });
  147. commit("SET_HISTORY_MESSAGE_LIST", tmpList);
  148. },
  149. resetMessageState({ commit }) {
  150. commit("SET_HISTORY_MESSAGE_LIST", []);
  151. commit("SET_PREVIEW_IMAGE_LIST", []);
  152. commit("SET_HAS_MORE_MESSAGE", true);
  153. commit("SET_QUOTE_MESSAGE", undefined);
  154. },
  155. updateMessageNicknameAndFaceUrl(
  156. { commit, state },
  157. { sendID, senderFaceUrl, senderNickname },
  158. ) {
  159. const tmpList = [...state.historyMessageList].map((message) => {
  160. if (message.sendID === sendID) {
  161. message.senderFaceUrl = senderFaceUrl;
  162. message.senderNickname = senderNickname;
  163. }
  164. return message;
  165. });
  166. commit("SET_HISTORY_MESSAGE_LIST", [...tmpList]);
  167. },
  168. updateQuoteMessageRevoke(
  169. { dispatch, state },
  170. { clientMsgID }
  171. ) {
  172. [...state.historyMessageList].map((message) => {
  173. if (message.contentType === MessageType.QuoteMessage && clientMsgID === message.quoteElem.quoteMessage.clientMsgID) {
  174. dispatch('updateOneMessage', {
  175. message: {
  176. ...message,
  177. quoteElem: {
  178. ...message.quoteElem,
  179. quoteMessage: {
  180. ...message.quoteElem.quoteMessage,
  181. contentType: 2101
  182. }
  183. }
  184. }
  185. })
  186. }
  187. return null;
  188. });
  189. }
  190. };
  191. function filterPreviewImage(messages) {
  192. return messages
  193. .filter((message) => {
  194. if (message.contentType === MessageType.PictureMessage) {
  195. return true;
  196. }
  197. if (message.contentType === MessageType.OANotification) {
  198. let notificationData = {};
  199. try {
  200. notificationData = JSON.parse(message.notificationElem.detail);
  201. } catch (error) {}
  202. if (notificationData.mixType === 1) {
  203. message.pictureElem.snapshotPicture.url =
  204. notificationData.pictureElem.sourcePicture.url;
  205. return true;
  206. }
  207. return false;
  208. }
  209. return false;
  210. })
  211. .map((item) => item.pictureElem.sourcePicture.url);
  212. }
  213. export default {
  214. namespaced: true,
  215. state,
  216. mutations,
  217. actions,
  218. };