actionsOld.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. import API from '@/api'
  2. import { getWsUrl } from '@/util/contract.js'
  3. import WsManager from '@/util/wsManager.js'
  4. import {
  5. decryptoMsg,
  6. dealErrorMsg,
  7. noticeManager,
  8. getUserOpt,
  9. setUserOpt
  10. } from '@/util/util.js'
  11. import axios from 'axios'
  12. import Message from '@/store/db/Message.js'
  13. var socket = null
  14. const actions = {
  15. /**
  16. * @des 获取群基础信息
  17. * @param {Object} store
  18. * @param {Object} params
  19. */
  20. async getGroupInfo ({ dispatch, commit, state }, params) {
  21. API.group.getGroupInfo2({ group_id: state.groupId }, info => {
  22. if (info.data.code === 0) {
  23. let adminList = info.data.data.adminList
  24. let blockList = info.data.data.blockList
  25. let pinMsg = info.data.data.pinMsg
  26. let group = info.data.data.group
  27. let members = info.data.data.members
  28. let membersNum = info.data.data.group.member_num
  29. let userInfo = info.data.data.userInfo
  30. let sessionInfo = info.data.data.sessionInfo
  31. let isJoin = info.data.data.members.some((item) => {
  32. return state.userId == item.user_id
  33. })
  34. let eosInfo = info.data.data.eosInfo
  35. pinMsg && (pinMsg.visible = true)
  36. let _members = {}
  37. members.forEach(n => {
  38. if (!n.user_id) return
  39. _members[n.user_id] = n
  40. })
  41. commit('setGroupInfo', {
  42. userCounts: members.length,
  43. groupName: group.group_title,
  44. shareName: group.group_name,
  45. members: _members,
  46. membersArray: members,
  47. membersNum: membersNum,
  48. userInfo,
  49. groupNotice: group.group_notice,
  50. url: group.url,
  51. inviteUrl: group.invite_url,
  52. sessionInfo: sessionInfo || {
  53. is_mute: 0,
  54. is_pin: 0,
  55. pin_time_int: 0
  56. },
  57. isJoin,
  58. coverPhoto: group.cover_photo,
  59. adminList: adminList,
  60. blockList: blockList,
  61. pinMsg: pinMsg || {},
  62. creator: group.creator,
  63. eosInfo: eosInfo
  64. })
  65. }
  66. })
  67. },
  68. /**
  69. * @des 群聊 获取最新消息【from indexeddb】
  70. * @param {Object} store
  71. * @param {Object} params
  72. */
  73. async getNewMsgFromDb (store, params = {}) {
  74. // 先读取indexDB的数据,来渲染
  75. let { state, commit } = store
  76. let { groupId } = state
  77. const objMessage = new Message()
  78. let data = await objMessage.getMsg(state.groupId)
  79. setUserOpt('lastShowMsgUid', 0)
  80. if (groupId === state.groupId && data && data.list.length > 0) {
  81. params.fromdb = true
  82. actions._renderGroupMsg(store, data, params)
  83. commit('setGroupMembers', Object.assign(data.userMap, state.members))
  84. return true
  85. } else {
  86. return false
  87. }
  88. },
  89. /**
  90. * @des 群聊 获取最新消息【不需要登录】
  91. * @param {Object} store
  92. * @param {Object} params
  93. */
  94. async getNewMsg (store, params = {}) {
  95. let { state, commit } = store
  96. let { groupId, endHash } = state
  97. // 然后再请求网上的数据,来渲染
  98. let msg = await API.group.getNewMsg({
  99. group_id: groupId,
  100. client_hash: endHash
  101. }).catch(() => {})
  102. if (groupId === state.groupId && msg.data.code === 0 && msg.data.data.list.length > 0) {
  103. params.fromdb = false
  104. commit('setGroupMembers', Object.assign(msg.data.data.userMap, state.members))
  105. actions._renderGroupMsg(store, msg.data.data, params)
  106. }
  107. return 'done'
  108. },
  109. _renderGroupMsg ({ dispatch, commit, state }, data, params) {
  110. let list = []
  111. // 解密数据&&计算头像信息
  112. let lastShowMsgUid = getUserOpt('lastShowMsgUid') || 0
  113. data.list.forEach(n => {
  114. let member = data.userMap[n.from] || {}
  115. if (!member) return
  116. // 显示信息
  117. list.push({
  118. name: member.nick_name || 'unknown',
  119. content: n.msg,
  120. userId: n.from,
  121. timestamp: n.create_time_int,
  122. avatar: member.cover_photo || '',
  123. hash: n.hash,
  124. type: n.from == state.userId ? 'me' : 'you',
  125. msg_type: n.msg_type,
  126. ext: n.ext ? n.ext : null,
  127. ext_info: n.ext_info ? n.ext_info : null,
  128. isShowFullInfo: n.from != lastShowMsgUid
  129. })
  130. if (n.msg_type == 5) lastShowMsgUid = 0
  131. lastShowMsgUid = n.msg_type == 5 ? 0 : n.from
  132. })
  133. setUserOpt('lastShowMsgUid', lastShowMsgUid)
  134. commit('updateMembers', data.userMap)
  135. // if (state.chatList.length) {
  136. // commit('setHash', {
  137. // endHash: list[list.length - 1].hash,
  138. // startHash: list[0].hash
  139. // })
  140. // } else {
  141. // commit('setHash', {
  142. // endHash: list[list.length - 1].hash,
  143. // startHash: list[0].hash
  144. // })
  145. // }
  146. commit('setHash', {
  147. endHash: list[list.length - 1] ? list[list.length - 1].hash : '',
  148. startHash: list[0] ? list[0].hash : ''
  149. })
  150. let itemData = params.newMsg ? { list: list, newMsg: params.newMsg, fromdb: false } : { list: list, fromdb: params.fromdb }
  151. commit('addChatItem', itemData)
  152. },
  153. async getPrivateNewMsgFromDb (store, params = {}) {
  154. let { rootState, state, commit } = store
  155. let { curSession } = rootState
  156. const objMessage = new Message()
  157. let data = await objMessage.getMsg(curSession)
  158. setUserOpt('lastShowMsgUid', 0)
  159. if (curSession === rootState.curSession && data && data.list.length > 0) {
  160. actions._renderPrivateMsg(store, data, params)
  161. commit('setGroupMembers', Object.assign(data.userMap, state.members))
  162. return true
  163. } else {
  164. return false
  165. }
  166. },
  167. /**
  168. * @des 私聊 获取最新消息【不需要登录】
  169. * @param {Object} store
  170. * @param {Object} params
  171. */
  172. async getPrivateNewMsg (store, params = {}) {
  173. let { state, rootState } = store
  174. let { curSession } = rootState
  175. let msg = await API.person.getNewMsg({
  176. session_id: curSession,
  177. client_hash: state.endHash
  178. }).catch(() => {})
  179. // 有聊天记录
  180. // if (msg.data.code === 0 && msg.data.data.list.length > 0) {
  181. // 没有聊天记录也要渲染,不然没设置member
  182. if (curSession === rootState.curSession && msg.data.code === 0) {
  183. actions._renderPrivateMsg(store, msg.data.data, params)
  184. }
  185. return msg.data
  186. },
  187. _renderPrivateMsg ({ dispatch, commit, state, rootState }, data, params) {
  188. let list = []
  189. // 更新members信息
  190. let members = data.userMap
  191. commit('updateGroup', {
  192. key: 'members',
  193. data: members
  194. })
  195. for (let k in members) {
  196. if (k !== rootState.userId) {
  197. commit('updateGroup', {
  198. key: 'privateName',
  199. data: (members[k] && members[k]['nick_name']) || ''
  200. })
  201. }
  202. }
  203. let lastShowMsgUid = getUserOpt('lastShowMsgUid') || 0
  204. data.list.forEach(n => {
  205. let member = data.userMap[n.from]
  206. if (!member) return
  207. list.push({
  208. name: member ? member.nick_name : 'unknown',
  209. content: n.msg,
  210. userId: n.from,
  211. timestamp: n.create_time_int,
  212. avatar: member.cover_photo || '',
  213. hash: n.hash,
  214. type: n.from == rootState.userId ? 'me' : 'you',
  215. msg_type: n.msg_type,
  216. ext: n.ext ? n.ext : null,
  217. ext_info: n.ext_info ? n.ext_info : null,
  218. isShowFullInfo: n.from != lastShowMsgUid
  219. })
  220. if (n.msg_type == 5) lastShowMsgUid = 0
  221. lastShowMsgUid = n.msg_type == 5 ? 0 : n.from
  222. })
  223. setUserOpt('lastShowMsgUid', lastShowMsgUid)
  224. if (list.length) {
  225. if (state.chatList.length) {
  226. commit('setHash', {
  227. endHash: list[list.length - 1].hash
  228. })
  229. } else {
  230. commit('setHash', {
  231. endHash: list[0].hash,
  232. startHash: list[0].hash
  233. })
  234. }
  235. let itemData = params.newMsg ? { list: list, newMsg: params.newMsg } : list
  236. commit('addChatItem', itemData)
  237. }
  238. },
  239. /**
  240. * @des 群聊 获取历史消息【不需要登录】
  241. * @param {Object} store
  242. * @param {Object} params
  243. */
  244. async getHistoryMsg ({ dispatch, commit, state, rootState }, params = {}) {
  245. let his = localStorage.getItem(params.hash) // 先从本地缓存中获取记录
  246. let dList = []
  247. let status = '' // 请求状态
  248. if (his && state.useCache) {
  249. dList = JSON.parse(his)
  250. } else {
  251. // 本地缓存中没有记录,从接口获取
  252. let msg = await API.group.getHistoryMsg({
  253. group_id: state.groupId,
  254. client_hash: state.startHash
  255. }).catch(() => {})
  256. if (msg && msg.data.code === 0 && msg.data.data.list.length > 0) {
  257. // 重新格式数据
  258. let lastShowMsgUid = 0
  259. msg.data.data.list.forEach(n => {
  260. if (n.msg_type == 4) {
  261. n.msg = JSON.parse(decryptoMsg(n.msg))
  262. } else {
  263. n.msg = decryptoMsg(n.msg)
  264. }
  265. let member = msg.data.data.userMap[n.from]
  266. commit('updateMembers', msg.data.data.userMap)
  267. if (!member) return
  268. dList.push({
  269. name: member ? member.nick_name : 'unknown',
  270. content: n.msg,
  271. userId: n.from,
  272. timestamp: n.create_time_int,
  273. avatar: member ? member.cover_photo : '',
  274. hash: n.hash,
  275. type: n.from == state.userId ? 'me' : 'you',
  276. msg_type: n.msg_type,
  277. ext: n.ext ? n.ext : null,
  278. isShowFullInfo: n.from != lastShowMsgUid
  279. })
  280. if (n.msg_type == 5) lastShowMsgUid = 0
  281. lastShowMsgUid = n.msg_type == 5 ? 0 : n.from
  282. })
  283. // 保存到本地缓存中
  284. if (state.useCache) {
  285. localStorage.setItem(params.hash, JSON.stringify(dList))
  286. }
  287. }
  288. }
  289. if (dList.length > 0) {
  290. commit('setHash', {
  291. startHash: dList[0].hash
  292. })
  293. commit('addHistoryList', dList)
  294. status = 'done'
  295. } else {
  296. commit('setHash', {
  297. startHash: null
  298. })
  299. status = 'end'
  300. }
  301. return status
  302. },
  303. /**
  304. * @des 私聊 获取历史消息【不需要登录】
  305. * @param {Object} store
  306. * @param {Object} params
  307. */
  308. async getPrivateHistoryMsg (
  309. { dispatch, commit, state, rootState },
  310. params = {}
  311. ) {
  312. let his = localStorage.getItem(params.hash) // 先从本地缓存中获取记录
  313. let dList = []
  314. let status = '' // 请求状态
  315. if (his && state.useCache) {
  316. dList = JSON.parse(his)
  317. } else {
  318. // 本地缓存中没有记录,从接口获取
  319. let msg = await API.person
  320. .getHistoryMsg({
  321. session_id: rootState.curSession,
  322. client_hash: state.startHash
  323. })
  324. .catch(() => {})
  325. if (msg.data.code === 0 && msg.data.data.list.length > 0) {
  326. // 重新格式数据
  327. let lastShowMsgUid = 0
  328. msg.data.data.list.forEach((n, index) => {
  329. n.msg = decryptoMsg(n.msg)
  330. let member = msg.data.data.userMap[n.from]
  331. dList.push({
  332. name: member ? member.nick_name : 'unknow',
  333. content: n.msg,
  334. userId: n.from,
  335. timestamp: n.create_time_int,
  336. avatar: member.cover_photo || '',
  337. hash: n.hash,
  338. type: n.from == rootState.userId ? 'me' : 'you',
  339. msg_type: n.msg_type,
  340. ext: n.ext ? n.ext : null,
  341. isShowFullInfo: n.from != lastShowMsgUid
  342. })
  343. if (n.msg_type == 5) lastShowMsgUid = 0
  344. lastShowMsgUid = n.msg_type == 5 ? 0 : n.from
  345. })
  346. // 保存到本地缓存中
  347. if (state.useCache) {
  348. localStorage.setItem(params.hash, JSON.stringify(dList))
  349. }
  350. }
  351. }
  352. if (dList.length > 0) {
  353. commit('setHash', {
  354. startHash: dList[0].hash
  355. })
  356. commit('addHistoryList', dList)
  357. status = 'done'
  358. } else {
  359. commit('setHash', {
  360. startHash: null
  361. })
  362. status = 'end'
  363. }
  364. return status
  365. },
  366. /**
  367. * 发送群聊消息
  368. * @param {Object} data
  369. * @param {Object} params
  370. */
  371. doSendMsg ({ dispatch, commit, state }, params = {}) {
  372. return new Promise((resolve, reject) => {
  373. API.group.sendMsg({
  374. group_id: state.groupId,
  375. msg_type: params.type,
  376. msg: params.msg
  377. }).then(({ data }) => {
  378. if (data.code === 0) {
  379. if (params.createTime) {
  380. let createTime = params.createTime
  381. data.data.content = decryptoMsg(data.data.content)
  382. let list = state.chatList
  383. for (let i = list.length - 1; i >= 0; i--) {
  384. let listItem = list[i]
  385. if (listItem.createTime === createTime) {
  386. commit('updataChatItem', {
  387. index: i,
  388. hash: data.data.hash,
  389. isCommfirm: true
  390. })
  391. break
  392. }
  393. }
  394. } else {
  395. // 针对用户第一次发言
  396. commit('updateMembers', data.data.userMap)
  397. data.data.content = decryptoMsg(data.data.content)
  398. commit('addChatItem', data.data)
  399. }
  400. commit('setHash', {
  401. endHash: data.data.hash
  402. })
  403. commit('updateSessionLastMsgNoDecode', Object.assign(data.data, { group_id: state.groupId }))
  404. resolve(data.data)
  405. } else {
  406. if (params.createTime) {
  407. dealErrorMsg(state, params.createTime)
  408. }
  409. }
  410. }).catch(err => {
  411. if (params.createTime) {
  412. dealErrorMsg(state, params.createTime)
  413. }
  414. reject(err)
  415. })
  416. })
  417. },
  418. /**
  419. * 发送聊天文件
  420. * @param {Object} data
  421. * @param {Object} params
  422. */
  423. async doSendFile ({ dispatch, commit, state, rootState }, params = {}) {
  424. return new Promise((resolve, reject) => {
  425. let userId = rootState.userId
  426. let token = rootState.token
  427. let formData = new FormData()
  428. formData.append('res', params.res)
  429. formData.append('user_id', userId)
  430. formData.append('token', token)
  431. // 私聊群聊参数调整
  432. if (state.groupId) {
  433. formData.append('group_id', state.groupId)
  434. } else {
  435. let arr = rootState.curSession.split('-')
  436. let toUser
  437. if (arr[0] == rootState.userId) toUser = arr[1]
  438. else toUser = arr[0]
  439. formData.append('to_user', toUser)
  440. }
  441. let sendUrl = state.groupId ? 'group/sendFile' : 'person/sendFile'
  442. axios({
  443. url: [API.host, sendUrl].join(''),
  444. method: 'post',
  445. data: formData,
  446. headers: { 'Content-Type': 'multipart/form-data' }
  447. })
  448. .then(({ data }) => {
  449. if (data.code === 0) {
  450. if (params.createTime) {
  451. let createTime = params.createTime
  452. data.data.content = decryptoMsg(data.data.content)
  453. let list = state.chatList
  454. list.forEach(item => {
  455. if (item.createTime === createTime) {
  456. item.res = undefined
  457. item.content = data.data.content
  458. item.hash = data.data.hash
  459. item.loading = false
  460. item.fail = false
  461. if (!item.ext_info) item.ext_info = {}
  462. item.ext_info.cover_url = data.data.ext_info && data.data.ext_info.cover_url
  463. }
  464. })
  465. } else {
  466. // 针对用户第一次发言
  467. commit('updateMembers', data.data.userMap)
  468. data.data.content = decryptoMsg(data.data.content)
  469. commit('addChatItem', data.data)
  470. }
  471. commit('setHash', {
  472. endHash: data.data.hash
  473. })
  474. commit('updateSessionLastMsgNoDecode', Object.assign(data.data, { group_id: state.groupId || rootState.curSession }))
  475. resolve(data.data)
  476. } else {
  477. if (params.createTime) {
  478. dealErrorMsg(state, params.createTime)
  479. }
  480. }
  481. })
  482. .catch(err => {
  483. if (params.createTime) {
  484. dealErrorMsg(state, params.createTime)
  485. }
  486. reject(err)
  487. })
  488. })
  489. },
  490. /**
  491. * 发送私聊消息
  492. * @param {Object} data
  493. * @param {Object} params
  494. */
  495. doSendPrivateMsg ({ dispatch, commit, state, rootState }, params = {}) {
  496. return new Promise((resolve, reject) => {
  497. API.person.sendMsg({
  498. session_id: rootState.curSession,
  499. msg_type: params.type,
  500. msg: params.msg
  501. }).then(({ data }) => {
  502. if (data.code === 0) {
  503. if (params.createTime) {
  504. let createTime = params.createTime
  505. data.data.content = decryptoMsg(data.data.content)
  506. let list = state.chatList
  507. for (let i = list.length - 1; i >= 0; i--) {
  508. let listItem = list[i]
  509. if (listItem.createTime === createTime) {
  510. commit('updataChatItem', {
  511. index: i,
  512. hash: data.data.hash,
  513. isCommfirm: true
  514. })
  515. break
  516. }
  517. }
  518. } else {
  519. // 针对用户第一次发言
  520. commit('updateMembers', data.data.userMap)
  521. data.data.content = decryptoMsg(data.data.content)
  522. commit('addChatItem', data.data)
  523. }
  524. commit('setHash', {
  525. endHash: data.data.hash
  526. })
  527. commit('updateSessionLastMsgNoDecode', Object.assign(data.data, { group_id: rootState.curSession }))
  528. resolve(data.data)
  529. } else {
  530. if (params.createTime) {
  531. dealErrorMsg(state, params.createTime)
  532. }
  533. }
  534. }).catch(err => {
  535. if (params.createTime) {
  536. dealErrorMsg(state, params.createTime)
  537. }
  538. reject(err)
  539. })
  540. })
  541. },
  542. /**
  543. * 链接websocket
  544. * @param {Object} data
  545. * @param {Object} params
  546. */
  547. initSocket ({ commit, state, rootState, rootGetters, dispatch }, params = {}) {
  548. if (!window.WebSocket) {
  549. console.error('Error: WebSocket is not supported .')
  550. return
  551. }
  552. let host = `${getWsUrl()}/?user_id=${rootState.userId}&token=${
  553. rootState.token
  554. }`
  555. if (socket) {
  556. socket.destroy()
  557. socket = null
  558. }
  559. socket = new WsManager(host, {
  560. autoConnect: true, // 自动连接
  561. reconnection: true, // 断开自动重连
  562. reconnectionDelay: 5000, // 重连间隔时间,单位秒
  563. keepAliveContent: JSON.stringify({ act: 'alive', player: name }) // 心跳包内容
  564. })
  565. socket.on('open', res => {})
  566. socket.on('message', e => {
  567. let _data = JSON.parse(e)
  568. let channel = _data.channel
  569. let data = _data.data
  570. let muteList = rootGetters.muteList // 免打扰列表
  571. // 群聊
  572. if (channel.match('chat:group')) {
  573. let isMute = muteList.some(v => {
  574. return v.session_id == data.group_id
  575. })
  576. if (data.type === 'msg') {
  577. if (rootState.curSession == data.group_id) {
  578. // 当前会话需要加载新消息
  579. dispatch('getNewMsg', { newMsg: true })
  580. if (data.from != rootState.userId) commit('addUnreadNums')
  581. }
  582. if (!isMute) {
  583. noticeManager.showNotification(data)
  584. }
  585. }
  586. if (data.type === 'update') {
  587. }
  588. if (data.type === 'add_admin') {
  589. }
  590. if (data.type === 'remove_admin') {
  591. }
  592. if (data.type === 'repeal') {
  593. commit('repealChatItem', data)
  594. // 左侧会话
  595. data.msg_type = -1
  596. commit('updateSessionLastmsg', data)
  597. }
  598. if (data.type === 'block') {
  599. commit('updateGroupBlockList', {
  600. type: 'add',
  601. id: data.to
  602. })
  603. }
  604. if (data.type === 'unblock') {
  605. commit('updateGroupBlockList', {
  606. type: 'delete',
  607. id: data.to
  608. })
  609. }
  610. if (data.type === 'leave') {
  611. if (data.group_id != state.groupId) return // 不是本群信息-无响应
  612. if (data.user_info.user_id == rootState.userId) return // 本人-不走这个ws
  613. // 退群仅通知管理员
  614. // let isAdmin = state.adminList.some(item => { return item == rootState.userId })
  615. // if (isAdmin) {
  616. // data.joinMsg = (data.user_info.nick_name || data.user_info.name) + '退出群组'
  617. // commit('addChatItem', data)
  618. // }
  619. // 本人退出刷新群信息
  620. if (data.user_id == rootState.userId) {
  621. dispatch('getGroupInfo')
  622. } else {
  623. commit('updateMembersArray', {
  624. type: 'del',
  625. userInfo: data.user_info
  626. })
  627. }
  628. }
  629. if (data.type === 'join') {
  630. if (data.group_id != state.groupId) return
  631. if (data.user_info.user_id == rootState.userId) return
  632. data.joinMsg = (data.user_info.nick_name || data.user_info.name) + '加入群组'
  633. commit('updateMembers', {
  634. [data.user_info.user_id]: data.user_info
  635. })
  636. commit('updateMembersArray', {
  637. type: 'add',
  638. userInfo: data.user_info
  639. })
  640. // commit('addChatItem', data)
  641. }
  642. if (data.type === 'pin_msg') {
  643. commit('updateGroupPinMsg', data.pinMsg)
  644. }
  645. if (data.type === 'unpin_msg') {
  646. commit('updateGroupPinMsg', null)
  647. }
  648. if (data.type === 'new_redpack' && rootState.curSession == data.group_id) {
  649. commit('addPacketItem', data)
  650. commit('updateSessionLastmsg', data)
  651. }
  652. if (data.type === 'grab_redpack' && rootState.curSession == data.group_id) {
  653. if (data.from == rootState.userId || data.to == rootState.userId) {
  654. commit('addPacketTip', data)
  655. commit('updateSessionLastmsg', data)
  656. }
  657. }
  658. }
  659. // 私聊
  660. if (channel.match('chat:person')) {
  661. if (!rootState.curSession) return
  662. let curId = +data.to > +data.from ? `${data.from}-${data.to}` : `${data.to}-${data.from}`
  663. let isMute = muteList.some(v => {
  664. return v.session_id == curId
  665. })
  666. if (data.type === 'msg') {
  667. if (rootState.curSession == curId) {
  668. // 当前会话需要加载新消息
  669. dispatch('getPrivateNewMsg')
  670. if (data.from != rootState.userId) commit('addUnreadNums')
  671. }
  672. // 如果在隐藏tab就需要 通知
  673. if (!isMute) {
  674. noticeManager.showNotification(data)
  675. }
  676. }
  677. if (data.type === 'repeal') {
  678. commit('repealChatItem', data)
  679. let sessionId = data.from > data.to ? `${data.to}-${data.from}` : `${data.from}-${data.to}`
  680. data.group_id = sessionId
  681. data.msg_type = -1
  682. commit('updateSessionLastmsg', data)
  683. }
  684. // if (data.type === 'new_redpack' && rootGetters.otherUserId == data.from) {
  685. if (data.type === 'new_redpack') {
  686. commit('addPacketItem', data)
  687. }
  688. if (data.type === 'grab_redpack' && rootGetters.otherUserId == data.from) {
  689. if (data.from == rootState.userId || data.to == rootState.userId) {
  690. commit('addPacketTip', data)
  691. }
  692. }
  693. if (data.type === 'join_group') {
  694. commit('updateMembers', {
  695. [rootState.userInfo.user_id]: rootState.userInfo
  696. })
  697. commit('updateMembersArray', {
  698. type: 'add',
  699. userInfo: rootState.userInfo
  700. })
  701. // commit('addChatItem', data)
  702. }
  703. if (data.type === 'leave_group') {
  704. commit('updateMembersArray', {
  705. type: 'del',
  706. userInfo: rootState.userInfo
  707. })
  708. // commit('addChatItem', data)
  709. dispatch('getGroupInfo')
  710. }
  711. }
  712. // 有新session 更新sessionList-自己退群不做处理
  713. let isLeaveGroup = data.msg_type == 5 && data.ext_info.event_type == 'leave_group'
  714. if (!isLeaveGroup && (data.type === 'msg' || data.type === 'new_redpack')) {
  715. let sessionList = rootState.chat.sessionList
  716. let curId
  717. if (data.group_id) {
  718. curId = data.group_id
  719. } else {
  720. curId = +data.to > +data.from ? `${data.from}-${data.to}` : `${data.to}-${data.from}`
  721. }
  722. let isInSession = sessionList.some(v => {
  723. return v.session_id == curId
  724. })
  725. let baseObj = {
  726. session_id: curId,
  727. cont: decryptoMsg(data.content),
  728. unread: rootState.userId == data.from ? 0 : 1,
  729. update_time_int: data.timestamp,
  730. timestamp: data.timestamp
  731. }
  732. if (!isInSession) {
  733. if (!data.group_id) {
  734. // 私聊要拿对方的头像
  735. API.user.getOtherInfo({
  736. target_id: rootState.userId === data.from ? data.to : data.from
  737. }).then(({ data }) => {
  738. let obj = {
  739. cover_photo: data.data.cover_photo,
  740. is_group: '0',
  741. name: data.data.nick_name
  742. }
  743. commit('addSessionItem', Object.assign(obj, baseObj))
  744. })
  745. } else {
  746. API.group.getGroupInfo({
  747. group_id: data.group_id
  748. }).then(({ data }) => {
  749. let group = data.data.group
  750. let obj = {
  751. cover_photo: group.cover_photo,
  752. is_group: '1',
  753. is_auth: group.is_auth,
  754. name: group.group_title
  755. }
  756. commit('addSessionItem', Object.assign(obj, baseObj))
  757. })
  758. }
  759. }
  760. if (data.from != rootState.userId) {
  761. commit('setSessionItemUnread', Object.assign({ curSession: rootState.curSession }, baseObj))
  762. }
  763. commit('updateSessionLastmsg', Object.assign(data, { sessionId: curId }))
  764. }
  765. })
  766. },
  767. /**
  768. * 撤回消息
  769. * @param {Object} params
  770. * {index:number, session_id:string, hash:string}
  771. */
  772. async doRepealGroupMsg ({ dispatch, commit, state }, params = {}) {
  773. try {
  774. await API.group.repealGroupMsg({
  775. group_id: state.groupId,
  776. hash: params.hash
  777. })
  778. } catch (error) {}
  779. },
  780. /**
  781. * 封禁群用户
  782. */
  783. async doBlockUser ({ state }, params = {}) {
  784. try {
  785. await API.group.blockUser({
  786. group_id: state.groupId,
  787. block_id: params.id
  788. })
  789. } catch (error) {}
  790. },
  791. /**
  792. * 解禁用户
  793. */
  794. async doUnBlockUser ({ state }, params = {}) {
  795. try {
  796. await API.group.unblockUser({
  797. group_id: state.groupId,
  798. block_id: params.id
  799. })
  800. } catch (error) {}
  801. },
  802. /**
  803. * 更新置顶
  804. */
  805. async doPinMsg ({ state }, params = {}) {
  806. try {
  807. await API.group.pinMsg({
  808. group_id: state.groupId,
  809. hash: params.hash
  810. })
  811. } catch (error) {}
  812. },
  813. /**
  814. * 取消置顶
  815. */
  816. async doUnpinMsg ({ state }, params = {}) {
  817. try {
  818. await API.group.unpinMsg({
  819. group_id: state.groupId,
  820. hash: params.hash
  821. })
  822. } catch (error) {}
  823. },
  824. /**
  825. * 加入群组
  826. */
  827. async joinGroup ({ state, commit }, params = {}) {
  828. try {
  829. await API.group.joinGroup({
  830. group_id: state.groupId
  831. })
  832. commit('updateJoin', true)
  833. commit('changeHotGroupStatus', {
  834. groupId: state.groupId,
  835. isJoin: 1
  836. })
  837. } catch (error) {}
  838. }
  839. }
  840. export default actions