conversation.js 3.6 KB

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