YM
5 天以前 6c526503d7b106e436393b4eac0a1c01ced0fe60
src/views/Start.vue
@@ -1,6 +1,6 @@
<template>
  <div class="chat-box">
    <div class="chat-box-content">
    <div class="chat-box-content" ref="messagesContainer">
      <div class="chat-date">03/15 13:10:00</div>
      <div class="chat-back-info">
        <div class="back-avator">
@@ -46,14 +46,45 @@
          <span>换一换</span>
        </div>
      </div>
      <div
        v-for="(item, index) in message"
        :key="index"
        :class="['message', item.type === 'user' ? 'user-message' : 'assistant-message']"
      >
        <el-avatar
          style="min-width: 40px"
          :size="screenWidth > 375 ? 60 : 40"
          v-if="item.type != 'user'"
          :src="assistantAvatar"
        />
        <div :class="['message-content', item.type === 'user' ? 'user-message-content' : '']">
          <div class="message-text" v-html="item.content"></div>
          <div class="message-footer" v-if="item.type != 'user'">
            <span class="message-time">{{ item.time }}</span>
          </div>
        </div>
        <div v-if="loading" class="message assistant-message loading-message">
          <el-avatar :size="40" :src="assistantAvatar" />
          <div :class="['message-content', item.type === 'user' ? 'user-message-content' : '']">
            <div class="message-text">
              <span class="dot-loading">思考中</span>
            </div>
          </div>
        </div>
      </div>
    </div>
      <div class="chat-footer">
        <div class="icon-list">
          <img src="../assets/images/liebiao1.png" alt="" />
          <img src="../assets/images/tianjia2.png" alt="" />
        </div>
        <div class="chat-text">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 10 }" placeholder="请输入您的问题"
            v-model="inputValue" />
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 10 }"
          placeholder="请输入您的问题"
          v-model="inputValue"
        />
        </div>
        <div class="select">
          <div class="select-left">
@@ -77,52 +108,99 @@
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { ref, inject } from 'vue'
  const request = inject('request')
import { nextTick, onMounted, ref, watch } from 'vue'
import defaultImg from '../assets/images/image2.png'
  const inputValue = ref('')
const message = ref<any>([])
const loading = ref(false)
const autoScroll = ref(true)
const messagesContainer = ref<HTMLElement | null>(null)
const assistantAvatar = defaultImg
const screenWidth = ref(window.innerWidth)
  const sendMsg = () => {
    console.log(inputValue.value);
    request({
      url: '/chat-messages',
      method: 'post',
      data: {
        query: inputValue.value,
        inputs: {},
        response_mode: "blocking",
        user: "test",
        files: []
const scrollToBottom = async () => {
  if (!autoScroll.value) return
  await nextTick()
  if (messagesContainer.value) {
    messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
      }
    }).then(res => {
      console.log(res, "res");
}
const formatMessage = (content: string) => {
  return content.replace(/\n/g, '<br>')
}
interface ChatMessage {
  content: string
  type: 'user' | 'assistant'
  time: string
}
const sendMsg = async () => {
  if (!inputValue.value.trim() || loading.value) return
  const userMessage: ChatMessage = {
    content: inputValue.value,
    type: 'user',
    time: new Date().toLocaleTimeString(),
  }
  message.value.push(userMessage)
  const messageToSend = inputValue.value
  inputValue.value = ''
  loading.value = true
  try {
    setTimeout(() => {
      const assistantMessage: ChatMessage = {
        content:
          '你好,十大杀个大傻瓜大厦提高党员湿地公园大刚刚撒反对哈挂号费哀伤的歌返回给法撒旦等哈的地方发生菲大使馆的撒',
        type: 'assistant',
        time: new Date().toLocaleTimeString(),
      }
      message.value.push(assistantMessage)
      loading.value = false
      console.log(message, 'dasdas')
    }, 1000)
    // 保存消息到本地存储
    localStorage.setItem('chatMessages', JSON.stringify(message.value))
  } catch (error) {
    console.error('发送消息失败', error)
  } finally {
    loading.value = false
  }
}
// 监听消息变化,自动滚动
watch(message.value, () => {
  scrollToBottom()
    })
// 从本地存储加载消息
onMounted(() => {
  const savedMessages = localStorage.getItem('chatMessages')
  if (savedMessages) {
    message.value = JSON.parse(savedMessages)
  }
  //   import { ref } from 'vue'
  // import { useRouter } from 'vue-router'
  // const router = useRouter()
  // const inputValue = ref('')
  // const handleChat = () => {
  //   router.push('/chat')
  // }
  scrollToBottom()
  screenWidth.value = window.innerWidth
})
</script>
<style lang="less" scoped>
  @media screen and (max-width: 750px) {
    .chat-box {
      width: 100%;
      height: auto;
    height: 100%;
      background-image: url('@/assets/images/beijing2.png');
      background-position: center;
      background-size: cover;
      .chat-box-content {
        width: 100%;
      height: calc(100vh - 170px);
        display: flex;
        flex-direction: column;
      overflow: auto;
        justify-content: flex-start;
        align-items: flex-start;
@@ -163,20 +241,22 @@
          }
          .base-book-box {
            width: 300px;
            padding: 20px;
          width: 80%;
          padding: 15px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 20px;
          font-size: 14px;
          }
        }
        .back-item {
          width: 300px;
        max-width: 80%;
          background: #ffffff;
          border-radius: 20px;
          margin-left: 50px;
          margin-bottom: 20px;
        font-size: 14px;
          .back-item-title {
            width: 80%;
@@ -216,14 +296,14 @@
            }
          }
        }
    }
        .chat-footer {
          width: 300px;
      width: 90%;
      margin: auto;
          background-color: #fff;
          border-radius: 20px;
          padding: 20px;
      padding: 10px;
          box-sizing: border-box;
          margin-left: 50px;
          margin-bottom: 20px;
          .icon-list img {
@@ -279,6 +359,7 @@
              .select-item {
                min-width: 70px;
                padding: 5px 8px;
            margin-left: 2px;
              }
            }
@@ -289,10 +370,65 @@
              .select-item {
                border: 0;
            margin-left: 2px;
              }
            }
          }
        }
    .message {
      display: flex;
      margin-bottom: 24px;
      align-items: flex-start;
      animation: slideIn 0.3s ease;
      animation-fill-mode: forwards;
      max-width: 85%;
      border-radius: 12px;
      // box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      padding: 16px;
      padding-left: 3px;
    }
    .message-content {
      background: #fff;
      margin-left: 10px;
      border-radius: 20px;
      min-width: 200px;
      padding: 5px;
    }
    .message-text {
      padding: 12px 16px;
      border-radius: 12px;
      word-wrap: break-word;
      line-height: 1.6;
      font-size: 0.95rem;
      transition: all 0.3s ease;
      white-space: pre-wrap;
      color: #333;
    }
    .user-message {
      flex-direction: row-reverse;
      margin-left: auto;
    }
    .user-message .message-text {
      color: #8b6e47;
    }
    .message-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 8px;
      padding: 0 4px;
      color: #999;
    }
    .message-time {
      font-size: 0.85rem;
      color: #999;
      margin-left: 10px;
      }
    }
  }
