index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. const store = new Vuex.Store({
  5. state: {
  6. isLogin: false,
  7. isSDKReady: false, // TIM SDK 是否 ready
  8. conversationActive: {}, //聊天进行中的会话
  9. toUserId: '', // 聊天对象id
  10. conversationList: [], // 会话列表
  11. currentMessageList: [], // 消息列表
  12. isBusy: false
  13. },
  14. mutations: {
  15. //更新登录状态
  16. toggleIsLogin(state, isLogin) {
  17. state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
  18. },
  19. //更新TIMSDK状态
  20. toggleIsSDKReady(state, isSDKReady) {
  21. state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
  22. },
  23. //退出登录重置状态
  24. reset(state) {
  25. state.isLogin = false
  26. state.isSDKReady = false
  27. },
  28. //选择好友聊天--创建会话/拼接会话id
  29. createConversationActive(state, toUserId) {
  30. state.conversationActive.conversationID = 'C2C' + toUserId
  31. state.toUserId = toUserId
  32. state.currentMessageList = []
  33. },
  34. //选择已有会话聊天--更新选中会话详情
  35. updateConversationActive(state, conversationItem) {
  36. state.conversationActive = Object.assign({}, conversationItem);
  37. state.toUserId = conversationItem.userProfile.userID
  38. state.currentMessageList = []
  39. },
  40. //更新会话列表
  41. updateConversationList(state, newConversationList) {
  42. state.conversationList = newConversationList
  43. },
  44. UPDATE_ISBUSY(state, isBusy) {
  45. state.isBusy = isBusy
  46. },
  47. /**
  48. * 将消息插入当前会话列表
  49. * 调用时机:收/发消息事件触发时
  50. * @param {Object} state
  51. * @param {Message[]|Message} data
  52. * @returns
  53. */
  54. pushCurrentMessageList(state, data) {
  55. // 还没当前会话,则跳过
  56. if (Array.isArray(data)) {
  57. // 筛选出当前会话的消息
  58. const result = data.filter(item => item.conversationID === state.conversationActive
  59. .conversationID)
  60. state.currentMessageList = [...state.currentMessageList, ...result]
  61. } else if (data.conversationID === state.conversationActive.conversationID) {
  62. state.currentMessageList = [...state.currentMessageList, data]
  63. }
  64. },
  65. /**
  66. * 滑到顶部请求更多的历史消息
  67. * */
  68. unshiftCurrentMessageList(state, data) {
  69. if (data) {
  70. state.currentMessageList = [...data, ...state.currentMessageList]
  71. }
  72. }
  73. },
  74. actions: {
  75. }
  76. })
  77. export default store