index15.vue 4.6 KB

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