chat-message-item.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <view class="chat-msg-item">
  3. <view class="chat-msg-tip" v-show="msgInfo.type==$enums.MESSAGE_TYPE.RECALL">{{msgInfo.content}}</view>
  4. <view class="chat-msg-normal" v-show="msgInfo.type!=$enums.MESSAGE_TYPE.RECALL"
  5. :class="{'chat-msg-mine':msgInfo.selfSend}">
  6. <view class="avatar" @click="onShowUserInfo(msgInfo.sendId)">
  7. <image class="head-image" :src="headImage"></image>
  8. </view>
  9. <view class="chat-msg-content" @longpress="onShowMenu($event)">
  10. <view class="chat-msg-top">
  11. <text>{{showName}}</text>
  12. <chat-time :time="msgInfo.sendTime"></chat-time>
  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" :src="JSON.parse(msgInfo.content).thumbUrl" lazy-load="true"
  20. @click.stop="onShowFullImage()">
  21. </image>
  22. <loading v-show="loading"></loading>
  23. </view>
  24. <text title="发送失败" v-show="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-show="loading"></loading>
  36. </view>
  37. <text title="发送失败" v-show="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-show="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. onShowUserInfo(userId){
  128. uni.navigateTo({
  129. url: "/pages/common/user-info?id=" + userId
  130. })
  131. }
  132. },
  133. computed: {
  134. loading() {
  135. return !this.isTimeout && this.msgInfo.loadStatus && this.msgInfo.loadStatus === "loading";
  136. },
  137. loadFail() {
  138. return this.msgInfo.loadStatus && (this.isTimeout || this.msgInfo.loadStatus === "fail");
  139. },
  140. isTimeout() {
  141. return (new Date().getTime() - new Date(this.msgInfo.sendTime).getTime()) > 30 * 1000;
  142. },
  143. data() {
  144. return JSON.parse(this.msgInfo.content)
  145. },
  146. fileSize() {
  147. let size = this.data.size;
  148. if (size > 1024 * 1024) {
  149. return Math.round(size / 1024 / 1024) + "M";
  150. }
  151. if (size > 1024) {
  152. return Math.round(size / 1024) + "KB";
  153. }
  154. return size + "B";
  155. },
  156. menuItems() {
  157. let items = [];
  158. items.push({
  159. key: 'DELETE',
  160. name: '删除',
  161. icon: 'trash'
  162. });
  163. if (this.msgInfo.selfSend && this.msgInfo.id > 0) {
  164. items.push({
  165. key: 'RECALL',
  166. name: '撤回',
  167. icon: 'refreshempty'
  168. });
  169. }
  170. if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) {
  171. items.push({
  172. key: 'DOWNLOAD',
  173. name: '下载并打开',
  174. icon: 'download'
  175. });
  176. }
  177. return items;
  178. }
  179. }
  180. }
  181. </script>
  182. <style scoped lang="scss">
  183. .chat-msg-item {
  184. padding: 20rpx;
  185. .chat-msg-tip {
  186. line-height: 60rpx;
  187. text-align: center;
  188. }
  189. .chat-msg-normal {
  190. position: relative;
  191. font-size: 0;
  192. margin-bottom: 15rpx;
  193. padding-left: 120rpx;
  194. min-height: 120rpx;
  195. .avatar {
  196. position: absolute;
  197. display: flex;
  198. justify-content: center;
  199. align-items: center;
  200. width: 100rpx;
  201. height: 100rpx;
  202. top: 0;
  203. left: 0;
  204. .head-image {
  205. width: 100%;
  206. height: 100%;
  207. border-radius: 5%;
  208. }
  209. }
  210. .chat-msg-content {
  211. display: flex;
  212. flex-direction: column;
  213. .chat-msg-top {
  214. display: flex;
  215. flex-wrap: nowrap;
  216. color: #333;
  217. font-size: 14px;
  218. line-height: 20px;
  219. text {
  220. margin-right: 12px;
  221. }
  222. }
  223. .chat-msg-bottom {
  224. text-align: left;
  225. .chat-msg-text {
  226. position: relative;
  227. line-height: 22px;
  228. margin-top: 10px;
  229. padding: 10px;
  230. background-color: #eeeeee;
  231. border-radius: 3px;
  232. color: #333;
  233. display: inline-block;
  234. font-size: 14px;
  235. word-break: break-all;
  236. white-space: pre-line;
  237. &:after {
  238. content: "";
  239. position: absolute;
  240. left: -10px;
  241. top: 13px;
  242. width: 0;
  243. height: 0;
  244. border-style: solid dashed dashed;
  245. border-color: #eeeeee transparent transparent;
  246. overflow: hidden;
  247. border-width: 10px;
  248. }
  249. }
  250. .chat-msg-image {
  251. display: flex;
  252. flex-wrap: nowrap;
  253. flex-direction: row;
  254. align-items: center;
  255. .img-load-box {
  256. position: relative;
  257. .send-image {
  258. min-width: 200rpx;
  259. min-height: 150rpx;
  260. max-width: 400rpx;
  261. max-height: 300rpx;
  262. border: #dddddd solid 1px;
  263. cursor: pointer;
  264. }
  265. }
  266. .send-fail {
  267. color: #e60c0c;
  268. font-size: 30px;
  269. cursor: pointer;
  270. margin: 0 20px;
  271. }
  272. }
  273. .chat-msg-file {
  274. display: flex;
  275. flex-wrap: nowrap;
  276. flex-direction: row;
  277. align-items: center;
  278. cursor: pointer;
  279. .chat-file-box {
  280. position: relative;
  281. display: flex;
  282. flex-wrap: nowrap;
  283. align-items: center;
  284. width: 65%;
  285. min-height: 80px;
  286. border: #dddddd solid 1px;
  287. border-radius: 3px;
  288. background-color: #eeeeee;
  289. padding: 10px 15px;
  290. .chat-file-info {
  291. flex: 1;
  292. height: 100%;
  293. text-align: left;
  294. font-size: 14px;
  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-msg-voice {
  315. font-size: 14px;
  316. cursor: pointer;
  317. audio {
  318. height: 45px;
  319. padding: 5px 0;
  320. }
  321. }
  322. }
  323. }
  324. &.chat-msg-mine {
  325. text-align: right;
  326. padding-left: 0;
  327. padding-right: 120rpx;
  328. .avatar {
  329. left: auto;
  330. right: 0;
  331. }
  332. .chat-msg-content {
  333. .chat-msg-top {
  334. flex-direction: row-reverse;
  335. text {
  336. margin-left: 12px;
  337. margin-right: 0;
  338. }
  339. }
  340. .chat-msg-bottom {
  341. text-align: right;
  342. .chat-msg-text {
  343. margin-left: 10px;
  344. background-color: #5fb878;
  345. color: #fff;
  346. display: inline-block;
  347. vertical-align: top;
  348. font-size: 14px;
  349. &:after {
  350. left: auto;
  351. right: -10px;
  352. border-top-color: #5fb878;
  353. }
  354. }
  355. .chat-msg-image {
  356. flex-direction: row-reverse;
  357. }
  358. .chat-msg-file {
  359. flex-direction: row-reverse;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. </style>