chatStore.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import {
  2. MESSAGE_TYPE,
  3. MESSAGE_STATUS
  4. } from '@/common/enums.js';
  5. import userStore from './userStore';
  6. export default {
  7. state: {
  8. activeIndex: -1,
  9. chats: [],
  10. privateMsgMaxId: 0,
  11. groupMsgMaxId: 0,
  12. loadingPrivateMsg: false,
  13. loadingGroupMsg: false,
  14. },
  15. mutations: {
  16. initChats(state, chatsData) {
  17. state.chats = chatsData.chats ||[];
  18. state.privateMsgMaxId = chatsData.privateMsgMaxId||0;
  19. state.groupMsgMaxId = chatsData.groupMsgMaxId||0;
  20. // 防止图片一直处在加载中状态
  21. state.chats.forEach((chat) => {
  22. chat.messages.forEach((msg) => {
  23. if (msg.loadStatus == "loading") {
  24. msg.loadStatus = "fail"
  25. }
  26. })
  27. })
  28. },
  29. openChat(state, chatInfo) {
  30. let chat = null;
  31. for (let i in state.chats) {
  32. if (state.chats[i].type == chatInfo.type &&
  33. state.chats[i].targetId === chatInfo.targetId) {
  34. chat = state.chats[i];
  35. // 放置头部(这个操作非常耗资源,正在加载消息时不执行)
  36. if(!state.loadingPrivateMsg && !state.loadingPrivateMsg){
  37. state.chats.splice(i, 1);
  38. state.chats.unshift(chat);
  39. }
  40. break;
  41. }
  42. }
  43. // 创建会话
  44. if (chat == null) {
  45. chat = {
  46. targetId: chatInfo.targetId,
  47. type: chatInfo.type,
  48. showName: chatInfo.showName,
  49. headImage: chatInfo.headImage,
  50. lastContent: "",
  51. lastSendTime: new Date().getTime(),
  52. unreadCount: 0,
  53. messages: [],
  54. };
  55. state.chats.unshift(chat);
  56. }
  57. this.commit("saveToStorage");
  58. },
  59. activeChat(state, idx) {
  60. state.activeIndex = idx;
  61. if (idx >= 0) {
  62. state.chats[idx].unreadCount = 0;
  63. }
  64. },
  65. resetUnreadCount(state, chatInfo) {
  66. for (let idx in state.chats) {
  67. if (state.chats[idx].type == chatInfo.type &&
  68. state.chats[idx].targetId == chatInfo.targetId) {
  69. state.chats[idx].unreadCount = 0;
  70. }
  71. }
  72. this.commit("saveToStorage");
  73. },
  74. readedMessage(state, friendId) {
  75. for (let idx in state.chats) {
  76. if (state.chats[idx].type == 'PRIVATE' &&
  77. state.chats[idx].targetId == friendId) {
  78. state.chats[idx].messages.forEach((m) => {
  79. console.log("readedMessage")
  80. if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) {
  81. m.status = MESSAGE_STATUS.READED
  82. }
  83. })
  84. }
  85. }
  86. this.commit("saveToStorage");
  87. },
  88. removeChat(state, idx) {
  89. state.chats.splice(idx, 1);
  90. this.commit("saveToStorage");
  91. },
  92. removeGroupChat(state, groupId) {
  93. for (let idx in state.chats) {
  94. if (state.chats[idx].type == 'GROUP' &&
  95. state.chats[idx].targetId == groupId) {
  96. this.commit("removeChat", idx);
  97. }
  98. }
  99. },
  100. removePrivateChat(state, userId) {
  101. for (let idx in state.chats) {
  102. if (state.chats[idx].type == 'PRIVATE' &&
  103. state.chats[idx].targetId == userId) {
  104. this.commit("removeChat", idx);
  105. }
  106. }
  107. },
  108. moveTop(state, idx) {
  109. let chat = state.chats[idx];
  110. // 放置头部
  111. state.chats.splice(idx, 1);
  112. state.chats.unshift(chat);
  113. },
  114. insertMessage(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. let chatIdx = -1;
  120. for (let idx in state.chats) {
  121. if (state.chats[idx].type == type &&
  122. state.chats[idx].targetId === targetId) {
  123. chat = state.chats[idx];
  124. chatIdx = idx;
  125. break;
  126. }
  127. }
  128. // 插入新的数据
  129. if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
  130. chat.lastContent = "[图片]";
  131. } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
  132. chat.lastContent = "[文件]";
  133. } else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
  134. chat.lastContent = "[语音]";
  135. } else {
  136. chat.lastContent = msgInfo.content;
  137. }
  138. chat.lastSendTime = msgInfo.sendTime;
  139. // 未读加1
  140. if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) {
  141. chat.unreadCount++;
  142. }
  143. // 记录消息的最大id
  144. if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) {
  145. state.privateMsgMaxId = msgInfo.id;
  146. }
  147. if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) {
  148. state.groupMsgMaxId = msgInfo.id;
  149. }
  150. // 如果是已存在消息,则覆盖旧的消息数据
  151. for (let idx in chat.messages) {
  152. if (msgInfo.id && chat.messages[idx].id == msgInfo.id) {
  153. Object.assign(chat.messages[idx], msgInfo);
  154. this.commit("saveToStorage");
  155. return;
  156. }
  157. // 正在发送中的消息可能没有id,通过发送时间判断
  158. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  159. chat.messages[idx].sendTime == msgInfo.sendTime) {
  160. Object.assign(chat.messages[idx], msgInfo);
  161. this.commit("saveToStorage");
  162. return;
  163. }
  164. }
  165. // 间隔大于10分钟插入时间显示
  166. if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) {
  167. chat.messages.push({
  168. sendTime: msgInfo.sendTime,
  169. type: MESSAGE_TYPE.TIP_TIME,
  170. });
  171. chat.lastTimeTip = msgInfo.sendTime;
  172. }
  173. // 新的消息
  174. chat.messages.push(msgInfo);
  175. this.commit("saveToStorage");
  176. },
  177. deleteMessage(state, msgInfo) {
  178. // 获取对方id或群id
  179. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  180. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  181. let chat = null;
  182. for (let idx in state.chats) {
  183. if (state.chats[idx].type == type &&
  184. state.chats[idx].targetId === targetId) {
  185. chat = state.chats[idx];
  186. break;
  187. }
  188. }
  189. for (let idx in chat.messages) {
  190. // 已经发送成功的,根据id删除
  191. if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) {
  192. chat.messages.splice(idx, 1);
  193. break;
  194. }
  195. // 正在发送中的消息可能没有id,根据发送时间删除
  196. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  197. chat.messages[idx].sendTime == msgInfo.sendTime) {
  198. chat.messages.splice(idx, 1);
  199. break;
  200. }
  201. }
  202. this.commit("saveToStorage");
  203. },
  204. updateChatFromFriend(state, friend) {
  205. for (let i in state.chats) {
  206. let chat = state.chats[i];
  207. if (chat.type == 'PRIVATE' && chat.targetId == friend.id) {
  208. chat.headImage = friend.headImageThumb;
  209. chat.showName = friend.nickName;
  210. break;
  211. }
  212. }
  213. this.commit("saveToStorage");
  214. },
  215. updateChatFromGroup(state, group) {
  216. for (let i in state.chats) {
  217. let chat = state.chats[i];
  218. if (chat.type == 'GROUP' && chat.targetId == group.id) {
  219. chat.headImage = group.headImageThumb;
  220. chat.showName = group.remark;
  221. break;
  222. }
  223. }
  224. this.commit("saveToStorage");
  225. },
  226. loadingPrivateMsg(state, loadding) {
  227. state.loadingPrivateMsg = loadding;
  228. },
  229. loadingGroupMsg(state, loadding) {
  230. state.loadingGroupMsg = loadding;
  231. },
  232. saveToStorage(state) {
  233. let userId = userStore.state.userInfo.id;
  234. let key = "chats-" + userId;
  235. let chatsData = {
  236. privateMsgMaxId: state.privateMsgMaxId,
  237. groupMsgMaxId: state.groupMsgMaxId,
  238. chats: state.chats
  239. }
  240. uni.setStorage({
  241. key: key,
  242. data: chatsData
  243. })
  244. },
  245. clear(state) {
  246. state.chats = [];
  247. state.activeIndex = -1;
  248. state.privateMsgMaxId = 0;
  249. state.groupMsgMaxId = 0;
  250. state.loadingPrivateMsg = false;
  251. state.loadingGroupMsg = false;
  252. }
  253. },
  254. actions: {
  255. loadChat(context) {
  256. return new Promise((resolve, reject) => {
  257. let userId = userStore.state.userInfo.id;
  258. uni.getStorage({
  259. key: "chats-" + userId,
  260. success(res) {
  261. context.commit("initChats", res.data);
  262. resolve()
  263. },
  264. fail(e) {
  265. resolve()
  266. }
  267. });
  268. })
  269. }
  270. }
  271. }