message.js 6.1 KB

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