index32.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view id='app'>
  3. <view class="placeholder"></view>
  4. <view class="pageView">
  5. <view class="sidebar-left"></view>
  6. <view class="body">
  7. <view class="head">
  8. <text>睿宝AI</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.chatList" :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">
  22. <view class="">
  23. <view class="loadingPoint" v-if='msgList.isloading[index]'>
  24. <loadingPointsVue></loadingPointsVue>
  25. </view>
  26. <view class="content left" v-if='!msgList.isloading[index]'>
  27. {{ msgList.otherChatList[index] }}</view>
  28. </view>
  29. <image class="avtar" src="../../static/Ai-avatar.png" mode="aspectFit"></image>
  30. </view>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </view>
  35. <view class="chat-bottom">
  36. <view class="send-msg">
  37. <view class="uni-textarea">
  38. <textarea id="input-textarea" type="text" v-model="msgList.chatMsg" placeholder="快来聊天吧"
  39. confirm-type="send" @confirm="handleSend" maxlength="300" />
  40. </view>
  41. <view class="uni-button">
  42. <button class="send-btn" @click="handleSend" type="primary">发送</button>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <view class="sidebar-right">
  48. <image src="../../static/logo.png" mode="aspectFit"></image>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script setup>
  54. import {
  55. nextTick,
  56. reactive,
  57. watch
  58. } from 'vue';
  59. import {
  60. getDataList
  61. } from '../../api/ChatApp.js'
  62. import axios from 'axios';
  63. import loadingPointsVue from '../../components/loadingPoints.vue';
  64. const msgList = reactive({
  65. // 输入框
  66. chatMsg: '',
  67. // 聊天数据列表
  68. chatList: [],
  69. // 滚动距离
  70. scrollTop: 0,
  71. //
  72. timer: null,
  73. textCount: 0,
  74. otherContent: '',
  75. otherChatList: [],
  76. len: 0,
  77. isloading: [],
  78. isClick:false
  79. })
  80. // 发送消息
  81. const handleSend = async () => {
  82. msgList.isClick = !msgList.isClick
  83. const data = {
  84. AppId: '0c8ff06b-91d9-43f7-b61e-ca5da1db15d4',
  85. AId: 'TK1234',
  86. content: ''
  87. }
  88. if (msgList.chatMsg != '') {
  89. msgList.isloading[msgList.len] = true
  90. msgList.len++
  91. let obj = {
  92. otherContent: '',
  93. userContent: '',
  94. image: ""
  95. }
  96. data.content = msgList.chatMsg
  97. obj.userContent = msgList.chatMsg
  98. msgList.chatList.push(obj)
  99. obj.otherContent = await GetNewsList(data)
  100. await sleep(16000);
  101. getChatContent(obj.otherContent, msgList.len)
  102. }
  103. msgList.otherContent = ''
  104. }
  105. // 滚动条回到最底部
  106. const scrollToBottom = (() => {
  107. nextTick(() => {
  108. const query = uni.createSelectorQuery();
  109. query.select("#content-box").boundingClientRect()
  110. query.select("#content-overflow").boundingClientRect()
  111. query.exec(res => {
  112. const scrollViewHeight = res[0].height
  113. const scrollContentHeight = res[1].height
  114. if (scrollViewHeight < scrollContentHeight) {
  115. const scrollTop = scrollContentHeight - scrollViewHeight
  116. msgList.scrollTop = scrollTop
  117. }
  118. })
  119. })
  120. })
  121. //滚动至聊天底部
  122. watch([msgList.otherChatList,msgList.chatList], (newVal) => {
  123. scrollToBottom()
  124. })
  125. // 调用数据接口
  126. const GetNewsList = async (data) => {
  127. msgList.chatMsg = ''
  128. var res = await getDataList(data)
  129. console.log(res)
  130. return res
  131. }
  132. // ------- 聊天逐字输出 -----------
  133. // 延时函数
  134. const sleep = ((delaytime = 10000) => {
  135. return new Promise(resolve => setTimeout(resolve, delaytime));
  136. })
  137. // 逐字显示内容
  138. const getChatContent = ((text, index) => {
  139. console.log(index)
  140. msgList.timer = setInterval(() => {
  141. msgList.textCount++;
  142. if (msgList.textCount == text.length + 1) {
  143. msgList.otherChatList[index - 1] = text;
  144. clearInterval(msgList.timer);
  145. } else {
  146. // 取字符串子串
  147. let nowStr = text.substring(0, msgList.textCount);
  148. msgList.otherChatList[index - 1] = nowStr;
  149. }
  150. }, 200);
  151. console.log(msgList.timer)
  152. msgList.textCount = 0
  153. msgList.isloading[index - 1] = false
  154. msgList.isClick = !msgList.isClick
  155. })
  156. </script>
  157. <style>
  158. @import url("../../theme/chat32.css");
  159. </style>