chat-message-item.vue 9.0 KB

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