chatStore.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) {
  80. m.status = MESSAGE_STATUS.READED
  81. }
  82. })
  83. }
  84. }
  85. this.commit("saveToStorage");
  86. },
  87. removeChat(state, idx) {
  88. state.chats.splice(idx, 1);
  89. this.commit("saveToStorage");
  90. },
  91. removeGroupChat(state, groupId) {
  92. for (let idx in state.chats) {
  93. if (state.chats[idx].type == 'GROUP' &&
  94. state.chats[idx].targetId == groupId) {
  95. this.commit("removeChat", idx);
  96. }
  97. }
  98. },
  99. removePrivateChat(state, userId) {
  100. for (let idx in state.chats) {
  101. if (state.chats[idx].type == 'PRIVATE' &&
  102. state.chats[idx].targetId == userId) {
  103. this.commit("removeChat", idx);
  104. }
  105. }
  106. },
  107. moveTop(state, idx) {
  108. let chat = state.chats[idx];
  109. // 放置头部
  110. state.chats.splice(idx, 1);
  111. state.chats.unshift(chat);
  112. },
  113. insertMessage(state, msgInfo) {
  114. // 获取对方id或群id
  115. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  116. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  117. let chat = null;
  118. let chatIdx = -1;
  119. for (let idx in state.chats) {
  120. if (state.chats[idx].type == type &&
  121. state.chats[idx].targetId === targetId) {
  122. chat = state.chats[idx];
  123. chatIdx = idx;
  124. break;
  125. }
  126. }
  127. // 会话列表内容
  128. if(!state.loadingPrivateMsg && !state.loadingPrivateMsg){
  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. }
  140. // 未读加1
  141. if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) {
  142. chat.unreadCount++;
  143. }
  144. // 记录消息的最大id
  145. if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) {
  146. state.privateMsgMaxId = msgInfo.id;
  147. }
  148. if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) {
  149. state.groupMsgMaxId = msgInfo.id;
  150. }
  151. // 如果是已存在消息,则覆盖旧的消息数据
  152. for (let idx in chat.messages) {
  153. if (msgInfo.id && chat.messages[idx].id == msgInfo.id) {
  154. Object.assign(chat.messages[idx], msgInfo);
  155. this.commit("saveToStorage");
  156. return;
  157. }
  158. // 正在发送中的消息可能没有id,通过发送时间判断
  159. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  160. chat.messages[idx].sendTime == msgInfo.sendTime) {
  161. Object.assign(chat.messages[idx], msgInfo);
  162. this.commit("saveToStorage");
  163. return;
  164. }
  165. }
  166. // 间隔大于10分钟插入时间显示
  167. if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) {
  168. chat.messages.push({
  169. sendTime: msgInfo.sendTime,
  170. type: MESSAGE_TYPE.TIP_TIME,
  171. });
  172. chat.lastTimeTip = msgInfo.sendTime;
  173. }
  174. // 新的消息
  175. chat.messages.push(msgInfo);
  176. this.commit("saveToStorage");
  177. },
  178. deleteMessage(state, msgInfo) {
  179. // 获取对方id或群id
  180. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  181. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  182. let chat = null;
  183. for (let idx in state.chats) {
  184. if (state.chats[idx].type == type &&
  185. state.chats[idx].targetId === targetId) {
  186. chat = state.chats[idx];
  187. break;
  188. }
  189. }
  190. for (let idx in chat.messages) {
  191. // 已经发送成功的,根据id删除
  192. if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) {
  193. chat.messages.splice(idx, 1);
  194. break;
  195. }
  196. // 正在发送中的消息可能没有id,根据发送时间删除
  197. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  198. chat.messages[idx].sendTime == msgInfo.sendTime) {
  199. chat.messages.splice(idx, 1);
  200. break;
  201. }
  202. }
  203. this.commit("saveToStorage");
  204. },
  205. updateChatFromFriend(state, friend) {
  206. for (let i in state.chats) {
  207. let chat = state.chats[i];
  208. if (chat.type == 'PRIVATE' && chat.targetId == friend.id) {
  209. chat.headImage = friend.headImageThumb;
  210. chat.showName = friend.nickName;
  211. break;
  212. }
  213. }
  214. this.commit("saveToStorage");
  215. },
  216. updateChatFromGroup(state, group) {
  217. for (let i in state.chats) {
  218. let chat = state.chats[i];
  219. if (chat.type == 'GROUP' && chat.targetId == group.id) {
  220. chat.headImage = group.headImageThumb;
  221. chat.showName = group.remark;
  222. break;
  223. }
  224. }
  225. this.commit("saveToStorage");
  226. },
  227. loadingPrivateMsg(state, loadding) {
  228. state.loadingPrivateMsg = loadding;
  229. },
  230. loadingGroupMsg(state, loadding) {
  231. state.loadingGroupMsg = loadding;
  232. },
  233. refreshChats(state){
  234. state.chats.forEach((chat)=>{
  235. if(chat.messages.length>0){
  236. let msgInfo = chat.messages[chat.messages.length-1];
  237. if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
  238. chat.lastContent = "[图片]";
  239. } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
  240. chat.lastContent = "[文件]";
  241. } else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
  242. chat.lastContent = "[语音]";
  243. } else {
  244. chat.lastContent = msgInfo.content;
  245. }
  246. chat.lastSendTime = msgInfo.sendTime;
  247. }else{
  248. chat.lastContent = "";
  249. chat.lastSendTime = new Date()
  250. }
  251. })
  252. state.chats.sort((chat1, chat2) => {
  253. return chat2.lastSendTime-chat1.lastSendTime;
  254. });
  255. },
  256. saveToStorage(state) {
  257. let userId = userStore.state.userInfo.id;
  258. let key = "chats-" + userId;
  259. let chatsData = {
  260. privateMsgMaxId: state.privateMsgMaxId,
  261. groupMsgMaxId: state.groupMsgMaxId,
  262. chats: state.chats
  263. }
  264. // uni.setStorage({
  265. // key: key,
  266. // data: chatsData
  267. // })
  268. },
  269. clear(state) {
  270. state.chats = [];
  271. state.activeIndex = -1;
  272. state.privateMsgMaxId = 0;
  273. state.groupMsgMaxId = 0;
  274. state.loadingPrivateMsg = false;
  275. state.loadingGroupMsg = false;
  276. }
  277. },
  278. actions: {
  279. loadChat(context) {
  280. return new Promise((resolve, reject) => {
  281. let userId = userStore.state.userInfo.id;
  282. uni.getStorage({
  283. key: "chats-" + userId,
  284. success(res) {
  285. context.commit("initChats", res.data);
  286. resolve()
  287. },
  288. fail(e) {
  289. resolve()
  290. }
  291. });
  292. })
  293. }
  294. }
  295. }