chat-message-item.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <template>
  2. <view class="chat-message-item">
  3. <view class="message-tip" v-if="msgInfo.type == $enums.MESSAGE_TYPE.TIP_TEXT">
  4. {{ msgInfo.content }}
  5. </view>
  6. <view class="message-tip" v-else-if="msgInfo.type == $enums.MESSAGE_TYPE.TIP_TIME">
  7. {{ $date.toTimeText(msgInfo.sendTime) }}
  8. </view>
  9. <view class="message-normal" v-else-if="isNormal" :class="{ 'message-mine': msgInfo.selfSend }">
  10. <head-image class="avatar" @longpress.prevent="$emit('longPressHead')" :id="msgInfo.sendId" :url="headImage"
  11. :name="showName" size="small"></head-image>
  12. <view class="content">
  13. <view v-if="msgInfo.groupId && !msgInfo.selfSend" class="top">
  14. <text class="name">{{ showName }}</text>
  15. </view>
  16. <view class="bottom">
  17. <view class="message-content-wrapper">
  18. <view v-if="msgInfo.type == $enums.MESSAGE_TYPE.TEXT">
  19. <long-press-menu :items="menuItems" @select="onSelectMenu">
  20. <!-- up-parse支持点击a标签,但是不支持显示emo表情,也不支持换行 -->
  21. <up-parse v-if="$url.containUrl(msgInfo.content)&&!$emo.containEmoji(msgInfo.content)"
  22. class="message-text" :showImgMenu="false" :content="nodesText"></up-parse>
  23. <!-- rich-text支持显示emo表情以及消息换行,但是不支持点击a标签 -->
  24. <rich-text v-else class="message-text" :nodes="nodesText"></rich-text>
  25. </long-press-menu>
  26. </view>
  27. <view class="message-image" v-else-if="msgInfo.type == $enums.MESSAGE_TYPE.IMAGE">
  28. <long-press-menu :items="menuItems" @select="onSelectMenu">
  29. <view class="image-box">
  30. <image class="send-image" :style="imageStyle" mode="aspectFill"
  31. :src="contentData.thumbUrl" lazy-load="true" @click.stop="onShowFullImage()">
  32. </image>
  33. <loading v-if="sending"></loading>
  34. </view>
  35. </long-press-menu>
  36. </view>
  37. <view class="message-file" v-else-if="msgInfo.type == $enums.MESSAGE_TYPE.FILE">
  38. <long-press-menu :items="menuItems" @select="onSelectMenu">
  39. <view class="file-box">
  40. <view class="file-info">
  41. <uni-link class="file-name" :text="contentData.name" showUnderLine="true"
  42. color="#007BFF" :href="contentData.url"></uni-link>
  43. <view class="file-size">{{ fileSize }}</view>
  44. </view>
  45. <view class="file-icon iconfont icon-file"></view>
  46. <loading v-if="sending"></loading>
  47. </view>
  48. </long-press-menu>
  49. </view>
  50. <long-press-menu v-else-if="msgInfo.type == $enums.MESSAGE_TYPE.AUDIO" :items="menuItems"
  51. @select="onSelectMenu">
  52. <view class="message-audio message-text" @click="onPlayAudio()">
  53. <text class="iconfont icon-voice-play"></text>
  54. <text class="chat-audio-text">{{ contentData.duration + '"' }}</text>
  55. <text v-if="audioPlayState == 'PAUSE'" class="iconfont icon-play"></text>
  56. <text v-if="audioPlayState == 'PLAYING'" class="iconfont icon-pause"></text>
  57. </view>
  58. </long-press-menu>
  59. <long-press-menu v-if="isAction" :items="menuItems" @select="onSelectMenu">
  60. <view class="chat-realtime message-text" @click="$emit('call')">
  61. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VOICE"
  62. class="iconfont icon-chat-voice"></text>
  63. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VIDEO"
  64. class="iconfont icon-chat-video"></text>
  65. <text>{{ msgInfo.content }}</text>
  66. </view>
  67. </long-press-menu>
  68. <view v-if="sending&&(isTextMessage||isAudioMessage)" class="sending">
  69. <loading :size="40" icon-color="#656adf" :mask="false"></loading>
  70. </view>
  71. <view v-else-if="sendFail" @click="onSendFail"
  72. class="send-fail iconfont icon-warning-circle-fill"></view>
  73. </view>
  74. <view class="message-status" v-if="!isAction && msgInfo.selfSend && !msgInfo.groupId">
  75. <text class="chat-readed" v-if="msgInfo.status == $enums.MESSAGE_STATUS.READED">已读</text>
  76. <text class="chat-unread" v-else>未读</text>
  77. </view>
  78. <view class="chat-receipt" v-if="msgInfo.receipt" @click="onShowReadedBox">
  79. <text v-if="msgInfo.receiptOk" class="tool-icon iconfont icon-ok"></text>
  80. <text v-else>{{ msgInfo.readedCount }}人已读</text>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. <chat-group-readed ref="chatGroupReaded" :groupMembers="groupMembers" :msgInfo="msgInfo"></chat-group-readed>
  86. </view>
  87. </template>
  88. <script>
  89. export default {
  90. name: "chat-message-item",
  91. props: {
  92. headImage: {
  93. type: String,
  94. required: true
  95. },
  96. showName: {
  97. type: String,
  98. required: true
  99. },
  100. msgInfo: {
  101. type: Object,
  102. required: true
  103. },
  104. groupMembers: {
  105. type: Array
  106. }
  107. },
  108. data() {
  109. return {
  110. audioPlayState: 'STOP',
  111. innerAudioContext: null,
  112. menu: {
  113. show: false,
  114. style: ""
  115. }
  116. }
  117. },
  118. methods: {
  119. onSendFail() {
  120. this.$emit("resend", this.msgInfo);
  121. },
  122. onPlayAudio() {
  123. // 初始化音频播放器
  124. if (!this.innerAudioContext) {
  125. this.innerAudioContext = uni.createInnerAudioContext();
  126. let url = this.contentData.url;
  127. this.innerAudioContext.src = url;
  128. this.innerAudioContext.onEnded((e) => {
  129. console.log('停止')
  130. this.audioPlayState = "STOP"
  131. this.emit();
  132. })
  133. this.innerAudioContext.onError((e) => {
  134. this.audioPlayState = "STOP"
  135. console.log("播放音频出错");
  136. console.log(e)
  137. this.emit();
  138. });
  139. }
  140. if (this.audioPlayState == 'STOP') {
  141. this.innerAudioContext.play();
  142. this.audioPlayState = "PLAYING";
  143. } else if (this.audioPlayState == 'PLAYING') {
  144. this.innerAudioContext.pause();
  145. this.audioPlayState = "PAUSE"
  146. } else if (this.audioPlayState == 'PAUSE') {
  147. this.innerAudioContext.play();
  148. this.audioPlayState = "PLAYING"
  149. }
  150. this.emit();
  151. },
  152. onSelectMenu(item) {
  153. this.$emit(item.key.toLowerCase(), this.msgInfo);
  154. this.menu.show = false;
  155. },
  156. onShowFullImage() {
  157. let imageUrl = this.contentData.originUrl;
  158. uni.previewImage({
  159. urls: [imageUrl]
  160. })
  161. },
  162. onShowReadedBox() {
  163. this.$refs.chatGroupReaded.open();
  164. },
  165. emit() {
  166. this.$emit("audioStateChange", this.audioPlayState, this.msgInfo);
  167. },
  168. stopPlayAudio() {
  169. if (this.innerAudioContext) {
  170. this.innerAudioContext.stop();
  171. this.innerAudioContext = null;
  172. this.audioPlayState = "STOP"
  173. }
  174. }
  175. },
  176. computed: {
  177. sending() {
  178. return this.msgInfo.status == this.$enums.MESSAGE_STATUS.SENDING;
  179. },
  180. sendFail() {
  181. return this.msgInfo.status == this.$enums.MESSAGE_STATUS.FAILED;
  182. },
  183. contentData() {
  184. return JSON.parse(this.msgInfo.content)
  185. },
  186. fileSize() {
  187. let size = this.contentData.size;
  188. if (size > 1024 * 1024) {
  189. return Math.round(size / 1024 / 1024) + "M";
  190. }
  191. if (size > 1024) {
  192. return Math.round(size / 1024) + "KB";
  193. }
  194. return size + "B";
  195. },
  196. menuItems() {
  197. let items = [];
  198. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.TEXT) {
  199. items.push({
  200. key: 'COPY',
  201. name: '复制',
  202. icon: 'bars'
  203. });
  204. }
  205. if (this.msgInfo.selfSend && this.msgInfo.id > 0) {
  206. items.push({
  207. key: 'RECALL',
  208. name: '撤回',
  209. icon: 'refreshempty'
  210. });
  211. }
  212. items.push({
  213. key: 'DELETE',
  214. name: '删除',
  215. icon: 'trash',
  216. color: '#e64e4e'
  217. });
  218. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) {
  219. items.push({
  220. key: 'DOWNLOAD',
  221. name: '下载并打开',
  222. icon: 'download'
  223. });
  224. }
  225. return items;
  226. },
  227. isTextMessage() {
  228. return this.msgInfo.type == this.$enums.MESSAGE_TYPE.TEXT
  229. },
  230. isAudioMessage() {
  231. return this.msgInfo.type == this.$enums.MESSAGE_TYPE.AUDIO
  232. },
  233. isAction() {
  234. return this.$msgType.isAction(this.msgInfo.type);
  235. },
  236. isNormal() {
  237. const type = this.msgInfo.type;
  238. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  239. },
  240. nodesText() {
  241. let color = this.msgInfo.selfSend ? 'white' : '';
  242. let text = this.$str.html2Escape(this.msgInfo.content)
  243. text = this.$url.replaceURLWithHTMLLinks(text, color)
  244. return this.$emo.transform(text, 'emoji-normal')
  245. },
  246. imageStyle() {
  247. console.log(uni.getSystemInfo())
  248. let maxSize = uni.getSystemInfoSync().windowWidth * 0.6;
  249. let minSize = uni.getSystemInfoSync().windowWidth * 0.2;
  250. let width = this.contentData.width;
  251. let height = this.contentData.height;
  252. if (width && height) {
  253. let ratio = Math.min(width, height) / Math.max(width, height);
  254. let w = Math.max(width > height ? maxSize : ratio * maxSize, minSize);
  255. let h = Math.max(width > height ? ratio * maxSize : maxSize, minSize);
  256. return `width: ${w}px;height:${h}px;`
  257. } else {
  258. // 兼容历史版本,历史数据没有记录宽高
  259. return `max-width: ${maxSize}px;min-width:100px;max-height: ${maxSize}px;min-height:100px;`
  260. }
  261. }
  262. }
  263. }
  264. </script>
  265. <style scoped lang="scss">
  266. .chat-message-item {
  267. padding: 2rpx 20rpx;
  268. .message-tip {
  269. line-height: 60rpx;
  270. text-align: center;
  271. color: $im-text-color-lighter;
  272. font-size: $im-font-size-smaller-extra;
  273. padding: 10rpx;
  274. }
  275. .message-normal {
  276. position: relative;
  277. margin-bottom: 22rpx;
  278. padding-left: 110rpx;
  279. min-height: 80rpx;
  280. .avatar {
  281. position: absolute;
  282. top: 0;
  283. left: 0;
  284. }
  285. .content {
  286. text-align: left;
  287. .top {
  288. display: flex;
  289. flex-wrap: nowrap;
  290. align-items: center;
  291. .name {
  292. color: $im-text-color-lighter;
  293. font-size: $im-font-size-smaller;
  294. line-height: $im-font-size-smaller;
  295. height: $im-font-size-smaller;
  296. white-space: nowrap;
  297. overflow: hidden;
  298. text-overflow: ellipsis;
  299. }
  300. }
  301. .bottom {
  302. display: inline-block;
  303. padding-right: 80rpx;
  304. margin-top: 5rpx;
  305. .message-content-wrapper {
  306. position: relative;
  307. display: flex;
  308. align-items: center;
  309. .sending {
  310. position: relative;
  311. margin: 0 6rpx;
  312. .icon-loading {
  313. color: $im-color-primary;
  314. }
  315. }
  316. .send-fail {
  317. color: #e60c0c;
  318. font-size: 50rpx;
  319. margin: 0 5rpx;
  320. }
  321. .message-text {
  322. position: relative;
  323. line-height: 1.6;
  324. margin-top: 10rpx;
  325. padding: 16rpx 24rpx;
  326. background-color: $im-bg;
  327. border-radius: 20rpx;
  328. color: $im-text-color;
  329. font-size: $im-font-size;
  330. text-align: left;
  331. display: block;
  332. word-break: break-word;
  333. white-space: pre-line;
  334. &:after {
  335. content: "";
  336. position: absolute;
  337. left: -20rpx;
  338. top: 26rpx;
  339. width: 6rpx;
  340. height: 6rpx;
  341. border-style: solid dashed dashed;
  342. border-color: $im-bg transparent transparent;
  343. overflow: hidden;
  344. border-width: 18rpx;
  345. }
  346. }
  347. .message-image {
  348. display: flex;
  349. flex-wrap: nowrap;
  350. flex-direction: row;
  351. align-items: center;
  352. .image-box {
  353. position: relative;
  354. .send-image {
  355. cursor: pointer;
  356. border-radius: 10rpx;
  357. background: $im-bg;
  358. border: 6rpx solid $im-color-primary-light-5;
  359. }
  360. }
  361. .send-fail {
  362. color: $im-color-danger;
  363. font-size: $im-font-size;
  364. cursor: pointer;
  365. margin: 0 20px;
  366. }
  367. }
  368. .message-file {
  369. display: flex;
  370. flex-wrap: nowrap;
  371. flex-direction: row;
  372. align-items: center;
  373. cursor: pointer;
  374. .file-box {
  375. position: relative;
  376. display: flex;
  377. flex-wrap: nowrap;
  378. align-items: center;
  379. min-height: 60px;
  380. border-radius: 4px;
  381. padding: 10px 15px;
  382. box-shadow: $im-box-shadow-dark;
  383. .file-info {
  384. flex: 1;
  385. height: 100%;
  386. text-align: left;
  387. font-size: 14px;
  388. width: 300rpx;
  389. .file-name {
  390. font-weight: 600;
  391. margin-bottom: 15px;
  392. word-break: break-all;
  393. }
  394. }
  395. .file-icon {
  396. font-size: 80rpx;
  397. color: #d42e07;
  398. }
  399. }
  400. .send-fail {
  401. color: #e60c0c;
  402. font-size: 50rpx;
  403. cursor: pointer;
  404. margin: 0 20rpx;
  405. }
  406. }
  407. .message-audio {
  408. display: flex;
  409. align-items: center;
  410. .chat-audio-text {
  411. padding-right: 8px;
  412. }
  413. .icon-voice-play {
  414. font-size: 18px;
  415. padding-right: 8px;
  416. }
  417. }
  418. }
  419. .chat-realtime {
  420. display: flex;
  421. align-items: center;
  422. .iconfont {
  423. font-size: 20px;
  424. padding-right: 8px;
  425. }
  426. }
  427. .message-status {
  428. line-height: $im-font-size-smaller-extra;
  429. font-size: $im-font-size-smaller-extra;
  430. padding-top: 2rpx;
  431. .chat-readed {
  432. display: block;
  433. padding-top: 2rpx;
  434. color: $im-text-color-lighter;
  435. }
  436. .chat-unread {
  437. color: $im-color-danger;
  438. }
  439. }
  440. .chat-receipt {
  441. font-size: $im-font-size-smaller;
  442. color: $im-text-color-lighter;
  443. font-weight: 600;
  444. .icon-ok {
  445. font-size: 20px;
  446. color: $im-color-success;
  447. }
  448. }
  449. }
  450. }
  451. &.message-mine {
  452. text-align: right;
  453. padding-left: 0;
  454. padding-right: 110rpx;
  455. .avatar {
  456. left: auto;
  457. right: 0;
  458. }
  459. .content {
  460. text-align: right;
  461. .bottom {
  462. padding-left: 80rpx;
  463. padding-right: 0;
  464. .message-content-wrapper {
  465. flex-direction: row-reverse;
  466. }
  467. .message-text {
  468. margin-left: 10px;
  469. background-color: $im-color-primary-light-2;
  470. color: #fff;
  471. &:after {
  472. left: auto;
  473. right: -9px;
  474. border-top-color: $im-color-primary-light-2;
  475. }
  476. }
  477. .message-audio {
  478. flex-direction: row-reverse;
  479. .chat-audio-text {
  480. padding-right: 0;
  481. padding-left: 8px;
  482. }
  483. .icon-voice-play {
  484. transform: rotateY(180deg);
  485. }
  486. }
  487. .chat-realtime {
  488. display: flex;
  489. flex-direction: row-reverse;
  490. .iconfont {
  491. transform: rotateY(180deg);
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. </style>