mutations.js 11 KB

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