conversation.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { v4 as uuidv4 } from "uuid";
  2. import IMSDK from "openim-uniapp-polyfill";
  3. const state = {
  4. conversationList: [],
  5. currentConversation: {},
  6. unReadCount: 0,
  7. currentGroup: {},
  8. currentMemberInGroup: {},
  9. revokeMap: {}
  10. };
  11. const mutations = {
  12. SET_CONVERSATION_LIST(state, list) {
  13. state.conversationList = [...list];
  14. },
  15. SET_CURRENT_CONVERSATION(state, conversation) {
  16. state.currentConversation = {
  17. ...conversation,
  18. };
  19. },
  20. SET_UNREAD_COUNT(state, count) {
  21. if (count) {
  22. uni.setTabBarBadge({
  23. index: 0,
  24. text: count < 99 ? count + "" : "99+",
  25. });
  26. } else {
  27. uni.removeTabBarBadge({
  28. index: 0,
  29. });
  30. }
  31. state.unReadCount = count;
  32. },
  33. SET_CURRENT_GROUP(state, group) {
  34. state.currentGroup = {
  35. ...group,
  36. };
  37. },
  38. SET_CURRENT_MEMBER_IN_GROUP(state, member) {
  39. state.currentMemberInGroup = {
  40. ...member,
  41. };
  42. },
  43. SET_REVOKE_MAP(state, message) {
  44. state.revokeMap = {
  45. ...state,
  46. [message.clientMsgID]: {
  47. text: getMessageText(message),
  48. quoteMessage: message.quoteElem?.quoteMessage,
  49. }
  50. };
  51. },
  52. };
  53. const actions = {
  54. async getConversationList({ state, commit }, isFirstPage = true) {
  55. try {
  56. const { data } = await IMSDK.asyncApi(
  57. IMSDK.IMMethods.GetConversationListSplit,
  58. uuidv4(),
  59. {
  60. offset: isFirstPage ? 0 : state.conversationList.length,
  61. count: 500,
  62. },
  63. );
  64. commit("SET_CONVERSATION_LIST", [
  65. ...(isFirstPage ? [] : state.conversationList),
  66. ...data,
  67. ]);
  68. return [...data];
  69. } catch (e) {
  70. console.log(e);
  71. commit("SET_CONVERSATION_LIST", []);
  72. return [];
  73. }
  74. },
  75. delConversationByCID({ state, commit }, conversationID) {
  76. const tmpList = [...state.conversationList];
  77. const idx = tmpList.findIndex(
  78. (conversation) => conversation.conversationID === conversationID,
  79. );
  80. if (idx > -1) {
  81. tmpList.splice(idx, 1);
  82. commit("SET_CONVERSATION_LIST", tmpList);
  83. }
  84. },
  85. getCurrentGroup({ commit }, groupID) {
  86. IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupsInfo, uuidv4(), [
  87. groupID,
  88. ]).then(({ data }) => {
  89. commit("SET_CURRENT_GROUP", data[0] ?? {});
  90. });
  91. },
  92. getCurrentMemberInGroup({ commit, rootState }, groupID) {
  93. IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupMembersInfo, uuidv4(), {
  94. groupID,
  95. userIDList: [rootState.user.selfInfo.userID],
  96. }).then(({ data }) => {
  97. commit("SET_CURRENT_MEMBER_IN_GROUP", data[0] ?? {});
  98. });
  99. },
  100. getUnReadCount({ commit }) {
  101. IMSDK.asyncApi(IMSDK.IMMethods.GetTotalUnreadMsgCount, uuidv4()).then((res) => {
  102. console.log(res);
  103. commit("SET_UNREAD_COUNT", res.data);
  104. },
  105. );
  106. },
  107. updateCurrentMemberInGroup({ commit, state }, memberInfo) {
  108. if (
  109. memberInfo.groupID === state.currentMemberInGroup.groupID &&
  110. memberInfo.userID === state.currentMemberInGroup.userID
  111. ) {
  112. commit("SET_CURRENT_MEMBER_IN_GROUP", memberInfo);
  113. }
  114. },
  115. resetConversationState({ commit }) {
  116. commit("SET_CURRENT_MEMBER_IN_GROUP", {});
  117. commit("SET_CURRENT_GROUP", {});
  118. commit("SET_CURRENT_CONVERSATION", {});
  119. },
  120. addRevokedMessage({ commit }, payload) {
  121. commit("SET_REVOKE_MAP", payload);
  122. }
  123. };
  124. const getMessageText = (message) => {
  125. if (message.atTextElem) {
  126. return message.atTextElem.text;
  127. }
  128. if (message.quoteElem) {
  129. return message.quoteElem.text;
  130. }
  131. return message.textElem.content;
  132. };
  133. export default {
  134. namespaced: true,
  135. state,
  136. mutations,
  137. actions,
  138. };