index.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <view class="chat-page">
  3. <scroll-view class="chat-body" scroll-y :scroll-top="scrollTop">
  4. <view v-for="(msg, idx) in messages" :key="idx" class="msg-item" :class="msg.role">
  5. <view class="bubble">{{ msg.content }}</view>
  6. </view>
  7. </scroll-view>
  8. <view class="chat-input">
  9. <input class="input" v-model="inputText" placeholder="输入您的问题..." @confirm="send" confirm-type="send" />
  10. <button class="send-btn" size="mini" @click="send">发送</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { aiCoachSend } from '../../utils/api.js'
  16. export default {
  17. data() {
  18. return {
  19. inputText: '',
  20. messages: [],
  21. scrollTop: 0
  22. }
  23. },
  24. onLoad() {
  25. this.messages.push({
  26. role: 'assistant',
  27. content: '你好,我是你的减脂AI教练,请告诉我你的目标和当前情况,我会帮你制定合适的减脂计划。'
  28. })
  29. },
  30. methods: {
  31. scrollToBottom() {
  32. setTimeout(() => {
  33. this.scrollTop = this.messages.length * 1000
  34. }, 100)
  35. },
  36. send() {
  37. if (!this.inputText.trim()) return
  38. var content = this.inputText.trim()
  39. this.messages.push({ role: 'user', content: content })
  40. this.inputText = ''
  41. this.scrollToBottom()
  42. var payload = {
  43. content: content,
  44. history: this.messages.slice(0, -1).map(m => ({ role: m.role, content: m.content }))
  45. }
  46. aiCoachSend(payload).then(res => {
  47. if (res.data && res.data.response) {
  48. this.messages.push({ role: 'assistant', content: res.data.response })
  49. } else if (res.data && res.data.reply) {
  50. this.messages.push({ role: 'assistant', content: res.data.reply })
  51. } else {
  52. this.messages.push({ role: 'assistant', content: '抱歉,我暂时无法回答这个问题。' })
  53. }
  54. this.scrollToBottom()
  55. }).catch(() => {
  56. this.messages.push({ role: 'assistant', content: '网络异常,请稍后重试。' })
  57. this.scrollToBottom()
  58. })
  59. }
  60. }
  61. }
  62. </script>
  63. <style>
  64. .chat-page { display: flex; flex-direction: column; height: 100vh; background: #f8f8f8; }
  65. .chat-body { flex: 1; padding: 30rpx; box-sizing: border-box; }
  66. .msg-item { margin-bottom: 30rpx; display: flex; }
  67. .msg-item.user { justify-content: flex-end; }
  68. .msg-item.assistant { justify-content: flex-start; }
  69. .bubble { max-width: 70%; padding: 20rpx 30rpx; border-radius: 16rpx; word-wrap: break-word; }
  70. .user .bubble { background: #F97316; color: #fff; }
  71. .assistant .bubble { background: #fff; color: #333; }
  72. .chat-input { display: flex; padding: 20rpx 30rpx; background: #fff; border-top: 1rpx solid #eee; align-items: center; }
  73. .input { flex: 1; height: 80rpx; padding: 0 20rpx; background: #f5f5f5; border-radius: 40rpx; margin-right: 20rpx; }
  74. .send-btn { background: #F97316; color: #fff; border-radius: 40rpx; }
  75. </style>