| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <template>
- <div class="chat-container">
- <div class="chat-header">
- <h2>蜀道安全管理AI智能助手</h2>
- </div>
- <div class="chat-messages" ref="messagesContainer">
- <div v-for="(msg, index) in messages" :key="index" :class="['message', msg.role]">
- <div class="avatar">{{ msg.role === 'user' ? '👤' : '🤖' }}</div>
- <div class="content">
- <div v-if="msg.role === 'ai'" v-html="renderMarkdown(msg.content)" class="markdown-content"></div>
- <div v-else>{{ msg.content }}</div>
- <div class="message-actions" v-if="msg.role === 'ai'">
- <button @click="copyMessage(msg.content)" class="action-btn">📋 复制</button>
- </div>
- </div>
- </div>
- <div v-if="isLoading" class="message ai">
- <div class="avatar">🤖</div>
- <div class="content">
- <div class="typing-indicator">思考中...</div>
- </div>
- </div>
- </div>
- <div class="chat-input-area">
- <div class="quick-questions" v-if="messages.length === 0">
- <div class="section-title">常见问题</div>
- <div class="question-list">
- <button
- v-for="q in quickQuestions"
- :key="q"
- @click="sendQuickQuestion(q)"
- class="quick-btn"
- >{{ q }}</button>
- </div>
- </div>
- <div class="input-wrapper">
- <textarea
- v-model="inputMessage"
- @keydown.enter.exact.prevent="sendMessage"
- placeholder="请输入您的问题..."
- :disabled="isLoading"
- rows="1"
- ></textarea>
- <button
- @click="sendMessage"
- :disabled="!inputMessage.trim() || isLoading"
- class="send-btn"
- >发送</button>
- </div>
- </div>
- </div>
- </template>
- <script setup>
- import { ref, nextTick, watch } from 'vue'
- import { marked } from 'marked'
- import { getMockResponse, getQuickQuestions } from '@/mock/chatMock'
- const messages = ref([])
- const inputMessage = ref('')
- const isLoading = ref(false)
- const messagesContainer = ref(null)
- const quickQuestions = ref(getQuickQuestions())
- const renderMarkdown = (content) => {
- return marked.parse(content)
- }
- const scrollToBottom = async () => {
- await nextTick()
- if (messagesContainer.value) {
- messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
- }
- }
- const sendMessage = async () => {
- const message = inputMessage.value.trim()
- if (!message || isLoading.value) return
- messages.value.push({ role: 'user', content: message })
- inputMessage.value = ''
- isLoading.value = true
- await scrollToBottom()
- // Mock AI response
- setTimeout(async () => {
- const response = getMockResponse(message)
- messages.value.push({ role: 'ai', content: response })
- isLoading.value = false
- await scrollToBottom()
- }, 1000)
- }
- const sendQuickQuestion = (question) => {
- inputMessage.value = question
- sendMessage()
- }
- const copyMessage = async (content) => {
- await navigator.clipboard.writeText(content)
- }
- </script>
- <style scoped lang="scss">
- .chat-container {
- display: flex;
- flex-direction: column;
- height: 100%;
- background: #f8f9fa;
- }
- .chat-header {
- padding: 20px 24px;
- background: #fff;
- border-bottom: 1px solid #e8e8e8;
- h2 {
- font-size: 18px;
- font-weight: 500;
- color: #333;
- }
- }
- .chat-messages {
- flex: 1;
- overflow-y: auto;
- padding: 24px;
- .message {
- display: flex;
- margin-bottom: 20px;
- gap: 12px;
- &.user {
- flex-direction: row-reverse;
- }
- .avatar {
- font-size: 32px;
- flex-shrink: 0;
- }
- .content {
- max-width: 70%;
- padding: 12px 16px;
- border-radius: 12px;
- background: #fff;
- box-shadow: 0 1px 2px rgba(0,0,0,0.05);
- .markdown-content {
- :deep(p) {
- margin-bottom: 8px;
- }
- :deep(p:last-child) {
- margin-bottom: 0;
- }
- }
- }
- &.ai .content {
- background: #fff;
- }
- &.user .content {
- background: #1890ff;
- color: #fff;
- }
- .message-actions {
- margin-top: 8px;
- padding-top: 8px;
- border-top: 1px solid #f0f0f0;
- .action-btn {
- background: none;
- border: none;
- color: #999;
- cursor: pointer;
- font-size: 12px;
- padding: 4px 8px;
- &:hover {
- color: #666;
- }
- }
- }
- }
- .typing-indicator {
- color: #999;
- font-style: italic;
- }
- }
- .chat-input-area {
- padding: 20px 24px;
- background: #fff;
- border-top: 1px solid #e8e8e8;
- }
- .quick-questions {
- margin-bottom: 16px;
- .section-title {
- font-size: 14px;
- color: #666;
- margin-bottom: 12px;
- }
- .question-list {
- display: flex;
- flex-wrap: wrap;
- gap: 8px;
- .quick-btn {
- padding: 8px 16px;
- border: 1px solid #d9d9d9;
- border-radius: 20px;
- background: #fff;
- cursor: pointer;
- font-size: 13px;
- color: #666;
- transition: all 0.2s;
- &:hover {
- border-color: #1890ff;
- color: #1890ff;
- }
- }
- }
- }
- .input-wrapper {
- display: flex;
- gap: 12px;
- align-items: flex-end;
- textarea {
- flex: 1;
- padding: 12px 16px;
- border: 1px solid #d9d9d9;
- border-radius: 8px;
- resize: none;
- font-size: 14px;
- line-height: 1.5;
- outline: none;
- transition: border-color 0.2s;
- &:focus {
- border-color: #1890ff;
- }
- &:disabled {
- background: #f5f5f5;
- }
- }
- .send-btn {
- padding: 12px 24px;
- background: #1890ff;
- color: #fff;
- border: none;
- border-radius: 8px;
- cursor: pointer;
- font-size: 14px;
- transition: background 0.2s;
- &:hover:not(:disabled) {
- background: #40a9ff;
- }
- &:disabled {
- background: #d9d9d9;
- cursor: not-allowed;
- }
- }
- }
- </style>
|