index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <div v-if="repealMsg" class="msg-repeal-item">
  3. {{repealStr}}
  4. </div>
  5. <div v-else-if="joinMsg" class="msg-join-item">
  6. <span>{{joinMsg}}</span>
  7. </div>
  8. <redPack-tip
  9. v-else-if="msgItem && msgItem.redPackTip"
  10. :info="msgItem">
  11. </redPack-tip>
  12. <div class="msg-item clearfix" :class="type" v-else>
  13. <msg-time :timestamp="timestamp" v-if="timeMsg"></msg-time>
  14. <img v-if="avatarUrl" class="user-avatar avatar" src="../../assets/loading.gif" :originurl="avatarUrl" @click="clickInfo" alt>
  15. <div v-else
  16. class="avatar"
  17. :class="'avatar_bg' + userId % 9"
  18. :data-name="name && name.slice(0,2).toUpperCase()"
  19. @click="clickInfo"
  20. ></div>
  21. <div class="content">
  22. <div class="metabar">
  23. <span class="name" @contextmenu.prevent="onToolBtn($event,'username')">{{name}}</span>
  24. <span class="admin" v-if="creator == userId">
  25. <i class="icon-creator" v-if="type === 'me'"></i>
  26. {{$t('public.owner')}}
  27. <i class="icon-creator" v-if="type === 'you'"></i>
  28. </span>
  29. <span class="admin" v-else-if="adminList.includes(Number(userId))">
  30. <i class="el-icon-star-on" v-if="type === 'me'"></i>
  31. {{$t('public.admin')}}
  32. <i class="el-icon-star-on" v-if="type === 'you'"></i>
  33. </span>
  34. <i class="icon-tele" v-if="msgItem.ext_info && msgItem.ext_info.is_tg"></i>
  35. <span class="time">{{timestamp|formatTimestamp}}</span>
  36. </div>
  37. <red-packet
  38. v-if="msg_type == 4 && msgItem"
  39. @click.native="$packetGet(msgItem)"
  40. :info="msgItem">
  41. </red-packet>
  42. <template v-else>
  43. <bubble-wrap
  44. :isMobile="isMobile"
  45. :showToolbar="showToolbar"
  46. @onTouchStartToolBtn="onTouchStartToolBtn"
  47. @onTouchEndToolBtn="onTouchEndToolBtn"
  48. @onToolBtn="onToolBtn"
  49. >
  50. <i class="loading-icon" v-if="loading"></i>
  51. <i class="error-icon" v-if="fail" @click="reSend"></i>
  52. <a :href="content" target="_blank" v-if="msg_type == 1 && meechatType=='mini' && !isMobile">
  53. <img class="img-msg"
  54. :style="{width:width,height:height}"
  55. src="" :originurl="formatUploadImg(content)"
  56. >
  57. <i class="pic-loading"></i>
  58. </a>
  59. <div v-else-if="msg_type == 1">
  60. <img
  61. @click="$showImgPreview(content)"
  62. class="img-msg"
  63. :style="{width:width,height:height}"
  64. src="" :originurl="formatUploadImg(content)"
  65. >
  66. <i class="pic-loading"></i>
  67. </div>
  68. <video
  69. class="video-msg"
  70. :class="{'limit-height': msg_type == 3}"
  71. controls="controls"
  72. preload="meta"
  73. v-else-if="msg_type == 2 || msg_type == 3"
  74. :src="content"
  75. ></video>
  76. <pre v-else class="text" v-html="content"></pre>
  77. <template v-if="toolBtnType=='username'">
  78. <ul @touchstart.stop class="pub-pop-toolbar ext-username" v-show="showToolbar">
  79. <li @click.prevent="handleCopy">{{$t('chat.copy')}}</li>
  80. </ul>
  81. </template>
  82. <template v-else>
  83. <ul @touchstart.stop class="pub-pop-toolbar username" v-show="showToolbar">
  84. <li @click.prevent="handleQuote" v-if="msg_type == 0 || msg_type == 4">{{$t('chat.quote')}}</li>
  85. <li @click.prevent="handleCopy">{{$t('chat.copy')}}</li>
  86. <!-- <li @click.prevent="handleDel">删除</li> -->
  87. <li class="split-line" v-if="(isAdmin && type === 'you') || (isAdmin || revoke)"></li>
  88. <li @click.prevent="handlePingMsg" v-if="isAdmin">{{$t('chat.sticky')}}</li>
  89. <li @click.prevent="handleBlock" v-if="isAdmin && type === 'you'">{{block?$t('chat.liftaBan'):$t('public.ban')}}</li>
  90. <li @click.prevent="handleRevoke" v-if="isAdmin || revoke">{{$t('chat.revoke')}}</li>
  91. </ul>
  92. </template>
  93. </bubble-wrap>
  94. </template>
  95. </div>
  96. </div>
  97. </template>
  98. <script>
  99. import dayjs from 'dayjs'
  100. import msgTime from '@/components/msgItem/time'
  101. import redPacket from '@/components/msgItem/redPacket'
  102. import redPackTip from '@/components/msgItem/redPackTip'
  103. import bubbleWrap from '@/components/msgItem/bubbleWrap'
  104. import { mapMutations, mapActions, mapState } from 'vuex'
  105. import { getMeechatType } from '@/util/util'
  106. export default {
  107. name: 'msgItem',
  108. components: {
  109. msgTime,
  110. redPacket,
  111. redPackTip,
  112. bubbleWrap
  113. },
  114. props: {
  115. msgItem: Object,
  116. isPrivate: Boolean,
  117. repealMsg: Boolean,
  118. joinMsg: String,
  119. from: [String, Number],
  120. timeMsg: Boolean,
  121. avatar: {
  122. type: String
  123. },
  124. name: {
  125. type: String
  126. },
  127. timestamp: [String, Number],
  128. hash: String,
  129. content: {
  130. type: [String, Number, Object]
  131. },
  132. userId: [String, Number],
  133. /**
  134. * 消息来源 {me: 我发的, you: 其他人发的}
  135. */
  136. type: {
  137. type: String
  138. },
  139. /**
  140. * 消息种类 (1 => 图片, 2 => 视频, 3 => 音频, 4 => 链接, 5 => 红包)
  141. */
  142. msg_type: {
  143. type: [Number, String]
  144. },
  145. createTime: [Number],
  146. loading: [Boolean],
  147. fail: [Boolean],
  148. res: [File, Blob],
  149. isMobile: Boolean,
  150. isAdmin: Boolean
  151. },
  152. data () {
  153. return {
  154. showToolbar: false,
  155. revoke: false,
  156. block: false,
  157. revokeTimeAllow: false,
  158. width: 'auto',
  159. height: 'auto',
  160. longTapTimer: null,
  161. meechatType: getMeechatType(), // meechat版本
  162. toolBtnType: ''
  163. }
  164. },
  165. watch: {
  166. content (val, oldVal) {
  167. if (this.msg_type != 1 && val == oldVal) return
  168. this.countPicSize()
  169. }
  170. },
  171. computed: {
  172. ...mapState({
  173. curSession: state => state.curSession,
  174. myId: state => state.userId,
  175. userInfo: state => state.group.userInfo,
  176. blockList: state => state.group.blockList,
  177. adminList: state => state.group.adminList,
  178. members: state => state.group.members,
  179. creator: state => state.group.creator
  180. }),
  181. isLogin () {
  182. return !!this.myId
  183. },
  184. repealStr () {
  185. if (this.repealMsg) {
  186. if (!this.from || this.from == this.userId) {
  187. return `${this.type == 'me' ? this.$t('public.you') : this.name}${this.$t('chat.revokeMsg')}`
  188. } else if (this.from != this.userId) {
  189. let admin = this.members[this.from]
  190. let adminName = admin ? admin.nick_name : this.$t('public.admin')
  191. return `${adminName}${this.$t('chat.revoked')}${this.name}${this.$t('chat.aMsg')}`
  192. } else {
  193. return `${this.name}${this.$t('chat.revokeMsg')}`
  194. }
  195. } else {
  196. return ''
  197. }
  198. },
  199. avatarUrl () {
  200. let membersCover = this.members[this.userId] && this.members[this.userId].cover_photo
  201. return membersCover || this.avatar || ''
  202. }
  203. },
  204. beforeMount () {
  205. if (this.msg_type == 1) {
  206. this.countPicSize()
  207. }
  208. },
  209. created () {
  210. },
  211. methods: {
  212. ...mapMutations(['setCopyText', 'updateChatInputFocus', 'reSendChatItem', 'setSessionRepeal']),
  213. ...mapActions([
  214. 'doRepealPersonMsg',
  215. 'doRepealGroupMsg',
  216. 'doBlockUser',
  217. 'doUnBlockUser',
  218. 'doPinMsg',
  219. 'doSendMsg',
  220. 'doSendFile'
  221. ]),
  222. clickInfo () {
  223. if (!this.isLogin) return
  224. if (this.meechatType == 'h5') {
  225. let infoUrl = this.type === 'me' ? '/me' : `/other/${this.userId}`
  226. this.$router.push(infoUrl)
  227. } else {
  228. this.type === 'me' ? this.$showUserInfo() : this.$showOtherInfo(this.userId)
  229. }
  230. },
  231. // 计算图片尺寸
  232. countPicSize () {
  233. let rect = /_size([0-9]+)x([0-9]+)/.exec(this.content)
  234. if (rect) {
  235. let originalWidth = parseInt(rect[1])
  236. let originalHeight = parseInt(rect[2])
  237. let holderWidth = (document.body.offsetWidth - 35) * 0.84
  238. let scaleX =
  239. originalWidth > holderWidth ? holderWidth / originalWidth : 1
  240. let scaleY = originalHeight > 250 ? 250 / originalHeight : 1
  241. let scale = Math.min(scaleX, scaleY)
  242. this.width = scale * originalWidth + 'px'
  243. this.height = scale * originalHeight + 'px'
  244. }
  245. },
  246. formatUploadImg (val) {
  247. if (/^data:image/.test(val)) return val
  248. else return `${val}?imageview/0/w/400`
  249. },
  250. hideToolbar (event) {
  251. if (this.showToolbar !== false) {
  252. this.showToolbar = false
  253. document.body.removeEventListener('touchstart', this.hideToolbar, false)
  254. document.body.removeEventListener('click', this.hideToolbar, false)
  255. document.body.removeEventListener('contextmenu', this.hideToolbar, false)
  256. }
  257. },
  258. /**
  259. * @des 触发自定义右键菜单
  260. * @param {string} type [{'username':仅复制}]
  261. */
  262. onToolBtn (event, type) {
  263. this.toolBtnType = type
  264. if (this.showToolbar) {
  265. this.hideToolbar(event)
  266. return
  267. }
  268. if (!this.isMobile) {
  269. setTimeout(() => {
  270. document.body.addEventListener('click', this.hideToolbar, false)
  271. document.body.addEventListener('contextmenu', this.hideToolbar, false)
  272. }, 0)
  273. }
  274. this.showToolbar = true
  275. this.block = this.blockList.some(id => id == this.userId)
  276. this.revokeTimeAllow =
  277. Date.now() - parseInt(this.timestamp) < 1e3 * 60 * 3
  278. this.revoke = this.type === 'me' && this.revokeTimeAllow
  279. },
  280. onTouchStartToolBtn (event) {
  281. clearTimeout(this.longTapTimer)
  282. this.longTapTimer = setTimeout(() => {
  283. this.onToolBtn(event)
  284. }, 800)
  285. },
  286. onTouchEndToolBtn (event) {
  287. clearTimeout(this.longTapTimer)
  288. setTimeout(() => {
  289. document.body.addEventListener('touchstart', this.hideToolbar, false)
  290. document.body.addEventListener('click', this.hideToolbar, false)
  291. }, 0)
  292. },
  293. replaceEmoji (content) {
  294. let emojiReg = /<img class="emoji" .+?\/>/gi
  295. return content.replace(emojiReg, function (match) {
  296. let emoji = match.match(/alt=.+?&*"/g)
  297. let emojiCont = emoji && emoji[0].replace(/"|alt=|/g, '')
  298. return emojiCont
  299. })
  300. },
  301. handleQuote () {
  302. let { name, content } = this
  303. let newCont = this.replaceEmoji(content)
  304. let quoteStr = `「${name}:${newCont}」\n- - - - - - - - - - - - - - -\n`
  305. this.$emit('quoteMsg', quoteStr)
  306. this.$nextTick(() => {
  307. this.updateChatInputFocus(true)
  308. })
  309. },
  310. handleCopy () {
  311. let userSelection
  312. let selectedText = ''
  313. if (window.getSelection) { // 现代浏览器
  314. userSelection = window.getSelection()
  315. selectedText = userSelection.toString()
  316. } else if (document.selection) { // IE浏览器 考虑到Opera,应该放在后面
  317. userSelection = document.selection.createRange()
  318. selectedText = userSelection.text
  319. }
  320. let copyTxt = this.replaceEmoji(selectedText || this.content)
  321. this.$copyText(copyTxt).then(
  322. e => {
  323. this.updateChatInputFocus(true)
  324. },
  325. e => {
  326. console.log('Can not copy')
  327. }
  328. )
  329. this.setCopyText(copyTxt)
  330. },
  331. handleShare () {
  332. this.$showInvite(this.content)
  333. },
  334. handleDel () {
  335. this.$emit('deleteMsg', this.hash)
  336. },
  337. handlePingMsg () {
  338. this.doPinMsg({ hash: this.hash })
  339. },
  340. handleRevoke () {
  341. if (this.isPrivate) {
  342. this.doRepealPersonMsg({ hash: this.hash }).then((data) => {
  343. this.$store.commit('setSessionRepeal', {
  344. me: true,
  345. sessionId: this.curSession
  346. })
  347. this.$store.commit('repealChatItem', {
  348. hash: this.hash,
  349. from: this.from
  350. })
  351. })
  352. } else {
  353. this.doRepealGroupMsg({ hash: this.hash })
  354. }
  355. },
  356. handleBlock () {
  357. if (this.block) {
  358. this.doUnBlockUser({ id: this.userId })
  359. } else {
  360. this.doBlockUser({ id: this.userId })
  361. }
  362. },
  363. reSend () {
  364. if (this.msg_type == 0 || this.msg_type == 4) {
  365. let opt = {
  366. type: 0,
  367. msg: this.content,
  368. createTime: this.createTime
  369. }
  370. this.reSendChatItem({ createTime: this.createTime })
  371. if (this.isPrivate) {
  372. this.doSendPrivateMsg(opt)
  373. } else {
  374. this.doSendMsg(opt)
  375. }
  376. } else {
  377. let opt = {
  378. res: this.res,
  379. createTime: this.createTime
  380. }
  381. this.reSendChatItem({ createTime: this.createTime })
  382. this.doSendFile(opt)
  383. }
  384. }
  385. },
  386. filters: {
  387. formatTimestamp (val) {
  388. if (!val) return ''
  389. return dayjs(val * 1).format('HH:mm')
  390. }
  391. }
  392. }
  393. </script>
  394. <style lang="scss">
  395. @import "./style.scss"
  396. </style>