chatStore.js 7.3 KB

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