conversation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. v4 as uuidv4
  3. } from "uuid";
  4. import IMSDK from "openim-uniapp-polyfill";
  5. const state = {
  6. conversationList: [],
  7. currentConversation: {},
  8. unReadCount: 0,
  9. currentGroup: {},
  10. currentMemberInGroup: {},
  11. revokeMap: {}
  12. };
  13. const mutations = {
  14. SET_CONVERSATION_LIST(state, list) {
  15. state.conversationList = [...list];
  16. },
  17. SET_CURRENT_CONVERSATION(state, conversation) {
  18. state.currentConversation = {
  19. ...conversation,
  20. };
  21. },
  22. SET_UNREAD_COUNT(state, count) {
  23. console.log("qxj SET_UNREAD_COUNT",count);
  24. if(count>0) {
  25. uni.setTabBarBadge({index: 0,text: "..."});
  26. // #ifdef APP-PLUS
  27. if(count==1){
  28. plus.runtime.setBadgeNumber(-1);
  29. }
  30. else{
  31. plus.runtime.setBadgeNumber(count);
  32. }
  33. //#endif
  34. } else {
  35. uni.removeTabBarBadge({ index: 0 });
  36. // #ifdef APP-PLUS
  37. const sysInfo = uni.getSystemInfoSync();
  38. const brand = (sysInfo.brand || '').toLowerCase();
  39. if (brand === 'vivo') {
  40. plus.runtime.setBadgeNumber(-1);
  41. } else {
  42. plus.runtime.setBadgeNumber(0);
  43. }
  44. //#endif
  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) {
  70. try {
  71. let uuid=uuidv4();
  72. const {data} = await IMSDK.asyncApi(IMSDK.IMMethods.GetConversationListSplit,uuidv4(), {
  73. offset: isFirstPage ? 0 : state.conversationList.length,
  74. count: 200,
  75. }
  76. );
  77. const conversationData = data || [];
  78. commit("SET_CONVERSATION_LIST", [
  79. ...(isFirstPage ? [] : state.conversationList),
  80. ...conversationData,
  81. ]);
  82. return conversationData;
  83. } catch (e) {
  84. commit("SET_CONVERSATION_LIST", []);
  85. return [];
  86. }
  87. },
  88. delConversationByCID({state,commit}, conversationID) {
  89. const tmpList = [...state.conversationList];
  90. const idx = tmpList.findIndex(
  91. (conversation) => conversation.conversationID === conversationID,
  92. );
  93. if (idx > -1) {
  94. tmpList.splice(idx, 1);
  95. commit("SET_CONVERSATION_LIST", tmpList);
  96. }
  97. },
  98. getCurrentGroup({commit}, groupID) {
  99. console.log("qxj getCurrentGroup groupID",groupID);
  100. IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupsInfo, uuidv4(), [
  101. groupID
  102. ]).then(({data}) => {
  103. console.log("qxj getCurrentGroup data",data);
  104. commit("SET_CURRENT_GROUP", data[0] ?? {});
  105. });
  106. },
  107. getCurrentMemberInGroup({
  108. commit,
  109. rootState
  110. }, groupID) {
  111. IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupMembersInfo, uuidv4(), {
  112. groupID,
  113. userIDList: [rootState.user.selfInfo.userID],
  114. }).then(({
  115. data
  116. }) => {
  117. commit("SET_CURRENT_MEMBER_IN_GROUP", data[0] ?? {});
  118. });
  119. },
  120. getUnReadCount({
  121. commit
  122. }) {
  123. IMSDK.asyncApi(IMSDK.IMMethods.GetTotalUnreadMsgCount, uuidv4()).then((res) => {
  124. commit("SET_UNREAD_COUNT", res.data);
  125. }, );
  126. },
  127. updateCurrentMemberInGroup({
  128. commit,
  129. state
  130. }, memberInfo) {
  131. if (memberInfo.groupID === state.currentMemberInGroup.groupID && memberInfo.userID === state
  132. .currentMemberInGroup.userID) {
  133. commit("SET_CURRENT_MEMBER_IN_GROUP", memberInfo);
  134. }
  135. },
  136. resetConversationState({
  137. commit
  138. }) {
  139. commit("SET_CURRENT_MEMBER_IN_GROUP", {});
  140. commit("SET_CURRENT_GROUP", {});
  141. commit("SET_CURRENT_CONVERSATION", {});
  142. },
  143. addRevokedMessage({
  144. commit
  145. }, payload) {
  146. commit("SET_REVOKE_MAP", payload);
  147. }
  148. };
  149. const getMessageText = (message) => {
  150. if (message.atTextElem) {
  151. return message.atTextElem.text;
  152. }
  153. if (message.quoteElem) {
  154. return message.quoteElem.text;
  155. }
  156. return message.textElem.content;
  157. };
  158. export default {
  159. namespaced: true,
  160. state,
  161. mutations,
  162. actions,
  163. };