ChatHistory.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <el-drawer title="聊天历史记录" size="700px" :visible.sync="visible" direction="rtl" :before-close="handleClose">
  3. <div class="chat-history" v-loading="loading"
  4. element-loading-text="拼命加载中">
  5. <el-scrollbar class="chat-history-scrollbar" ref="scrollbar" id="historyScrollbar" >
  6. <ul>
  7. <li v-for="(msgInfo,idx) in messages" :key="idx">
  8. <chat-message-item :mode="2" :mine="msgInfo.sendId == mine.id" :headImage="headImage(msgInfo)" :showName="showName(msgInfo)"
  9. :msgInfo="msgInfo" :menu="false">
  10. </chat-message-item>
  11. </li>
  12. </ul>
  13. </el-scrollbar>
  14. </div>
  15. </el-drawer>
  16. </template>
  17. <script>
  18. import ChatMessageItem from './ChatMessageItem.vue';
  19. export default {
  20. name: 'chatHistory',
  21. components: {
  22. ChatMessageItem
  23. },
  24. props: {
  25. visible: {
  26. type: Boolean
  27. },
  28. chat: {
  29. type: Object
  30. },
  31. friend: {
  32. type: Object
  33. },
  34. group: {
  35. type: Object
  36. },
  37. groupMembers: {
  38. type: Array,
  39. }
  40. },
  41. data() {
  42. return {
  43. page: 1,
  44. size: 10,
  45. messages: [],
  46. loadAll: false,
  47. loading: false,
  48. lastScrollTime: new Date()
  49. }
  50. },
  51. methods: {
  52. handleClose() {
  53. this.page = 1;
  54. this.messages = [];
  55. this.loadAll = false;
  56. this.$emit('close');
  57. },
  58. handleScroll() {
  59. let high = this.$refs.scrollbar.$refs.wrap.scrollTop; //距离顶部的距离
  60. let timeDiff = new Date().getTime() - this.lastScrollTime.getTime();
  61. if ( high < 30 && timeDiff>500) {
  62. this.lastScrollTime = new Date();
  63. this.loadMessages();
  64. }
  65. },
  66. loadMessages() {
  67. if(this.loadAll){
  68. return this.$message.success("已到达顶部");
  69. }
  70. let param = {
  71. page: this.page++,
  72. size: this.size
  73. }
  74. if (this.chat.type == 'GROUP') {
  75. param.groupId = this.group.id;
  76. } else {
  77. param.friendId = this.friend.id;
  78. }
  79. this.loading = true;
  80. this.$http({
  81. url: this.histroyAction,
  82. method: 'get',
  83. params: param
  84. }).then(messages => {
  85. messages.forEach(m => this.messages.unshift(m));
  86. this.loading = false;
  87. if(messages.length <this.size){
  88. this.loadAll = true;
  89. }
  90. this.refreshScrollPos();
  91. }).catch(()=>{
  92. this.loading = false;
  93. })
  94. },
  95. showName(msgInfo) {
  96. if (this.chat.type == 'GROUP') {
  97. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  98. return member ? member.aliasName : "";
  99. } else {
  100. return msgInfo.sendId == this.mine.id ? this.mine.nickName : this.chat.showName
  101. }
  102. },
  103. headImage(msgInfo) {
  104. if (this.chat.type == 'GROUP') {
  105. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  106. return member ? member.headImage : "";
  107. } else {
  108. return msgInfo.sendId == this.mine.id ? this.mine.headImageThumb : this.chat.headImage
  109. }
  110. },
  111. refreshScrollPos(){
  112. let scrollWrap = this.$refs.scrollbar.$refs.wrap;
  113. let scrollHeight = scrollWrap.scrollHeight;
  114. let scrollTop = scrollWrap.scrollTop;
  115. this.$nextTick(() => {
  116. let offsetTop = scrollWrap.scrollHeight - scrollHeight;
  117. scrollWrap.scrollTop = scrollTop + offsetTop;
  118. // 滚动条没出来,继续加载
  119. if(scrollWrap.scrollHeight == scrollHeight){
  120. this.loadMessages();
  121. }
  122. });
  123. }
  124. },
  125. computed: {
  126. mine() {
  127. return this.$store.state.userStore.userInfo;
  128. },
  129. histroyAction() {
  130. return `/message/${this.chat.type.toLowerCase()}/history`;
  131. }
  132. },
  133. watch: {
  134. visible: {
  135. handler(newValue, oldValue) {
  136. if (newValue) {
  137. this.loadMessages();
  138. this.$nextTick(() => {
  139. document.getElementById('historyScrollbar').addEventListener("mousewheel", this.handleScroll,true);
  140. });
  141. }
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss">
  148. .chat-history {
  149. display: flex;
  150. height: 100%;
  151. .chat-history-scrollbar {
  152. flex: 1;
  153. .el-scrollbar__thumb {
  154. background-color: #555555;
  155. }
  156. ul {
  157. padding: 20px;
  158. li {
  159. list-style-type: none;
  160. }
  161. }
  162. }
  163. }
  164. </style>