chat.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="app-layout">
  3. <div class="sidebar">
  4. <div class="logo-section">
  5. <img src="@/assets/images/common/ai/logo.png" alt="新基础 小新" width="160" height="160" />
  6. <span class="logo-text">新基础 小新</span>
  7. </div>
  8. </div>
  9. <div>
  10. <div class="content-box">
  11. <div v-html="streamHtmlData"></div>
  12. <!-- <span>{{streamData}}</span>-->
  13. <span class="loading-dots"></span>
  14. <span class="dot"></span>
  15. <span class="dot"></span>
  16. </div>
  17. <el-button @click="stopMessage">停止回答</el-button>
  18. </div>
  19. </div>
  20. </template>
  21. <script setup>
  22. import {getAiChatRecordList} from '@/api/xjc-integratedmachine/common/aiChat.js'
  23. import { getToken } from '@/utils/auth'
  24. import { marked } from 'marked'
  25. let data = ref()
  26. const streamData = ref(''); // 存储流式数据
  27. let streamHtmlData = ref(''); // 存储流式数据
  28. const isLoading = ref(false); // 加载状态
  29. let reader = null; // 读取器实例
  30. let controller = null; // AbortController用于中止请求
  31. // 查看所有聊天记录
  32. list();
  33. function list() {
  34. getAiChatRecordList().then(resp =>{
  35. console.log(resp)
  36. data.value = resp;
  37. })
  38. }
  39. const sendMessage = async() => {
  40. try{
  41. isLoading.value = true;
  42. streamData.value = ''; // 清空之前的数据
  43. // 创建AbortController以便可以中止请求
  44. controller = new AbortController();
  45. // 请求体
  46. let form = {
  47. "content": "生成一个高中学习计划表格"
  48. }
  49. // 发送fetch请求
  50. const response = await fetch('/dev-api/ai/chat/stream', {
  51. method: 'POST',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. 'Authorization': 'Bearer ' + getToken()
  55. },
  56. body: JSON.stringify(form)
  57. });
  58. // 获取可读流的读取器
  59. reader = response.body.getReader();
  60. const decoder = new TextDecoder('utf-8');
  61. // 循环读取流数据
  62. while (true) {
  63. const { done, value } = await reader.read();
  64. if (done) break; // 如果流读取完成则退出循环
  65. // 解码并追加数据
  66. let chunk = decoder.decode(value, { stream: true });
  67. chunk = chunk.replace(/\n\n/g, ' ').replace(/data:/g, '')
  68. streamData.value += chunk;
  69. streamHtmlData.value = marked(streamData.value)
  70. }
  71. }catch (error) {
  72. // 如果是手动中止,不显示错误
  73. if (error.name !== 'AbortError') {
  74. console.error('流式读取失败:', error);
  75. streamData.value = 'Error: ' + error.message;
  76. }
  77. } finally {
  78. streamHtmlData.value = marked(streamData.value)
  79. isLoading.value = false;
  80. }
  81. }
  82. const stopMessage = () => {
  83. if (reader) {
  84. // 取消读取
  85. reader.cancel().catch(() => {});
  86. reader = null;
  87. }
  88. if (controller) {
  89. // 中止请求
  90. controller.abort();
  91. controller = null;
  92. }
  93. isLoading.value = false;
  94. }
  95. onMounted(()=>{
  96. sendMessage()
  97. })
  98. </script>
  99. <script>
  100. export default {
  101. name: "chat"
  102. }
  103. </script>
  104. <style scoped>
  105. .app-layout {
  106. display: flex;
  107. height: 100vh;
  108. }
  109. .sidebar {
  110. width: 200px;
  111. background-color: #f4f4f9;
  112. padding: 20px;
  113. display: flex;
  114. flex-direction: column;
  115. align-items: center;
  116. }
  117. .logo-section {
  118. display: flex;
  119. flex-direction: column;
  120. align-items: center;
  121. }
  122. .content-box {
  123. /*max-height: 300px;*/
  124. width: 80vw;
  125. overflow-y: auto;
  126. border: 1px solid #eee;
  127. padding: 10px;
  128. margin-top: 10px;
  129. background: #f9f9f9;
  130. }
  131. pre {
  132. white-space: pre-wrap;
  133. word-wrap: break-word;
  134. }
  135. .loading-dots {
  136. padding-left: 5px;
  137. }
  138. .dot {
  139. display: inline-block;
  140. margin-left: 5px;
  141. width: 8px;
  142. height: 8px;
  143. background-color: #000000;
  144. border-radius: 50%;
  145. animation: pulse 1.2s infinite ease-in-out both;
  146. }
  147. .dot:nth-child(2) {
  148. animation-delay: -0.6s;
  149. }
  150. @keyframes pulse {
  151. 0%,
  152. 100% {
  153. transform: scale(0.6);
  154. opacity: 0.4;
  155. }
  156. 50% {
  157. transform: scale(1);
  158. opacity: 1;
  159. }
  160. }
  161. </style>