Files
im/store/modules/message.js
T

85 lines
2.0 KiB
JavaScript
Raw Normal View History

2025-11-23 01:01:52 +08:00
import IMSDK, {
MessageStatus,
MessageType
} from "openim-uniapp-polyfill";
import {
v4 as uuidv4
} from "uuid";
import {
UpdateMessageTypes
} from "@/constant";
2025-11-07 09:56:20 +08:00
const state = {
2025-11-23 01:01:52 +08:00
historyMessageList: [],
hasMoreMessage: true,
2025-11-07 09:56:20 +08:00
};
const mutations = {
2025-11-23 01:01:52 +08:00
SET_HISTORY_MESSAGE_LIST(state, list) {
state.historyMessageList = [...list];
},
SET_HAS_MORE_MESSAGE(state, hasMore) {
state.hasMoreMessage = hasMore;
},
2025-11-07 09:56:20 +08:00
};
const actions = {
2025-12-05 16:10:52 +08:00
async getHistoryMesageList({commit,state}, params) {
2025-11-23 01:01:52 +08:00
let emptyFlag = true;
try {
2025-12-05 16:10:52 +08:00
//console.log(params);
const {data} = await IMSDK.asyncApi(
2025-11-23 01:01:52 +08:00
IMSDK.IMMethods.GetAdvancedHistoryMessageList,
uuidv4(),
params,
);
2025-12-05 16:10:52 +08:00
//console.log(data);
2025-11-23 01:01:52 +08:00
const isFistPage = !params.startClientMsgID
const messages = data.messageList ?? [];
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,
};
},
2025-12-05 16:10:52 +08:00
pushNewMessage({commit,state}, message) {
2025-11-23 01:01:52 +08:00
commit("SET_HISTORY_MESSAGE_LIST", [...state.historyMessageList, message]);
},
2025-12-05 16:10:52 +08:00
updateOneMessage({commit,state}, {message,type = UpdateMessageTypes.Overall,keyWords = [],isSuccess = false,}, ) {
2025-11-23 01:01:52 +08:00
const tmpList = state.historyMessageList;
const idx = tmpList.findIndex(
(msg) => msg.clientMsgID === message.clientMsgID,
);
if (idx !== -1) {
if (type === UpdateMessageTypes.Overall) {
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);
}
},
2025-12-05 16:10:52 +08:00
resetMessageState({commit}) {
2025-11-23 01:01:52 +08:00
commit("SET_HISTORY_MESSAGE_LIST", []);
commit("SET_HAS_MORE_MESSAGE", true);
},
2025-11-07 09:56:20 +08:00
};
export default {
2025-11-23 01:01:52 +08:00
namespaced: true,
state,
mutations,
actions,
};