chatStore.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. let userId = userStore.state.userInfo.id;
  150. if(msgInfo.atUserIds.indexOf(userId)>=0){
  151. chat.atMe = true;
  152. }
  153. if(msgInfo.atUserIds.indexOf(-1)>=0){
  154. chat.atAll = true;
  155. }
  156. }
  157. // 记录消息的最大id
  158. if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) {
  159. state.privateMsgMaxId = msgInfo.id;
  160. }
  161. if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) {
  162. state.groupMsgMaxId = msgInfo.id;
  163. }
  164. // 如果是已存在消息,则覆盖旧的消息数据
  165. for (let idx in chat.messages) {
  166. if (msgInfo.id && chat.messages[idx].id == msgInfo.id) {
  167. Object.assign(chat.messages[idx], msgInfo);
  168. this.commit("saveToStorage");
  169. return;
  170. }
  171. // 正在发送中的消息可能没有id,通过发送时间判断
  172. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  173. chat.messages[idx].sendTime == msgInfo.sendTime) {
  174. Object.assign(chat.messages[idx], msgInfo);
  175. this.commit("saveToStorage");
  176. return;
  177. }
  178. }
  179. // 间隔大于10分钟插入时间显示
  180. if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) {
  181. chat.messages.push({
  182. sendTime: msgInfo.sendTime,
  183. type: MESSAGE_TYPE.TIP_TIME,
  184. });
  185. chat.lastTimeTip = msgInfo.sendTime;
  186. }
  187. // 新的消息
  188. chat.messages.push(msgInfo);
  189. this.commit("saveToStorage");
  190. },
  191. deleteMessage(state, msgInfo) {
  192. // 获取对方id或群id
  193. let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
  194. let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
  195. let chat = null;
  196. for (let idx in state.chats) {
  197. if (state.chats[idx].type == type &&
  198. state.chats[idx].targetId === targetId) {
  199. chat = state.chats[idx];
  200. break;
  201. }
  202. }
  203. for (let idx in chat.messages) {
  204. // 已经发送成功的,根据id删除
  205. if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) {
  206. chat.messages.splice(idx, 1);
  207. break;
  208. }
  209. // 正在发送中的消息可能没有id,根据发送时间删除
  210. if (msgInfo.selfSend && chat.messages[idx].selfSend &&
  211. chat.messages[idx].sendTime == msgInfo.sendTime) {
  212. chat.messages.splice(idx, 1);
  213. break;
  214. }
  215. }
  216. this.commit("saveToStorage");
  217. },
  218. updateChatFromFriend(state, friend) {
  219. for (let i in state.chats) {
  220. let chat = state.chats[i];
  221. if (chat.type == 'PRIVATE' && chat.targetId == friend.id) {
  222. chat.headImage = friend.headImageThumb;
  223. chat.showName = friend.nickName;
  224. break;
  225. }
  226. }
  227. this.commit("saveToStorage");
  228. },
  229. updateChatFromGroup(state, group) {
  230. for (let i in state.chats) {
  231. let chat = state.chats[i];
  232. if (chat.type == 'GROUP' && chat.targetId == group.id) {
  233. chat.headImage = group.headImageThumb;
  234. chat.showName = group.remark;
  235. break;
  236. }
  237. }
  238. this.commit("saveToStorage");
  239. },
  240. loadingPrivateMsg(state, loadding) {
  241. state.loadingPrivateMsg = loadding;
  242. },
  243. loadingGroupMsg(state, loadding) {
  244. state.loadingGroupMsg = loadding;
  245. },
  246. refreshChats(state){
  247. state.chats.forEach((chat)=>{
  248. if(chat.messages.length>0){
  249. let msgInfo = chat.messages[chat.messages.length-1];
  250. if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
  251. chat.lastContent = "[图片]";
  252. } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
  253. chat.lastContent = "[文件]";
  254. } else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
  255. chat.lastContent = "[语音]";
  256. } else {
  257. chat.lastContent = msgInfo.content;
  258. }
  259. chat.lastSendTime = msgInfo.sendTime;
  260. }else{
  261. chat.lastContent = "";
  262. chat.lastSendTime = new Date()
  263. }
  264. })
  265. state.chats.sort((chat1, chat2) => {
  266. return chat2.lastSendTime-chat1.lastSendTime;
  267. });
  268. },
  269. saveToStorage(state) {
  270. let userId = userStore.state.userInfo.id;
  271. let key = "chats-" + userId;
  272. let chatsData = {
  273. privateMsgMaxId: state.privateMsgMaxId,
  274. groupMsgMaxId: state.groupMsgMaxId,
  275. chats: state.chats
  276. }
  277. uni.setStorage({
  278. key: key,
  279. data: chatsData
  280. })
  281. },
  282. clear(state) {
  283. state.chats = [];
  284. state.activeIndex = -1;
  285. state.privateMsgMaxId = 0;
  286. state.groupMsgMaxId = 0;
  287. state.loadingPrivateMsg = false;
  288. state.loadingGroupMsg = false;
  289. }
  290. },
  291. actions: {
  292. loadChat(context) {
  293. return new Promise((resolve, reject) => {
  294. let userId = userStore.state.userInfo.id;
  295. uni.getStorage({
  296. key: "chats-" + userId,
  297. success(res) {
  298. context.commit("initChats", res.data);
  299. resolve()
  300. },
  301. fail(e) {
  302. resolve()
  303. }
  304. });
  305. })
  306. }
  307. }
  308. }