mutations.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import {
  2. addSomeInArray,
  3. addTimeMsgInItem,
  4. decryptoMsg,
  5. addLinkItem,
  6. formatPinMsg,
  7. checkAtMe,
  8. getUserOpt,
  9. setUserOpt
  10. } from '@/util/util.js'
  11. import _ from 'lodash'
  12. import Vue from 'vue'
  13. import Message from '@/store/db/Message.js'
  14. import { initGroupData } from '@/store/state'
  15. const objMessage = new Message()
  16. const mutations = {
  17. initGroup (state, data) {
  18. state.userId = data.userId
  19. state.groupId = data.groupId
  20. state.useCache = data.useCache
  21. },
  22. initState (state, data) {
  23. let groupData = initGroupData()
  24. for (let i in groupData) {
  25. if (i == 'userId' || i == 'userInfo') {
  26. break
  27. } else state[i] = groupData[i]
  28. }
  29. },
  30. setGroupInfo (state, data) {
  31. for (let i in data) {
  32. if (i == 'pinMsg') {
  33. formatPinMsg(data.pinMsg, state.userId)
  34. state.pinMsg = data.pinMsg || {}
  35. } else if (i == 'members') {
  36. state.members = Object.assign(data.members, state.userMap)
  37. } else {
  38. state[i] = data[i]
  39. }
  40. }
  41. },
  42. setGroupUserInfo (state, data) {
  43. state.userInfo = data
  44. state.userId = data.user_id
  45. },
  46. /**
  47. * @des 添加历史消息到当前chatList队列
  48. * @param {Object} state
  49. * @param {Array} data 待添加的消息队列
  50. */
  51. addHistoryList (state, data) {
  52. if (data.length) {
  53. addSomeInArray(data)
  54. // 根据 hash 求data相对于chatList的差集(去重)
  55. state.lastMsgUid = 0
  56. let newList = _.differenceBy(data, state.chatList, 'hash') || []
  57. state.chatList = newList.concat(state.chatList)
  58. }
  59. },
  60. /**
  61. * 清空聊天记录
  62. */
  63. clearChatList (state) {
  64. state.chatList = []
  65. state.pinMsg = {}
  66. state.groupId = null
  67. state.lastMsgUid = 0
  68. state.eosInfo = null
  69. },
  70. /**
  71. * @des 消息列表插入置顶消息
  72. * @param {*} state
  73. * @param {*} data
  74. */
  75. addPinChatItem (state, data) {
  76. let item = {
  77. name: data ? data.nick_name : 'unknown',
  78. content: data.content,
  79. userId: data.userId,
  80. timestamp: data.timestamp,
  81. avatar: data ? data.cover_photo : '',
  82. hash: data.hash,
  83. type: data.userId == state.userId ? 'me' : 'you',
  84. msg_type: data.msg_type,
  85. loading: false,
  86. res: data.res,
  87. joinMsg: '',
  88. fail: false,
  89. isShowFullInfo: true
  90. }
  91. state.chatList.splice(0, 0, item)
  92. },
  93. addChatItem (state, data) {
  94. let newMsg = !!data.newMsg
  95. let fromdb = !!data.fromdb
  96. let chatList = state.chatList
  97. let chatListLen = state.chatList.length
  98. if (data.group_id && data.group_id != state.groupId) return
  99. if (data.list) data = data.list
  100. if (chatListLen > 0) state.lastMsgUid = chatList[chatListLen - 1].userId
  101. if (data && data.length > 0) {
  102. addSomeInArray(data)
  103. if (fromdb) state.atList = []
  104. // 检测是否被@
  105. newMsg && !fromdb && data.forEach(item => {
  106. if (state.userInfo && checkAtMe(item.content, state.userInfo.user_name)) {
  107. state.atList.push(item)
  108. }
  109. })
  110. // // 根据 hash 求data相对于chatList的差集(去重)
  111. let newList = _.differenceBy(data, chatList, 'hash') || []
  112. chatList = chatList.concat(newList)
  113. } else {
  114. let lastShowMsgUid = getUserOpt('lastShowMsgUid') || 0
  115. let user = state.members[data.from] || data.user_info
  116. let item = {
  117. name: user ? user.nick_name : 'unknown',
  118. content: data.content,
  119. userId: data.from,
  120. timestamp: data.timestamp,
  121. avatar: user ? user.cover_photo : '',
  122. hash: data.hash,
  123. type: data.from == state.userId ? 'me' : 'you',
  124. msg_type: data.msg_type,
  125. loading: Boolean(data.loading),
  126. res: data.res,
  127. joinMsg: data.joinMsg,
  128. fail: false,
  129. isShowFullInfo: data.from != lastShowMsgUid
  130. }
  131. lastShowMsgUid = data.msg_type == 5 ? 0 : data.from
  132. setUserOpt('lastShowMsgUid', lastShowMsgUid)
  133. // 针对自己发送的情况
  134. if (data.createTime) {
  135. item.createTime = data.createTime
  136. }
  137. addTimeMsgInItem(item, chatList)
  138. addLinkItem(item)
  139. let index = chatList.findIndex(n => { return item.hash && n.hash === item.hash })
  140. if (index < 0) {
  141. chatList.push(item)
  142. }
  143. }
  144. state.chatList = chatList
  145. },
  146. addPacketItem (state, data) {
  147. let lastShowMsgUid = getUserOpt('lastShowMsgUid') || 0
  148. let item = {
  149. name: state.members[data.from]
  150. ? state.members[data.from].nick_name
  151. : 'unknown',
  152. timestamp: data.timestamp,
  153. avatar: state.members[data.from] ? state.members[data.from].cover_photo : '',
  154. userId: data.from,
  155. msg_type: data.type === 'new_redpack' ? 4 : 5,
  156. content: JSON.parse(decryptoMsg(data.content)),
  157. type: data.from == state.userId ? 'me' : 'you',
  158. ext: { grabbed: 0, redpack_status: 0 },
  159. hash: data.hash,
  160. isShowFullInfo: data.from != lastShowMsgUid
  161. }
  162. if (data.from != lastShowMsgUid) lastShowMsgUid = data.from
  163. setUserOpt('lastShowMsgUid', lastShowMsgUid)
  164. addTimeMsgInItem(item, state.chatList)
  165. state.chatList.push(item)
  166. state.endHash = data.hash
  167. },
  168. addPacketTip (state, data) {
  169. let item = {
  170. content: {
  171. trxId: data.content.redpack_trx_id,
  172. title: data.content.title
  173. },
  174. ext: {
  175. grabbed: 1,
  176. redpack_status: 0
  177. },
  178. avatar: state.members[data.from] ? state.members[data.from].cover_photo : '',
  179. name: state.members[data.from]
  180. ? state.members[data.from].nick_name
  181. : 'unknown',
  182. from: data.from,
  183. to: data.to,
  184. redPackTip: true
  185. }
  186. state.chatList.push(item)
  187. },
  188. unpdatePacketItem (state, { type, trxId, data }) {
  189. let list = state.chatList
  190. let ind = list.findIndex(e => {
  191. return e.msg_type == 4 && e.content.trxId == trxId
  192. })
  193. let item = list[ind]
  194. item.ext[type] = data
  195. Vue.set(state.chatList, ind, item)
  196. objMessage.updatePackInfo(item.hash, type, data)
  197. },
  198. reSendChatItem (state, data) {
  199. let chatList = state.chatList
  200. chatList.forEach(item => {
  201. if (item.createTime == data.createTime) {
  202. item.loading = true
  203. item.fail = false
  204. }
  205. })
  206. },
  207. removeAtListLast (state) {
  208. state.atList.pop()
  209. },
  210. clearAtList (state) {
  211. state.atList = []
  212. },
  213. /**
  214. * 撤回消息,设置消息数据为{ repealMsg: true }
  215. * @param {state} state
  216. * @param {Object|Number} data
  217. */
  218. repealChatItem (state, data) {
  219. let { chatList } = state
  220. for (let i = chatList.length - 1; i >= 0; i--) {
  221. let item = chatList[i]
  222. if (item.hash === data.hash) {
  223. Vue.set(item, 'repealMsg', true)
  224. Vue.set(item, 'from', data.from)
  225. }
  226. }
  227. objMessage.removeMsg(data['session_id'], data.hash)
  228. // 更新消息头像
  229. let lastShowMsgUid = 0
  230. state.chatList.forEach((n, index) => {
  231. n.isShowFullInfo = n.userId != lastShowMsgUid
  232. lastShowMsgUid = n.msg_type == 5 || n.repealMsg ? 0 : n.userId
  233. })
  234. setUserOpt('lastShowMsgUid', lastShowMsgUid)
  235. },
  236. deleteChatItem (state, hash) {
  237. let index = state.chatList.findIndex(item => item.hash === hash)
  238. state.chatList.splice(index, index)
  239. },
  240. addUnreadNums (state) {
  241. state.unreadNums++
  242. },
  243. resetUnreadNums (state) {
  244. state.unreadNums = 0
  245. },
  246. setHash (state, data) {
  247. if (data.startHash) {
  248. state.startHash = data.startHash
  249. }
  250. if (data.endHash) {
  251. state.endHash = data.endHash
  252. }
  253. },
  254. clearHash (state, data) {
  255. state.endHash = null
  256. },
  257. /**
  258. * @des 更新置顶
  259. */
  260. updatePin (state, data) {
  261. state.sessionInfo.is_pin = data
  262. },
  263. /**
  264. * @des 更新免打扰
  265. */
  266. updateMute (state, data) {
  267. state.sessionInfo.is_mute = data
  268. },
  269. /**
  270. * @des 更新加入信息
  271. */
  272. updateJoin (state, data) {
  273. state.isJoin = data
  274. },
  275. /**
  276. * @des 更新群信息
  277. */
  278. updateGroup (state, { key, data }) {
  279. state[key] = data
  280. },
  281. /**
  282. * @更新群成员
  283. */
  284. updateMembers (state, data) {
  285. state.members = Object.assign({}, data, state.members)
  286. },
  287. /**
  288. * @更新群成员组
  289. * @param type {add,del}
  290. * @param userInfo 用户信息
  291. */
  292. updateMembersArray (state, { type, userInfo }) {
  293. if (type == 'add') {
  294. let isAdd = state.membersArray.some((item) => {
  295. return userInfo.user_id == item.user_id
  296. })
  297. if (!isAdd) {
  298. ++state.membersNum
  299. state.membersArray.push(userInfo)
  300. }
  301. } else {
  302. let index = state.membersArray.findIndex((item) => {
  303. return item.user_id == userInfo.user_id
  304. })
  305. if (index > -1) {
  306. --state.membersNum
  307. state.membersArray.splice(index, 1)
  308. }
  309. }
  310. },
  311. /**
  312. * @des 更新members里自己的信息
  313. * @param {String} nickName 昵称
  314. * @param {String} imageUrl 头像
  315. */
  316. updateMemberInfo (state, { userId, nickName, imageUrl }) {
  317. for (let i in state.members) {
  318. if (i == userId) {
  319. let item = state.members[i]
  320. if (nickName) item['name'] = nickName
  321. if (imageUrl) {
  322. item['cover_photo'] = imageUrl
  323. state.isRefreshImg = true
  324. }
  325. Vue.set(state.members, i, item)
  326. }
  327. }
  328. },
  329. /**
  330. * @des 更新聊天输入框聚焦状态
  331. */
  332. updateChatInputFocus (state, focus) {
  333. state.chatInputFocus = focus
  334. },
  335. /**
  336. * @des 更新群组封禁人员列表
  337. * @param {String} param.type [delete,add]
  338. * @param {String} param.id
  339. */
  340. updateGroupBlockList (state, param) {
  341. let { type, id } = param
  342. if (type === 'delete') {
  343. let index = state.blockList.findIndex(uid => uid == id)
  344. index !== -1 && state.blockList.splice(index, 1)
  345. } else if (type === 'add') {
  346. state.blockList.push(id)
  347. }
  348. },
  349. /**
  350. * @param {Object|Null} param
  351. */
  352. updateGroupPinMsg (state, param) {
  353. if (param) {
  354. param.visible = true
  355. formatPinMsg(param, state.userId)
  356. state.pinMsg = param
  357. } else {
  358. state.pinMsg = {}
  359. }
  360. },
  361. /**
  362. * @des 更新是否停止搜索监听
  363. * @param {Object|Null} param
  364. */
  365. updateIsNewAt (state, param) {
  366. state.isNewAt = param.isNewAt
  367. state.isNewAtFound = param.isNewAtFound
  368. },
  369. /**
  370. * @des 更新搜索框提示列表
  371. * @param {Object|Null} param
  372. */
  373. updateGroupSearchList (state, param) {
  374. state.searchList = param
  375. },
  376. initGroupData (state) {
  377. let data = initGroupData()
  378. for (let i in state) {
  379. if (data.hasOwnProperty(i)) state[i] = data[i]
  380. }
  381. },
  382. /**
  383. * @des 更新刚发送消息的状态
  384. * @param {Number} data.index
  385. * @param {String} data.hash
  386. */
  387. updataChatItem (state, data) {
  388. let listItem = state.chatList[data.index]
  389. listItem.hash = data.hash
  390. listItem.loading = false
  391. listItem.isCommfirm = data.isCommfirm
  392. },
  393. /**
  394. * @des 记录上次信息用户id
  395. * */
  396. setLastMsgUid (state, data) {
  397. state.lastMsgUid = data
  398. },
  399. setGroupMembers (state, data) {
  400. state.members = data
  401. },
  402. /**
  403. *
  404. * @des 用于刷新图片
  405. * @param {Boolean} data
  406. */
  407. setIsRefreshImg (state, data) {
  408. state.isRefreshImg = data
  409. }
  410. }
  411. export default mutations