chatMiniHandle.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. import msgItem from '@/components/msgItem'
  2. import emoji from '@/components/emoji'
  3. import chatAt from '@/components/chatAt'
  4. import atMe from '@/components/chatAt/atme'
  5. import chatPin from '@/components/chatPin'
  6. import toolbar from '@/components/chatInput/toolbar'
  7. import { mapActions, mapState, mapMutations } from 'vuex'
  8. import { getMiniWsUrl } from '@/util/contract.js'
  9. import { isMobile, lazyloadImage } from '@/util/util.js'
  10. import WsManager from '@/util/wsManager.js'
  11. import { Message } from 'element-ui'
  12. import ImageMin from '@/util/imageMin.js'
  13. import { chatAtMixin, chatInputMixin, changeLangMixin } from '@/mixins'
  14. import { chatCommonMixin } from '@/mixins/chat'
  15. import { accountLoginMixin } from '@/mixins/login'
  16. import { emojiList } from '@/util/emoji'
  17. import api from '@/api'
  18. import loginBox from '@/components/login/loginBox'
  19. export default {
  20. name: 'chatMini',
  21. mixins: [accountLoginMixin, chatAtMixin, chatInputMixin, chatCommonMixin, changeLangMixin],
  22. components: {
  23. msgItem,
  24. emoji,
  25. chatAt,
  26. chatPin,
  27. atMe,
  28. toolbar,
  29. loginBox
  30. },
  31. props: {
  32. width: {
  33. type: Number,
  34. default: 274
  35. },
  36. height: {
  37. type: Number,
  38. default: 390
  39. },
  40. show: {
  41. type: Boolean,
  42. default: false
  43. },
  44. groupId: [Number, String]
  45. },
  46. computed: {
  47. ...mapState([
  48. 'account',
  49. 'group',
  50. 'userId',
  51. 'userInfo'
  52. ]),
  53. ...mapState({
  54. chatInputFocus: state => state.group.chatInputFocus,
  55. blockList: state => state.group.blockList,
  56. pinMsg: state => state.group.pinMsg,
  57. atList: state => state.group.atList,
  58. chatList: state => state.group.chatList,
  59. unreadNums: state => state.group.unreadNums,
  60. sessionList: state => state.chat.sessionList,
  61. isJoin: state => state.group.isJoin
  62. }),
  63. avatarUrl () {
  64. if (/^http/.test(this.userInfo.cover_photo)) return `${this.userInfo.cover_photo}?imageview/0/w/400`
  65. else return this.userInfo.cover_photo
  66. },
  67. emojiMap () {
  68. var emojiMap = {}
  69. for (let i in emojiList) {
  70. let arr = emojiList[i]
  71. arr.forEach(v => {
  72. let names = JSON.stringify(v.names)
  73. let emoji = v.surrogates
  74. emojiMap[names] = emoji
  75. })
  76. }
  77. return emojiMap
  78. },
  79. linkToCreator () {
  80. let { creator, userId } = this.group
  81. let sessionId = creator > userId ? `${userId}-${creator}` : `${creator}-${userId}`
  82. return `${location.origin}/#/pm/${sessionId}`
  83. },
  84. isAdmin () {
  85. return (this.group.adminList && this.group.adminList.some(id => id == this.userId)) || this.group.creator == this.userId
  86. }
  87. },
  88. data () {
  89. return {
  90. loginBoxVisible: false,
  91. isLoadingRoom: true,
  92. isMobile: isMobile(),
  93. showChat: !!this.show, // 显示聊天窗
  94. showEmoji: false, // 显示表情选择框
  95. showMenuExtra: false, // 显示左上角菜单
  96. showLoginBtn: true, // 显示登录按钮
  97. lockMore: false,
  98. isScrollToView: false,
  99. lockEnd: false,
  100. loading: false,
  101. unreadCounts: 0, // 未读消息数
  102. inputMsg: '', // 用户输入的内容
  103. atInd: 0, // @人索引
  104. inputHeight: 18,
  105. enableScroll: false, // 记录滚动条是否激活的状态
  106. isBottom: true,
  107. toolShow: false,
  108. chatImageArrSel: null,
  109. serverUnRead: 0, // 客服未读
  110. personUnRead: 0 // 私聊未读
  111. }
  112. },
  113. watch: {
  114. inputMsg (val, newval) {
  115. let ele = this.$refs.chatInput
  116. this.inputHeight = 'auto'
  117. this.$nextTick(() => {
  118. this.inputHeight = Math.max(18, Math.min(ele.scrollHeight, 75)) + 'px'
  119. })
  120. },
  121. chatList (val) {
  122. let lastVal = val[val.length - 1]
  123. if ((lastVal && lastVal.msg_type == 4) || this.isBottom) {
  124. // 自己发的红包自动滚动到底部
  125. this.$nextTick(this.resizeToBottom)
  126. }
  127. },
  128. chatInputFocus (val, newval) {
  129. if (this.showLoginBtn) return
  130. let ele = this.$refs.chatInput
  131. if (val) {
  132. if (document.activeElement !== ele) {
  133. this.placeEnd(ele)
  134. ele.focus()
  135. }
  136. } else {
  137. if (document.activeElement === ele) {
  138. ele.blur()
  139. }
  140. }
  141. }
  142. },
  143. async mounted () {
  144. // 设置groupId
  145. this.initGroup({
  146. userId: this.userId,
  147. groupId: this.groupId,
  148. useCache: false
  149. })
  150. // 检查登录态
  151. let isLogin = await this.checkLocalLogin()
  152. if (isLogin) {
  153. this.showLoginBtn = false
  154. await this.getUserInfo()
  155. }
  156. this.$nextTick(this.initChat)
  157. this.$nextTick(this.initMiniSocket)
  158. document.getElementById('app').addEventListener('contextmenu', e => e.preventDefault())
  159. document.addEventListener('paste', this.initPaste)
  160. document.addEventListener('drop', this.initDrop)
  161. document.addEventListener('dragover', this.initDragOver)
  162. document.body.addEventListener('click', () => {
  163. this.showEmoji = false
  164. this.showMenuExtra = false
  165. this.toolShow = false
  166. })
  167. },
  168. beforeDestroy () {
  169. document.removeEventListener('paste', this.initPaste)
  170. document.removeEventListener('drop', this.initDrop)
  171. document.removeEventListener('dragover', this.initDragOver)
  172. },
  173. methods: {
  174. ...mapMutations([
  175. 'initGroup',
  176. 'setUserId',
  177. 'setToken',
  178. 'addChatItem',
  179. 'updateChatInputFocus',
  180. 'updateGroupBlockList',
  181. 'updateMembers',
  182. 'updateGroupPinMsg',
  183. 'repealChatItem',
  184. 'clearAtList',
  185. 'initState',
  186. 'addPacketItem',
  187. 'addPacketTip',
  188. 'addUnreadNums',
  189. 'resetUnreadNums',
  190. 'addPinChatItem',
  191. 'setLogining'
  192. ]),
  193. ...mapActions([
  194. 'getUserInfo',
  195. 'setAccount',
  196. 'doGameLogin',
  197. 'doScatterLogout',
  198. 'getGroupInfo',
  199. 'getNewMsgFromDb',
  200. 'getNewMsg',
  201. 'getHistoryMsg',
  202. 'doSendMsg',
  203. 'doSendFile',
  204. 'updateSessionLastmsg'
  205. ]),
  206. handleMoreClick () {
  207. this.showEmoji = false
  208. this.toolShow = !this.toolShow
  209. this.checkNeedToBottom()
  210. },
  211. handleEmojiClick () {
  212. this.toolShow = false
  213. this.showEmoji = !this.showEmoji
  214. this.checkNeedToBottom()
  215. },
  216. checkNeedToBottom () {
  217. if (!this.isBottom) return
  218. this.$nextTick(() => {
  219. this.resizeToBottom()
  220. })
  221. },
  222. async initMiniLoginCallback () {
  223. // await this.initMiniSocket()
  224. // this.showLoginBtn = false
  225. // this.initGroup({
  226. // userId: this.userId,
  227. // groupId: this.groupId,
  228. // useCache: false
  229. // })
  230. // await this.getUserInfo()
  231. // this.loginBoxVisible = false
  232. location.reload()
  233. },
  234. // 连接socket
  235. initMiniSocket () {
  236. if (!window.WebSocket) {
  237. console.log('Error: WebSocket is not supported .')
  238. return
  239. }
  240. let host = getMiniWsUrl() + `?group_id=${this.groupId}`
  241. if (this.socket) {
  242. this.socket.destroy()
  243. this.socket = null
  244. }
  245. this.socket = new WsManager(host, {
  246. autoConnect: true, // 自动连接
  247. reconnection: true, // 断开自动重连
  248. reconnectionDelay: 2000 // 重连间隔时间,单位秒
  249. })
  250. this.socket.on('open', res => {})
  251. this.socket.on('message', (data) => {
  252. data = JSON.parse(data)
  253. if (data.channel.match('chat:group')) {
  254. if (data.data.type === 'msg') {
  255. this.getNewMsg({ newMsg: true })
  256. if (data.data.from != this.userId) {
  257. // 未读消息数+1
  258. if (!this.showChat) {
  259. if (this.unreadCounts === 0) {
  260. this.postResize(130, 50)
  261. }
  262. this.unreadCounts++
  263. } else {
  264. this.addUnreadNums()
  265. }
  266. }
  267. }
  268. if (data.data.type === 'repeal') {
  269. this.repealChatItem(data.data)
  270. }
  271. if (data.data.type === 'block') {
  272. this.updateGroupBlockList({
  273. type: 'add',
  274. id: data.data.to
  275. })
  276. }
  277. if (data.data.type === 'unblock') {
  278. this.updateGroupBlockList({
  279. type: 'delete',
  280. id: data.data.to
  281. })
  282. }
  283. if (data.data.type === 'join') {
  284. this.updateMembers(data.data.user_info)
  285. }
  286. if (data.data.type === 'pin_msg') {
  287. this.updateGroupPinMsg(data.data.pinMsg)
  288. }
  289. if (data.data.type === 'unpin_msg') {
  290. this.updateGroupPinMsg(null)
  291. }
  292. if (data.data.type === 'new_redpack') {
  293. this.addPacketItem(data.data)
  294. if (data.data.from == this.userId) {
  295. this.$nextTick(this.resizeToBottom)
  296. }
  297. }
  298. if (data.data.type === 'grab_redpack') {
  299. if (data.data.from == this.userId || data.data.to == this.userId) {
  300. this.addPacketTip(data.data)
  301. }
  302. }
  303. }
  304. })
  305. },
  306. /**
  307. * 聊天群初始化处理
  308. * 先后调用 group/info, group/msg
  309. */
  310. async initChat () {
  311. this.handleToggleChat(this.show)
  312. this.isLoadingRoom = true
  313. await this.getGroupInfo()
  314. await this.getNewMsgFromDb()
  315. await this.getNewMsg()
  316. this.isLoadingRoom = false
  317. // 有登录态才要请求
  318. if (!this.showLoginBtn) {
  319. await this.getPmUnRead()
  320. }
  321. if (this.show) {
  322. this.$nextTick(this.resizeToBottom)
  323. }
  324. this.chatImageArrSel = this.$refs.scrollWrap.getElementsByTagName('img')
  325. },
  326. getPmUnRead () {
  327. api.session.getMiniUnRead().then(({ data }) => {
  328. this.personUnRead = data.data['0']
  329. this.serverUnRead = data.data[this.groupId] || 0
  330. })
  331. },
  332. /**
  333. * 清空私聊未读
  334. * @param {type} 1.客服2.私聊
  335. **/
  336. clearPmUnread (type) {
  337. if (type == 1) this.serverUnRead = 0
  338. else this.personUnRead = 0
  339. },
  340. async handleLogout () {
  341. this.doScatterLogout()
  342. this.showLoginBtn = true
  343. if (self !== top) {
  344. localStorage.removeItem('account')
  345. this.postMessager.send({
  346. action: 'meechat:logout'
  347. })
  348. }
  349. // 初始vuex数据
  350. this.$store.commit('setUserInfo', null)
  351. // this.$store.commit('initChatData')
  352. // this.$store.commit('initGroupData')
  353. // 注销后,刷新页面
  354. location.replace(location.href.replace('show=false', 'show=true'))
  355. },
  356. async handleLogout2 () {
  357. this.doScatterLogout()
  358. this.showLoginBtn = true
  359. },
  360. /**
  361. * 登录处理
  362. */
  363. async handleLogin () {
  364. // 设置登录按钮状态
  365. this.loginBoxVisible = true
  366. },
  367. /**
  368. * 聊天窗体滚动到底部
  369. */
  370. resizeToBottom () {
  371. this.$refs.scrollWrap.scrollTop = this.$refs.msgWrap.offsetHeight
  372. this.resetUnreadNums()
  373. this.isBottom = true
  374. lazyloadImage({
  375. wrap: this.$refs.scrollWrap,
  376. imageArr: this.chatImageArrSel,
  377. derection: 'up'
  378. })
  379. },
  380. /**
  381. * @des 点击,查看未读消息
  382. * 直接滚动到聊天列表底部
  383. */
  384. doSetRead () {
  385. this.resizeToBottom()
  386. },
  387. /**
  388. * 添加表情
  389. */
  390. addEmoji (value) {
  391. this.inputMsg += value
  392. },
  393. /**
  394. * @des 聊天窗体滚动事件处理集
  395. */
  396. handleScroll (e) {
  397. this.enableScroll = true
  398. e.target.focus()
  399. // 防止切换房间时触发滚动处理
  400. if (!this.group.chatList.length) {
  401. return
  402. }
  403. // 防止滚动到置顶消息触发滚动
  404. // if (this.isScrollToView) {
  405. // return
  406. // }
  407. let msgWrap = this.$refs.msgWrap
  408. let totalHeight = msgWrap.offsetHeight
  409. let scrollTop = e.target.scrollTop
  410. if (scrollTop === 0 && !this.lockMore) {
  411. if (this.group.endHash !== null) {
  412. this.lockMore = true
  413. this.getHistoryMsg().then((res) => {
  414. if (res === 'end') {
  415. this.lockEnd = true
  416. } else {
  417. let scrollBottom = totalHeight - scrollTop
  418. this.$nextTick(() => {
  419. e.target.scrollTop = msgWrap.offsetHeight - scrollBottom
  420. this.ps && this.ps.update()
  421. setTimeout(() => {
  422. this.lockMore = false
  423. }, 800)
  424. })
  425. }
  426. })
  427. }
  428. }
  429. // 滚动到底部清空未读消息状态
  430. if (scrollTop + e.target.offsetHeight > totalHeight) {
  431. this.isBottom = true
  432. if (this.unreadNums) {
  433. this.resetUnreadNums()
  434. }
  435. } else {
  436. this.isBottom = false
  437. }
  438. lazyloadImage({
  439. wrap: this.$refs.scrollWrap,
  440. imageArr: this.chatImageArrSel,
  441. derection: 'up'
  442. })
  443. },
  444. /**
  445. * @des 处理消息发送
  446. */
  447. async handleSend (e) {
  448. // 判断是否被禁言
  449. if (this.blockList.some(id => id == this.userId)) {
  450. Message({
  451. message: this.$t('chat.youAreBan'),
  452. type: 'error'
  453. })
  454. return
  455. }
  456. // 替换emoji字符串
  457. let _inputMsg = this.inputMsg
  458. let parts = _inputMsg.match(/\["[a-z0-9A-Z_]+"\]/g)
  459. for (let k in parts) {
  460. let emoji = this.emojiMap[parts[k]]
  461. if (emoji) {
  462. _inputMsg = _inputMsg.replace(parts[k], emoji)
  463. }
  464. }
  465. let text = _inputMsg.trim()
  466. if (text.length === 0) {
  467. Message({
  468. message: this.$t('chat.cannotBeEmpty'),
  469. type: 'warning'
  470. })
  471. return
  472. }
  473. let opt = {
  474. type: 0,
  475. msg: text
  476. }
  477. // 清空输入框
  478. this.inputMsg = ''
  479. // 用户不是第一次发言
  480. if (this.group.members[this.userId]) {
  481. let createTime = Date.now()
  482. this.addChatItem({
  483. from: this.userId,
  484. content: text,
  485. hash: `${createTime}`,
  486. timestamp: createTime,
  487. createTime,
  488. msg_type: '0',
  489. loading: true
  490. })
  491. opt.createTime = createTime
  492. await this.doSendMsg(opt)
  493. } else {
  494. // 发言后,才滚动底部
  495. await this.doSendMsg(opt)
  496. }
  497. // 滚到底部
  498. this.$nextTick(function () {
  499. this.resizeToBottom()
  500. })
  501. e.preventDefault()
  502. return false
  503. },
  504. placeEnd (el) {
  505. var range = document.createRange()
  506. range.selectNodeContents(el)
  507. range.collapse(false)
  508. var sel = window.getSelection()
  509. sel.removeAllRanges()
  510. sel.addRange(range)
  511. },
  512. initDrop (e) {
  513. e.preventDefault()
  514. let files = Array.from(e.dataTransfer.files)
  515. files.forEach(file => this.handleFile(file))
  516. },
  517. initDragOver (e) {
  518. e.preventDefault()
  519. },
  520. initPaste (event) {
  521. var items = (event.clipboardData || window.clipboardData).items
  522. if (items && items.length) {
  523. Array.from(items).forEach(item => {
  524. let file = item.getAsFile()
  525. if (file) {
  526. this.handleFile(file)
  527. }
  528. })
  529. }
  530. },
  531. /**
  532. * 文件预处理
  533. * @return {Object} data 预处理文件信息
  534. * @param {Number} data.type
  535. * @param {File} data.res
  536. */
  537. async preHandleFile (file) {
  538. let type = file.type
  539. let size = file.size
  540. if (type.match('video')) {
  541. return size > 3 * 1024 * 1024 ? Promise.reject(new Error(file)) : Promise.resolve({
  542. type: 2,
  543. res: file
  544. })
  545. } else if (type.match('audio')) {
  546. return size > 2 * 1024 * 1024 ? Promise.reject(new Error(file)) : Promise.resolve({
  547. type: 3,
  548. res: file
  549. })
  550. } else if (type.match('image')) {
  551. let image = await new ImageMin({
  552. file: file,
  553. maxSize: 1024 * 1024
  554. })
  555. return {
  556. type: 1,
  557. preview: image.base64,
  558. res: image.res
  559. }
  560. }
  561. },
  562. /**
  563. * @des 处理文件发送
  564. */
  565. async handleFile (e) {
  566. let inputfile
  567. if (e.constructor === File) {
  568. inputfile = e
  569. } else {
  570. inputfile = e.target.files[0]
  571. }
  572. try {
  573. let fileInfo = await this.preHandleFile(inputfile)
  574. let opt = { res: fileInfo.res }
  575. if (this.group.members[this.userId]) {
  576. let createTime = Date.now()
  577. this.addChatItem({
  578. content: fileInfo.preview || '',
  579. from: this.userId,
  580. hash: `${createTime}`,
  581. msg_type: fileInfo.type,
  582. timestamp: createTime,
  583. res: fileInfo.res,
  584. loading: true,
  585. createTime
  586. })
  587. opt.createTime = createTime
  588. }
  589. this.doSendFile(opt)
  590. this.toolShow = false
  591. setTimeout(() => {
  592. this.$refs.toolbar.resetInput()
  593. this.resizeToBottom()
  594. }, 100)
  595. } catch (error) {
  596. Message({
  597. message: this.$t('chat.maxUploadTips'),
  598. type: 'warning'
  599. })
  600. }
  601. },
  602. /**
  603. * @des 控制聊天窗口开关
  604. */
  605. handleToggleChat (flag) {
  606. if (flag) {
  607. this.showChat = true
  608. this.unreadCounts = 0
  609. this.$nextTick(() => {
  610. this.postResize(this.width, this.height + 16)
  611. this.resizeToBottom()
  612. })
  613. } else {
  614. this.showChat = false
  615. this.$nextTick(() => {
  616. this.postResize(56, 50)
  617. })
  618. }
  619. },
  620. postResize (width, height) {
  621. let request = {
  622. action: 'meechat:resize',
  623. data: {
  624. ch: height,
  625. cw: width
  626. }
  627. }
  628. return window.parent.postMessage(request, '*')
  629. },
  630. /**
  631. * @des 引用某条消息
  632. */
  633. quoteMsg (msg) {
  634. this.inputMsg = msg
  635. },
  636. hideLoginBox () {
  637. this.loginBoxVisible = false
  638. this.setLogining(false)
  639. }
  640. }
  641. }