123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- import IMSDK, {
- MessageStatus,
- MessageType
- } from "openim-uniapp-polyfill";
- import {
- v4 as uuidv4
- } from "uuid";
- import {
- UpdateMessageTypes,
- FileMessageTypes
- } from "@/pages_im/constant";
- const state = {
- historyMessageList: [],
- previewImageList: [],
- hasMoreMessage: true,
- quoteMessage: undefined,
- };
- const mutations = {
- SET_HISTORY_MESSAGE_LIST(state, list) {
- state.historyMessageList = [...list];
- },
- SET_PREVIEW_IMAGE_LIST(state, list) {
- state.previewImageList = [...list];
- },
- SET_HAS_MORE_MESSAGE(state, hasMore) {
- state.hasMoreMessage = hasMore;
- },
- SET_QUOTE_MESSAGE(state, message) {
- state.quoteMessage = message;
- },
- };
- const actions = {
- async getHistoryMesageList({
- commit,
- state
- }, params) {
- let emptyFlag = true;
- try {
- console.log("getHistoryMesageList params:::");
- console.log(params);
- const {
- data
- } = await IMSDK.asyncApi(
- IMSDK.IMMethods.GetAdvancedHistoryMessageList,
- uuidv4(),
- params,
- );
-
- const isFistPage = !params.startClientMsgID;
- const messages = data.messageList ?? [];
- if (!params.conversationID.includes("sn_")) {
- const imageList = filterPreviewImage([...messages]);
- commit("SET_PREVIEW_IMAGE_LIST", [
- ...imageList,
- ...(isFistPage ? [] : state.previewImageList),
- ]);
- }
- emptyFlag = messages.length === 0;
- commit("SET_HISTORY_MESSAGE_LIST", [
- ...messages,
- ...(isFistPage ? [] : state.historyMessageList),
- ]);
- commit("SET_HAS_MORE_MESSAGE", !data.isEnd && messages.length === 20);
- } catch (e) {
- commit("SET_HISTORY_MESSAGE_LIST", []);
- }
- return {
- emptyFlag,
- };
- },
- pushNewPreviewImage({
- state,
- commit
- }, url) {
- commit("SET_PREVIEW_IMAGE_LIST", [...state.previewImageList, url]);
- },
- pushNewMessage({
- commit,
- state
- }, message) {
- if (
- message.contentType === MessageType.PictureMessage &&
- message.status === MessageStatus.Succeed
- ) {
- const imageList = filterPreviewImage([message]);
- if (imageList.length > 0) {
- commit("SET_PREVIEW_IMAGE_LIST", [
- ...state.previewImageList,
- ...imageList,
- ]);
- }
- }
- commit("SET_HISTORY_MESSAGE_LIST", [...state.historyMessageList, message]);
- },
- updateOneMessage({commit,dispatch,state}, {
- message,
- type = UpdateMessageTypes.Overall,
- keyWords = [],
- isSuccess = false,
- }) {
- const tmpList = state.historyMessageList;
- console.log("qxj updateOneMessage message:"+JSON.stringify(message));
- const idx = tmpList.findIndex((msg) => msg.clientMsgID === message.clientMsgID);
- if (idx !== -1) {
- if (type === UpdateMessageTypes.Overall) {
- console.log("qxj FileMessageTypes:" + FileMessageTypes);
- if (FileMessageTypes.includes(message.contentType) && isSuccess) {
- let filePath = "";
- switch (message.contentType) {
- case MessageType.FileMessage:
- filePath = message.fileElem.filePath;
- break;
- case MessageType.PictureMessage:
- filePath = message.pictureElem.sourcePath;
- break;
- case MessageType.VideoMessage:
- filePath = message.videoElem.videoUrl;
- break;
- case MessageType.VoiceMessage:
- filePath = message.soundElem.soundPath;
- break;
- default:
- break;
- }
- if (filePath) {
- dispatch("user/addLocalPathToCache", {
- key: message.clientMsgID,
- path: filePath
- })
- }
- }
- if (message.contentType === MessageType.PictureMessage && isSuccess) {
- const imageList = filterPreviewImage([message]);
- if (imageList.length > 0) {
- commit("SET_PREVIEW_IMAGE_LIST", [
- ...state.previewImageList,
- ...imageList,
- ]);
- }
- }
- tmpList[idx] = {
- ...message,
- };
- } else if (type === UpdateMessageTypes.KeyWords) {
- const updateFields = Array.isArray(keyWords) ? keyWords : [keyWords];
- updateFields.forEach(
- (field) => (tmpList[idx][field.key] = field.value),
- );
- }
- commit("SET_HISTORY_MESSAGE_LIST", tmpList);
- }
- },
- deleteMessages({
- commit,
- state
- }, messages) {
- const tmpList = [...state.historyMessageList];
- messages.map((message) => {
- const idx = tmpList.findIndex(
- (msg) => msg.clientMsgID === message.clientMsgID,
- );
- if (idx !== -1) {
- tmpList.splice(idx, 1);
- }
- });
- commit("SET_HISTORY_MESSAGE_LIST", tmpList);
- },
- resetMessageState({
- commit
- }) {
- commit("SET_HISTORY_MESSAGE_LIST", []);
- commit("SET_PREVIEW_IMAGE_LIST", []);
- commit("SET_HAS_MORE_MESSAGE", true);
- commit("SET_QUOTE_MESSAGE", undefined);
- },
- updateMessageNicknameAndFaceUrl({
- commit,
- state
- }, {
- sendID,
- senderFaceUrl,
- senderNickname
- }, ) {
- const tmpList = [...state.historyMessageList].map((message) => {
- if (message.sendID === sendID) {
- message.senderFaceUrl = senderFaceUrl;
- message.senderNickname = senderNickname;
- }
- return message;
- });
- commit("SET_HISTORY_MESSAGE_LIST", [...tmpList]);
- },
- updateQuoteMessageRevoke({
- dispatch,
- state
- }, {
- clientMsgID
- }) {
- [...state.historyMessageList].map((message) => {
- if (message.contentType === MessageType.QuoteMessage && clientMsgID === message.quoteElem
- .quoteMessage.clientMsgID) {
- dispatch('updateOneMessage', {
- message: {
- ...message,
- quoteElem: {
- ...message.quoteElem,
- quoteMessage: {
- ...message.quoteElem.quoteMessage,
- contentType: 2101
- }
- }
- }
- })
- }
- return null;
- });
- }
- };
- function filterPreviewImage(messages) {
- return messages
- .filter((message) => {
- if (message.contentType === MessageType.PictureMessage) {
- return true;
- }
- if (message.contentType === MessageType.OANotification) {
- let notificationData = {};
- try {
- notificationData = JSON.parse(message.notificationElem.detail);
- } catch (error) {}
- if (notificationData.mixType === 1) {
- message.pictureElem.snapshotPicture.url =
- notificationData.pictureElem.sourcePicture.url;
- return true;
- }
- return false;
- }
- return false;
- })
- .map((item) => item.pictureElem.sourcePicture.url);
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions,
- };
|