mutations.js 11 KB

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