chat-message-item.vue 13 KB

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