chat-message-item.vue 11 KB

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