chatStore.js 8.9 KB

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