chat.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import API from '@/api'
  2. const state = {
  3. sessionList: [], // 会话列表
  4. isLogin: true,
  5. toApp: false,
  6. friendList: [] // 好友列表
  7. }
  8. const mutations = {
  9. setSessionList (state, data) {
  10. // 按is_pin(是否置顶) 属性排序
  11. state.sessionList = data.sort((a, b) => {
  12. return b.is_pin - a.is_pin
  13. })
  14. },
  15. addSession (state, data) {
  16. state.sessionList.push(data)
  17. },
  18. chatAppLogin (state, flag) {
  19. state.isLogin = flag
  20. },
  21. toApp (state, flag) {
  22. state.toApp = flag
  23. },
  24. addSessionItem (state, data) {
  25. state.sessionList.unshift(data)
  26. },
  27. setFriendList (state, data) {
  28. state.friendList = data
  29. },
  30. setSessionItem (state, data) {
  31. state.sessionList.forEach(item => {
  32. if (item.session_id == data.session_id) {
  33. item = data
  34. }
  35. })
  36. },
  37. /**
  38. * @des 根据置顶目标更新会话列表顺序
  39. * @param {Object} state
  40. * @param {String} sessionId 待置顶的会话
  41. */
  42. updateSessionListByPin (state, sessionId) {
  43. state.sessionList.forEach((item, index) => {
  44. if (item.session_id == sessionId) {
  45. state.sessionList.unshift(state.sessionList.splice(index, 1)[0])
  46. }
  47. })
  48. },
  49. /**
  50. * @des 根据取消置顶目标更新会话列表顺序
  51. * @param {Object} state
  52. * @param {String} sessionId 待取消置顶的会话
  53. */
  54. cancelSessionListByPin (state, sessionId) {
  55. state.sessionList.forEach((item, index) => {
  56. if (item.session_id == sessionId) {
  57. state.sessionList.push(state.sessionList.splice(index, 1)[0])
  58. }
  59. })
  60. }
  61. }
  62. const actions = {
  63. async getSessionList ({ commit, state }, params) {
  64. try {
  65. let { data } = await API.session.sessionList()
  66. commit('setSessionList', data.data)
  67. } catch (error) {}
  68. },
  69. async getUserInfo ({ commit, state }) {
  70. try {
  71. let { data } = await API.user.getInfo()
  72. commit('setUserInfo', data.data)
  73. commit('setGroupUserInfo', data.data)
  74. } catch (error) {}
  75. },
  76. /**
  77. * @des 更新用户群组列表中指定sessionId的内容
  78. * @param {String} params.sessionId 待更新的sessionId
  79. * @param {Object} params.data 待更新的内容
  80. */
  81. updateSessionItem ({ commit, state }, params) {
  82. let targetObj = state.sessionList.find(n => {
  83. return n.session_id == params.sessionId
  84. })
  85. targetObj = Object.assign(targetObj, params.data)
  86. commit('setSessionItem', targetObj)
  87. },
  88. /**
  89. * 撤回消息
  90. * @param {Object} params
  91. * {index:number, session_id:string, hash:string}
  92. */
  93. async doRepealPersonMsg ({ dispatch, commit, state, rootState }, params = {}) {
  94. try {
  95. await API.person.repealPersonMsg({
  96. session_id: rootState.curSession,
  97. hash: params.hash
  98. })
  99. } catch (error) {}
  100. },
  101. async getFriendList ({ commit, state }) {
  102. try {
  103. let { data } = await API.group.getFriends()
  104. commit('setFriendList', data.data)
  105. } catch (error) {}
  106. }
  107. }
  108. export default {
  109. state,
  110. mutations,
  111. actions
  112. }