index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class="chat-page">
  3. <scroll-view scroll-y="true" class="chat-container" :scroll-into-view="scrollIntoView" scroll-with-animation>
  4. <view v-for="(msg, index) in messages" :key="index" :id="'msg-' + index" class="message" :class="msg.role">
  5. <text>{{ msg.content }}</text>
  6. </view>
  7. </scroll-view>
  8. <view class="input-bar">
  9. <input v-model="inputText" :disabled="loading" class="chat-input" placeholder="请输入你的问题..." @confirm="sendMessage" />
  10. <button :disabled="loading || !inputText.trim()" @click="sendMessage" class="send-button">发送</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. chartStream
  17. } from '@/api/services/chart.js';
  18. export default {
  19. data() {
  20. return {
  21. inputText: '',
  22. messages: [],
  23. loading: false,
  24. scrollIntoView: ''
  25. };
  26. },
  27. methods: {
  28. sendMessage() {
  29. const content = this.inputText.trim();
  30. if (!content || this.loading) return;
  31. this.messages.push({ role: 'user', content });
  32. this.inputText = '';
  33. this.scrollToBottom();
  34. const aiMsg = { role: 'ai', content: '' };
  35. this.messages.push(aiMsg);
  36. this.loading = true;
  37. this.scrollToBottom();
  38. const requestBody = {
  39. query: content,
  40. user: 'test_user_123' // 实际项目中应为真实用户ID
  41. };
  42. uni.request({
  43. url: 'http://localhost:9203/dify/chat/stream', // ⚠️ 换成你自己的后端
  44. method: 'POST',
  45. header: {
  46. 'Content-Type': 'application/json'
  47. },
  48. data: requestBody,
  49. success: (res) => {
  50. const reader = res.data.getReader?.();
  51. const decoder = new TextDecoder();
  52. let buffer = '';
  53. const readStream = () => {
  54. reader.read().then(({ done, value }) => {
  55. if (done) {
  56. this.loading = false;
  57. return;
  58. }
  59. buffer += decoder.decode(value, { stream: true });
  60. const events = buffer.split('\n\n');
  61. buffer = events.pop(); // 保留未完成部分
  62. events.forEach(evt => {
  63. if (!evt.trim()) return;
  64. const lines = evt.split('\n');
  65. let eventData = '';
  66. lines.forEach(line => {
  67. if (line.startsWith('data:')) {
  68. eventData = line.replace(/^data:\s*/, '');
  69. }
  70. });
  71. try {
  72. const json = JSON.parse(eventData);
  73. if (json.event === 'message') {
  74. aiMsg.content += json.answer || '';
  75. this.scrollToBottom();
  76. } else if (json.event === 'message_end') {
  77. this.loading = false;
  78. }
  79. } catch (e) {
  80. console.warn('解析流数据失败:', e);
  81. }
  82. });
  83. readStream();
  84. });
  85. };
  86. readStream();
  87. },
  88. fail: (err) => {
  89. this.loading = false;
  90. aiMsg.content = '[请求失败: ' + err.errMsg + ']';
  91. }
  92. });
  93. },
  94. scrollToBottom() {
  95. this.$nextTick(() => {
  96. this.scrollIntoView = 'msg-' + (this.messages.length - 1);
  97. });
  98. }
  99. }
  100. };
  101. </script>
  102. <style scoped>
  103. .chat-page {
  104. display: flex;
  105. flex-direction: column;
  106. height: 100vh;
  107. background-color: #f3f3f3;
  108. }
  109. .chat-container {
  110. flex: 1;
  111. padding: 20rpx;
  112. overflow-y: auto;
  113. background-color: #f3f3f3;
  114. }
  115. .message {
  116. margin-bottom: 20rpx;
  117. padding: 20rpx;
  118. border-radius: 20rpx;
  119. max-width: 80%;
  120. word-break: break-word;
  121. }
  122. .message.user {
  123. align-self: flex-end;
  124. background-color: #dfffd6;
  125. }
  126. .message.ai {
  127. align-self: flex-start;
  128. background-color: #ffffff;
  129. }
  130. .input-bar {
  131. display: flex;
  132. align-items: center;
  133. padding: 10rpx;
  134. border-top: 1px solid #ddd;
  135. background-color: #fff;
  136. }
  137. .chat-input {
  138. flex: 1;
  139. padding: 10rpx;
  140. border: 1px solid #ccc;
  141. border-radius: 10rpx;
  142. }
  143. .send-button {
  144. margin-left: 10rpx;
  145. background-color: #4caf50;
  146. color: white;
  147. border: none;
  148. border-radius: 10rpx;
  149. padding: 10rpx 20rpx;
  150. }
  151. </style>