chatStore.js 8.8 KB

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