index15.vue 3.2 KB

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