chat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import Vue from 'vue'
  2. import API from '@/api'
  3. import _ from 'lodash'
  4. import Session from '@/store/db/Session.js'
  5. const state = {
  6. popInviteType: 1, // 弹框邀请类型{1:建群,2:邀请好友进群,3:删除群好友,4:添加群管理,5:转让群主}
  7. sessionList: [], // 会话列表
  8. isLogin: true,
  9. toApp: false,
  10. friendList: [] // 好友列表
  11. }
  12. const objSession = new Session()
  13. const mutations = {
  14. setSessionList (state, data) {
  15. // 按is_pin(是否置顶) 属性排序
  16. state.sessionList = data
  17. },
  18. addSession (state, data) {
  19. state.sessionList.push(data)
  20. },
  21. chatAppLogin (state, flag) {
  22. state.isLogin = flag
  23. },
  24. toApp (state, flag) {
  25. state.toApp = flag
  26. },
  27. addSessionItem (state, data) {
  28. state.sessionList.unshift(data)
  29. },
  30. setSessionItemUnread (state, data) {
  31. console.log('进来寻找session', data.unread, data, data.cont)
  32. state.sessionList.forEach((item) => {
  33. if (item.session_id == `${data.session_id}`) {
  34. let unread = item.unread
  35. if (data.unread && data.curSession != data.session_id) {
  36. unread += data.unread
  37. } else {
  38. unread = 0
  39. }
  40. Vue.set(item, 'unread', unread)
  41. Vue.set(item, 'cont', data.cont ? data.cont : item.cont)
  42. objSession.setUnread(item.session_id, unread)
  43. }
  44. })
  45. },
  46. setFriendList (state, data) {
  47. state.friendList = data
  48. },
  49. setSessionItem (state, data) {
  50. state.sessionList.forEach(item => {
  51. if (item.session_id == data.session_id) {
  52. item = data
  53. }
  54. })
  55. },
  56. /**
  57. * @des 根据置顶目标更新会话列表顺序
  58. * @param {Object} state
  59. * @param {String} sessionId 待置顶的会话
  60. */
  61. updateSessionListByPin (state, sessionId) {
  62. objSession.setPin(sessionId, 1)
  63. state.sessionList.forEach((item, index) => {
  64. if (item.session_id == sessionId) {
  65. item.is_pin = 1
  66. state.sessionList.unshift(state.sessionList.splice(index, 1)[0])
  67. }
  68. })
  69. },
  70. /**
  71. * @des 根据取消置顶目标更新会话列表顺序
  72. * @param {Object} state
  73. * @param {String} sessionId 待取消置顶的会话
  74. */
  75. cancelSessionListByPin (state, sessionId) {
  76. objSession.setPin(sessionId, 0)
  77. let targetItem = null
  78. state.sessionList.forEach((item, index) => {
  79. if (item.session_id == sessionId) {
  80. item.is_pin = 0
  81. targetItem = state.sessionList.splice(index, 1)[0]
  82. } else if (targetItem && item.is_pin < 1) {
  83. // 插入到合适的位置
  84. state.sessionList.splice(index, 0, targetItem)
  85. targetItem = null
  86. }
  87. })
  88. // state.sessionList.push(targetItem)
  89. },
  90. /**
  91. * @des 根据免打扰更新会话列表
  92. * @param {Object} state
  93. * @param {String} sessionId 免打扰的会话
  94. */
  95. updateSessionListByMute (state, sessionId) {
  96. objSession.setMute(sessionId, 1)
  97. state.sessionList.forEach((item, index) => {
  98. if (item.session_id == sessionId) {
  99. item.is_mute = 1
  100. }
  101. })
  102. },
  103. /**
  104. * @des 根据免打扰置顶目标更新会话列表
  105. * @param {Object} state
  106. * @param {String} sessionId 待取消免打扰的会话
  107. */
  108. cancelSessionListByMute (state, sessionId) {
  109. objSession.setMute(sessionId, 0)
  110. state.sessionList.forEach((item, index) => {
  111. if (item.session_id == sessionId) {
  112. item.is_mute = 0
  113. }
  114. })
  115. },
  116. /**
  117. * @des 根据群id删除会话列表item
  118. * @param {Object} state
  119. * @param {String} sessionId 待取消免打扰的会话
  120. */
  121. removeSessionListById (state, sessionId) {
  122. state.sessionList = _.filter(state.sessionList, (item, index) => {
  123. return item.session_id != sessionId
  124. })
  125. },
  126. /**
  127. *设置弹框邀请类型
  128. *type1.建群2.邀请好友进群3.添加群管理
  129. */
  130. setPopInviteType (state, type) {
  131. if (!/\d/.test(type)) return
  132. state.popInviteType = type
  133. }
  134. }
  135. const actions = {
  136. async getSessionList ({ commit, state }, params) {
  137. let list = await objSession.getSortList()
  138. if (list) {
  139. // 从indexDB拿数据
  140. commit('setSessionList', list)
  141. }
  142. API.session.sessionList(function ({ data }) {
  143. commit('setSessionList', data.data)
  144. // 存储到indexDB
  145. objSession.replaceObjects(data.data)
  146. })
  147. },
  148. async getUserInfo ({ commit, state, rootState }) {
  149. try {
  150. let { data } = await API.user.getInfo({
  151. target_id: rootState.userId
  152. })
  153. commit('setUserInfo', data.data)
  154. commit('setGroupUserInfo', data.data)
  155. } catch (error) {}
  156. },
  157. /**
  158. * @des 更新用户群组列表中指定sessionId的内容
  159. * @param {String} params.sessionId 待更新的sessionId
  160. * @param {Object} params.data 待更新的内容
  161. */
  162. updateSessionItem ({ commit, state }, params) {
  163. let targetObj = state.sessionList.find(n => {
  164. return n.session_id == params.sessionId
  165. })
  166. targetObj = Object.assign(targetObj, params.data)
  167. commit('setSessionItem', targetObj)
  168. },
  169. /**
  170. * 撤回消息
  171. * @param {Object} params
  172. * {index:number, session_id:string, hash:string}
  173. */
  174. async doRepealPersonMsg ({ dispatch, commit, state, rootState }, params = {}) {
  175. try {
  176. await API.person.repealPersonMsg({
  177. session_id: rootState.curSession,
  178. hash: params.hash
  179. })
  180. } catch (error) {}
  181. },
  182. async getFriendList ({ commit, state }) {
  183. try {
  184. let { data } = await API.group.getFriends()
  185. commit('setFriendList', data.data)
  186. } catch (error) {}
  187. }
  188. }
  189. const getters = {
  190. muteList: state => {
  191. // 免打扰
  192. return state.sessionList.filter(v => {
  193. if (v.is_mute == '1') {
  194. return v.session_id
  195. }
  196. })
  197. }
  198. }
  199. export default {
  200. state,
  201. mutations,
  202. actions,
  203. getters
  204. }