index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view class="zhanwei">
  3. </view>
  4. <view class="pageView">
  5. <view class="sidebar-left"></view>
  6. <view class="body">
  7. <view class="head">
  8. <text>uniapp聊天框</text>
  9. </view>
  10. <view class="list-wrap">
  11. <scroll-view id="content-box" scroll-y="true" class="scrollview" :scroll-top="msgList.scrollTop">
  12. <!-- 聊天主体 -->
  13. <view id="content-overflow">
  14. <view class="scrollview-item" v-for="(item,index) in msgList.myList" :key='index'>
  15. <!-- user方-聊天内容 -->
  16. <view class="item self" v-if="item.userContent != ''">
  17. <image class="avtar" src="../../static/user-avatar.png" mode="aspectFit"></image>
  18. <view class="content right" disabled>{{ item.userContent }}</view>
  19. </view>
  20. <!-- ai方-聊天内容 -->
  21. <view class="item other" v-if="item.otherContent != ''">
  22. <view class="content left">{{ item.otherContent }}</view>
  23. <image class="avtar" src="../../static/Ai-avatar.png" mode="aspectFit"></image>
  24. </view>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <view class="chat-bottom">
  30. <view class="send-msg">
  31. <view class="uni-textarea">
  32. <textarea id="input-textarea" type="text" v-model="msgList.chatMsg" placeholder="快来聊天吧"
  33. confirm-type="send" @confirm="handleSend" maxlength="300" />
  34. </view>
  35. <view class="uni-button">
  36. <button class="send-btn" @click="handleSend" type="primary">发送</button>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="sidebar-right">
  42. <image src="../../static/logo.png" mode="aspectFit"></image>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup>
  47. import {
  48. nextTick,
  49. reactive,
  50. watch
  51. } from 'vue';
  52. const msgList = reactive({
  53. chatMsg: '',
  54. myList: [{
  55. userContent: '',
  56. otherContent: '',
  57. image: ''
  58. }, ],
  59. // 滚动距离
  60. scrollTop: 0,
  61. boxWord: '选择无处不在,面朝大海春暖花开是海子的选择,人不是生来被打败的是海明威的选择,人固有一死,或重于泰山,或轻于鸿毛,是司马迁的选择,选择是一次又一次自我重塑的过程,让我们不断地成长,不断地完善,如果说人生是一次不断选择的旅程,那么当千帆阅尽最终留下的就是一片属于自己的独一无二的风景 ——董卿《朗读者》'
  62. })
  63. // 发送消息
  64. const handleSend = (() => {
  65. if (msgList.chatMsg != '') {
  66. let obj = {
  67. otherContent: msgList.boxWord,
  68. userContent: msgList.chatMsg,
  69. image: ""
  70. }
  71. msgList.myList.push(obj)
  72. }
  73. msgList.chatMsg = ''
  74. })
  75. // 滚动条回到最底部
  76. const scrollToBottom = (() => {
  77. nextTick(() => {
  78. const query = uni.createSelectorQuery();
  79. query.select("#content-box").boundingClientRect()
  80. query.select("#content-overflow").boundingClientRect()
  81. query.exec(res => {
  82. const scrollViewHeight = res[0].height
  83. const scrollContentHeight = res[1].height
  84. if (scrollViewHeight < scrollContentHeight) {
  85. const scrollTop = scrollContentHeight - scrollViewHeight
  86. msgList.scrollTop = scrollTop
  87. }
  88. })
  89. })
  90. })
  91. //滚动至聊天底部
  92. watch(msgList.myList, (newVal) => {
  93. scrollToBottom()
  94. })
  95. </script>
  96. <style>
  97. @import url("../../theme/chat.css");
  98. </style>