chat-message-item.vue 12 KB

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