chatStore.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. let lastTimeMsg = chat.messages.findLast(m=>m.type==MESSAGE_TYPE.TIP_TIME);
  129. if(!lastTimeMsg || (lastTimeMsg.sendTime < msgInfo.sendTime - 600*1000)){
  130. chat.messages.push({
  131. id:0,
  132. sendTime: msgInfo.sendTime,
  133. type: MESSAGE_TYPE.TIP_TIME,
  134. });
  135. }
  136. // 新的消息
  137. chat.messages.push(msgInfo);
  138. console.log(chat.unreadCount)
  139. this.commit("saveToStorage");
  140. },
  141. deleteMessage(state, msgInfo){
  142. // 获取对方id或群id
  143. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  144. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  145. let chat = null;
  146. for (let idx in state.chats) {
  147. if (state.chats[idx].type == type &&
  148. state.chats[idx].targetId === targetId) {
  149. chat = state.chats[idx];
  150. break;
  151. }
  152. }
  153. for (let idx in chat.messages) {
  154. // 已经发送成功的,根据id删除
  155. if(chat.messages[idx].id && chat.messages[idx].id == msgInfo.id){
  156. chat.messages.splice(idx, 1);
  157. break;
  158. }
  159. // 正在发送中的消息可能没有id,根据发送时间删除
  160. if(msgInfo.selfSend && chat.messages[idx].selfSend
  161. &&chat.messages[idx].sendTime == msgInfo.sendTime){
  162. chat.messages.splice(idx, 1);
  163. break;
  164. }
  165. }
  166. this.commit("saveToStorage");
  167. },
  168. updateChatFromFriend(state, friend) {
  169. for (let i in state.chats) {
  170. let chat = state.chats[i];
  171. if (chat.type=='PRIVATE' && chat.targetId == friend.id) {
  172. chat.headImage = friend.headImageThumb;
  173. chat.showName = friend.nickName;
  174. break;
  175. }
  176. }
  177. this.commit("saveToStorage");
  178. },
  179. updateChatFromGroup(state, group) {
  180. for (let i in state.chats) {
  181. let chat = state.chats[i];
  182. if (chat.type=='GROUP' && chat.targetId == group.id) {
  183. chat.headImage = group.headImageThumb;
  184. chat.showName = group.remark;
  185. break;
  186. }
  187. }
  188. this.commit("saveToStorage");
  189. },
  190. saveToStorage(state){
  191. let userId = userStore.state.userInfo.id;
  192. uni.setStorage({
  193. key:"chats-"+userId,
  194. data: state.chats
  195. })
  196. },
  197. clear(state){
  198. state.chats = [];
  199. }
  200. },
  201. actions:{
  202. loadChat(context) {
  203. return new Promise((resolve, reject) => {
  204. let userId = userStore.state.userInfo.id;
  205. uni.getStorage({
  206. key:"chats-"+userId,
  207. success(res) {
  208. context.commit("initChats",res.data);
  209. resolve()
  210. },
  211. fail(e) {
  212. // 不存在聊天记录,清空聊天列表
  213. context.commit("initChats",[]);
  214. resolve()
  215. }
  216. });
  217. })
  218. }
  219. }
  220. }