message.js 6.7 KB

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