chat-message-item.vue 12 KB

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