chat-message-item.vue 8.9 KB

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