chatStore.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. this.commit("saveToStorage");
  128. return;
  129. }
  130. // 会话列表内容
  131. if(!state.loadingPrivateMsg && !state.loadingGroupMsg){
  132. if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
  133. chat.lastContent = "[图片]";
  134. } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
  135. chat.lastContent = "[文件]";
  136. } else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
  137. chat.lastContent = "[语音]";
  138. } else if (msgInfo.type == MESSAGE_TYPE.TEXT || msgInfo.type == MESSAGE_TYPE.RECALL) {
  139. chat.lastContent = msgInfo.content;
  140. }
  141. chat.lastSendTime = msgInfo.sendTime;
  142. chat.sendNickName = msgInfo.sendNickName;
  143. }
  144. // 未读加1
  145. if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED
  146. && msgInfo.type != MESSAGE_TYPE.TIP_TEXT) {
  147. chat.unreadCount++;
  148. }
  149. // 是否有人@我
  150. if(!msgInfo.selfSend && chat.type=="GROUP" && msgInfo.atUserIds
  151. && msgInfo.status != MESSAGE_STATUS.READED){
  152. let userId = userStore.state.userInfo.id;
  153. if(msgInfo.atUserIds.indexOf(userId)>=0){
  154. chat.atMe = true;
  155. }
  156. if(msgInfo.atUserIds.indexOf(-1)>=0){
  157. chat.atAll = true;
  158. }
  159. }
  160. // 间隔大于10分钟插入时间显示
  161. if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) {
  162. chat.messages.push({
  163. sendTime: msgInfo.sendTime,
  164. type: MESSAGE_TYPE.TIP_TIME,
  165. });
  166. chat.lastTimeTip = msgInfo.sendTime;
  167. }
  168. // 新的消息
  169. chat.messages.push(msgInfo);
  170. this.commit("saveToStorage");
  171. },
  172. updateMessage(state, msgInfo) {
  173. // 获取对方id或群id
  174. let chat = this.getters.findChat(msgInfo);
  175. let message = this.getters.findMessage(chat, msgInfo);
  176. if(message){
  177. // 属性拷贝
  178. Object.assign(message, msgInfo);
  179. this.commit("saveToStorage");
  180. }
  181. },
  182. deleteMessage(state, msgInfo) {
  183. // 获取对方id或群id
  184. let chat = this.getters.findChat(msgInfo);
  185. for (let idx in chat.messages) {
  186. // 已经发送成功的,根据id删除
  187. if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) {
  188. chat.messages.splice(idx, 1);
  189. break;
  190. }
  191. // 正在发送中的消息可能没有id,根据发送时间删除
  192. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  193. chat.messages[idx].sendTime == msgInfo.sendTime) {
  194. chat.messages.splice(idx, 1);
  195. break;
  196. }
  197. }
  198. this.commit("saveToStorage");
  199. },
  200. updateChatFromFriend(state, friend) {
  201. for (let i in state.chats) {
  202. let chat = state.chats[i];
  203. if (chat.type == 'PRIVATE' && chat.targetId == friend.id) {
  204. chat.headImage = friend.headImageThumb;
  205. chat.showName = friend.nickName;
  206. break;
  207. }
  208. }
  209. this.commit("saveToStorage");
  210. },
  211. updateChatFromGroup(state, group) {
  212. for (let i in state.chats) {
  213. let chat = state.chats[i];
  214. if (chat.type == 'GROUP' && chat.targetId == group.id) {
  215. chat.headImage = group.headImageThumb;
  216. chat.showName = group.remark;
  217. break;
  218. }
  219. }
  220. this.commit("saveToStorage");
  221. },
  222. loadingPrivateMsg(state, loadding) {
  223. state.loadingPrivateMsg = loadding;
  224. if(!state.loadingPrivateMsg && !state.loadingGroupMsg){
  225. this.commit("refreshChats")
  226. }
  227. },
  228. loadingGroupMsg(state, loadding) {
  229. state.loadingGroupMsg = loadding;
  230. if(!state.loadingPrivateMsg && !state.loadingGroupMsg){
  231. this.commit("refreshChats")
  232. }
  233. },
  234. refreshChats(state){
  235. state.chats.forEach((chat)=>{
  236. if(chat.messages.length>0){
  237. let msgInfo = chat.messages[chat.messages.length-1];
  238. if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
  239. chat.lastContent = "[图片]";
  240. } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
  241. chat.lastContent = "[文件]";
  242. } else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
  243. chat.lastContent = "[语音]";
  244. } else if (msgInfo.type == MESSAGE_TYPE.TEXT || msgInfo.type == MESSAGE_TYPE.RECALL) {
  245. chat.lastContent = msgInfo.content;
  246. }
  247. chat.lastSendTime = msgInfo.sendTime;
  248. }else{
  249. chat.lastContent = "";
  250. chat.lastSendTime = new Date()
  251. }
  252. })
  253. state.chats.sort((chat1, chat2) => {
  254. return chat2.lastSendTime-chat1.lastSendTime;
  255. });
  256. },
  257. saveToStorage(state) {
  258. let userId = userStore.state.userInfo.id;
  259. let key = "chats-" + userId;
  260. let chatsData = {
  261. privateMsgMaxId: state.privateMsgMaxId,
  262. groupMsgMaxId: state.groupMsgMaxId,
  263. chats: state.chats
  264. }
  265. uni.setStorage({
  266. key: key,
  267. data: chatsData
  268. })
  269. },
  270. clear(state) {
  271. state.chats = [];
  272. state.activeIndex = -1;
  273. state.privateMsgMaxId = 0;
  274. state.groupMsgMaxId = 0;
  275. state.loadingPrivateMsg = false;
  276. state.loadingGroupMsg = false;
  277. }
  278. },
  279. actions: {
  280. loadChat(context) {
  281. return new Promise((resolve, reject) => {
  282. let userId = userStore.state.userInfo.id;
  283. uni.getStorage({
  284. key: "chats-" + userId,
  285. success(res) {
  286. context.commit("initChats", res.data);
  287. resolve()
  288. },
  289. fail(e) {
  290. resolve()
  291. }
  292. });
  293. })
  294. }
  295. },
  296. getters: {
  297. findChatIdx: (state) => (chat) => {
  298. for (let idx in state.chats) {
  299. if (state.chats[idx].type == chat.type &&
  300. state.chats[idx].targetId === chat.targetId) {
  301. chat = state.chats[idx];
  302. return idx;
  303. }
  304. }
  305. },
  306. findChat: (state) => (msgInfo) => {
  307. // 获取对方id或群id
  308. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  309. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  310. let chat = null;
  311. for (let idx in state.chats) {
  312. if (state.chats[idx].type == type &&
  313. state.chats[idx].targetId === targetId) {
  314. chat = state.chats[idx];
  315. break;
  316. }
  317. }
  318. return chat;
  319. },
  320. findMessage: (state) => (chat, msgInfo) => {
  321. if (!chat) {
  322. return null;
  323. }
  324. for (let idx in chat.messages) {
  325. // 通过id判断
  326. if (msgInfo.id && chat.messages[idx].id == msgInfo.id) {
  327. return chat.messages[idx];
  328. }
  329. // 正在发送中的消息可能没有id,通过发送时间判断
  330. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  331. chat.messages[idx].sendTime == msgInfo.sendTime) {
  332. return chat.messages[idx];
  333. }
  334. }
  335. }
  336. }
  337. }