index.vue 3.1 KB

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