chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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-show="msgInfo.selfSend && !msgInfo.groupId
  69. && msgInfo.status == $enums.MESSAGE_STATUS.READED">已读</text>
  70. <text class="chat-unread" v-show="msgInfo.selfSend && !msgInfo.groupId
  71. && msgInfo.status != $enums.MESSAGE_STATUS.READED">未读</text>
  72. </view>
  73. <view class="chat-receipt" v-show="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. console.log(url);
  127. this.innerAudioContext.onEnded((e) => {
  128. console.log('停止')
  129. this.audioPlayState = "STOP"
  130. })
  131. this.innerAudioContext.onError((e) => {
  132. console.log("播放音频出错");
  133. console.log(e)
  134. });
  135. }
  136. if (this.audioPlayState == 'STOP') {
  137. this.innerAudioContext.play();
  138. this.audioPlayState = "PLAYING";
  139. } else if (this.audioPlayState == 'PLAYING') {
  140. this.innerAudioContext.pause();
  141. this.audioPlayState = "PAUSE"
  142. } else if (this.audioPlayState == 'PAUSE') {
  143. this.innerAudioContext.play();
  144. this.audioPlayState = "PLAYING"
  145. }
  146. },
  147. onSelectMenu(item) {
  148. this.$emit(item.key.toLowerCase(), this.msgInfo);
  149. this.menu.show = false;
  150. },
  151. onShowFullImage() {
  152. let imageUrl = JSON.parse(this.msgInfo.content).originUrl;
  153. uni.previewImage({
  154. urls: [imageUrl]
  155. })
  156. },
  157. onShowReadedBox() {
  158. this.$refs.chatGroupReaded.open();
  159. }
  160. },
  161. computed: {
  162. loading() {
  163. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "loading";
  164. },
  165. loadFail() {
  166. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "fail";
  167. },
  168. data() {
  169. return JSON.parse(this.msgInfo.content)
  170. },
  171. fileSize() {
  172. let size = this.data.size;
  173. if (size > 1024 * 1024) {
  174. return Math.round(size / 1024 / 1024) + "M";
  175. }
  176. if (size > 1024) {
  177. return Math.round(size / 1024) + "KB";
  178. }
  179. return size + "B";
  180. },
  181. menuItems() {
  182. let items = [];
  183. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.TEXT) {
  184. items.push({
  185. key: 'COPY',
  186. name: '复制',
  187. icon: 'bars'
  188. });
  189. }
  190. if (this.msgInfo.selfSend && this.msgInfo.id > 0) {
  191. items.push({
  192. key: 'RECALL',
  193. name: '撤回',
  194. icon: 'refreshempty'
  195. });
  196. }
  197. items.push({
  198. key: 'DELETE',
  199. name: '删除',
  200. icon: 'trash',
  201. color: '#e64e4e'
  202. });
  203. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) {
  204. items.push({
  205. key: 'DOWNLOAD',
  206. name: '下载并打开',
  207. icon: 'download'
  208. });
  209. }
  210. return items;
  211. },
  212. isAction() {
  213. return this.$msgType.isAction(this.msgInfo.type);
  214. },
  215. isNormal() {
  216. const type = this.msgInfo.type;
  217. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  218. }
  219. }
  220. }
  221. </script>
  222. <style scoped lang="scss">
  223. .chat-msg-item {
  224. padding: 2rpx 20rpx;
  225. .chat-msg-tip {
  226. line-height: 60rpx;
  227. text-align: center;
  228. color: $im-text-color-lighter;
  229. font-size: $im-font-size-smaller-extra;
  230. padding: 10rpx;
  231. }
  232. .chat-msg-normal {
  233. position: relative;
  234. margin-bottom: 22rpx;
  235. padding-left: 110rpx;
  236. min-height: 80rpx;
  237. .avatar {
  238. position: absolute;
  239. top: 0;
  240. left: 0;
  241. }
  242. .chat-msg-content {
  243. text-align: left;
  244. .chat-msg-top {
  245. display: flex;
  246. flex-wrap: nowrap;
  247. color: $im-text-color-lighter;
  248. font-size: $im-font-size-smaller;
  249. line-height: $im-font-size-smaller;
  250. height: $im-font-size-smaller;
  251. }
  252. .chat-msg-bottom {
  253. display: inline-block;
  254. padding-right: 80rpx;
  255. .chat-msg-text {
  256. position: relative;
  257. line-height: 1.6;
  258. margin-top: 10rpx;
  259. padding: 16rpx 24rpx;
  260. background-color: $im-bg;
  261. border-radius: 20rpx;
  262. color: $im-text-color;
  263. font-size: $im-font-size;
  264. text-align: left;
  265. display: block;
  266. word-break: break-all;
  267. white-space: pre-line;
  268. &:after {
  269. content: "";
  270. position: absolute;
  271. left: -20rpx;
  272. top: 26rpx;
  273. width: 6rpx;
  274. height: 6rpx;
  275. border-style: solid dashed dashed;
  276. border-color: $im-bg transparent transparent;
  277. overflow: hidden;
  278. border-width: 18rpx;
  279. //box-shadow: $im-box-shadow-dark;
  280. }
  281. }
  282. .chat-msg-image {
  283. display: flex;
  284. flex-wrap: nowrap;
  285. flex-direction: row;
  286. align-items: center;
  287. .img-load-box {
  288. position: relative;
  289. .send-image {
  290. min-width: 200rpx;
  291. max-width: 420rpx;
  292. height: 350rpx;
  293. cursor: pointer;
  294. border-radius: 4px;
  295. }
  296. }
  297. .send-fail {
  298. color: $im-color-danger;
  299. font-size: $im-font-size;
  300. cursor: pointer;
  301. margin: 0 20px;
  302. }
  303. }
  304. .chat-msg-file {
  305. display: flex;
  306. flex-wrap: nowrap;
  307. flex-direction: row;
  308. align-items: center;
  309. cursor: pointer;
  310. .chat-file-box {
  311. position: relative;
  312. display: flex;
  313. flex-wrap: nowrap;
  314. align-items: center;
  315. min-height: 60px;
  316. border-radius: 4px;
  317. padding: 10px 15px;
  318. box-shadow: $im-box-shadow-dark;
  319. .chat-file-info {
  320. flex: 1;
  321. height: 100%;
  322. text-align: left;
  323. font-size: 14px;
  324. width: 300rpx;
  325. .chat-file-name {
  326. font-weight: 600;
  327. margin-bottom: 15px;
  328. word-break: break-all;
  329. }
  330. }
  331. .chat-file-icon {
  332. font-size: 80rpx;
  333. color: #d42e07;
  334. }
  335. }
  336. .send-fail {
  337. color: #e60c0c;
  338. font-size: 50rpx;
  339. cursor: pointer;
  340. margin: 0 20rpx;
  341. }
  342. }
  343. .chat-msg-audio {
  344. display: flex;
  345. align-items: center;
  346. .chat-audio-text {
  347. padding-right: 8px;
  348. }
  349. .icon-voice-play {
  350. font-size: 18px;
  351. padding-right: 8px;
  352. }
  353. }
  354. .chat-realtime {
  355. display: flex;
  356. align-items: center;
  357. .iconfont {
  358. font-size: 20px;
  359. padding-right: 8px;
  360. }
  361. }
  362. .chat-msg-status {
  363. line-height: $im-font-size-smaller-extra;
  364. font-size: $im-font-size-smaller-extra;
  365. padding-top: 2rpx;
  366. .chat-readed {
  367. display: block;
  368. padding-top: 2rpx;
  369. color: $im-text-color-lighter;
  370. }
  371. .chat-unread {
  372. color: $im-color-danger;
  373. }
  374. }
  375. .chat-receipt {
  376. font-size: $im-font-size-smaller;
  377. color: $im-text-color-lighter;
  378. font-weight: 600;
  379. .icon-ok {
  380. font-size: 20px;
  381. color: $im-color-success;
  382. }
  383. }
  384. }
  385. }
  386. &.chat-msg-mine {
  387. text-align: right;
  388. padding-left: 0;
  389. padding-right: 110rpx;
  390. .avatar {
  391. left: auto;
  392. right: 0;
  393. }
  394. .chat-msg-content {
  395. text-align: right;
  396. .chat-msg-bottom {
  397. padding-left: 80rpx;
  398. padding-right: 0;
  399. .chat-msg-text {
  400. margin-left: 10px;
  401. background-color: $im-color-primary-light-2;
  402. color: #fff;
  403. &:after {
  404. left: auto;
  405. right: -9px;
  406. border-top-color: $im-color-primary-light-2;
  407. }
  408. }
  409. .chat-msg-image {
  410. flex-direction: row-reverse;
  411. }
  412. .chat-msg-file {
  413. flex-direction: row-reverse;
  414. }
  415. .chat-msg-audio {
  416. flex-direction: row-reverse;
  417. .chat-audio-text {
  418. padding-right: 0;
  419. padding-left: 8px;
  420. }
  421. .icon-voice-play {
  422. transform: rotateY(180deg);
  423. }
  424. }
  425. .chat-realtime {
  426. display: flex;
  427. flex-direction: row-reverse;
  428. .iconfont {
  429. transform: rotateY(180deg);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. </style>