index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view class="chat-container">
  3. <scroll-view
  4. class="messages"
  5. scroll-y="true"
  6. :scroll-top="scrollTop"
  7. @scrolltolower="onScrollToLower"
  8. >
  9. <view
  10. v-for="(msg, index) in messages"
  11. :key="index"
  12. :class="['message', msg.role === 'user' ? 'user-message' : 'ai-message']"
  13. >
  14. <text>{{ msg.content }}</text>
  15. </view>
  16. <view v-if="loading" class="message ai-message">
  17. <text class="loading-dots"></text>
  18. </view>
  19. </scroll-view>
  20. <view class="input-area">
  21. <input
  22. v-model="inputMessage"
  23. @confirm="sendMessage"
  24. placeholder="和小智聊天吧~"
  25. :disabled="loading"
  26. placeholder-class="input-placeholder"
  27. />
  28. <button @click="sendMessage" :disabled="!inputMessage || loading">
  29. {{ loading ? '思考中' : '发送' }}
  30. </button>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. data() {
  37. return {
  38. messages: [],
  39. inputMessage: '',
  40. loading: false,
  41. scrollTop: 0
  42. }
  43. },
  44. methods: {
  45. async sendMessage() {
  46. if (!this.inputMessage.trim() || this.loading) return;
  47. const userMessage = this.inputMessage;
  48. this.messages.push({ role: 'user', content: userMessage });
  49. this.inputMessage = '';
  50. this.loading = true;
  51. // 滚动到底部
  52. this.$nextTick(() => {
  53. this.scrollTop = 99999;
  54. });
  55. try {
  56. // 使用 uni-app 的网络请求 API
  57. const response = await uni.request({
  58. url: 'http://localhost:8080/ai/chat',
  59. method: 'GET',
  60. data: {
  61. message: userMessage
  62. }
  63. });
  64. // 处理返回数据
  65. const text = response.data || '抱歉,小智暂时无法回答';
  66. this.messages.push({ role: 'assistant', content: text });
  67. } catch (error) {
  68. console.error('请求出错:', error);
  69. this.messages.push({
  70. role: 'assistant',
  71. content: '哎呀,小智暂时无法连接,请稍后再试~'
  72. });
  73. } finally {
  74. this.loading = false;
  75. // 确保回复后滚动到底部
  76. this.$nextTick(() => {
  77. this.scrollTop = 99999;
  78. });
  79. }
  80. },
  81. onScrollToLower() {
  82. // 处理滚动到底部事件
  83. }
  84. },
  85. onReady() {
  86. // 初始欢迎语
  87. this.messages.push({
  88. role: 'assistant',
  89. content: '你好!我是小智,有什么可以帮您的?'
  90. });
  91. // 滚动到底部
  92. this.$nextTick(() => {
  93. this.scrollTop = 99999;
  94. });
  95. }
  96. }
  97. </script>
  98. <style scoped>
  99. /* 现代聊天界面样式 */
  100. :root {
  101. --primary: #4CAF50;
  102. --bg: #f5f5f5;
  103. --user-bg: #e3f2fd;
  104. --ai-bg: #ffffff;
  105. --tabbar-height: 50px;
  106. }
  107. .chat-container {
  108. max-width: 800px;
  109. margin: 0 auto;
  110. background: white;
  111. height: 100vh;
  112. display: flex;
  113. flex-direction: column;
  114. }
  115. .messages {
  116. flex: 1;
  117. overflow-y: auto;
  118. padding: 20px;
  119. display: flex;
  120. flex-direction: column;
  121. gap: 15px;
  122. }
  123. .message {
  124. max-width: 70%;
  125. padding: 12px 16px;
  126. border-radius: 18px;
  127. animation: fadeIn 0.3s ease;
  128. }
  129. .user-message {
  130. background: var(--user-bg);
  131. align-self: flex-end;
  132. border-bottom-right-radius: 4px;
  133. }
  134. .ai-message {
  135. background: var(--ai-bg);
  136. align-self: flex-start;
  137. border-bottom-left-radius: 4px;
  138. box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  139. }
  140. .loading-dots {
  141. display: inline-block;
  142. font-size: 24px;
  143. }
  144. .loading-dots::after {
  145. content: '...';
  146. animation: dots 1.5s infinite;
  147. }
  148. .input-area {
  149. padding: 20px;
  150. border-top: 1px solid #eee;
  151. display: flex;
  152. gap: 10px;
  153. }
  154. input {
  155. flex: 1;
  156. padding: 12px;
  157. border: 1px solid #ddd;
  158. border-radius: 25px;
  159. font-size: 16px;
  160. outline: none;
  161. transition: 0.3s;
  162. }
  163. input:focus {
  164. border-color: var(--primary);
  165. box-shadow: 0 0 0 3px rgba(76,175,80,0.1);
  166. }
  167. button {
  168. padding: 12px 24px;
  169. background: var(--primary);
  170. border: none;
  171. border-radius: 25px;
  172. color: white;
  173. cursor: pointer;
  174. transition: 0.3s;
  175. }
  176. button:disabled {
  177. opacity: 0.7;
  178. cursor: not-allowed;
  179. }
  180. .input-placeholder {
  181. color: #999;
  182. }
  183. /* 动画 */
  184. @keyframes dots {
  185. 0%, 20% { content: '.'; }
  186. 40% { content: '..'; }
  187. 60%, 100% { content: '...'; }
  188. }
  189. @keyframes fadeIn {
  190. from { opacity: 0; transform: translateY(10px); }
  191. to { opacity: 1; transform: translateY(0); }
  192. }
  193. </style>