chat-message-item.vue 9.6 KB

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