chatStore.js 9.7 KB

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