Files
im/store/modules/conversation.js
T

132 lines
3.0 KiB
JavaScript
Raw Normal View History

2025-11-25 05:36:02 +08:00
import {v4 as uuidv4} from "uuid";
2025-11-07 09:56:20 +08:00
import IMSDK from "openim-uniapp-polyfill";
const state = {
2025-11-21 01:20:28 +08:00
conversationList: [],
currentConversation: {},
unReadCount: 0,
currentGroup: {},
currentMemberInGroup: {},
2025-11-07 09:56:20 +08:00
};
const mutations = {
2025-11-21 01:20:28 +08:00
SET_CONVERSATION_LIST(state, list) {
state.conversationList = [...list];
},
SET_CURRENT_CONVERSATION(state, conversation) {
state.currentConversation = {
...conversation,
};
},
SET_UNREAD_COUNT(state, count) {
state.unReadCount = count;
},
SET_CURRENT_GROUP(state, group) {
state.currentGroup = {
...group,
};
},
SET_CURRENT_MEMBER_IN_GROUP(state, member) {
state.currentMemberInGroup = {
...member,
};
},
2025-11-07 09:56:20 +08:00
};
const actions = {
2025-11-25 05:36:02 +08:00
async getConversationList({state,commit}, isFirstPage = true) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-21 01:20:28 +08:00
try {
2025-11-25 05:36:02 +08:00
const {data} = await IMSDK.asyncApi(
2025-11-21 01:20:28 +08:00
IMSDK.IMMethods.GetConversationListSplit,
uuidv4(), {
offset: isFirstPage ? 0 : state.conversationList.length,
count: 500,
},
);
2026-01-12 18:07:21 +08:00
//console.log(data);
2025-11-21 01:20:28 +08:00
commit("SET_CONVERSATION_LIST", [
...(isFirstPage ? [] : state.conversationList),
...data,
]);
return [...data];
} catch (e) {
console.log(e);
commit("SET_CONVERSATION_LIST", []);
return [];
}
},
2025-11-25 05:36:02 +08:00
getCurrentGroup({commit}, groupID) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-21 01:20:28 +08:00
IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupsInfo, uuidv4(), [
groupID,
]).then(({
data
}) => {
commit("SET_CURRENT_GROUP", data[0] ?? {});
});
},
2025-11-25 05:36:02 +08:00
getCurrentMemberInGroup({commit,rootState}, groupID) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-21 01:20:28 +08:00
IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupMembersInfo, uuidv4(), {
groupID,
userIDList: [rootState.user.selfInfo.userID],
}).then(({
data
}) => {
commit("SET_CURRENT_MEMBER_IN_GROUP", data[0] ?? {});
});
},
2025-11-25 05:36:02 +08:00
getUnReadCount({commit}) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-21 01:20:28 +08:00
IMSDK.asyncApi(IMSDK.IMMethods.GetTotalUnreadMsgCount, uuidv4()).then(
(res) => {
2025-11-25 05:36:02 +08:00
//console.log(res);
2025-11-21 01:20:28 +08:00
commit("SET_UNREAD_COUNT", res.data);
},
);
},
2025-11-25 05:36:02 +08:00
updateCurrentMemberInGroup({commit,state}, memberInfo) {
2026-01-10 15:40:38 +08:00
//console.log(memberInfo);
2025-11-21 01:20:28 +08:00
if (
memberInfo.groupID === state.currentMemberInGroup.groupID &&
memberInfo.userID === state.currentMemberInGroup.userID
) {
commit("SET_CURRENT_MEMBER_IN_GROUP", memberInfo);
}
},
2026-02-15 19:40:36 +08:00
resetConversationState({commit}) {
2025-11-21 01:20:28 +08:00
commit("SET_CURRENT_MEMBER_IN_GROUP", {});
commit("SET_CURRENT_GROUP", {});
commit("SET_CURRENT_CONVERSATION", {});
},
2026-02-15 19:40:36 +08:00
deleteConversation({state,commit},conversationID) {
return new Promise((reject,resolve)=>{
IMSDK.asyncApi('deleteConversationAndDeleteAllMsg',uuidv4(), conversationID).then(res=>{
const list = state.conversationList.filter((item)=>{
return item.conversationID != conversationID;
});
commit("SET_CONVERSATION_LIST", [...list]);
resolve();
}).catch((e)=>{
console.error(e);
reject(e);
})
})
},
2025-11-07 09:56:20 +08:00
};
export default {
2025-11-21 01:20:28 +08:00
namespaced: true,
state,
mutations,
actions,
};