conversation.js 4.0 KB

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