Files
im/store/modules/contact.js
T

347 lines
8.8 KiB
JavaScript
Raw Normal View History

2026-02-15 19:40:36 +08:00
import {v4 as uuidv4} from "uuid";
2025-11-07 09:56:20 +08:00
import IMSDK from "openim-uniapp-polyfill";
2026-02-15 19:40:36 +08:00
import conversation from "./conversation";
2025-11-07 09:56:20 +08:00
const state = {
2025-11-23 01:01:52 +08:00
friendList: [],
blackList: [],
groupList: [],
recvFriendApplications: [],
sentFriendApplications: [],
recvGroupApplications: [],
sentGroupApplications: [],
unHandleFriendApplicationNum: 0,
unHandleGroupApplicationNum: 0,
2025-11-07 09:56:20 +08:00
};
const mutations = {
2025-11-23 01:01:52 +08:00
SET_FRIEND_LIST(state, list) {
state.friendList = [...list];
},
SET_BLACK_LIST(state, list) {
state.blackList = [...list];
},
SET_GROUP_LIST(state, list) {
state.groupList = [...list];
},
SET_RECV_FRIEND_APPLICATIONS(state, list) {
state.recvFriendApplications = [...list];
2026-01-01 04:15:30 +08:00
const count = list.filter((item) => item.handleResult === 0);
state.unHandleFriendApplicationNum = count.length;
2025-11-23 01:01:52 +08:00
},
SET_SENT_FRIEND_APPLICATIONS(state, list) {
state.sentFriendApplications = [...list];
},
SET_RECV_GROUP_APPLICATIONS(state, list) {
state.recvGroupApplications = [...list];
2026-01-15 22:50:35 +08:00
const count = list.filter((item) => item.handleResult === 0);
state.unHandleGroupApplicationNum = count.length;
2025-11-23 01:01:52 +08:00
},
SET_SENT_GROUP_APPLICATIONS(state, list) {
state.sentGroupApplications = [...list];
},
2025-11-07 09:56:20 +08:00
};
const actions = {
2025-11-27 07:40:32 +08:00
async getFriendList({commit }) {
2025-11-23 01:01:52 +08:00
let offset = 0;
let friendInfoList = [];
let initialFetch = true;
while (true) {
try {
const count = initialFetch ? 10000 : 1000;
2025-12-02 03:05:52 +08:00
const {data} = await IMSDK.asyncApi("getFriendListPage", uuidv4(), {offset,count,filterBlack:true});
2025-11-27 07:40:32 +08:00
//console.log(data);
2025-11-23 01:01:52 +08:00
friendInfoList = [
...friendInfoList,
...data,
];
offset += count;
if (data.length < count) break;
initialFetch = false;
} catch (error) {
2026-01-01 04:15:30 +08:00
console.error("getFriendListPage error",error);
2026-01-09 09:15:59 +08:00
break;
2025-11-23 01:01:52 +08:00
}
}
commit("SET_FRIEND_LIST", friendInfoList);
},
2025-11-27 07:40:32 +08:00
async getGrouplist({commit}) {
2025-11-23 01:01:52 +08:00
let offset = 0;
let groupList = [];
while (true) {
try {
const {
data
} = await IMSDK.asyncApi(
"getJoinedGroupListPage",
uuidv4(), {
offset,
count: 1000,
}
);
groupList = [...groupList, ...data];
offset += 1000;
if (data.length < 1000) break;
} catch (error) {
console.error("getGrouplist error");
}
}
2026-01-09 09:15:59 +08:00
//console.log(groupList);
2025-11-23 01:01:52 +08:00
commit("SET_GROUP_LIST", groupList);
},
2025-11-27 07:40:32 +08:00
getBlacklist({commit}) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-23 01:01:52 +08:00
IMSDK.asyncApi(IMSDK.IMMethods.GetBlackList, uuidv4()).then(({
data
}) => {
commit("SET_BLACK_LIST", data);
});
},
2025-11-27 07:40:32 +08:00
getRecvFriendApplications({commit}) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-23 01:01:52 +08:00
IMSDK.asyncApi(
IMSDK.IMMethods.GetFriendApplicationListAsRecipient,
uuidv4(),
).then(({
data
}) => {
commit("SET_RECV_FRIEND_APPLICATIONS", data);
});
},
2025-11-27 07:40:32 +08:00
getSentFriendApplications({ commit }) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-23 01:01:52 +08:00
IMSDK.asyncApi(
IMSDK.IMMethods.GetFriendApplicationListAsApplicant,
uuidv4(),
).then(({
data
}) => {
commit("SET_SENT_FRIEND_APPLICATIONS", data);
});
},
2026-01-15 22:50:35 +08:00
getRecvGroupApplications({ commit,state}) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-23 01:01:52 +08:00
IMSDK.asyncApi(
IMSDK.IMMethods.GetGroupApplicationListAsRecipient,
uuidv4(),
).then(({
data
}) => {
commit("SET_RECV_GROUP_APPLICATIONS", data);
});
},
2025-11-27 07:40:32 +08:00
getSentGroupApplications({ commit }) {
2025-12-24 04:12:56 +08:00
//#ifndef APP
return [];
//#endif
2025-11-23 01:01:52 +08:00
IMSDK.asyncApi(
IMSDK.IMMethods.GetGroupApplicationListAsApplicant,
uuidv4(),
).then(({
data
}) => {
commit("SET_SENT_GROUP_APPLICATIONS", data);
});
},
2025-11-27 07:40:32 +08:00
pushNewFriend({ commit, state}, friendInfo) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.friendList];
const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
if (idx === -1) {
commit("SET_FRIEND_LIST", [...tmpList, friendInfo]);
}
},
2026-02-15 19:40:36 +08:00
updateFriendInfo({commit,state,dispatch }, { friendInfo,isRemove = false}) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.friendList];
const idx = tmpList.findIndex((item) => item.userID === friendInfo.userID);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...friendInfo,
};
}
commit("SET_FRIEND_LIST", tmpList);
}
2026-02-15 19:40:36 +08:00
if (isRemove) {
const conversationID = friendInfo.userID ? `si_${friendInfo.ownerUserID}_${friendInfo.userID}` : `si_${friendInfo.groupID}`;
dispatch('conversation/deleteConversation',conversationID, { root: true });
}
2025-11-23 01:01:52 +08:00
},
2025-11-27 07:40:32 +08:00
pushNewBlack({ commit, state}, blackInfo) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.blackList];
const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
if (idx === -1) {
commit("SET_BLACK_LIST", [...tmpList, blackInfo]);
}
},
2025-11-27 07:40:32 +08:00
updateBlackInfo({commit,state}, {blackInfo,isRemove = false}) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.blackList];
const idx = tmpList.findIndex((item) => item.userID === blackInfo.userID);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...blackInfo,
};
}
commit("SET_BLACK_LIST", tmpList);
}
},
2025-11-27 07:40:32 +08:00
pushNewGroup({commit,state}, groupInfo) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.groupList];
const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
if (idx === -1) {
commit("SET_GROUP_LIST", [...tmpList, groupInfo]);
}
},
2025-11-27 07:40:32 +08:00
updateGroupInfo({commit,state,rootState}, {groupInfo,isRemove = false}, ) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.groupList];
const idx = tmpList.findIndex((item) => item.groupID === groupInfo.groupID);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (rootState.conversation.currentGroup.groupID === groupInfo.groupID) {
commit("conversation/SET_CURRENT_GROUP", groupInfo, {
root: true
});
}
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...groupInfo,
};
}
commit("SET_GROUP_LIST", tmpList);
}
},
2025-11-27 07:40:32 +08:00
pushNewRecvFriendApplition({commit,state}, application) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.recvFriendApplications];
const idx = tmpList.findIndex(
(item) => item.fromUserID === application.fromUserID,
);
if (idx !== -1) {
tmpList.splice(idx, 1);
}
commit("SET_RECV_FRIEND_APPLICATIONS", [...tmpList, application]);
},
2025-11-27 07:40:32 +08:00
updateRecvFriendApplition({commit,state,rootState}, {application,isRemove = false}, ) {
2026-01-15 22:50:35 +08:00
console.log(application);
2025-11-23 01:01:52 +08:00
const tmpList = [...state.recvFriendApplications];
const idx = tmpList.findIndex(
(item) => item.fromUserID === application.fromUserID,
);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...application,
};
}
commit("SET_RECV_FRIEND_APPLICATIONS", tmpList);
}
},
2025-11-27 07:40:32 +08:00
pushNewSentFriendApplition({commit,state}, application) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.sentFriendApplications];
const idx = tmpList.findIndex(
(item) => item.toUserID === application.toUserID,
);
if (idx !== -1) {
tmpList.splice(idx, 1);
}
commit("SET_SENT_FRIEND_APPLICATIONS", [...tmpList, application]);
},
2025-11-27 07:40:32 +08:00
updateSentFriendApplition({commit,state,rootState}, {application,isRemove = false}, ) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.sentFriendApplications];
const idx = tmpList.findIndex(
(item) => item.toUserID === application.toUserID,
);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...application,
};
}
commit("SET_SENT_FRIEND_APPLICATIONS", tmpList);
}
},
2025-11-27 07:40:32 +08:00
pushNewRecvGroupApplition({commit,state}, application) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.recvGroupApplications];
const idx = tmpList.findIndex((item) => item.userID === application.userID);
if (idx !== -1) {
tmpList.splice(idx, 1);
}
commit("SET_RECV_GROUP_APPLICATIONS", [...tmpList, application]);
2026-01-15 22:50:35 +08:00
// const newList = state.recvGroupApplications.filter((item)=>{
// item.userID !== application.userID
// });
// commit("SET_RECV_GROUP_APPLICATIONS", [...newList, application]);
2025-11-23 01:01:52 +08:00
},
2025-11-27 07:40:32 +08:00
updateRecvGroupApplition({commit,state,rootState}, {application,isRemove = false}, ) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.recvGroupApplications];
const idx = tmpList.findIndex((item) => item.userID === application.userID);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...application,
};
}
commit("SET_RECV_GROUP_APPLICATIONS", tmpList);
}
},
2025-11-27 07:40:32 +08:00
pushNewSentGroupApplition({commit,state}, application) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.sentGroupApplications];
const idx = tmpList.findIndex(
(item) => item.groupID === application.groupID,
);
if (idx !== -1) {
tmpList.splice(idx, 1);
}
commit("SET_SENT_GROUP_APPLICATIONS", [...tmpList, application]);
},
2025-11-27 07:40:32 +08:00
updateSentGroupApplition({commit,state,rootState}, {application,isRemove = false}, ) {
2025-11-23 01:01:52 +08:00
const tmpList = [...state.sentGroupApplications];
const idx = tmpList.findIndex(
(item) => item.groupID === application.groupID,
);
2025-11-07 09:56:20 +08:00
2025-11-23 01:01:52 +08:00
if (idx !== -1) {
if (isRemove) {
tmpList.splice(idx, 1);
} else {
tmpList[idx] = {
...application,
};
}
commit("SET_SENT_GROUP_APPLICATIONS", tmpList);
}
},
2025-11-07 09:56:20 +08:00
};
export default {
2025-11-23 01:01:52 +08:00
namespaced: true,
state,
mutations,
actions,
};