message.js 6.0 KB

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