chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. <!-- rich-text支持显示表情,但是不支持点击a标签 -->
  20. <rich-text v-if="$emo.containEmoji(msgInfo.content)" class="message-text"
  21. :nodes="nodesText"></rich-text>
  22. <!-- up-parse支持点击a标签,但安卓打包后表情无法显示,原因未知 -->
  23. <up-parse v-else class="message-text" :showImgMenu="false" :content="nodesText"></up-parse>
  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="img-load-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"
  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="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.$url.replaceURLWithHTMLLinks(this.msgInfo.content, color)
  239. return this.$emo.transform(text, 'emoji-normal')
  240. }
  241. }
  242. }
  243. </script>
  244. <style scoped lang="scss">
  245. .chat-message-item {
  246. padding: 2rpx 20rpx;
  247. .message-tip {
  248. line-height: 60rpx;
  249. text-align: center;
  250. color: $im-text-color-lighter;
  251. font-size: $im-font-size-smaller-extra;
  252. padding: 10rpx;
  253. }
  254. .message-normal {
  255. position: relative;
  256. margin-bottom: 22rpx;
  257. padding-left: 110rpx;
  258. min-height: 80rpx;
  259. .avatar {
  260. position: absolute;
  261. top: 0;
  262. left: 0;
  263. }
  264. .content {
  265. text-align: left;
  266. .top {
  267. display: flex;
  268. flex-wrap: nowrap;
  269. color: $im-text-color-lighter;
  270. font-size: $im-font-size-smaller;
  271. line-height: $im-font-size-smaller;
  272. height: $im-font-size-smaller;
  273. }
  274. .bottom {
  275. display: inline-block;
  276. padding-right: 80rpx;
  277. margin-top: 5rpx;
  278. .message-text {
  279. position: relative;
  280. line-height: 1.6;
  281. margin-top: 10rpx;
  282. padding: 16rpx 24rpx;
  283. background-color: $im-bg;
  284. border-radius: 20rpx;
  285. color: $im-text-color;
  286. font-size: $im-font-size;
  287. text-align: left;
  288. display: block;
  289. word-break: break-all;
  290. white-space: pre-line;
  291. &:after {
  292. content: "";
  293. position: absolute;
  294. left: -20rpx;
  295. top: 26rpx;
  296. width: 6rpx;
  297. height: 6rpx;
  298. border-style: solid dashed dashed;
  299. border-color: $im-bg transparent transparent;
  300. overflow: hidden;
  301. border-width: 18rpx;
  302. }
  303. }
  304. .message-image {
  305. display: flex;
  306. flex-wrap: nowrap;
  307. flex-direction: row;
  308. align-items: center;
  309. .img-load-box {
  310. position: relative;
  311. .send-image {
  312. min-width: 200rpx;
  313. max-width: 420rpx;
  314. height: 350rpx;
  315. cursor: pointer;
  316. border-radius: 4px;
  317. }
  318. }
  319. .send-fail {
  320. color: $im-color-danger;
  321. font-size: $im-font-size;
  322. cursor: pointer;
  323. margin: 0 20px;
  324. }
  325. }
  326. .message-file {
  327. display: flex;
  328. flex-wrap: nowrap;
  329. flex-direction: row;
  330. align-items: center;
  331. cursor: pointer;
  332. .file-box {
  333. position: relative;
  334. display: flex;
  335. flex-wrap: nowrap;
  336. align-items: center;
  337. min-height: 60px;
  338. border-radius: 4px;
  339. padding: 10px 15px;
  340. box-shadow: $im-box-shadow-dark;
  341. .file-info {
  342. flex: 1;
  343. height: 100%;
  344. text-align: left;
  345. font-size: 14px;
  346. width: 300rpx;
  347. .file-name {
  348. font-weight: 600;
  349. margin-bottom: 15px;
  350. word-break: break-all;
  351. }
  352. }
  353. .file-icon {
  354. font-size: 80rpx;
  355. color: #d42e07;
  356. }
  357. }
  358. .send-fail {
  359. color: #e60c0c;
  360. font-size: 50rpx;
  361. cursor: pointer;
  362. margin: 0 20rpx;
  363. }
  364. }
  365. .message-audio {
  366. display: flex;
  367. align-items: center;
  368. .chat-audio-text {
  369. padding-right: 8px;
  370. }
  371. .icon-voice-play {
  372. font-size: 18px;
  373. padding-right: 8px;
  374. }
  375. }
  376. .chat-realtime {
  377. display: flex;
  378. align-items: center;
  379. .iconfont {
  380. font-size: 20px;
  381. padding-right: 8px;
  382. }
  383. }
  384. .message-status {
  385. line-height: $im-font-size-smaller-extra;
  386. font-size: $im-font-size-smaller-extra;
  387. padding-top: 2rpx;
  388. .chat-readed {
  389. display: block;
  390. padding-top: 2rpx;
  391. color: $im-text-color-lighter;
  392. }
  393. .chat-unread {
  394. color: $im-color-danger;
  395. }
  396. }
  397. .chat-receipt {
  398. font-size: $im-font-size-smaller;
  399. color: $im-text-color-lighter;
  400. font-weight: 600;
  401. .icon-ok {
  402. font-size: 20px;
  403. color: $im-color-success;
  404. }
  405. }
  406. }
  407. }
  408. &.message-mine {
  409. text-align: right;
  410. padding-left: 0;
  411. padding-right: 110rpx;
  412. .avatar {
  413. left: auto;
  414. right: 0;
  415. }
  416. .content {
  417. text-align: right;
  418. .bottom {
  419. padding-left: 80rpx;
  420. padding-right: 0;
  421. .message-text {
  422. margin-left: 10px;
  423. background-color: $im-color-primary-light-2;
  424. color: #fff;
  425. &:after {
  426. left: auto;
  427. right: -9px;
  428. border-top-color: $im-color-primary-light-2;
  429. }
  430. }
  431. .message-image {
  432. flex-direction: row-reverse;
  433. }
  434. .message-file {
  435. flex-direction: row-reverse;
  436. }
  437. .message-audio {
  438. flex-direction: row-reverse;
  439. .chat-audio-text {
  440. padding-right: 0;
  441. padding-left: 8px;
  442. }
  443. .icon-voice-play {
  444. transform: rotateY(180deg);
  445. }
  446. }
  447. .chat-realtime {
  448. display: flex;
  449. flex-direction: row-reverse;
  450. .iconfont {
  451. transform: rotateY(180deg);
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. </style>