feat: Step 6 - 前端聊天界面 (v0.9.5)
- 创建 Vue.js 3 前端项目 (frontend/) - 实现核心功能: - 登录页面 (LoginView) - 首页 - 机器人和会话列表 (HomeView) - 聊天页面 (ChatView) - 聊天侧边栏 (ChatSidebar) - 聊天窗口 (ChatWindow) - 机器人选择器 (BotSelector) - 集成功能: - Socket.io WebSocket 连接 - Pinia 状态管理 - Axios API 客户端 - JWT 认证 - 更新版本号到 0.9.5
This commit is contained in:
291
frontend/src/views/HomeView.vue
Normal file
291
frontend/src/views/HomeView.vue
Normal file
@@ -0,0 +1,291 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { useChatStore } from '@/stores/chat'
|
||||
|
||||
const router = useRouter()
|
||||
const authStore = useAuthStore()
|
||||
const chatStore = useChatStore()
|
||||
|
||||
onMounted(async () => {
|
||||
await chatStore.fetchBots()
|
||||
await chatStore.fetchSessions()
|
||||
chatStore.setupSocket()
|
||||
})
|
||||
|
||||
function startChat(botId: string) {
|
||||
router.push({ name: 'chat', params: {} })
|
||||
chatStore.createSession(botId).then(session => {
|
||||
router.push({ name: 'chat', params: { sessionId: session.id } })
|
||||
})
|
||||
}
|
||||
|
||||
function resumeChat(sessionId: string) {
|
||||
router.push({ name: 'chat', params: { sessionId } })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="home-page">
|
||||
<header class="header">
|
||||
<div class="header-content">
|
||||
<h1>🐕 智队中枢</h1>
|
||||
<div class="user-info">
|
||||
<span>{{ authStore.user?.nickname || authStore.user?.username }}</span>
|
||||
<button class="logout-btn" @click="authStore.logout">退出</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="main-content">
|
||||
<!-- Bot List -->
|
||||
<section class="section">
|
||||
<h2 class="section-title">选择机器人开始聊天</h2>
|
||||
<div class="bot-grid">
|
||||
<div
|
||||
v-for="bot in chatStore.bots"
|
||||
:key="bot.id"
|
||||
class="bot-card"
|
||||
@click="startChat(bot.id)"
|
||||
>
|
||||
<div class="bot-avatar">
|
||||
{{ bot.avatar || '🤖' }}
|
||||
</div>
|
||||
<div class="bot-info">
|
||||
<h3 class="bot-name">{{ bot.display_name || bot.name }}</h3>
|
||||
<p class="bot-status" :class="bot.status">
|
||||
{{ bot.status === 'online' ? '🟢 在线' : '⚪ 离线' }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Recent Sessions -->
|
||||
<section class="section" v-if="chatStore.sessions.length > 0">
|
||||
<h2 class="section-title">最近会话</h2>
|
||||
<div class="session-list">
|
||||
<div
|
||||
v-for="session in chatStore.sessions.slice(0, 10)"
|
||||
:key="session.id"
|
||||
class="session-item"
|
||||
@click="resumeChat(session.id)"
|
||||
>
|
||||
<div class="session-avatar">
|
||||
{{ session.bot?.avatar || '🤖' }}
|
||||
</div>
|
||||
<div class="session-info">
|
||||
<div class="session-header">
|
||||
<h4 class="session-title">{{ session.title }}</h4>
|
||||
<span class="session-time">{{ new Date(session.last_active_at || session.created_at).toLocaleDateString() }}</span>
|
||||
</div>
|
||||
<p class="session-preview" v-if="session.last_message">
|
||||
{{ session.last_message.content.slice(0, 50) }}...
|
||||
</p>
|
||||
<p class="session-preview" v-else>暂无消息</p>
|
||||
</div>
|
||||
<div class="session-badge" v-if="session.unread_count > 0">
|
||||
{{ session.unread_count }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.home-page {
|
||||
min-height: 100vh;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.header {
|
||||
background: var(--bg-primary);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
padding: var(--spacing-md) var(--spacing-lg);
|
||||
}
|
||||
|
||||
.header-content {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: var(--font-size-lg);
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.logout-btn {
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
background: transparent;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
.logout-btn:hover {
|
||||
border-color: var(--error-color);
|
||||
color: var(--error-color);
|
||||
}
|
||||
|
||||
.main-content {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.section {
|
||||
margin-bottom: var(--spacing-xl);
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
.bot-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.bot-card {
|
||||
background: var(--bg-primary);
|
||||
border-radius: var(--border-radius-lg);
|
||||
padding: var(--spacing-md);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.bot-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: var(--shadow-md);
|
||||
}
|
||||
|
||||
.bot-avatar {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-secondary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.bot-info {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.bot-name {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.bot-status {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.bot-status.online {
|
||||
color: var(--success-color);
|
||||
}
|
||||
|
||||
.session-list {
|
||||
background: var(--bg-primary);
|
||||
border-radius: var(--border-radius-lg);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.session-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
padding: var(--spacing-md);
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.session-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.session-item:hover {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.session-avatar {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-secondary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.session-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.session-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.session-title {
|
||||
font-weight: 500;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.session-time {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.session-preview {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.session-badge {
|
||||
min-width: 20px;
|
||||
height: 20px;
|
||||
padding: 0 6px;
|
||||
background: var(--error-color);
|
||||
color: white;
|
||||
border-radius: 10px;
|
||||
font-size: var(--font-size-xs);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user