chatStore.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import {MESSAGE_TYPE} from '@/common/enums.js';
  2. import userStore from './userStore';
  3. export default {
  4. state: {
  5. chats: []
  6. },
  7. mutations: {
  8. initChats(state,chats){
  9. // 防止图片一直处在加载中状态
  10. chats.forEach((chat)=>{
  11. chat.messages.forEach((msg)=>{
  12. if(msg.loadStatus == "loading"){
  13. msg.loadStatus = "fail"
  14. }
  15. })
  16. })
  17. state.chats = chats;
  18. },
  19. openChat(state, chatInfo) {
  20. let chat = null;
  21. for (let i in state.chats) {
  22. if (state.chats[i].type == chatInfo.type &&
  23. state.chats[i].targetId === chatInfo.targetId) {
  24. chat = state.chats[i];
  25. // 放置头部
  26. state.chats.splice(i, 1);
  27. state.chats.unshift(chat);
  28. break;
  29. }
  30. }
  31. // 创建会话
  32. if (chat == null) {
  33. chat = {
  34. targetId: chatInfo.targetId,
  35. type: chatInfo.type,
  36. showName: chatInfo.showName,
  37. headImage: chatInfo.headImage,
  38. lastContent: "",
  39. lastSendTime: new Date().getTime(),
  40. unreadCount: 0,
  41. messages: [],
  42. };
  43. state.chats.unshift(chat);
  44. }
  45. this.commit("saveToStorage");
  46. },
  47. activeChat(state, idx) {
  48. state.activeIndex = idx;
  49. if(idx>=0){
  50. state.chats[idx].unreadCount = 0;
  51. }
  52. },
  53. removeChat(state, idx) {
  54. state.chats.splice(idx, 1);
  55. this.commit("saveToStorage");
  56. },
  57. removeGroupChat(state, groupId) {
  58. for (let idx in state.chats) {
  59. if (state.chats[idx].type == 'GROUP' &&
  60. state.chats[idx].targetId == groupId) {
  61. this.commit("removeChat", idx);
  62. }
  63. }
  64. },
  65. removePrivateChat(state, userId) {
  66. for (let idx in state.chats) {
  67. if (state.chats[idx].type == 'PRIVATE' &&
  68. state.chats[idx].targetId == userId) {
  69. this.commit("removeChat", idx);
  70. }
  71. }
  72. },
  73. moveTop(state,idx){
  74. let chat = state.chats[idx];
  75. // 放置头部
  76. state.chats.splice(idx, 1);
  77. state.chats.unshift(chat);
  78. },
  79. insertMessage(state, msgInfo) {
  80. // 获取对方id或群id
  81. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  82. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  83. let chat = null;
  84. let chatIdx = -1;
  85. for (let idx in state.chats) {
  86. if (state.chats[idx].type == type &&
  87. state.chats[idx].targetId === targetId) {
  88. chat = state.chats[idx];
  89. chatIdx = idx;
  90. break;
  91. }
  92. }
  93. // 插入新的数据
  94. if(msgInfo.type == MESSAGE_TYPE.IMAGE){
  95. chat.lastContent = "[图片]";
  96. }else if(msgInfo.type == MESSAGE_TYPE.FILE){
  97. chat.lastContent = "[文件]";
  98. }else if(msgInfo.type == MESSAGE_TYPE.AUDIO){
  99. chat.lastContent = "[语音]";
  100. }else{
  101. chat.lastContent = msgInfo.content;
  102. }
  103. chat.lastSendTime = msgInfo.sendTime;
  104. // 如果不是当前会话,未读加1
  105. if(chatIdx != state.activeIndex){
  106. chat.unreadCount++;
  107. }
  108. // 自己回复了消息,说明消息已读
  109. if(msgInfo.selfSend){
  110. chat.unreadCount=0;
  111. }
  112. // 如果是已存在消息,则覆盖旧的消息数据
  113. for (let idx in chat.messages) {
  114. if(msgInfo.id && chat.messages[idx].id == msgInfo.id){
  115. Object.assign(chat.messages[idx], msgInfo);
  116. this.commit("saveToStorage");
  117. return;
  118. }
  119. // 正在发送中的消息可能没有id,通过发送时间判断
  120. if(msgInfo.selfSend && chat.messages[idx].selfSend
  121. && chat.messages[idx].sendTime == msgInfo.sendTime){
  122. Object.assign(chat.messages[idx], msgInfo);
  123. this.commit("saveToStorage");
  124. return;
  125. }
  126. }
  127. // 间隔大于10分钟插入时间显示
  128. if(!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600*1000)){
  129. chat.messages.push({
  130. sendTime: msgInfo.sendTime,
  131. type: MESSAGE_TYPE.TIP_TIME,
  132. });
  133. chat.lastTimeTip = msgInfo.sendTime;
  134. }
  135. // 新的消息
  136. chat.messages.push(msgInfo);
  137. this.commit("saveToStorage");
  138. },
  139. deleteMessage(state, msgInfo){
  140. // 获取对方id或群id
  141. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  142. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  143. let chat = null;
  144. for (let idx in state.chats) {
  145. if (state.chats[idx].type == type &&
  146. state.chats[idx].targetId === targetId) {
  147. chat = state.chats[idx];
  148. break;
  149. }
  150. }
  151. for (let idx in chat.messages) {
  152. // 已经发送成功的,根据id删除
  153. if(chat.messages[idx].id && chat.messages[idx].id == msgInfo.id){
  154. chat.messages.splice(idx, 1);
  155. break;
  156. }
  157. // 正在发送中的消息可能没有id,根据发送时间删除
  158. if(msgInfo.selfSend && chat.messages[idx].selfSend
  159. &&chat.messages[idx].sendTime == msgInfo.sendTime){
  160. chat.messages.splice(idx, 1);
  161. break;
  162. }
  163. }
  164. this.commit("saveToStorage");
  165. },
  166. updateChatFromFriend(state, friend) {
  167. for (let i in state.chats) {
  168. let chat = state.chats[i];
  169. if (chat.type=='PRIVATE' && chat.targetId == friend.id) {
  170. chat.headImage = friend.headImageThumb;
  171. chat.showName = friend.nickName;
  172. break;
  173. }
  174. }
  175. this.commit("saveToStorage");
  176. },
  177. updateChatFromGroup(state, group) {
  178. for (let i in state.chats) {
  179. let chat = state.chats[i];
  180. if (chat.type=='GROUP' && chat.targetId == group.id) {
  181. chat.headImage = group.headImageThumb;
  182. chat.showName = group.remark;
  183. break;
  184. }
  185. }
  186. this.commit("saveToStorage");
  187. },
  188. saveToStorage(state){
  189. let userId = userStore.state.userInfo.id;
  190. uni.setStorage({
  191. key:"chats-"+userId,
  192. data: state.chats
  193. })
  194. },
  195. clear(state){
  196. state.chats = [];
  197. }
  198. },
  199. actions:{
  200. loadChat(context) {
  201. return new Promise((resolve, reject) => {
  202. let userId = userStore.state.userInfo.id;
  203. uni.getStorage({
  204. key:"chats-"+userId,
  205. success(res) {
  206. context.commit("initChats",res.data);
  207. resolve()
  208. },
  209. fail(e) {
  210. // 不存在聊天记录,清空聊天列表
  211. context.commit("initChats",[]);
  212. resolve()
  213. }
  214. });
  215. })
  216. }
  217. }
  218. }