chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <template>
  2. <view class="chat-msg-item">
  3. <view class="chat-msg-tip"
  4. v-if="msgInfo.type == $enums.MESSAGE_TYPE.RECALL || msgInfo.type == $enums.MESSAGE_TYPE.TIP_TEXT">
  5. {{ msgInfo.content }}
  6. </view>
  7. <view class="chat-msg-tip" v-if="msgInfo.type == $enums.MESSAGE_TYPE.TIP_TIME">
  8. {{ $date.toTimeText(msgInfo.sendTime) }}
  9. </view>
  10. <view class="chat-msg-normal" v-if="isNormal" :class="{ 'chat-msg-mine': msgInfo.selfSend }">
  11. <head-image class="avatar" @longpress.prevent="$emit('longPressHead')" :id="msgInfo.sendId" :url="headImage"
  12. :name="showName" size="small"></head-image>
  13. <view class="chat-msg-content">
  14. <view v-if="msgInfo.groupId && !msgInfo.selfSend" class="chat-msg-top">
  15. <text>{{ showName }}</text>
  16. </view>
  17. <view class="chat-msg-bottom">
  18. <view v-if="msgInfo.type == $enums.MESSAGE_TYPE.TEXT">
  19. <long-press-menu :items="menuItems" @select="onSelectMenu">
  20. <up-parse class="chat-msg-text" :showImgMenu="false" :content="nodesText"></up-parse>
  21. <!-- <rich-text class="chat-msg-text" :nodes="nodesText"></rich-text> -->
  22. </long-press-menu>
  23. </view>
  24. <view class="chat-msg-image" v-if="msgInfo.type == $enums.MESSAGE_TYPE.IMAGE">
  25. <long-press-menu :items="menuItems" @select="onSelectMenu">
  26. <view class="img-load-box">
  27. <image class="send-image" mode="heightFix" :src="JSON.parse(msgInfo.content).thumbUrl"
  28. lazy-load="true" @click.stop="onShowFullImage()">
  29. </image>
  30. <loading v-if="loading"></loading>
  31. </view>
  32. </long-press-menu>
  33. <text title="发送失败" v-if="loadFail" @click="onSendFail"
  34. class="send-fail iconfont icon-warning-circle-fill"></text>
  35. </view>
  36. <view class="chat-msg-file" v-if="msgInfo.type == $enums.MESSAGE_TYPE.FILE">
  37. <long-press-menu :items="menuItems" @select="onSelectMenu">
  38. <view class="chat-file-box">
  39. <view class="chat-file-info">
  40. <uni-link class="chat-file-name" :text="data.name" showUnderLine="true"
  41. color="#007BFF" :href="data.url"></uni-link>
  42. <view class="chat-file-size">{{ fileSize }}</view>
  43. </view>
  44. <view class="chat-file-icon iconfont icon-file"></view>
  45. <loading v-if="loading"></loading>
  46. </view>
  47. </long-press-menu>
  48. <text title="发送失败" v-if="loadFail" @click="onSendFail"
  49. class="send-fail iconfont icon-warning-circle-fill"></text>
  50. </view>
  51. <long-press-menu v-if="msgInfo.type == $enums.MESSAGE_TYPE.AUDIO" :items="menuItems"
  52. @select="onSelectMenu">
  53. <view class="chat-msg-audio chat-msg-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. <long-press-menu v-if="isAction" :items="menuItems" @select="onSelectMenu">
  61. <view class="chat-realtime chat-msg-text" @click="$emit('call')">
  62. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VOICE"
  63. class="iconfont icon-chat-voice"></text>
  64. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VIDEO"
  65. class="iconfont icon-chat-video"></text>
  66. <text>{{ msgInfo.content }}</text>
  67. </view>
  68. </long-press-menu>
  69. <view class="chat-msg-status" v-if="!isAction">
  70. <text class="chat-readed" v-if="msgInfo.selfSend && !msgInfo.groupId
  71. && msgInfo.status == $enums.MESSAGE_STATUS.READED">已读</text>
  72. <text class="chat-unread" v-if="msgInfo.selfSend && !msgInfo.groupId
  73. && msgInfo.status != $enums.MESSAGE_STATUS.READED">未读</text>
  74. </view>
  75. <view class="chat-receipt" v-if="msgInfo.receipt" @click="onShowReadedBox">
  76. <text v-if="msgInfo.receiptOk" class="tool-icon iconfont icon-ok"></text>
  77. <text v-else>{{ msgInfo.readedCount }}人已读</text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. <chat-group-readed ref="chatGroupReaded" :groupMembers="groupMembers" :msgInfo="msgInfo"></chat-group-readed>
  83. </view>
  84. </template>
  85. <script>
  86. export default {
  87. name: "chat-message-item",
  88. props: {
  89. headImage: {
  90. type: String,
  91. required: true
  92. },
  93. showName: {
  94. type: String,
  95. required: true
  96. },
  97. msgInfo: {
  98. type: Object,
  99. required: true
  100. },
  101. groupMembers: {
  102. type: Array
  103. }
  104. },
  105. data() {
  106. return {
  107. audioPlayState: 'STOP',
  108. innerAudioContext: null,
  109. menu: {
  110. show: false,
  111. style: ""
  112. }
  113. }
  114. },
  115. methods: {
  116. onSendFail() {
  117. uni.showToast({
  118. title: "该文件已发送失败,目前不支持自动重新发送,建议手动重新发送",
  119. icon: "none"
  120. })
  121. },
  122. onPlayAudio() {
  123. // 初始化音频播放器
  124. if (!this.innerAudioContext) {
  125. this.innerAudioContext = uni.createInnerAudioContext();
  126. let url = JSON.parse(this.msgInfo.content).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 = JSON.parse(this.msgInfo.content).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. loading() {
  178. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "loading";
  179. },
  180. loadFail() {
  181. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "fail";
  182. },
  183. data() {
  184. return JSON.parse(this.msgInfo.content)
  185. },
  186. fileSize() {
  187. let size = this.data.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. isAction() {
  228. return this.$msgType.isAction(this.msgInfo.type);
  229. },
  230. isNormal() {
  231. const type = this.msgInfo.type;
  232. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  233. },
  234. nodesText() {
  235. let color = this.msgInfo.selfSend ? 'white' : '';
  236. let text = this.$url.replaceURLWithHTMLLinks(this.msgInfo.content, color)
  237. return this.$emo.transform(text, 'emoji-normal')
  238. }
  239. }
  240. }
  241. </script>
  242. <style scoped lang="scss">
  243. .chat-msg-item {
  244. padding: 2rpx 20rpx;
  245. .chat-msg-tip {
  246. line-height: 60rpx;
  247. text-align: center;
  248. color: $im-text-color-lighter;
  249. font-size: $im-font-size-smaller-extra;
  250. padding: 10rpx;
  251. }
  252. .chat-msg-normal {
  253. position: relative;
  254. margin-bottom: 22rpx;
  255. padding-left: 110rpx;
  256. min-height: 80rpx;
  257. .avatar {
  258. position: absolute;
  259. top: 0;
  260. left: 0;
  261. }
  262. .chat-msg-content {
  263. text-align: left;
  264. .chat-msg-top {
  265. display: flex;
  266. flex-wrap: nowrap;
  267. color: $im-text-color-lighter;
  268. font-size: $im-font-size-smaller;
  269. line-height: $im-font-size-smaller;
  270. height: $im-font-size-smaller;
  271. }
  272. .chat-msg-bottom {
  273. display: inline-block;
  274. padding-right: 80rpx;
  275. margin-top: 5rpx;
  276. .chat-msg-text {
  277. position: relative;
  278. line-height: 1.6;
  279. margin-top: 10rpx;
  280. padding: 16rpx 24rpx;
  281. background-color: $im-bg;
  282. border-radius: 20rpx;
  283. color: $im-text-color;
  284. font-size: $im-font-size;
  285. text-align: left;
  286. display: block;
  287. word-break: break-all;
  288. white-space: pre-line;
  289. &:after {
  290. content: "";
  291. position: absolute;
  292. left: -20rpx;
  293. top: 26rpx;
  294. width: 6rpx;
  295. height: 6rpx;
  296. border-style: solid dashed dashed;
  297. border-color: $im-bg transparent transparent;
  298. overflow: hidden;
  299. border-width: 18rpx;
  300. }
  301. }
  302. .chat-msg-image {
  303. display: flex;
  304. flex-wrap: nowrap;
  305. flex-direction: row;
  306. align-items: center;
  307. .img-load-box {
  308. position: relative;
  309. .send-image {
  310. min-width: 200rpx;
  311. max-width: 420rpx;
  312. height: 350rpx;
  313. cursor: pointer;
  314. border-radius: 4px;
  315. }
  316. }
  317. .send-fail {
  318. color: $im-color-danger;
  319. font-size: $im-font-size;
  320. cursor: pointer;
  321. margin: 0 20px;
  322. }
  323. }
  324. .chat-msg-file {
  325. display: flex;
  326. flex-wrap: nowrap;
  327. flex-direction: row;
  328. align-items: center;
  329. cursor: pointer;
  330. .chat-file-box {
  331. position: relative;
  332. display: flex;
  333. flex-wrap: nowrap;
  334. align-items: center;
  335. min-height: 60px;
  336. border-radius: 4px;
  337. padding: 10px 15px;
  338. box-shadow: $im-box-shadow-dark;
  339. .chat-file-info {
  340. flex: 1;
  341. height: 100%;
  342. text-align: left;
  343. font-size: 14px;
  344. width: 300rpx;
  345. .chat-file-name {
  346. font-weight: 600;
  347. margin-bottom: 15px;
  348. word-break: break-all;
  349. }
  350. }
  351. .chat-file-icon {
  352. font-size: 80rpx;
  353. color: #d42e07;
  354. }
  355. }
  356. .send-fail {
  357. color: #e60c0c;
  358. font-size: 50rpx;
  359. cursor: pointer;
  360. margin: 0 20rpx;
  361. }
  362. }
  363. .chat-msg-audio {
  364. display: flex;
  365. align-items: center;
  366. .chat-audio-text {
  367. padding-right: 8px;
  368. }
  369. .icon-voice-play {
  370. font-size: 18px;
  371. padding-right: 8px;
  372. }
  373. }
  374. .chat-realtime {
  375. display: flex;
  376. align-items: center;
  377. .iconfont {
  378. font-size: 20px;
  379. padding-right: 8px;
  380. }
  381. }
  382. .chat-msg-status {
  383. line-height: $im-font-size-smaller-extra;
  384. font-size: $im-font-size-smaller-extra;
  385. padding-top: 2rpx;
  386. .chat-readed {
  387. display: block;
  388. padding-top: 2rpx;
  389. color: $im-text-color-lighter;
  390. }
  391. .chat-unread {
  392. color: $im-color-danger;
  393. }
  394. }
  395. .chat-receipt {
  396. font-size: $im-font-size-smaller;
  397. color: $im-text-color-lighter;
  398. font-weight: 600;
  399. .icon-ok {
  400. font-size: 20px;
  401. color: $im-color-success;
  402. }
  403. }
  404. }
  405. }
  406. &.chat-msg-mine {
  407. text-align: right;
  408. padding-left: 0;
  409. padding-right: 110rpx;
  410. .avatar {
  411. left: auto;
  412. right: 0;
  413. }
  414. .chat-msg-content {
  415. text-align: right;
  416. .chat-msg-bottom {
  417. padding-left: 80rpx;
  418. padding-right: 0;
  419. .chat-msg-text {
  420. margin-left: 10px;
  421. background-color: $im-color-primary-light-2;
  422. color: #fff;
  423. &:after {
  424. left: auto;
  425. right: -9px;
  426. border-top-color: $im-color-primary-light-2;
  427. }
  428. }
  429. .chat-msg-image {
  430. flex-direction: row-reverse;
  431. }
  432. .chat-msg-file {
  433. flex-direction: row-reverse;
  434. }
  435. .chat-msg-audio {
  436. flex-direction: row-reverse;
  437. .chat-audio-text {
  438. padding-right: 0;
  439. padding-left: 8px;
  440. }
  441. .icon-voice-play {
  442. transform: rotateY(180deg);
  443. }
  444. }
  445. .chat-realtime {
  446. display: flex;
  447. flex-direction: row-reverse;
  448. .iconfont {
  449. transform: rotateY(180deg);
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. </style>