chat-message-item.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. items.push({
  184. key: 'DELETE',
  185. name: '删除',
  186. icon: 'trash',
  187. color: '#e64e4e'
  188. });
  189. if (this.msgInfo.selfSend && this.msgInfo.id > 0) {
  190. items.push({
  191. key: 'RECALL',
  192. name: '撤回',
  193. icon: 'refreshempty'
  194. });
  195. }
  196. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) {
  197. items.push({
  198. key: 'DOWNLOAD',
  199. name: '下载并打开',
  200. icon: 'download'
  201. });
  202. }
  203. return items;
  204. },
  205. isAction() {
  206. return this.$msgType.isAction(this.msgInfo.type);
  207. },
  208. isNormal() {
  209. const type = this.msgInfo.type;
  210. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  211. }
  212. }
  213. }
  214. </script>
  215. <style scoped lang="scss">
  216. .chat-msg-item {
  217. padding: 2rpx 20rpx;
  218. .chat-msg-tip {
  219. line-height: 60rpx;
  220. text-align: center;
  221. color: #555;
  222. font-size: 24rpx;
  223. padding: 10rpx;
  224. }
  225. .chat-msg-normal {
  226. position: relative;
  227. font-size: 0;
  228. margin-bottom: 15rpx;
  229. padding-left: 110rpx;
  230. min-height: 80rpx;
  231. .avatar {
  232. position: absolute;
  233. top: 0;
  234. left: 0;
  235. }
  236. .chat-msg-content {
  237. text-align: left;
  238. .chat-msg-top {
  239. display: flex;
  240. flex-wrap: nowrap;
  241. color: #333;
  242. font-size: 24rpx;
  243. line-height: 24rpx;
  244. }
  245. .chat-msg-bottom {
  246. display: inline-block;
  247. padding-right: 80rpx;
  248. .chat-msg-text {
  249. position: relative;
  250. line-height: 60rpx;
  251. margin-top: 10rpx;
  252. padding: 8rpx 20rpx;
  253. background-color: #eee;
  254. border-radius: 20rpx;
  255. color: #333;
  256. font-size: 30rpx;
  257. text-align: left;
  258. display: block;
  259. word-break: break-all;
  260. white-space: pre-line;
  261. &:after {
  262. content: "";
  263. position: absolute;
  264. left: -20rpx;
  265. top: 26rpx;
  266. width: 6rpx;
  267. height: 6rpx;
  268. border-style: solid dashed dashed;
  269. border-color: #eee transparent transparent;
  270. overflow: hidden;
  271. border-width: 18rpx;
  272. }
  273. }
  274. .chat-msg-image {
  275. display: flex;
  276. flex-wrap: nowrap;
  277. flex-direction: row;
  278. align-items: center;
  279. .img-load-box {
  280. position: relative;
  281. .send-image {
  282. min-width: 200rpx;
  283. min-height: 200rpx;
  284. max-width: 400rpx;
  285. max-height: 400rpx;
  286. border: 8rpx solid #ebebf5;
  287. cursor: pointer;
  288. }
  289. }
  290. .send-fail {
  291. color: #e60c0c;
  292. font-size: 30px;
  293. cursor: pointer;
  294. margin: 0 20px;
  295. }
  296. }
  297. .chat-msg-file {
  298. display: flex;
  299. flex-wrap: nowrap;
  300. flex-direction: row;
  301. align-items: center;
  302. cursor: pointer;
  303. .chat-file-box {
  304. position: relative;
  305. display: flex;
  306. flex-wrap: nowrap;
  307. align-items: center;
  308. min-height: 80px;
  309. border: #dddddd solid 1px;
  310. border-radius: 10rpx;
  311. background-color: #eeeeee;
  312. padding: 10px 15px;
  313. box-shadow: 2px 2px 2px #c0c0c0;
  314. .chat-file-info {
  315. flex: 1;
  316. height: 100%;
  317. text-align: left;
  318. font-size: 14px;
  319. width: 300rpx;
  320. .chat-file-name {
  321. font-size: 16px;
  322. font-weight: 600;
  323. margin-bottom: 15px;
  324. word-break: break-all;
  325. }
  326. }
  327. .chat-file-icon {
  328. font-size: 80rpx;
  329. color: #d42e07;
  330. }
  331. }
  332. .send-fail {
  333. color: #e60c0c;
  334. font-size: 50rpx;
  335. cursor: pointer;
  336. margin: 0 20rpx;
  337. }
  338. }
  339. .chat-msg-audio {
  340. display: flex;
  341. align-items: center;
  342. .chat-audio-text {
  343. padding-right: 8px;
  344. }
  345. .icon-voice-play {
  346. font-size: 20px;
  347. padding-right: 8px;
  348. }
  349. }
  350. .chat-realtime {
  351. display: flex;
  352. align-items: center;
  353. .iconfont {
  354. font-size: 20px;
  355. padding-right: 8px;
  356. }
  357. }
  358. .chat-msg-status {
  359. display: block;
  360. .chat-readed {
  361. font-size: 12px;
  362. color: #888;
  363. font-weight: 600;
  364. }
  365. .chat-unread {
  366. font-size: 12px;
  367. color: #f23c0f;
  368. font-weight: 600;
  369. }
  370. }
  371. .chat-receipt {
  372. font-size: 13px;
  373. color: darkblue;
  374. font-weight: 600;
  375. .icon-ok {
  376. font-size: 20px;
  377. color: #329432;
  378. }
  379. }
  380. }
  381. }
  382. &.chat-msg-mine {
  383. text-align: right;
  384. padding-left: 0;
  385. padding-right: 110rpx;
  386. .avatar {
  387. left: auto;
  388. right: 0;
  389. }
  390. .chat-msg-content {
  391. text-align: right;
  392. .chat-msg-bottom {
  393. padding-left: 80rpx;
  394. padding-right: 0;
  395. .chat-msg-text {
  396. margin-left: 10px;
  397. background-color: #587ff0;
  398. color: #fff;
  399. &:after {
  400. left: auto;
  401. right: -10px;
  402. border-top-color: #587ff0;
  403. }
  404. }
  405. .chat-msg-image {
  406. flex-direction: row-reverse;
  407. }
  408. .chat-msg-file {
  409. flex-direction: row-reverse;
  410. }
  411. .chat-msg-audio {
  412. flex-direction: row-reverse;
  413. .chat-audio-text {
  414. padding-right: 0;
  415. padding-left: 8px;
  416. }
  417. .icon-voice-play {
  418. transform: rotateY(180deg);
  419. }
  420. }
  421. .chat-realtime {
  422. display: flex;
  423. flex-direction: row-reverse;
  424. .iconfont {
  425. transform: rotateY(180deg);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }
  432. }
  433. </style>