conversation.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { v4 as uuidv4 } from "uuid";
  2. // import IMSDK from "openim-uniapp-polyfill";
  3. let IMSDK;
  4. const getIMSDK = () => {
  5. if (!IMSDK) {
  6. IMSDK = require("openim-uniapp-polyfill").default;
  7. }
  8. return IMSDK;
  9. };
  10. const state = {
  11. conversationList: [],
  12. currentConversation: {},
  13. unReadCount: 0,
  14. currentGroup: {},
  15. currentMemberInGroup: {},
  16. revokeMap: {}
  17. };
  18. const mutations = {
  19. SET_CONVERSATION_LIST(state, list) {
  20. state.conversationList = [...list];
  21. // 异步缓存到本地,避免阻塞主线程
  22. setTimeout(() => {
  23. try {
  24. uni.setStorageSync('IM_CONVERSATION_LIST_CACHE', list);
  25. } catch (e) {
  26. console.warn('Failed to cache conversation list', e);
  27. }
  28. }, 0);
  29. },
  30. SET_CURRENT_CONVERSATION(state, conversation) {
  31. state.currentConversation = {
  32. ...conversation,
  33. };
  34. },
  35. SET_UNREAD_COUNT(state, count) {
  36. if (count) {
  37. uni.setTabBarBadge({
  38. index: 3,
  39. text: count < 99 ? count + "" : "99+",
  40. });
  41. } else {
  42. uni.removeTabBarBadge({
  43. index: 0,
  44. });
  45. }
  46. state.unReadCount = count;
  47. },
  48. SET_CURRENT_GROUP(state, group) {
  49. state.currentGroup = {
  50. ...group,
  51. };
  52. },
  53. SET_CURRENT_MEMBER_IN_GROUP(state, member) {
  54. state.currentMemberInGroup = {
  55. ...member,
  56. };
  57. },
  58. SET_REVOKE_MAP(state, message) {
  59. state.revokeMap = {
  60. ...state,
  61. [message.clientMsgID]: {
  62. text: getMessageText(message),
  63. quoteMessage: message.quoteElem?.quoteMessage,
  64. }
  65. };
  66. },
  67. };
  68. const actions = {
  69. async getConversationList({ state, commit }, { isFirstPage = true, pageSize = 20 }) {
  70. try {
  71. const _IMSDK = getIMSDK();
  72. const { data } = await _IMSDK.asyncApi(
  73. _IMSDK.IMMethods.GetConversationListSplit,
  74. uuidv4(),
  75. {
  76. offset: isFirstPage ? 0 : state.conversationList.length,
  77. count: pageSize,
  78. },
  79. );
  80. const newList = isFirstPage ? data : [...state.conversationList, ...data];
  81. console.log("qxj newList",newList);
  82. commit("SET_CONVERSATION_LIST", newList);
  83. return {
  84. list: data,
  85. hasMore: data.length == pageSize,
  86. total: newList.length
  87. };
  88. } catch (e) {
  89. console.log("qxj getConversationList error",e);
  90. // 如果 SDK 获取失败,尝试保留当前数据而不是清空,除非是明确的清空指令
  91. // commit("SET_CONVERSATION_LIST", []);
  92. return [];
  93. }
  94. },
  95. // 新增:从本地缓存加载会话列表
  96. loadConversationListFromCache({ commit }) {
  97. try {
  98. const cache = uni.getStorageSync('IM_CONVERSATION_LIST_CACHE');
  99. if (cache && Array.isArray(cache) && cache.length > 0) {
  100. console.log('Loaded conversation list from cache', cache.length);
  101. commit("SET_CONVERSATION_LIST", cache);
  102. return true;
  103. }
  104. } catch (e) {
  105. console.warn('Failed to load conversation list from cache', e);
  106. }
  107. return false;
  108. },
  109. delConversationByCID({ state, commit }, conversationID) {
  110. const tmpList = [...state.conversationList];
  111. const idx = tmpList.findIndex(
  112. (conversation) => conversation.conversationID === conversationID,
  113. );
  114. if (idx > -1) {
  115. tmpList.splice(idx, 1);
  116. commit("SET_CONVERSATION_LIST", tmpList);
  117. }
  118. },
  119. getCurrentGroup({ commit }, groupID) {
  120. const _IMSDK = getIMSDK();
  121. _IMSDK.asyncApi(_IMSDK.IMMethods.GetSpecifiedGroupsInfo, uuidv4(), [
  122. groupID,
  123. ]).then(({ data }) => {
  124. commit("SET_CURRENT_GROUP", data[0] ?? {});
  125. });
  126. },
  127. getCurrentMemberInGroup({ commit, rootState }, groupID) {
  128. const _IMSDK = getIMSDK();
  129. _IMSDK.asyncApi(_IMSDK.IMMethods.GetSpecifiedGroupMembersInfo, uuidv4(), {
  130. groupID,
  131. userIDList: [rootState.user.selfInfo.userID],
  132. }).then(({ data }) => {
  133. commit("SET_CURRENT_MEMBER_IN_GROUP", data[0] ?? {});
  134. });
  135. },
  136. getUnReadCount({ commit }) {
  137. const _IMSDK = getIMSDK();
  138. _IMSDK.asyncApi(_IMSDK.IMMethods.GetTotalUnreadMsgCount, uuidv4()).then(
  139. (res) => {
  140. console.log(res);
  141. commit("SET_UNREAD_COUNT", res.data);
  142. },
  143. );
  144. },
  145. updateCurrentMemberInGroup({ commit, state }, memberInfo) {
  146. if (
  147. memberInfo.groupID === state.currentMemberInGroup.groupID &&
  148. memberInfo.userID === state.currentMemberInGroup.userID
  149. ) {
  150. commit("SET_CURRENT_MEMBER_IN_GROUP", memberInfo);
  151. }
  152. },
  153. resetConversationState({ commit }) {
  154. commit("SET_CURRENT_MEMBER_IN_GROUP", {});
  155. commit("SET_CURRENT_GROUP", {});
  156. commit("SET_CURRENT_CONVERSATION", {});
  157. },
  158. addRevokedMessage({ commit }, payload) {
  159. commit("SET_REVOKE_MAP", payload);
  160. }
  161. };
  162. const getMessageText = (message) => {
  163. if (message.atTextElem) {
  164. return message.atTextElem.text;
  165. }
  166. if (message.quoteElem) {
  167. return message.quoteElem.text;
  168. }
  169. return message.textElem.content;
  170. };
  171. export default {
  172. namespaced: true,
  173. state,
  174. mutations,
  175. actions,
  176. };