chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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="80"></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. <pop-menu :items="menuItems" @select="onSelectMenu">
  20. <rich-text class="chat-msg-text" :nodes="$emo.transform(msgInfo.content)"></rich-text>
  21. </pop-menu>
  22. </view>
  23. <view class="chat-msg-image" v-if="msgInfo.type==$enums.MESSAGE_TYPE.IMAGE">
  24. <pop-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. </pop-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. <pop-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. </pop-menu>
  47. <text title="发送失败" v-if="loadFail" @click="onSendFail"
  48. class="send-fail iconfont icon-warning-circle-fill"></text>
  49. </view>
  50. <pop-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. </pop-menu>
  58. <pop-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. </pop-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: #555;
  229. font-size: 24rpx;
  230. padding: 10rpx;
  231. }
  232. .chat-msg-normal {
  233. position: relative;
  234. font-size: 0;
  235. margin-bottom: 15rpx;
  236. padding-left: 110rpx;
  237. min-height: 80rpx;
  238. .avatar {
  239. position: absolute;
  240. top: 0;
  241. left: 0;
  242. }
  243. .chat-msg-content {
  244. text-align: left;
  245. .chat-msg-top {
  246. display: flex;
  247. flex-wrap: nowrap;
  248. color: #333;
  249. font-size: 24rpx;
  250. line-height: 24rpx;
  251. }
  252. .chat-msg-bottom {
  253. display: inline-block;
  254. padding-right: 80rpx;
  255. .chat-msg-text {
  256. position: relative;
  257. line-height: 60rpx;
  258. margin-top: 10rpx;
  259. padding: 8rpx 20rpx;
  260. background-color: #eee;
  261. border-radius: 20rpx;
  262. color: #333;
  263. font-size: 30rpx;
  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: #eee transparent transparent;
  277. overflow: hidden;
  278. border-width: 18rpx;
  279. }
  280. }
  281. .chat-msg-image {
  282. display: flex;
  283. flex-wrap: nowrap;
  284. flex-direction: row;
  285. align-items: center;
  286. .img-load-box {
  287. position: relative;
  288. .send-image {
  289. min-width: 200rpx;
  290. min-height: 200rpx;
  291. max-width: 400rpx;
  292. max-height: 400rpx;
  293. border: 8rpx solid #ebebf5;
  294. cursor: pointer;
  295. }
  296. }
  297. .send-fail {
  298. color: #e60c0c;
  299. font-size: 30px;
  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: 80px;
  316. border: #dddddd solid 1px;
  317. border-radius: 10rpx;
  318. background-color: #eeeeee;
  319. padding: 10px 15px;
  320. box-shadow: 2px 2px 2px #c0c0c0;
  321. .chat-file-info {
  322. flex: 1;
  323. height: 100%;
  324. text-align: left;
  325. font-size: 14px;
  326. width: 300rpx;
  327. .chat-file-name {
  328. font-size: 16px;
  329. font-weight: 600;
  330. margin-bottom: 15px;
  331. word-break: break-all;
  332. }
  333. }
  334. .chat-file-icon {
  335. font-size: 80rpx;
  336. color: #d42e07;
  337. }
  338. }
  339. .send-fail {
  340. color: #e60c0c;
  341. font-size: 50rpx;
  342. cursor: pointer;
  343. margin: 0 20rpx;
  344. }
  345. }
  346. .chat-msg-audio {
  347. display: flex;
  348. align-items: center;
  349. .chat-audio-text {
  350. padding-right: 8px;
  351. }
  352. .icon-voice-play {
  353. font-size: 20px;
  354. padding-right: 8px;
  355. }
  356. }
  357. .chat-realtime {
  358. display: flex;
  359. align-items: center;
  360. .iconfont {
  361. font-size: 20px;
  362. padding-right: 8px;
  363. }
  364. }
  365. .chat-msg-status {
  366. display: block;
  367. .chat-readed {
  368. font-size: 12px;
  369. color: #888;
  370. font-weight: 600;
  371. }
  372. .chat-unread {
  373. font-size: 12px;
  374. color: #f23c0f;
  375. font-weight: 600;
  376. }
  377. }
  378. .chat-receipt {
  379. font-size: 13px;
  380. color: darkblue;
  381. font-weight: 600;
  382. .icon-ok {
  383. font-size: 20px;
  384. color: #329432;
  385. }
  386. }
  387. }
  388. }
  389. &.chat-msg-mine {
  390. text-align: right;
  391. padding-left: 0;
  392. padding-right: 110rpx;
  393. .avatar {
  394. left: auto;
  395. right: 0;
  396. }
  397. .chat-msg-content {
  398. text-align: right;
  399. .chat-msg-bottom {
  400. padding-left: 80rpx;
  401. padding-right: 0;
  402. .chat-msg-text {
  403. margin-left: 10px;
  404. background-color: #587ff0;
  405. color: #fff;
  406. &:after {
  407. left: auto;
  408. right: -10px;
  409. border-top-color: #587ff0;
  410. }
  411. }
  412. .chat-msg-image {
  413. flex-direction: row-reverse;
  414. }
  415. .chat-msg-file {
  416. flex-direction: row-reverse;
  417. }
  418. .chat-msg-audio {
  419. flex-direction: row-reverse;
  420. .chat-audio-text {
  421. padding-right: 0;
  422. padding-left: 8px;
  423. }
  424. .icon-voice-play {
  425. transform: rotateY(180deg);
  426. }
  427. }
  428. .chat-realtime {
  429. display: flex;
  430. flex-direction: row-reverse;
  431. .iconfont {
  432. transform: rotateY(180deg);
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. </style>