chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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支持显示表情,但是不支持点击a标签 -->
  21. <rich-text v-if="$emo.containEmoji(msgInfo.content)" class="chat-msg-text"
  22. :nodes="nodesText"></rich-text>
  23. <!-- up-parse支持点击a标签,但安卓打包后表情无法显示,原因未知 -->
  24. <up-parse v-else class="chat-msg-text" :showImgMenu="false" :content="nodesText"></up-parse>
  25. </long-press-menu>
  26. </view>
  27. <view class="chat-msg-image" v-if="msgInfo.type == $enums.MESSAGE_TYPE.IMAGE">
  28. <long-press-menu :items="menuItems" @select="onSelectMenu">
  29. <view class="img-load-box">
  30. <image class="send-image" mode="heightFix" :src="JSON.parse(msgInfo.content).thumbUrl"
  31. lazy-load="true" @click.stop="onShowFullImage()">
  32. </image>
  33. <loading v-if="loading"></loading>
  34. </view>
  35. </long-press-menu>
  36. <text title="发送失败" v-if="loadFail" @click="onSendFail"
  37. class="send-fail iconfont icon-warning-circle-fill"></text>
  38. </view>
  39. <view class="chat-msg-file" v-if="msgInfo.type == $enums.MESSAGE_TYPE.FILE">
  40. <long-press-menu :items="menuItems" @select="onSelectMenu">
  41. <view class="chat-file-box">
  42. <view class="chat-file-info">
  43. <uni-link class="chat-file-name" :text="data.name" showUnderLine="true"
  44. color="#007BFF" :href="data.url"></uni-link>
  45. <view class="chat-file-size">{{ fileSize }}</view>
  46. </view>
  47. <view class="chat-file-icon iconfont icon-file"></view>
  48. <loading v-if="loading"></loading>
  49. </view>
  50. </long-press-menu>
  51. <text title="发送失败" v-if="loadFail" @click="onSendFail"
  52. class="send-fail iconfont icon-warning-circle-fill"></text>
  53. </view>
  54. <long-press-menu v-if="msgInfo.type == $enums.MESSAGE_TYPE.AUDIO" :items="menuItems"
  55. @select="onSelectMenu">
  56. <view class="chat-msg-audio chat-msg-text" @click="onPlayAudio()">
  57. <text class="iconfont icon-voice-play"></text>
  58. <text class="chat-audio-text">{{ JSON.parse(msgInfo.content).duration + '"' }}</text>
  59. <text v-if="audioPlayState == 'PAUSE'" class="iconfont icon-play"></text>
  60. <text v-if="audioPlayState == 'PLAYING'" class="iconfont icon-pause"></text>
  61. </view>
  62. </long-press-menu>
  63. <long-press-menu v-if="isAction" :items="menuItems" @select="onSelectMenu">
  64. <view class="chat-realtime chat-msg-text" @click="$emit('call')">
  65. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VOICE"
  66. class="iconfont icon-chat-voice"></text>
  67. <text v-if="msgInfo.type == $enums.MESSAGE_TYPE.ACT_RT_VIDEO"
  68. class="iconfont icon-chat-video"></text>
  69. <text>{{ msgInfo.content }}</text>
  70. </view>
  71. </long-press-menu>
  72. <view class="chat-msg-status" v-if="!isAction">
  73. <text class="chat-readed" v-if="msgInfo.selfSend && !msgInfo.groupId
  74. && msgInfo.status == $enums.MESSAGE_STATUS.READED">已读</text>
  75. <text class="chat-unread" v-if="msgInfo.selfSend && !msgInfo.groupId
  76. && msgInfo.status != $enums.MESSAGE_STATUS.READED">未读</text>
  77. </view>
  78. <view class="chat-receipt" v-if="msgInfo.receipt" @click="onShowReadedBox">
  79. <text v-if="msgInfo.receiptOk" class="tool-icon iconfont icon-ok"></text>
  80. <text v-else>{{ msgInfo.readedCount }}人已读</text>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. <chat-group-readed ref="chatGroupReaded" :groupMembers="groupMembers" :msgInfo="msgInfo"></chat-group-readed>
  86. </view>
  87. </template>
  88. <script>
  89. export default {
  90. name: "chat-message-item",
  91. props: {
  92. headImage: {
  93. type: String,
  94. required: true
  95. },
  96. showName: {
  97. type: String,
  98. required: true
  99. },
  100. msgInfo: {
  101. type: Object,
  102. required: true
  103. },
  104. groupMembers: {
  105. type: Array
  106. }
  107. },
  108. data() {
  109. return {
  110. audioPlayState: 'STOP',
  111. innerAudioContext: null,
  112. menu: {
  113. show: false,
  114. style: ""
  115. }
  116. }
  117. },
  118. methods: {
  119. onSendFail() {
  120. uni.showToast({
  121. title: "该文件已发送失败,目前不支持自动重新发送,建议手动重新发送",
  122. icon: "none"
  123. })
  124. },
  125. onPlayAudio() {
  126. // 初始化音频播放器
  127. if (!this.innerAudioContext) {
  128. this.innerAudioContext = uni.createInnerAudioContext();
  129. let url = JSON.parse(this.msgInfo.content).url;
  130. this.innerAudioContext.src = url;
  131. this.innerAudioContext.onEnded((e) => {
  132. console.log('停止')
  133. this.audioPlayState = "STOP"
  134. this.emit();
  135. })
  136. this.innerAudioContext.onError((e) => {
  137. this.audioPlayState = "STOP"
  138. console.log("播放音频出错");
  139. console.log(e)
  140. this.emit();
  141. });
  142. }
  143. if (this.audioPlayState == 'STOP') {
  144. this.innerAudioContext.play();
  145. this.audioPlayState = "PLAYING";
  146. } else if (this.audioPlayState == 'PLAYING') {
  147. this.innerAudioContext.pause();
  148. this.audioPlayState = "PAUSE"
  149. } else if (this.audioPlayState == 'PAUSE') {
  150. this.innerAudioContext.play();
  151. this.audioPlayState = "PLAYING"
  152. }
  153. this.emit();
  154. },
  155. onSelectMenu(item) {
  156. this.$emit(item.key.toLowerCase(), this.msgInfo);
  157. this.menu.show = false;
  158. },
  159. onShowFullImage() {
  160. let imageUrl = JSON.parse(this.msgInfo.content).originUrl;
  161. uni.previewImage({
  162. urls: [imageUrl]
  163. })
  164. },
  165. onShowReadedBox() {
  166. this.$refs.chatGroupReaded.open();
  167. },
  168. emit() {
  169. this.$emit("audioStateChange", this.audioPlayState, this.msgInfo);
  170. },
  171. stopPlayAudio() {
  172. if (this.innerAudioContext) {
  173. this.innerAudioContext.stop();
  174. this.innerAudioContext = null;
  175. this.audioPlayState = "STOP"
  176. }
  177. }
  178. },
  179. computed: {
  180. loading() {
  181. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "loading";
  182. },
  183. loadFail() {
  184. return this.msgInfo.loadStatus && this.msgInfo.loadStatus === "fail";
  185. },
  186. data() {
  187. return JSON.parse(this.msgInfo.content)
  188. },
  189. fileSize() {
  190. let size = this.data.size;
  191. if (size > 1024 * 1024) {
  192. return Math.round(size / 1024 / 1024) + "M";
  193. }
  194. if (size > 1024) {
  195. return Math.round(size / 1024) + "KB";
  196. }
  197. return size + "B";
  198. },
  199. menuItems() {
  200. let items = [];
  201. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.TEXT) {
  202. items.push({
  203. key: 'COPY',
  204. name: '复制',
  205. icon: 'bars'
  206. });
  207. }
  208. if (this.msgInfo.selfSend && this.msgInfo.id > 0) {
  209. items.push({
  210. key: 'RECALL',
  211. name: '撤回',
  212. icon: 'refreshempty'
  213. });
  214. }
  215. items.push({
  216. key: 'DELETE',
  217. name: '删除',
  218. icon: 'trash',
  219. color: '#e64e4e'
  220. });
  221. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) {
  222. items.push({
  223. key: 'DOWNLOAD',
  224. name: '下载并打开',
  225. icon: 'download'
  226. });
  227. }
  228. return items;
  229. },
  230. isAction() {
  231. return this.$msgType.isAction(this.msgInfo.type);
  232. },
  233. isNormal() {
  234. const type = this.msgInfo.type;
  235. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  236. },
  237. nodesText() {
  238. let color = this.msgInfo.selfSend ? 'white' : '';
  239. let text = this.$url.replaceURLWithHTMLLinks(this.msgInfo.content, color)
  240. return this.$emo.transform(text, 'emoji-normal')
  241. }
  242. }
  243. }
  244. </script>
  245. <style scoped lang="scss">
  246. .chat-msg-item {
  247. padding: 2rpx 20rpx;
  248. .chat-msg-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. .chat-msg-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. .chat-msg-content {
  266. text-align: left;
  267. .chat-msg-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. .chat-msg-bottom {
  276. display: inline-block;
  277. padding-right: 80rpx;
  278. margin-top: 5rpx;
  279. .chat-msg-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. .chat-msg-image {
  306. display: flex;
  307. flex-wrap: nowrap;
  308. flex-direction: row;
  309. align-items: center;
  310. .img-load-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. .chat-msg-file {
  328. display: flex;
  329. flex-wrap: nowrap;
  330. flex-direction: row;
  331. align-items: center;
  332. cursor: pointer;
  333. .chat-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. .chat-file-info {
  343. flex: 1;
  344. height: 100%;
  345. text-align: left;
  346. font-size: 14px;
  347. width: 300rpx;
  348. .chat-file-name {
  349. font-weight: 600;
  350. margin-bottom: 15px;
  351. word-break: break-all;
  352. }
  353. }
  354. .chat-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. .chat-msg-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. .chat-msg-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. &.chat-msg-mine {
  410. text-align: right;
  411. padding-left: 0;
  412. padding-right: 110rpx;
  413. .avatar {
  414. left: auto;
  415. right: 0;
  416. }
  417. .chat-msg-content {
  418. text-align: right;
  419. .chat-msg-bottom {
  420. padding-left: 80rpx;
  421. padding-right: 0;
  422. .chat-msg-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. .chat-msg-image {
  433. flex-direction: row-reverse;
  434. }
  435. .chat-msg-file {
  436. flex-direction: row-reverse;
  437. }
  438. .chat-msg-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>