chat.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, 'cont', params.noDecryptoMsg ? params.content : decryptoMsg(params.content))
  181. },
  182. initChatData (state) {
  183. let data = initChatData()
  184. for (let i in state) {
  185. if (data.hasOwnProperty(i)) state[i] = data[i]
  186. }
  187. }
  188. }
  189. const actions = {
  190. async refreshSessionList ({ commit, state }, params) {
  191. let list = await objSession.getSortList()
  192. if (list && list.length) {
  193. // 从indexDB拿数据
  194. commit('setSessionList', list)
  195. }
  196. },
  197. async getSessionList ({ dispatch, commit, state }, params) {
  198. // 本地刷新缓存
  199. dispatch('refreshSessionList')
  200. // 从接口读取缓存数据
  201. API.session.sessionList(async ({ data }) => {
  202. if (data && data.data) {
  203. // 清空老数据
  204. await objSession.clearData()
  205. // 存储到indexDB,用过滤过的数据来渲染
  206. let list = await objSession.replaceSession(data.data)
  207. commit('setSessionList', list)
  208. }
  209. })
  210. },
  211. async getUserInfo ({ commit, state, rootState }) {
  212. try {
  213. let userId = rootState.userId || localStorage.getItem('user_id')
  214. let { data } = await API.user.getInfo({
  215. target_id: userId
  216. })
  217. commit('setUserInfo', data.data)
  218. commit('setGroupUserInfo', data.data)
  219. if (data.data.user_id) {
  220. // 更新自己的信息
  221. let objUser = new User()
  222. let newData = {
  223. cover_photo: data.data.cover_photo,
  224. nick_name: data.data.nick_name,
  225. user_name: data.data.user_name
  226. }
  227. objUser.updateObject(newData, { user_id: data.data.user_id })
  228. }
  229. } catch (error) {}
  230. },
  231. /**
  232. * @des 更新用户群组列表中指定sessionId的内容
  233. * @param {String} params.sessionId 待更新的sessionId
  234. * @param {Object} params.data 待更新的内容
  235. */
  236. updateSessionItem ({ commit, state }, params) {
  237. let targetObj = state.sessionList.find(n => {
  238. return n.session_id == params.sessionId
  239. })
  240. targetObj = Object.assign(targetObj, params.data)
  241. commit('setSessionItem', targetObj)
  242. },
  243. /**
  244. * 撤回消息
  245. * @param {Object} params
  246. * {index:number, session_id:string, hash:string}
  247. */
  248. async doRepealPersonMsg ({ dispatch, commit, state, rootState }, params = {}) {
  249. try {
  250. await API.person.repealPersonMsg({
  251. session_id: rootState.curSession,
  252. hash: params.hash
  253. })
  254. } catch (error) {}
  255. },
  256. async getFriendList ({ commit, state }) {
  257. try {
  258. let { data } = await API.group.getFriends()
  259. commit('setFriendList', data.data)
  260. } catch (error) {}
  261. }
  262. }
  263. const getters = {
  264. muteList: state => {
  265. // 免打扰
  266. return state.sessionList.filter(v => {
  267. if (v.is_mute == '1') {
  268. return v.session_id
  269. }
  270. })
  271. }
  272. }
  273. export default {
  274. state,
  275. mutations,
  276. actions,
  277. getters
  278. }