mutations.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. 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. * @param {String} nickName 昵称
  307. * @param {String} imageUrl 头像
  308. */
  309. updateMemberInfo (state, { userId, nickName, imageUrl }) {
  310. for (let i in state.members) {
  311. if (i == userId) {
  312. let item = state.members[i]
  313. if (nickName) item['name'] = nickName
  314. if (imageUrl) {
  315. item['cover_photo'] = imageUrl
  316. state.isRefreshImg = true
  317. }
  318. Vue.set(state.members, i, item)
  319. }
  320. }
  321. },
  322. /**
  323. * @des 更新聊天输入框聚焦状态
  324. */
  325. updateChatInputFocus (state, focus) {
  326. state.chatInputFocus = focus
  327. },
  328. /**
  329. * @des 更新群组封禁人员列表
  330. * @param {String} param.type [delete,add]
  331. * @param {String} param.id
  332. */
  333. updateGroupBlockList (state, param) {
  334. let { type, id } = param
  335. if (type === 'delete') {
  336. let index = state.blockList.findIndex(uid => uid == id)
  337. index !== -1 && state.blockList.splice(index, 1)
  338. } else if (type === 'add') {
  339. state.blockList.push(id)
  340. }
  341. },
  342. /**
  343. * @param {Object|Null} param
  344. */
  345. updateGroupPinMsg (state, param) {
  346. if (param) {
  347. param.visible = true
  348. formatPinMsg(param, state.userId)
  349. state.pinMsg = param
  350. } else {
  351. state.pinMsg = {}
  352. }
  353. },
  354. /**
  355. * @des 更新是否停止搜索监听
  356. * @param {Object|Null} param
  357. */
  358. updateIsNewAt (state, param) {
  359. state.isNewAt = param.isNewAt
  360. state.isNewAtFound = param.isNewAtFound
  361. },
  362. /**
  363. * @des 更新搜索框提示列表
  364. * @param {Object|Null} param
  365. */
  366. updateGroupSearchList (state, param) {
  367. state.searchList = param
  368. },
  369. initGroupData (state) {
  370. let data = initGroupData()
  371. for (let i in state) {
  372. if (data.hasOwnProperty(i)) state[i] = data[i]
  373. }
  374. },
  375. /**
  376. * @des 更新刚发送消息的状态
  377. * @param {Number} data.index
  378. * @param {String} data.hash
  379. */
  380. updataChatItem (state, data) {
  381. let listItem = state.chatList[data.index]
  382. listItem.hash = data.hash
  383. listItem.loading = false
  384. listItem.isCommfirm = data.isCommfirm
  385. },
  386. /**
  387. * @des 记录上次信息用户id
  388. * */
  389. setLastMsgUid (state, data) {
  390. state.lastMsgUid = data
  391. },
  392. setGroupMembers (state, data) {
  393. state.members = data
  394. },
  395. /**
  396. *
  397. * @des 用于刷新图片
  398. * @param {Boolean} data
  399. */
  400. setIsRefreshImg (state, data) {
  401. state.isRefreshImg = data
  402. }
  403. }
  404. export default mutations