chatStore.js 9.4 KB

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