chatStore.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. export default {
  2. state: {
  3. activeIndex: -1,
  4. chats: []
  5. },
  6. mutations: {
  7. openChat(state, chatInfo) {
  8. let chat = null;
  9. let activeChat = state.activeIndex>=0?state.chats[state.activeIndex]:null;
  10. for (let i in state.chats) {
  11. if (state.chats[i].type == chatInfo.type &&
  12. state.chats[i].targetId === chatInfo.targetId) {
  13. chat = state.chats[i];
  14. // 放置头部
  15. state.chats.splice(i, 1);
  16. state.chats.unshift(chat);
  17. break;
  18. }
  19. }
  20. // 创建会话
  21. if (chat == null) {
  22. chat = {
  23. targetId: chatInfo.targetId,
  24. type: chatInfo.type,
  25. showName: chatInfo.showName,
  26. headImage: chatInfo.headImage,
  27. lastContent: "",
  28. lastSendTime: new Date().getTime(),
  29. unreadCount: 0,
  30. messages: [],
  31. };
  32. state.chats.unshift(chat);
  33. }
  34. // 选中会话保持不变
  35. if(activeChat){
  36. state.chats.forEach((chat,idx)=>{
  37. if(activeChat.type == chat.type
  38. && activeChat.targetId == chat.targetId){
  39. state.activeIndex = idx;
  40. }
  41. })
  42. }
  43. },
  44. activeChat(state, idx) {
  45. state.activeIndex = idx;
  46. state.chats[idx].unreadCount = 0;
  47. },
  48. removeChat(state, idx) {
  49. state.chats.splice(idx, 1);
  50. if (state.activeIndex >= state.chats.length) {
  51. state.activeIndex = state.chats.length - 1;
  52. }
  53. },
  54. removeGroupChat(state, groupId) {
  55. for (let idx in state.chats) {
  56. if (state.chats[idx].type == 'GROUP' &&
  57. state.chats[idx].targetId == groupId) {
  58. this.commit("removeChat", idx);
  59. }
  60. }
  61. },
  62. removePrivateChat(state, userId) {
  63. for (let idx in state.chats) {
  64. if (state.chats[idx].type == 'PRIVATE' &&
  65. state.chats[idx].targetId == userId) {
  66. this.commit("removeChat", idx);
  67. }
  68. }
  69. },
  70. insertMessage(state, msgInfo) {
  71. // 获取对方id或群id
  72. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  73. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  74. let chat = null;
  75. for (let idx in state.chats) {
  76. if (state.chats[idx].type == type &&
  77. state.chats[idx].targetId === targetId) {
  78. chat = state.chats[idx];
  79. break;
  80. }
  81. }
  82. // 插入新的数据
  83. if(msgInfo.type == 1){
  84. chat.lastContent = "[图片]";
  85. }else if(msgInfo.type == 2){
  86. chat.lastContent = "[文件]";
  87. }else if(msgInfo.type == 3){
  88. chat.lastContent = "[语音]";
  89. }else{
  90. chat.lastContent = msgInfo.content;
  91. }
  92. chat.lastSendTime = msgInfo.sendTime;
  93. // 如果不是当前会话,未读加1
  94. chat.unreadCount++;
  95. if(msgInfo.selfSend){
  96. chat.unreadCount=0;
  97. }
  98. // 如果是已存在消息,则覆盖旧的消息数据
  99. for (let idx in chat.messages) {
  100. if(msgInfo.id && chat.messages[idx].id == msgInfo.id){
  101. Object.assign(chat.messages[idx], msgInfo);
  102. return;
  103. }
  104. // 正在发送中的消息可能没有id,通过发送时间判断
  105. if(msgInfo.selfSend && chat.messages[idx].selfSend
  106. && chat.messages[idx].sendTime == msgInfo.sendTime){
  107. Object.assign(chat.messages[idx], msgInfo);
  108. return;
  109. }
  110. }
  111. // 新的消息
  112. chat.messages.push(msgInfo);
  113. },
  114. deleteMessage(state, msgInfo){
  115. // 获取对方id或群id
  116. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  117. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  118. let chat = null;
  119. for (let idx in state.chats) {
  120. if (state.chats[idx].type == type &&
  121. state.chats[idx].targetId === targetId) {
  122. chat = state.chats[idx];
  123. break;
  124. }
  125. }
  126. for (let idx in chat.messages) {
  127. // 已经发送成功的,根据id删除
  128. if(chat.messages[idx].id && chat.messages[idx].id == msgInfo.id){
  129. chat.messages.splice(idx, 1);
  130. break;
  131. }
  132. // 正在发送中的消息可能没有id,根据发送时间删除
  133. if(msgInfo.selfSend && chat.messages[idx].selfSend
  134. &&chat.messages[idx].sendTime == msgInfo.sendTime){
  135. chat.messages.splice(idx, 1);
  136. break;
  137. }
  138. }
  139. },
  140. updateChatFromFriend(state, friend) {
  141. for (let i in state.chats) {
  142. let chat = state.chats[i];
  143. if (chat.type=='PRIVATE' && chat.targetId == friend.id) {
  144. chat.headImage = friend.headImageThumb;
  145. chat.showName = friend.nickName;
  146. break;
  147. }
  148. }
  149. },
  150. updateChatFromGroup(state, group) {
  151. for (let i in state.chats) {
  152. let chat = state.chats[i];
  153. if (chat.type=='GROUP' && chat.targetId == group.id) {
  154. chat.headImage = group.headImageThumb;
  155. chat.showName = group.remark;
  156. break;
  157. }
  158. }
  159. },
  160. resetChatStore(state) {
  161. state.activeIndex = -1;
  162. state.chats = [];
  163. }
  164. },
  165. }