chat-message-item.vue 8.8 KB

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