index.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {createStore} from "vuex";
  2. import {setLocalStorage, getLocalStorage} from "../utils";
  3. import {Obj, StrNumObj} from "../interface";
  4. import router from "../router"
  5. import {logout} from "../api";
  6. interface State {
  7. userInfo: StrNumObj,
  8. chatUserInfo: Obj,
  9. isShowChat: boolean,
  10. isShowUserInfo: boolean,
  11. isShowGroupInfo: boolean,
  12. isShowRight: boolean,
  13. isSearching: boolean,
  14. userList: any[],
  15. }
  16. export default createStore({
  17. state: {
  18. userInfo: getLocalStorage('userInfo') || {}, //自己的信息
  19. chatUserInfo: {
  20. friendId: 0,
  21. friendName: '',
  22. id: 0,
  23. isGroup: 0,
  24. noReadNum: 0,
  25. userId: 0,
  26. username: '',
  27. nickname: '',
  28. email: '',
  29. }, //当前聊天用户的信息
  30. isShowChat: false,
  31. isShowUserInfo: false,
  32. isShowGroupInfo: false,
  33. isShowRight: true,
  34. isSearching: false,
  35. userList: [],
  36. } as State,
  37. mutations: {
  38. logout() {
  39. setLocalStorage('userInfo', {})
  40. router.push('/')
  41. },
  42. setUserInfo(state, data) {
  43. state.userInfo = data
  44. setLocalStorage('userInfo', data)
  45. },
  46. updateUserInfo(state, obj) {
  47. state.userInfo = {...state.userInfo, ...obj}
  48. setLocalStorage('userInfo', state.userInfo)
  49. },
  50. setUserList(state, data) {
  51. state.userList = data
  52. },
  53. setChatUserInfo(state, data) {
  54. state.chatUserInfo = data
  55. },
  56. setIsShowChat(state, status) {
  57. state.isShowChat = status
  58. },
  59. setIsShowUserInfo(state, status) {
  60. state.isShowUserInfo = status
  61. },
  62. setIsShowGroupInfo(state, status) {
  63. state.isShowGroupInfo = status
  64. },
  65. setIsSearching(state, status) {
  66. state.isSearching = status
  67. },
  68. setIsShowRight(state, status) {
  69. state.isShowRight = status
  70. },
  71. setNotReadMessage(state, id) {
  72. state.userList.forEach((item: Obj) => {
  73. if (id === item.friendId) {
  74. item.noReadNum++;
  75. }
  76. })
  77. },
  78. // 把有未读消息的好友移到上面
  79. hasNotReadMsgUserSort(state) {
  80. const userList: Obj[] = state.userList.filter(item => item.noReadNum > 0)
  81. state.userList = Array.from(new Set([...userList, ...state.userList]))
  82. },
  83. // 正在聊天的好友移到上面
  84. chatUserSort(state, obj: Obj) {
  85. const i = state.userList.findIndex(item => item === obj)
  86. state.userList.splice(i, 1)
  87. state.userList.unshift(obj)
  88. },
  89. },
  90. actions: {
  91. async logout({commit}) {
  92. await logout()
  93. commit('logout')
  94. }
  95. },
  96. modules: {}
  97. });