chatStore.js 5.3 KB

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