SsChat.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <scroll-view id="myScrollView" class="ss-chat" :scroll-x="false" :scroll-y="true" :scroll-anchoring="true"
  3. :scroll-into-view="toView" :class="{ spread: globalStore.spread }" :style="chatScrollStyle"
  4. @scroll="handlerScroll">
  5. <view class="ss-chat-container">
  6. <template v-if="globalStore.chatList" v-for="(item, index) in globalStore.chatList" :key="index">
  7. <ss-chat-send-message v-if="item.type == 'send_message'" :message="item.content"
  8. @edit-message="editMessage" />
  9. <ss-chat-reply v-else :reply="item" :last="index + 1 >= globalStore.chatList.length"
  10. @quick-send="quickSend" @change-chat="changeChat" />
  11. </template>
  12. </view>
  13. <view id="scrollBottom" class="scroll-bottom"></view>
  14. </scroll-view>
  15. </template>
  16. <script>
  17. import {
  18. useGlobalStore
  19. } from '@/stores/global';
  20. import SsChatSendMessage from '@/components/ss_chat/child/SsChatSendMessage.vue';
  21. import SsChatReply from '@/components/ss_chat/child/SsChatReply.vue';
  22. export default {
  23. components: {
  24. SsChatSendMessage,
  25. SsChatReply
  26. },
  27. data() {
  28. return {
  29. globalStore: useGlobalStore(),
  30. bottomSafe: 0,
  31. toView: '',
  32. scrollFlag: true,
  33. scrollViewHeight: 0,
  34. handlerScrollFlag: false,
  35. handlerScrollTimeout: false,
  36. innerAudioContext: null,
  37. scrollDirection: 'down',
  38. beforeScrollTop: 0
  39. };
  40. },
  41. computed: {
  42. chatScrollStyle() {
  43. let h = '0';
  44. if (this.globalStore.spread) {
  45. let a = this.globalStore.paddingTop + 66;
  46. a += (this.globalStore.inputLine <= 3 ? this.globalStore.inputLine : 3) * 10;
  47. a += 60;
  48. a += this.bottomSafe;
  49. h = 'height: calc(100vh - ' + a + 'px)';
  50. }
  51. return h;
  52. }
  53. },
  54. mounted() {
  55. this.setBottomSafe();
  56. },
  57. methods: {
  58. editMessage(message) {
  59. this.$emit('editMessage', message);
  60. },
  61. setBottomSafe() {
  62. let safeArea = uni.getSystemInfoSync().safeArea;
  63. this.bottomSafe = uni.getSystemInfoSync().screenHeight - safeArea.bottom;
  64. },
  65. scrollToBottom(force = false) {
  66. this.toView = '';
  67. this.getScrollViewHeight();
  68. this.$nextTick(() => {
  69. if (this.scrollFlag && this.scrollDirection == 'down' || force) {
  70. this.toView = 'scrollBottom';
  71. }
  72. });
  73. },
  74. newMessage() {
  75. this.scrollFlag = true;
  76. this.scrollDirection = 'down';
  77. this.scrollToBottom();
  78. },
  79. getScrollViewHeight() {
  80. const query = uni.createSelectorQuery().in(this);
  81. query.select('#myScrollView').boundingClientRect(res => {
  82. if (res) {
  83. this.scrollViewHeight = res.height;
  84. }
  85. }).exec();
  86. },
  87. handlerScroll(event) {
  88. this.$nextTick(() => {
  89. let scrollTop = this.scrollViewHeight + event.detail.scrollTop ?? 0;
  90. if (scrollTop < 50) {
  91. return ;
  92. }
  93. if (this.scrollFlag && this.beforeScrollTop - 100 > scrollTop) {
  94. this.scrollDirection = 'up'
  95. } else {
  96. this.scrollDirection = 'down'
  97. }
  98. let scrollHeight = event.detail.scrollHeight ?? 0;
  99. // 如果用户手动向上滚动,暂停自动滚动
  100. this.scrollFlag = scrollTop >= scrollHeight - 100;
  101. this.beforeScrollTop = scrollTop;
  102. });
  103. },
  104. quickSend(msg) {
  105. this.$emit('quickSend', msg);
  106. },
  107. changeChat() {
  108. this.$emit('changeChat');
  109. }
  110. }
  111. };
  112. </script>
  113. <style scoped lang="scss">
  114. .ss-chat {
  115. height: 0;
  116. overflow: hidden;
  117. transition: height 0.3s linear;
  118. overflow-anchor: auto;
  119. .ss-chat-container {
  120. margin: 0 auto;
  121. }
  122. .scroll-bottom {
  123. height: 6rpx;
  124. }
  125. &.spread {
  126. margin-bottom: 36rpx;
  127. }
  128. }
  129. </style>