|
|
@@ -0,0 +1,242 @@
|
|
|
+<template>
|
|
|
+ <!-- 悬浮按钮 -->
|
|
|
+ <div class="chat-fab" @click="toggleChat">
|
|
|
+ <van-icon :name="showChat ? 'cross' : 'chat-o'" size="22" color="#fff" />
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <!-- 聊天面板 -->
|
|
|
+ <van-popup
|
|
|
+ v-model:show="showChat"
|
|
|
+ position="bottom"
|
|
|
+ round
|
|
|
+ :style="{ height: '65vh' }"
|
|
|
+ teleport="body"
|
|
|
+ >
|
|
|
+ <div class="chat-panel">
|
|
|
+ <div class="chat-header">
|
|
|
+ <span class="chat-title">🤖 小愿 · 智能客服</span>
|
|
|
+ <van-icon name="cross" size="18" @click="showChat = false" />
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div class="chat-body" ref="msgBody">
|
|
|
+ <div v-if="messages.length === 0" class="chat-empty">
|
|
|
+ <p>👋 你好,我是许愿树小助手"小愿"</p>
|
|
|
+ <p>有什么关于许愿的问题都可以问我~</p>
|
|
|
+ </div>
|
|
|
+ <div
|
|
|
+ v-for="(msg, i) in messages"
|
|
|
+ :key="i"
|
|
|
+ :class="['chat-msg', msg.role]"
|
|
|
+ >
|
|
|
+ <div class="chat-bubble">{{ msg.content }}</div>
|
|
|
+ </div>
|
|
|
+ <div v-if="sending" class="chat-msg ai">
|
|
|
+ <div class="chat-bubble typing">...</div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div class="chat-footer">
|
|
|
+ <van-field
|
|
|
+ v-model="input"
|
|
|
+ placeholder="输入你的问题..."
|
|
|
+ :border="false"
|
|
|
+ @keypress.enter="send"
|
|
|
+ />
|
|
|
+ <van-button
|
|
|
+ type="primary"
|
|
|
+ size="small"
|
|
|
+ round
|
|
|
+ :disabled="!input.trim() || sending"
|
|
|
+ @click="send"
|
|
|
+ >
|
|
|
+ 发送
|
|
|
+ </van-button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </van-popup>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang="ts">
|
|
|
+import { ref, nextTick, watch } from 'vue'
|
|
|
+import { sendMessage, fetchHistory } from '@/api/chat'
|
|
|
+
|
|
|
+const props = defineProps<{
|
|
|
+ treeId?: number
|
|
|
+}>()
|
|
|
+
|
|
|
+interface ChatMsg {
|
|
|
+ role: 'user' | 'ai'
|
|
|
+ content: string
|
|
|
+}
|
|
|
+
|
|
|
+const showChat = ref(false)
|
|
|
+const input = ref('')
|
|
|
+const sending = ref(false)
|
|
|
+const messages = ref<ChatMsg[]>([])
|
|
|
+const msgBody = ref<HTMLElement | null>(null)
|
|
|
+const historyLoaded = ref(false)
|
|
|
+
|
|
|
+async function loadHistory() {
|
|
|
+ try {
|
|
|
+ const list = await fetchHistory(props.treeId)
|
|
|
+ if (list.length > 0) {
|
|
|
+ messages.value = list.map((m) => ({ role: m.role, content: m.content }))
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ // 加载失败不影响使用
|
|
|
+ }
|
|
|
+ historyLoaded.value = true
|
|
|
+}
|
|
|
+
|
|
|
+function toggleChat() {
|
|
|
+ showChat.value = !showChat.value
|
|
|
+}
|
|
|
+
|
|
|
+function scrollToBottom() {
|
|
|
+ nextTick(() => {
|
|
|
+ const el = msgBody.value
|
|
|
+ if (el) {
|
|
|
+ el.scrollTop = el.scrollHeight
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+async function send() {
|
|
|
+ const text = input.value.trim()
|
|
|
+ if (!text || sending.value) return
|
|
|
+
|
|
|
+ messages.value.push({ role: 'user', content: text })
|
|
|
+ input.value = ''
|
|
|
+ scrollToBottom()
|
|
|
+
|
|
|
+ sending.value = true
|
|
|
+ try {
|
|
|
+ const reply = await sendMessage(text, props.treeId)
|
|
|
+ messages.value.push({ role: 'ai', content: reply })
|
|
|
+ } catch {
|
|
|
+ messages.value.push({ role: 'ai', content: '抱歉,网络出了问题,请稍后再试~' })
|
|
|
+ } finally {
|
|
|
+ sending.value = false
|
|
|
+ scrollToBottom()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+watch(showChat, (val) => {
|
|
|
+ if (val) {
|
|
|
+ if (!historyLoaded.value) {
|
|
|
+ loadHistory()
|
|
|
+ }
|
|
|
+ scrollToBottom()
|
|
|
+ }
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped>
|
|
|
+.chat-fab {
|
|
|
+ position: fixed;
|
|
|
+ right: 16px;
|
|
|
+ bottom: 80px;
|
|
|
+ width: 48px;
|
|
|
+ height: 48px;
|
|
|
+ border-radius: 50%;
|
|
|
+ background: linear-gradient(135deg, #07c160, #05a650);
|
|
|
+ box-shadow: 0 4px 12px rgba(7, 193, 96, 0.4);
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+ justify-content: center;
|
|
|
+ z-index: 999;
|
|
|
+ cursor: pointer;
|
|
|
+ transition: transform 0.2s;
|
|
|
+}
|
|
|
+.chat-fab:active {
|
|
|
+ transform: scale(0.9);
|
|
|
+}
|
|
|
+
|
|
|
+.chat-panel {
|
|
|
+ display: flex;
|
|
|
+ flex-direction: column;
|
|
|
+ height: 100%;
|
|
|
+ background: #f7f8fa;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-header {
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+ justify-content: space-between;
|
|
|
+ padding: 14px 16px;
|
|
|
+ background: #fff;
|
|
|
+ border-bottom: 1px solid #ebedf0;
|
|
|
+ flex-shrink: 0;
|
|
|
+}
|
|
|
+.chat-title {
|
|
|
+ font-size: 16px;
|
|
|
+ font-weight: 600;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-body {
|
|
|
+ flex: 1;
|
|
|
+ overflow-y: auto;
|
|
|
+ padding: 12px 14px;
|
|
|
+ display: flex;
|
|
|
+ flex-direction: column;
|
|
|
+ gap: 10px;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-empty {
|
|
|
+ text-align: center;
|
|
|
+ color: #999;
|
|
|
+ margin-top: 40px;
|
|
|
+ font-size: 14px;
|
|
|
+ line-height: 2;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-msg {
|
|
|
+ display: flex;
|
|
|
+ max-width: 80%;
|
|
|
+}
|
|
|
+.chat-msg.user {
|
|
|
+ align-self: flex-end;
|
|
|
+}
|
|
|
+.chat-msg.ai {
|
|
|
+ align-self: flex-start;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-bubble {
|
|
|
+ padding: 10px 14px;
|
|
|
+ border-radius: 16px;
|
|
|
+ font-size: 14px;
|
|
|
+ line-height: 1.6;
|
|
|
+ word-break: break-word;
|
|
|
+}
|
|
|
+.chat-msg.user .chat-bubble {
|
|
|
+ background: #07c160;
|
|
|
+ color: #fff;
|
|
|
+ border-bottom-right-radius: 4px;
|
|
|
+}
|
|
|
+.chat-msg.ai .chat-bubble {
|
|
|
+ background: #fff;
|
|
|
+ color: #333;
|
|
|
+ border-bottom-left-radius: 4px;
|
|
|
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
|
|
|
+}
|
|
|
+.chat-bubble.typing {
|
|
|
+ color: #999;
|
|
|
+ padding: 10px 18px;
|
|
|
+}
|
|
|
+
|
|
|
+.chat-footer {
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+ padding: 8px 12px;
|
|
|
+ background: #fff;
|
|
|
+ border-top: 1px solid #ebedf0;
|
|
|
+ gap: 8px;
|
|
|
+ flex-shrink: 0;
|
|
|
+}
|
|
|
+.chat-footer :deep(.van-cell) {
|
|
|
+ flex: 1;
|
|
|
+ background: #f5f5f5;
|
|
|
+ border-radius: 20px;
|
|
|
+ padding: 6px 14px;
|
|
|
+}
|
|
|
+</style>
|