| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import { v4 as uuidv4 } from "uuid";
- // import IMSDK from "openim-uniapp-polyfill";
- let IMSDK;
- const getIMSDK = () => {
- if (!IMSDK) {
- IMSDK = require("openim-uniapp-polyfill").default;
- }
- return IMSDK;
- };
- const state = {
- conversationList: [],
- currentConversation: {},
- unReadCount: 0,
- currentGroup: {},
- currentMemberInGroup: {},
- revokeMap: {}
- };
- const mutations = {
- SET_CONVERSATION_LIST(state, list) {
- state.conversationList = [...list];
- // 异步缓存到本地,避免阻塞主线程
- setTimeout(() => {
- try {
- uni.setStorageSync('IM_CONVERSATION_LIST_CACHE', list);
- } catch (e) {
- console.warn('Failed to cache conversation list', e);
- }
- }, 0);
- },
- SET_CURRENT_CONVERSATION(state, conversation) {
- state.currentConversation = {
- ...conversation,
- };
- },
- SET_UNREAD_COUNT(state, count) {
- if (count) {
- uni.setTabBarBadge({
- index: 3,
- text: count < 99 ? count + "" : "99+",
- });
- } else {
- uni.removeTabBarBadge({
- index: 0,
- });
- }
- state.unReadCount = count;
- },
- SET_CURRENT_GROUP(state, group) {
- state.currentGroup = {
- ...group,
- };
- },
- SET_CURRENT_MEMBER_IN_GROUP(state, member) {
- state.currentMemberInGroup = {
- ...member,
- };
- },
- SET_REVOKE_MAP(state, message) {
- state.revokeMap = {
- ...state,
- [message.clientMsgID]: {
- text: getMessageText(message),
- quoteMessage: message.quoteElem?.quoteMessage,
- }
- };
- },
- };
- const actions = {
- async getConversationList({ state, commit }, { isFirstPage = true, pageSize = 20 }) {
- try {
- const _IMSDK = getIMSDK();
- const { data } = await _IMSDK.asyncApi(
- _IMSDK.IMMethods.GetConversationListSplit,
- uuidv4(),
- {
- offset: isFirstPage ? 0 : state.conversationList.length,
- count: pageSize,
- },
- );
- const newList = isFirstPage ? data : [...state.conversationList, ...data];
- console.log("qxj newList",newList);
- commit("SET_CONVERSATION_LIST", newList);
- return {
- list: data,
- hasMore: data.length == pageSize,
- total: newList.length
- };
- } catch (e) {
- console.log("qxj getConversationList error",e);
- // 如果 SDK 获取失败,尝试保留当前数据而不是清空,除非是明确的清空指令
- // commit("SET_CONVERSATION_LIST", []);
- return [];
- }
- },
- // 新增:从本地缓存加载会话列表
- loadConversationListFromCache({ commit }) {
- try {
- const cache = uni.getStorageSync('IM_CONVERSATION_LIST_CACHE');
- if (cache && Array.isArray(cache) && cache.length > 0) {
- console.log('Loaded conversation list from cache', cache.length);
- commit("SET_CONVERSATION_LIST", cache);
- return true;
- }
- } catch (e) {
- console.warn('Failed to load conversation list from cache', e);
- }
- return false;
- },
- delConversationByCID({ state, commit }, conversationID) {
- const tmpList = [...state.conversationList];
- const idx = tmpList.findIndex(
- (conversation) => conversation.conversationID === conversationID,
- );
- if (idx > -1) {
- tmpList.splice(idx, 1);
- commit("SET_CONVERSATION_LIST", tmpList);
- }
- },
- getCurrentGroup({ commit }, groupID) {
- const _IMSDK = getIMSDK();
- _IMSDK.asyncApi(_IMSDK.IMMethods.GetSpecifiedGroupsInfo, uuidv4(), [
- groupID,
- ]).then(({ data }) => {
- commit("SET_CURRENT_GROUP", data[0] ?? {});
- });
- },
- getCurrentMemberInGroup({ commit, rootState }, groupID) {
- const _IMSDK = getIMSDK();
- _IMSDK.asyncApi(_IMSDK.IMMethods.GetSpecifiedGroupMembersInfo, uuidv4(), {
- groupID,
- userIDList: [rootState.user.selfInfo.userID],
- }).then(({ data }) => {
- commit("SET_CURRENT_MEMBER_IN_GROUP", data[0] ?? {});
- });
- },
- getUnReadCount({ commit }) {
- const _IMSDK = getIMSDK();
- _IMSDK.asyncApi(_IMSDK.IMMethods.GetTotalUnreadMsgCount, uuidv4()).then(
- (res) => {
- console.log(res);
- commit("SET_UNREAD_COUNT", res.data);
- },
- );
- },
- updateCurrentMemberInGroup({ commit, state }, memberInfo) {
- if (
- memberInfo.groupID === state.currentMemberInGroup.groupID &&
- memberInfo.userID === state.currentMemberInGroup.userID
- ) {
- commit("SET_CURRENT_MEMBER_IN_GROUP", memberInfo);
- }
- },
- resetConversationState({ commit }) {
- commit("SET_CURRENT_MEMBER_IN_GROUP", {});
- commit("SET_CURRENT_GROUP", {});
- commit("SET_CURRENT_CONVERSATION", {});
- },
- addRevokedMessage({ commit }, payload) {
- commit("SET_REVOKE_MAP", payload);
- }
- };
- const getMessageText = (message) => {
- if (message.atTextElem) {
- return message.atTextElem.text;
- }
- if (message.quoteElem) {
- return message.quoteElem.text;
- }
- return message.textElem.content;
- };
- export default {
- namespaced: true,
- state,
- mutations,
- actions,
- };
|