chat-message-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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="isNormal"
  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="isAction"
  54. @click="$emit('call')" @longpress="onShowMenu($event)">
  55. <text v-if="msgInfo.type==$enums.MESSAGE_TYPE.ACT_RT_VOICE" class="iconfont icon-chat-voice"></text>
  56. <text v-if="msgInfo.type==$enums.MESSAGE_TYPE.ACT_RT_VIDEO" class="iconfont icon-chat-video"></text>
  57. <text>{{msgInfo.content}}</text>
  58. </view>
  59. <view class="chat-msg-status" v-if="!isAction">
  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. isAction(){
  225. return this.$msgType.isAction(this.msgInfo.type);
  226. },
  227. isNormal() {
  228. const type = this.msgInfo.type;
  229. return this.$msgType.isNormal(type) || this.$msgType.isAction(type)
  230. }
  231. }
  232. }
  233. </script>
  234. <style scoped lang="scss">
  235. .chat-msg-item {
  236. padding: 2rpx 20rpx;
  237. .chat-msg-tip {
  238. line-height: 60rpx;
  239. text-align: center;
  240. color: #555;
  241. font-size: 24rpx;
  242. padding: 10rpx;
  243. }
  244. .chat-msg-normal {
  245. position: relative;
  246. font-size: 0;
  247. margin-bottom: 15rpx;
  248. padding-left: 110rpx;
  249. min-height: 80rpx;
  250. .avatar {
  251. position: absolute;
  252. top: 0;
  253. left: 0;
  254. }
  255. .chat-msg-content {
  256. text-align: left;
  257. .chat-msg-top {
  258. display: flex;
  259. flex-wrap: nowrap;
  260. color: #333;
  261. font-size: 24rpx;
  262. line-height: 24rpx;
  263. }
  264. .chat-msg-bottom {
  265. display: inline-block;
  266. padding-right: 80rpx;
  267. .chat-msg-text {
  268. position: relative;
  269. line-height: 60rpx;
  270. margin-top: 10rpx;
  271. padding: 10rpx 20rpx;
  272. background-color: white;
  273. border-radius: 10rpx;
  274. color: #333;
  275. font-size: 30rpx;
  276. text-align: left;
  277. display: block;
  278. word-break: break-all;
  279. white-space: pre-line;
  280. box-shadow: 1px 1px 1px #c0c0f0;
  281. &:after {
  282. content: "";
  283. position: absolute;
  284. left: -20rpx;
  285. top: 26rpx;
  286. width: 6rpx;
  287. height: 6rpx;
  288. border-style: solid dashed dashed;
  289. border-color: white transparent transparent;
  290. overflow: hidden;
  291. border-width: 18rpx;
  292. }
  293. }
  294. .chat-msg-image {
  295. display: flex;
  296. flex-wrap: nowrap;
  297. flex-direction: row;
  298. align-items: center;
  299. .img-load-box {
  300. position: relative;
  301. .send-image {
  302. min-width: 200rpx;
  303. min-height: 200rpx;
  304. max-width: 400rpx;
  305. max-height: 400rpx;
  306. border: 8rpx solid #ebebf5;
  307. cursor: pointer;
  308. }
  309. }
  310. .send-fail {
  311. color: #e60c0c;
  312. font-size: 30px;
  313. cursor: pointer;
  314. margin: 0 20px;
  315. }
  316. }
  317. .chat-msg-file {
  318. display: flex;
  319. flex-wrap: nowrap;
  320. flex-direction: row;
  321. align-items: center;
  322. cursor: pointer;
  323. .chat-file-box {
  324. position: relative;
  325. display: flex;
  326. flex-wrap: nowrap;
  327. align-items: center;
  328. min-height: 80px;
  329. border: #dddddd solid 1px;
  330. border-radius: 10rpx;
  331. background-color: #eeeeee;
  332. padding: 10px 15px;
  333. box-shadow: 2px 2px 2px #c0c0c0;
  334. .chat-file-info {
  335. flex: 1;
  336. height: 100%;
  337. text-align: left;
  338. font-size: 14px;
  339. width: 300rpx;
  340. .chat-file-name {
  341. font-size: 16px;
  342. font-weight: 600;
  343. margin-bottom: 15px;
  344. word-break: break-all;
  345. }
  346. }
  347. .chat-file-icon {
  348. font-size: 80rpx;
  349. color: #d42e07;
  350. }
  351. }
  352. .send-fail {
  353. color: #e60c0c;
  354. font-size: 50rpx;
  355. cursor: pointer;
  356. margin: 0 20rpx;
  357. }
  358. }
  359. .chat-msg-audio {
  360. display: flex;
  361. align-items: center;
  362. .chat-audio-text {
  363. padding-right: 8px;
  364. }
  365. .icon-voice-play {
  366. font-size: 20px;
  367. padding-right: 8px;
  368. }
  369. }
  370. .chat-realtime {
  371. display: flex;
  372. align-items: center;
  373. .iconfont {
  374. font-size: 20px;
  375. padding-right: 8px;
  376. }
  377. }
  378. .chat-msg-status {
  379. display: block;
  380. .chat-readed {
  381. font-size: 12px;
  382. color: #888;
  383. font-weight: 600;
  384. }
  385. .chat-unread {
  386. font-size: 12px;
  387. color: #f23c0f;
  388. font-weight: 600;
  389. }
  390. }
  391. .chat-receipt {
  392. font-size: 13px;
  393. color: darkblue;
  394. font-weight: 600;
  395. .icon-ok {
  396. font-size: 20px;
  397. color: #329432;
  398. }
  399. }
  400. }
  401. }
  402. &.chat-msg-mine {
  403. text-align: right;
  404. padding-left: 0;
  405. padding-right: 110rpx;
  406. .avatar {
  407. left: auto;
  408. right: 0;
  409. }
  410. .chat-msg-content {
  411. text-align: right;
  412. .chat-msg-bottom {
  413. padding-left: 80rpx;
  414. padding-right: 0;
  415. .chat-msg-text {
  416. margin-left: 10px;
  417. background-color: #587ff0;
  418. color: #fff;
  419. box-shadow: 1px 1px 1px #ccc;
  420. &:after {
  421. left: auto;
  422. right: -10px;
  423. border-top-color: #587ff0;
  424. }
  425. }
  426. .chat-msg-image {
  427. flex-direction: row-reverse;
  428. }
  429. .chat-msg-file {
  430. flex-direction: row-reverse;
  431. }
  432. .chat-msg-audio {
  433. flex-direction: row-reverse;
  434. .chat-audio-text {
  435. padding-right: 0;
  436. padding-left: 8px;
  437. }
  438. .icon-voice-play {
  439. transform: rotateY(180deg);
  440. }
  441. }
  442. .chat-realtime {
  443. display: flex;
  444. flex-direction: row-reverse;
  445. .iconfont {
  446. transform: rotateY(180deg);
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. </style>