actions.js 25 KB

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