chat-message-item.vue 12 KB

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