mutations.js 9.5 KB

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