chat.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import Vue from 'vue'
  2. import API from '@/api'
  3. import _ from 'lodash'
  4. import Session from '@/store/db/Session.js'
  5. import User from '@/store/db/User.js'
  6. import { initChatData } from '@/store/state'
  7. import {
  8. decryptoMsg
  9. } from '@/util/util.js'
  10. const state = initChatData()
  11. const objSession = new Session()
  12. const mutations = {
  13. setSessionList (state, data) {
  14. data.forEach(value => {
  15. let lastMsg = value['last_msg']
  16. value.cont = lastMsg ? decryptoMsg(lastMsg.content) : ''
  17. })
  18. // 按is_pin(是否置顶) 属性排序
  19. state.sessionList = data
  20. },
  21. addSession (state, data) {
  22. state.sessionList.push(data)
  23. },
  24. chatAppLogin (state, flag) {
  25. state.isLogin = flag
  26. },
  27. toApp (state, flag) {
  28. state.toApp = flag
  29. },
  30. addSessionItem (state, data) {
  31. let pinSession = state.sessionList.filter(item => {
  32. return item.is_pin === 1
  33. })
  34. let dataPos = pinSession.length || 0
  35. state.sessionList.splice(dataPos, 0, data)
  36. objSession.recover(data.session_id)
  37. },
  38. // 会话列表未读消息设置
  39. setSessionItemUnread (state, data) {
  40. let itemIndex = 0
  41. state.sessionList.forEach((item, index) => {
  42. if (item.session_id == `${data.session_id}`) {
  43. let unread = item.unread
  44. if (data.unread && data.curSession != data.session_id) {
  45. unread += data.unread
  46. } else {
  47. unread = 0
  48. }
  49. Vue.set(item, 'unread', unread)
  50. objSession.setUnread(item.session_id, unread)
  51. itemIndex = index
  52. }
  53. })
  54. // 重新排序 非pin的会话
  55. let temp = state.sessionList[itemIndex]
  56. if (itemIndex && !temp.is_pin && data.cont) {
  57. state.sessionList.splice(itemIndex, 1)
  58. mutations.addSessionItem(state, temp)
  59. }
  60. },
  61. setFriendList (state, data) {
  62. state.friendList = data
  63. },
  64. // 左侧会话撤回-用于私聊主动撤回
  65. setSessionRepeal (state, data) {
  66. state.sessionList.forEach((item) => {
  67. if (item.session_id == (data.sessionId || data.group_id)) {
  68. Vue.set(item.last_msg, 'msg_type', -1)
  69. Vue.set(item, 'update_time_int', data.timestamp)
  70. }
  71. })
  72. },
  73. setSessionItem (state, data) {
  74. state.sessionList.forEach(item => {
  75. if (item.session_id == data.session_id) {
  76. item = data
  77. }
  78. })
  79. },
  80. /**
  81. * @des 根据置顶目标更新会话列表顺序
  82. * @param {Object} state
  83. * @param {String} sessionId 待置顶的会话
  84. */
  85. updateSessionListByPin (state, data) {
  86. objSession.setPin(data.session_id, 1)
  87. state.sessionList.forEach((item, index) => {
  88. if (item.session_id == data.session_id) {
  89. item.is_pin = data.is_pin
  90. item.pin_time_int = data.pin_time_int
  91. state.sessionList.unshift(state.sessionList.splice(index, 1)[0])
  92. }
  93. })
  94. },
  95. /**
  96. * @des 根据取消置顶目标更新会话列表顺序
  97. * @param {Object} state
  98. * @param {String} sessionId 待取消置顶的会话
  99. */
  100. cancelSessionListByPin (state, data) {
  101. objSession.setPin(data.session_id, 0)
  102. let targetItem = null
  103. state.sessionList.forEach((item, index) => {
  104. if (item.session_id == data.session_id) {
  105. item.is_pin = data.is_pin
  106. item.pin_time_int = data.pin_time_int
  107. targetItem = state.sessionList.splice(index, 1)[0]
  108. } else if (targetItem && item.is_pin < 1) {
  109. // 插入到合适的位置
  110. state.sessionList.splice(index, 0, targetItem)
  111. targetItem = null
  112. }
  113. })
  114. // state.sessionList.push(targetItem)
  115. },
  116. /**
  117. * @des 根据免打扰更新会话列表
  118. * @param {Object} state
  119. * @param {String} sessionId 免打扰的会话
  120. */
  121. updateSessionListByMute (state, sessionId) {
  122. objSession.setMute(sessionId, 1)
  123. state.sessionList.forEach((item, index) => {
  124. if (item.session_id == sessionId) {
  125. item.is_mute = 1
  126. }
  127. })
  128. },
  129. /**
  130. * @des 根据免打扰置顶目标更新会话列表
  131. * @param {Object} state
  132. * @param {String} sessionId 待取消免打扰的会话
  133. */
  134. cancelSessionListByMute (state, sessionId) {
  135. objSession.setMute(sessionId, 0)
  136. state.sessionList.forEach((item, index) => {
  137. if (item.session_id == sessionId) {
  138. item.is_mute = 0
  139. }
  140. })
  141. },
  142. /**
  143. * @des 根据群id删除会话列表item
  144. * @param {Object} state
  145. * @param {String} sessionId 待取消免打扰的会话
  146. */
  147. removeSessionListById (state, sessionId) {
  148. objSession.remove(sessionId)
  149. state.sessionList = _.filter(state.sessionList, (item, index) => {
  150. return item.session_id != sessionId
  151. })
  152. },
  153. /**
  154. * @des 更新用户群主列表中的last_msg内容
  155. * @param {String} params.sessionId 待更新的sessionId
  156. * @param {Object} params.data 待更新的内容
  157. */
  158. updateSessionLastmsg (state, params) {
  159. let targetObj = state.sessionList.find(n => {
  160. return n.session_id == (params.group_id || params.sessionId || params.session_id)
  161. })
  162. if (targetObj) {
  163. mutations.setSessionLastmsg(state, targetObj, params)
  164. }
  165. },
  166. updateSessionLastMsgNoDecode (state, params) {
  167. mutations.updateSessionLastmsg(state, Object.assign(params, { noDecryptoMsg: true }))
  168. },
  169. setSessionLastmsg (state, targetObj, params) {
  170. if (!targetObj.last_msg) {
  171. Vue.set(targetObj, 'last_msg', {})
  172. }
  173. let lastMsg = targetObj.last_msg
  174. Vue.set(lastMsg, 'content', params.content)
  175. Vue.set(lastMsg, 'from', params.from)
  176. Vue.set(lastMsg, 'msg_type', params.msg_type)
  177. Vue.set(lastMsg, 'name', params.name)
  178. Vue.set(lastMsg, 'nick_name', params.nick_name)
  179. Vue.set(lastMsg, 'time', params.timestamp)
  180. Vue.set(targetObj, 'update_time_int', params.timestamp)
  181. Vue.set(targetObj, 'cont', params.noDecryptoMsg ? params.content : decryptoMsg(params.content))
  182. },
  183. initChatData (state) {
  184. let data = initChatData()
  185. for (let i in state) {
  186. if (data.hasOwnProperty(i)) state[i] = data[i]
  187. }
  188. }
  189. }
  190. const actions = {
  191. async refreshSessionList ({ commit, state }, params) {
  192. let list = await objSession.getSortList()
  193. if (list && list.length) {
  194. // 从indexDB拿数据
  195. commit('setSessionList', list)
  196. }
  197. },
  198. async getSessionList ({ dispatch, commit, state }, params) {
  199. // 本地刷新缓存
  200. dispatch('refreshSessionList')
  201. // 从接口读取缓存数据
  202. API.session.sessionList(async ({ data }) => {
  203. if (data && data.data) {
  204. // 清空老数据
  205. await objSession.clearData()
  206. // 存储到indexDB,用过滤过的数据来渲染
  207. let list = await objSession.replaceSession(data.data)
  208. commit('setSessionList', list)
  209. }
  210. })
  211. },
  212. async getUserInfo ({ commit, state, rootState }) {
  213. try {
  214. let userId = rootState.userId || localStorage.getItem('user_id')
  215. let { data } = await API.user.getInfo({
  216. target_id: userId
  217. })
  218. commit('setUserInfo', data.data)
  219. commit('setGroupUserInfo', data.data)
  220. if (data.data.user_id) {
  221. // 更新自己的信息
  222. let objUser = new User()
  223. let newData = {
  224. cover_photo: data.data.cover_photo,
  225. nick_name: data.data.nick_name,
  226. user_name: data.data.user_name
  227. }
  228. objUser.updateObject(newData, { user_id: data.data.user_id })
  229. }
  230. } catch (error) {}
  231. },
  232. /**
  233. * @des 更新用户群组列表中指定sessionId的内容
  234. * @param {String} params.sessionId 待更新的sessionId
  235. * @param {Object} params.data 待更新的内容
  236. */
  237. updateSessionItem ({ commit, state }, params) {
  238. let targetObj = state.sessionList.find(n => {
  239. return n.session_id == params.sessionId
  240. })
  241. targetObj = Object.assign(targetObj, params.data)
  242. commit('setSessionItem', targetObj)
  243. },
  244. /**
  245. * 撤回消息
  246. * @param {Object} params
  247. * {index:number, session_id:string, hash:string}
  248. */
  249. async doRepealPersonMsg ({ dispatch, commit, state, rootState }, params = {}) {
  250. try {
  251. await API.person.repealPersonMsg({
  252. session_id: rootState.curSession,
  253. hash: params.hash
  254. })
  255. } catch (error) {}
  256. },
  257. async getFriendList ({ commit, state }) {
  258. try {
  259. let { data } = await API.group.getFriends()
  260. commit('setFriendList', data.data)
  261. } catch (error) {}
  262. }
  263. }
  264. const getters = {
  265. muteList: state => {
  266. // 免打扰
  267. return state.sessionList.filter(v => {
  268. if (v.is_mute == '1') {
  269. return v.session_id
  270. }
  271. })
  272. }
  273. }
  274. export default {
  275. state,
  276. mutations,
  277. actions,
  278. getters
  279. }