chat-message-item.vue 12 KB

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