chat-message-item.vue 8.6 KB

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