chatStore.js 4.7 KB

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