chatStore.js 8.6 KB

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