@@ -306,15 +442,48 @@
      background-position: center;
      background-repeat: no-repeat;
      display: flex;
      justify-content: center;
    flex-direction: column;
    justify-content: flex-start;
      align-items: center;
    padding-bottom: 10px;
      .chat-box-content {
        width: 1200px;
      height: calc(100vh - 180px);
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
      overflow: auto;
        align-items: flex-start;
      .chat-send {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: flex-end;
        margin-bottom: 20px;
        .chat-send-info {
          width: 263px;
          height: 40px;
          background: #af8b56;
          border-radius: 20px;
          line-height: 40px;
          padding: 0 20px;
          color: #fff;
        }
        .chat-message {
          width: 263px;
          height: 40px;
          background: #fff;
          border-radius: 20px;
          line-height: 40px;
          padding: 0 20px;
          color: #000;
        }
      }
        .chat-date {
          width: 100%;
@@ -403,15 +572,24 @@
            }
          }
        }
    }
    .chat-box-content::-webkit-scrollbar {
      width: 1px;
    }
    /* 隐藏水平滚动条 */
    .chat-box-content::-webkit-scrollbar {
      height: 0;
    }
        .chat-footer {
          width: 800px;
      width: 1200px;
          height: 170px;
          background-color: #fff;
          border-radius: 20px;
          padding: 20px;
          box-sizing: border-box;
          margin-left: 103px;
          .icon-list img {
            margin-right: 10px;
@@ -476,7 +654,69 @@
            }
          }
        }
    .message {
      display: flex;
      margin-bottom: 24px;
      align-items: flex-start;
      animation: slideIn 0.3s ease;
      animation-fill-mode: forwards;
      max-width: 95%;
      border-radius: 12px;
      // box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      padding: 16px;
      }
    .message-content {
      background: #fff;
      margin-left: 20px;
      border-radius: 20px;
      min-width: 200px;
      padding: 5px;
    }
    .message-text {
      padding: 12px 16px;
      border-radius: 12px;
      word-wrap: break-word;
      line-height: 1.6;
      font-size: 0.95rem;
      transition: all 0.3s ease;
      white-space: pre-wrap;
      color: #333;
    }
    .user-message {
      flex-direction: row-reverse;
      margin-left: auto;
    }
    .user-message .message-text {
      color: #8b6e47;
    }
    .message-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 8px;
      padding: 0 4px;
      color: #999;
    }
    .message-time {
      font-size: 0.85rem;
      color: #999;
      margin-left: 10px;
    }
  }
}
.user-message-content {
  background: #af8b56 !important;
  .message-text {
    color: #fff !important;
    padding: 5px 10px;
    }
  }
</style>