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:
15
frontend/index.html
Normal file
15
frontend/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>智队中枢 - Chat</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
29
frontend/package.json
Normal file
29
frontend/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "pit-chat-ui",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"typecheck": "vue-tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"vue": "^3.4.0",
|
||||
"vue-router": "^4.2.0",
|
||||
"pinia": "^2.1.0",
|
||||
"socket.io-client": "^4.7.0",
|
||||
"axios": "^1.6.0",
|
||||
"markdown-it": "^14.0.0",
|
||||
"highlight.js": "^11.9.0",
|
||||
"@highlightjs/vue-plugin": "^2.1.0",
|
||||
"date-fns": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^5.0.0",
|
||||
"@vue/tsconfig": "^0.5.0",
|
||||
"typescript": "^5.3.0",
|
||||
"vite": "^5.0.0",
|
||||
"vue-tsc": "^1.8.0"
|
||||
}
|
||||
}
|
||||
17
frontend/src/App.vue
Normal file
17
frontend/src/App.vue
Normal file
@@ -0,0 +1,17 @@
|
||||
<script setup lang="ts">
|
||||
import { RouterView } from 'vue-router'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="app">
|
||||
<RouterView />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.app {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
79
frontend/src/api/index.ts
Normal file
79
frontend/src/api/index.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import axios from 'axios'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
// Request interceptor - add auth token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('access_token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
// Response interceptor - handle errors
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('access_token')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// Auth API
|
||||
export const authApi = {
|
||||
login: (username: string, password: string) =>
|
||||
api.post('/auth/login', { username, password }),
|
||||
register: (username: string, email: string, password: string) =>
|
||||
api.post('/auth/register', { username, email, password }),
|
||||
refresh: () => api.post('/auth/refresh'),
|
||||
me: () => api.get('/auth/me')
|
||||
}
|
||||
|
||||
// Bot API
|
||||
export const botApi = {
|
||||
list: (params?: { owner_only?: boolean }) =>
|
||||
api.get('/bots', { params }),
|
||||
get: (id: string) => api.get(`/bots/${id}`),
|
||||
create: (data: any) => api.post('/bots', data),
|
||||
update: (id: string, data: any) => api.put(`/bots/${id}`, data),
|
||||
delete: (id: string) => api.delete(`/bots/${id}`),
|
||||
bind: (id: string, agentId: string) =>
|
||||
api.post(`/bots/${id}/bind`, { agent_id: agentId }),
|
||||
unbind: (id: string) => api.post(`/bots/${id}/unbind`),
|
||||
status: (id: string) => api.get(`/bots/${id}/status`),
|
||||
stats: (id: string) => api.get(`/bots/${id}/stats`)
|
||||
}
|
||||
|
||||
// Chat API
|
||||
export const chatApi = {
|
||||
sessions: {
|
||||
list: (params?: { status?: string; bot_id?: string; limit?: number; offset?: number }) =>
|
||||
api.get('/chat/sessions', { params }),
|
||||
get: (id: string) => api.get(`/chat/sessions/${id}`),
|
||||
create: (data: { bot_id: string; title?: string }) =>
|
||||
api.post('/chat/sessions', data),
|
||||
close: (id: string) => api.delete(`/chat/sessions/${id}`),
|
||||
messages: {
|
||||
list: (sessionId: string, params?: { limit?: number; before?: string }) =>
|
||||
api.get(`/chat/sessions/${sessionId}/messages`, { params }),
|
||||
send: (sessionId: string, data: { content: string; reply_to?: string }) =>
|
||||
api.post(`/chat/sessions/${sessionId}/messages`, data)
|
||||
},
|
||||
markRead: (id: string) => api.put(`/chat/sessions/${id}/read`)
|
||||
}
|
||||
}
|
||||
|
||||
export default api
|
||||
167
frontend/src/components/chat/BotSelector.vue
Normal file
167
frontend/src/components/chat/BotSelector.vue
Normal file
@@ -0,0 +1,167 @@
|
||||
<script setup lang="ts">
|
||||
import type { Bot } from '@/stores/chat'
|
||||
|
||||
defineProps<{
|
||||
bots: Bot[]
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
select: [botId: string]
|
||||
close: []
|
||||
}>()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="modal-overlay" @click.self="emit('close')">
|
||||
<div class="modal">
|
||||
<div class="modal-header">
|
||||
<h3>选择机器人</h3>
|
||||
<button class="close-btn" @click="emit('close')">×</button>
|
||||
</div>
|
||||
|
||||
<div class="bot-list">
|
||||
<div
|
||||
v-for="bot in bots"
|
||||
:key="bot.id"
|
||||
class="bot-item"
|
||||
@click="emit('select', bot.id)"
|
||||
>
|
||||
<div class="bot-avatar">
|
||||
{{ bot.avatar || '🤖' }}
|
||||
</div>
|
||||
<div class="bot-info">
|
||||
<h4>{{ bot.display_name || bot.name }}</h4>
|
||||
<p v-if="bot.description">{{ bot.description }}</p>
|
||||
<span class="status" :class="bot.status">
|
||||
{{ bot.status === 'online' ? '🟢 在线' : '⚪ 离线' }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="empty" v-if="bots.length === 0">
|
||||
<p>暂无可用机器人</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.modal {
|
||||
background: var(--bg-primary);
|
||||
border-radius: var(--border-radius-lg);
|
||||
width: 100%;
|
||||
max-width: 480px;
|
||||
max-height: 80vh;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
padding: var(--spacing-md);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.modal-header h3 {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.close-btn {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border: none;
|
||||
background: transparent;
|
||||
font-size: 24px;
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.close-btn:hover {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.bot-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: var(--spacing-md);
|
||||
}
|
||||
|
||||
.bot-item {
|
||||
display: flex;
|
||||
gap: var(--spacing-md);
|
||||
padding: var(--spacing-md);
|
||||
border-radius: var(--border-radius);
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.bot-item:hover {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.bot-avatar {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-tertiary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 24px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.bot-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.bot-info h4 {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.bot-info p {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.bot-info .status {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.bot-info .status.online {
|
||||
color: var(--success-color);
|
||||
}
|
||||
|
||||
.empty {
|
||||
text-align: center;
|
||||
padding: var(--spacing-xl);
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
</style>
|
||||
174
frontend/src/components/chat/ChatSidebar.vue
Normal file
174
frontend/src/components/chat/ChatSidebar.vue
Normal file
@@ -0,0 +1,174 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
import { useChatStore } from '@/stores/chat'
|
||||
|
||||
const emit = defineEmits<{
|
||||
newChat: []
|
||||
}>()
|
||||
|
||||
const chatStore = useChatStore()
|
||||
|
||||
onMounted(() => {
|
||||
chatStore.fetchSessions()
|
||||
})
|
||||
|
||||
function formatDate(dateStr: string): string {
|
||||
const date = new Date(dateStr)
|
||||
const now = new Date()
|
||||
const diff = now.getTime() - date.getTime()
|
||||
const days = Math.floor(diff / (1000 * 60 * 60 * 24))
|
||||
|
||||
if (days === 0) return '今天'
|
||||
if (days === 1) return '昨天'
|
||||
if (days < 7) return `${days}天前`
|
||||
return date.toLocaleDateString()
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<aside class="sidebar">
|
||||
<div class="sidebar-header">
|
||||
<h2>对话</h2>
|
||||
<button class="new-chat-btn" @click="emit('newChat')">
|
||||
+ 新对话
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="session-list">
|
||||
<div
|
||||
v-for="session in chatStore.sessions"
|
||||
:key="session.id"
|
||||
class="session-item"
|
||||
:class="{ active: chatStore.currentSession?.id === session.id }"
|
||||
@click="chatStore.joinSession(session.id)"
|
||||
>
|
||||
<div class="session-avatar">
|
||||
{{ session.bot?.avatar || '🤖' }}
|
||||
</div>
|
||||
<div class="session-content">
|
||||
<div class="session-header">
|
||||
<span class="session-title">{{ session.title }}</span>
|
||||
<span class="session-time">{{ formatDate(session.last_active_at || session.created_at) }}</span>
|
||||
</div>
|
||||
<p class="session-preview" v-if="session.last_message">
|
||||
{{ session.last_message.content.slice(0, 30) }}...
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="empty" v-if="chatStore.sessions.length === 0">
|
||||
<p>暂无对话</p>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.sidebar {
|
||||
width: 280px;
|
||||
background: var(--bg-primary);
|
||||
border-right: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.sidebar-header {
|
||||
padding: var(--spacing-md);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.sidebar-header h2 {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.new-chat-btn {
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-sm);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.new-chat-btn:hover {
|
||||
background: var(--primary-dark);
|
||||
}
|
||||
|
||||
.session-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.session-item {
|
||||
padding: var(--spacing-md);
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.session-item:hover {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.session-item.active {
|
||||
background: var(--bg-secondary);
|
||||
border-left: 3px solid var(--primary-color);
|
||||
}
|
||||
|
||||
.session-avatar {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-tertiary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.session-content {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.session-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.session-title {
|
||||
font-weight: 500;
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-primary);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.session-time {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.session-preview {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.empty {
|
||||
padding: var(--spacing-xl);
|
||||
text-align: center;
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
</style>
|
||||
209
frontend/src/components/chat/ChatWindow.vue
Normal file
209
frontend/src/components/chat/ChatWindow.vue
Normal file
@@ -0,0 +1,209 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, watch, nextTick } from 'vue'
|
||||
import { useChatStore } from '@/stores/chat'
|
||||
|
||||
const chatStore = useChatStore()
|
||||
const messagesContainer = ref<HTMLElement | null>(null)
|
||||
|
||||
const messages = computed(() => chatStore.messages)
|
||||
|
||||
watch(messages, () => {
|
||||
nextTick(() => {
|
||||
scrollToBottom()
|
||||
})
|
||||
}, { deep: true })
|
||||
|
||||
function scrollToBottom() {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
function formatTime(dateStr: string): string {
|
||||
return new Date(dateStr).toLocaleTimeString('zh-CN', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
})
|
||||
}
|
||||
|
||||
function isOwnMessage(msg: any): boolean {
|
||||
return msg.sender_type === 'user'
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-window">
|
||||
<div class="chat-header">
|
||||
<div class="chat-info">
|
||||
<div class="bot-avatar">
|
||||
{{ chatStore.currentSession?.bot?.avatar || '🤖' }}
|
||||
</div>
|
||||
<div class="chat-title">
|
||||
<h3>{{ chatStore.currentSession?.title }}</h3>
|
||||
<span class="status" :class="chatStore.currentSession?.bot?.status">
|
||||
{{ chatStore.currentSession?.bot?.status === 'online' ? '在线' : '离线' }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="messages-container" ref="messagesContainer">
|
||||
<div class="messages-list">
|
||||
<div
|
||||
v-for="message in messages"
|
||||
:key="message.id"
|
||||
class="message"
|
||||
:class="{ own: isOwnMessage(message) }"
|
||||
>
|
||||
<div class="message-avatar">
|
||||
{{ message.sender_type === 'user' ? '👤' : (chatStore.currentSession?.bot?.avatar || '🤖') }}
|
||||
</div>
|
||||
<div class="message-content">
|
||||
<div class="message-header">
|
||||
<span class="sender-name">{{ message.sender_name }}</span>
|
||||
<span class="message-time">{{ formatTime(message.created_at) }}</span>
|
||||
</div>
|
||||
<div class="message-bubble">
|
||||
{{ message.content }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="empty-messages" v-if="messages.length === 0">
|
||||
<p>暂无消息,开始对话吧</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-window {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-header {
|
||||
background: var(--bg-primary);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
padding: var(--spacing-md);
|
||||
}
|
||||
|
||||
.chat-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.bot-avatar {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-secondary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.chat-title h3 {
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.chat-title .status {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.chat-title .status.online {
|
||||
color: var(--success-color);
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: var(--spacing-md);
|
||||
}
|
||||
|
||||
.messages-list {
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
max-width: 80%;
|
||||
}
|
||||
|
||||
.message.own {
|
||||
flex-direction: row-reverse;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.message-avatar {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 50%;
|
||||
background: var(--bg-tertiary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 16px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.message-header {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
align-items: center;
|
||||
font-size: var(--font-size-xs);
|
||||
}
|
||||
|
||||
.message.own .message-header {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.sender-name {
|
||||
font-weight: 500;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.message-time {
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.message-bubble {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
border-radius: var(--border-radius-lg);
|
||||
color: var(--text-primary);
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.message.own .message-bubble {
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.empty-messages {
|
||||
text-align: center;
|
||||
color: var(--text-tertiary);
|
||||
padding: var(--spacing-xl);
|
||||
}
|
||||
</style>
|
||||
12
frontend/src/main.ts
Normal file
12
frontend/src/main.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { createApp } from 'vue'
|
||||
import { createPinia } from 'pinia'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import './style.css'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(createPinia())
|
||||
app.use(router)
|
||||
|
||||
app.mount('#app')
|
||||
40
frontend/src/router/index.ts
Normal file
40
frontend/src/router/index.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import type { RouteRecordRaw } from 'vue-router'
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: () => import('@/views/HomeView.vue')
|
||||
},
|
||||
{
|
||||
path: '/chat/:sessionId?',
|
||||
name: 'chat',
|
||||
component: () => import('@/views/ChatView.vue')
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('@/views/LoginView.vue')
|
||||
}
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
})
|
||||
|
||||
// Navigation guard
|
||||
router.beforeEach((to, from, next) => {
|
||||
const token = localStorage.getItem('access_token')
|
||||
|
||||
if (to.name !== 'login' && !token) {
|
||||
next({ name: 'login' })
|
||||
} else if (to.name === 'login' && token) {
|
||||
next({ name: 'home' })
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
70
frontend/src/stores/auth.ts
Normal file
70
frontend/src/stores/auth.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { authApi } from '@/api'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username: string
|
||||
email: string
|
||||
nickname?: string
|
||||
role: string
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const user = ref<User | null>(null)
|
||||
const token = ref<string | null>(localStorage.getItem('access_token'))
|
||||
const loading = ref(false)
|
||||
|
||||
const isLoggedIn = computed(() => !!token.value)
|
||||
const isAdmin = computed(() => user.value?.role === 'admin')
|
||||
|
||||
async function login(username: string, password: string) {
|
||||
loading.value = true
|
||||
try {
|
||||
const response = await authApi.login(username, password)
|
||||
const { access_token } = response.data
|
||||
token.value = access_token
|
||||
localStorage.setItem('access_token', access_token)
|
||||
await fetchUser()
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error)
|
||||
return false
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUser() {
|
||||
if (!token.value) return
|
||||
try {
|
||||
const response = await authApi.me()
|
||||
user.value = response.data.user
|
||||
} catch (error) {
|
||||
console.error('Fetch user failed:', error)
|
||||
logout()
|
||||
}
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null
|
||||
user.value = null
|
||||
localStorage.removeItem('access_token')
|
||||
}
|
||||
|
||||
// Initialize
|
||||
if (token.value) {
|
||||
fetchUser()
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
token,
|
||||
loading,
|
||||
isLoggedIn,
|
||||
isAdmin,
|
||||
login,
|
||||
logout,
|
||||
fetchUser
|
||||
}
|
||||
})
|
||||
212
frontend/src/stores/chat.ts
Normal file
212
frontend/src/stores/chat.ts
Normal file
@@ -0,0 +1,212 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { io, Socket } from 'socket.io-client'
|
||||
import { chatApi, botApi } from '@/api'
|
||||
|
||||
export interface Bot {
|
||||
id: string
|
||||
name: string
|
||||
display_name?: string
|
||||
avatar?: string
|
||||
description?: string
|
||||
status: string
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
id: string
|
||||
session_id: string
|
||||
sender_type: string
|
||||
sender_id: string
|
||||
sender_name?: string
|
||||
bot_id?: string
|
||||
content: string
|
||||
content_type: string
|
||||
reply_to?: string
|
||||
status: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface ChatSession {
|
||||
id: string
|
||||
user_id: string
|
||||
bot_id?: string
|
||||
title: string
|
||||
status: string
|
||||
message_count: number
|
||||
unread_count: number
|
||||
created_at: string
|
||||
last_active_at?: string
|
||||
bot?: Bot
|
||||
last_message?: {
|
||||
id: string
|
||||
content: string
|
||||
sender_type: string
|
||||
created_at: string
|
||||
}
|
||||
}
|
||||
|
||||
export const useChatStore = defineStore('chat', () => {
|
||||
const socket = ref<Socket | null>(null)
|
||||
const connected = ref(false)
|
||||
|
||||
const bots = ref<Bot[]>([])
|
||||
const sessions = ref<ChatSession[]>([])
|
||||
const currentSession = ref<ChatSession | null>(null)
|
||||
const messages = ref<Message[]>([])
|
||||
const loading = ref(false)
|
||||
|
||||
const hasActiveSession = computed(() => !!currentSession.value)
|
||||
|
||||
// Socket event handlers
|
||||
function setupSocket() {
|
||||
if (socket.value) return
|
||||
|
||||
const token = localStorage.getItem('access_token')
|
||||
socket.value = io({
|
||||
auth: { token },
|
||||
transports: ['websocket']
|
||||
})
|
||||
|
||||
socket.value.on('connect', () => {
|
||||
connected.value = true
|
||||
console.log('Socket connected')
|
||||
})
|
||||
|
||||
socket.value.on('disconnect', () => {
|
||||
connected.value = false
|
||||
console.log('Socket disconnected')
|
||||
})
|
||||
|
||||
socket.value.on('chat.message', (data: Message) => {
|
||||
if (currentSession.value?.id === data.session_id) {
|
||||
messages.value.push(data)
|
||||
}
|
||||
})
|
||||
|
||||
socket.value.on('chat.typing', (data: any) => {
|
||||
// Handle typing indicator
|
||||
})
|
||||
|
||||
socket.value.on('chat_error', (data: any) => {
|
||||
console.error('Chat error:', data)
|
||||
})
|
||||
}
|
||||
|
||||
function disconnectSocket() {
|
||||
if (socket.value) {
|
||||
socket.value.disconnect()
|
||||
socket.value = null
|
||||
connected.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Bot actions
|
||||
async function fetchBots() {
|
||||
try {
|
||||
const response = await botApi.list()
|
||||
bots.value = response.data.bots
|
||||
} catch (error) {
|
||||
console.error('Fetch bots failed:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Session actions
|
||||
async function fetchSessions() {
|
||||
loading.value = true
|
||||
try {
|
||||
const response = await chatApi.sessions.list({ limit: 50 })
|
||||
sessions.value = response.data.sessions
|
||||
} catch (error) {
|
||||
console.error('Fetch sessions failed:', error)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function createSession(botId: string, title?: string) {
|
||||
try {
|
||||
const response = await chatApi.sessions.create({ bot_id: botId, title })
|
||||
const session = response.data.session
|
||||
sessions.value.unshift(session)
|
||||
return session
|
||||
} catch (error) {
|
||||
console.error('Create session failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function joinSession(sessionId: string) {
|
||||
try {
|
||||
// Fetch session details
|
||||
const sessionResponse = await chatApi.sessions.get(sessionId)
|
||||
currentSession.value = sessionResponse.data.session
|
||||
|
||||
// Fetch messages
|
||||
const messagesResponse = await chatApi.sessions.messages.list(sessionId)
|
||||
messages.value = messagesResponse.data.messages
|
||||
|
||||
// Join socket room
|
||||
if (socket.value) {
|
||||
socket.value.emit('chat.send.join', { session_id: sessionId })
|
||||
}
|
||||
|
||||
return currentSession.value
|
||||
} catch (error) {
|
||||
console.error('Join session failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function leaveSession() {
|
||||
if (socket.value && currentSession.value) {
|
||||
socket.value.emit('chat.send.leave', { session_id: currentSession.value.id })
|
||||
}
|
||||
currentSession.value = null
|
||||
messages.value = []
|
||||
}
|
||||
|
||||
// Message actions
|
||||
async function sendMessage(content: string, replyTo?: string) {
|
||||
if (!currentSession.value) return
|
||||
|
||||
try {
|
||||
const response = await chatApi.sessions.messages.send(currentSession.value.id, {
|
||||
content,
|
||||
reply_to: replyTo
|
||||
})
|
||||
return response.data.message
|
||||
} catch (error) {
|
||||
console.error('Send message failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function sendTyping(isTyping: boolean) {
|
||||
if (socket.value && currentSession.value) {
|
||||
socket.value.emit('chat.send.typing', {
|
||||
session_id: currentSession.value.id,
|
||||
is_typing: isTyping
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
socket,
|
||||
connected,
|
||||
bots,
|
||||
sessions,
|
||||
currentSession,
|
||||
messages,
|
||||
loading,
|
||||
hasActiveSession,
|
||||
setupSocket,
|
||||
disconnectSocket,
|
||||
fetchBots,
|
||||
fetchSessions,
|
||||
createSession,
|
||||
joinSession,
|
||||
leaveSession,
|
||||
sendMessage,
|
||||
sendTyping
|
||||
}
|
||||
})
|
||||
143
frontend/src/style.css
Normal file
143
frontend/src/style.css
Normal file
@@ -0,0 +1,143 @@
|
||||
/* Base styles */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
/* Colors */
|
||||
--primary-color: #6366f1;
|
||||
--primary-light: #818cf8;
|
||||
--primary-dark: #4f46e5;
|
||||
--secondary-color: #ec4899;
|
||||
--success-color: #10b981;
|
||||
--warning-color: #f59e0b;
|
||||
--error-color: #ef4444;
|
||||
|
||||
/* Background */
|
||||
--bg-primary: #ffffff;
|
||||
--bg-secondary: #f3f4f6;
|
||||
--bg-tertiary: #e5e7eb;
|
||||
|
||||
/* Text */
|
||||
--text-primary: #111827;
|
||||
--text-secondary: #6b7280;
|
||||
--text-tertiary: #9ca3af;
|
||||
|
||||
/* Border */
|
||||
--border-color: #e5e7eb;
|
||||
--border-radius: 8px;
|
||||
--border-radius-lg: 12px;
|
||||
|
||||
/* Spacing */
|
||||
--spacing-xs: 4px;
|
||||
--spacing-sm: 8px;
|
||||
--spacing-md: 16px;
|
||||
--spacing-lg: 24px;
|
||||
--spacing-xl: 32px;
|
||||
|
||||
/* Shadow */
|
||||
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
||||
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
|
||||
|
||||
/* Font */
|
||||
--font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||
--font-size-xs: 12px;
|
||||
--font-size-sm: 14px;
|
||||
--font-size-md: 16px;
|
||||
--font-size-lg: 18px;
|
||||
--font-size-xl: 20px;
|
||||
}
|
||||
|
||||
/* Dark mode */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg-primary: #0f172a;
|
||||
--bg-secondary: #1e293b;
|
||||
--bg-tertiary: #334155;
|
||||
|
||||
--text-primary: #f1f5f9;
|
||||
--text-secondary: #94a3b8;
|
||||
--text-tertiary: #64748b;
|
||||
|
||||
--border-color: #334155;
|
||||
}
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: var(--font-family);
|
||||
font-size: var(--font-size-md);
|
||||
color: var(--text-primary);
|
||||
background-color: var(--bg-primary);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
/* Scrollbar */
|
||||
::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: var(--text-tertiary);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background: var(--text-secondary);
|
||||
}
|
||||
|
||||
/* Utility classes */
|
||||
.flex {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.flex-col {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.items-center {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.justify-center {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.justify-between {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.gap-sm {
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.gap-md {
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.gap-lg {
|
||||
gap: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.text-secondary {
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.text-tertiary {
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.truncate {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
222
frontend/src/views/ChatView.vue
Normal file
222
frontend/src/views/ChatView.vue
Normal file
@@ -0,0 +1,222 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch, nextTick } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useChatStore } from '@/stores/chat'
|
||||
import ChatSidebar from '@/components/chat/ChatSidebar.vue'
|
||||
import ChatWindow from '@/components/chat/ChatWindow.vue'
|
||||
import BotSelector from '@/components/chat/BotSelector.vue'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const chatStore = useChatStore()
|
||||
|
||||
const showBotSelector = ref(false)
|
||||
const messageInput = ref('')
|
||||
const messagesContainer = ref<HTMLElement | null>(null)
|
||||
|
||||
onMounted(async () => {
|
||||
await chatStore.fetchBots()
|
||||
chatStore.setupSocket()
|
||||
|
||||
const sessionId = route.params.sessionId as string
|
||||
if (sessionId) {
|
||||
await chatStore.joinSession(sessionId)
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => route.params.sessionId, async (newId) => {
|
||||
if (newId && typeof newId === 'string') {
|
||||
await chatStore.joinSession(newId)
|
||||
} else {
|
||||
chatStore.leaveSession()
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => chatStore.messages, () => {
|
||||
nextTick(() => {
|
||||
scrollToBottom()
|
||||
})
|
||||
}, { deep: true })
|
||||
|
||||
function scrollToBottom() {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSend() {
|
||||
if (!messageInput.value.trim() || !chatStore.currentSession) return
|
||||
|
||||
const content = messageInput.value.trim()
|
||||
messageInput.value = ''
|
||||
|
||||
try {
|
||||
await chatStore.sendMessage(content)
|
||||
} catch (error) {
|
||||
console.error('Send failed:', error)
|
||||
}
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
handleSend()
|
||||
}
|
||||
}
|
||||
|
||||
async function createSession(botId: string) {
|
||||
showBotSelector.value = false
|
||||
const session = await chatStore.createSession(botId)
|
||||
router.push({ name: 'chat', params: { sessionId: session.id } })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-page">
|
||||
<ChatSidebar @new-chat="showBotSelector = true" />
|
||||
|
||||
<div class="chat-main" v-if="chatStore.currentSession">
|
||||
<ChatWindow />
|
||||
|
||||
<div class="chat-input-area">
|
||||
<div class="input-wrapper">
|
||||
<textarea
|
||||
v-model="messageInput"
|
||||
placeholder="输入消息..."
|
||||
rows="1"
|
||||
@keydown="handleKeydown"
|
||||
@input="chatStore.sendTyping(true)"
|
||||
/>
|
||||
<button
|
||||
class="send-btn"
|
||||
@click="handleSend"
|
||||
:disabled="!messageInput.trim()"
|
||||
>
|
||||
发送
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="empty-state" v-else>
|
||||
<div class="empty-content">
|
||||
<h2>👋 欢迎使用智队中枢</h2>
|
||||
<p>选择一个机器人开始聊天</p>
|
||||
<button class="start-btn" @click="showBotSelector = true">
|
||||
开始新对话
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<BotSelector
|
||||
v-if="showBotSelector"
|
||||
:bots="chatStore.bots"
|
||||
@select="createSession"
|
||||
@close="showBotSelector = false"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-page {
|
||||
display: flex;
|
||||
height: 100vh;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.chat-main {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-input-area {
|
||||
background: var(--bg-primary);
|
||||
border-top: 1px solid var(--border-color);
|
||||
padding: var(--spacing-md);
|
||||
}
|
||||
|
||||
.input-wrapper {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.input-wrapper textarea {
|
||||
flex: 1;
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-md);
|
||||
background: var(--bg-secondary);
|
||||
color: var(--text-primary);
|
||||
resize: none;
|
||||
min-height: 44px;
|
||||
max-height: 120px;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.input-wrapper textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary-color);
|
||||
}
|
||||
|
||||
.send-btn {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.send-btn:hover:not(:disabled) {
|
||||
background: var(--primary-dark);
|
||||
}
|
||||
|
||||
.send-btn:disabled {
|
||||
background: var(--text-tertiary);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.empty-content {
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.empty-content h2 {
|
||||
font-size: var(--font-size-xl);
|
||||
color: var(--text-primary);
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.empty-content p {
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.start-btn {
|
||||
padding: var(--spacing-sm) var(--spacing-lg);
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-md);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.start-btn:hover {
|
||||
background: var(--primary-dark);
|
||||
}
|
||||
</style>
|
||||
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>
|
||||
166
frontend/src/views/LoginView.vue
Normal file
166
frontend/src/views/LoginView.vue
Normal file
@@ -0,0 +1,166 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
|
||||
const router = useRouter()
|
||||
const authStore = useAuthStore()
|
||||
|
||||
const username = ref('')
|
||||
const password = ref('')
|
||||
const error = ref('')
|
||||
|
||||
async function handleLogin() {
|
||||
error.value = ''
|
||||
|
||||
if (!username.value || !password.value) {
|
||||
error.value = '请输入用户名和密码'
|
||||
return
|
||||
}
|
||||
|
||||
const success = await authStore.login(username.value, password.value)
|
||||
|
||||
if (success) {
|
||||
router.push('/')
|
||||
} else {
|
||||
error.value = '登录失败,请检查用户名和密码'
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="login-page">
|
||||
<div class="login-card">
|
||||
<div class="logo">
|
||||
<h1>🐕 智队中枢</h1>
|
||||
<p>Personal Intelligent Team</p>
|
||||
</div>
|
||||
|
||||
<form @submit.prevent="handleLogin" class="login-form">
|
||||
<div class="form-group">
|
||||
<label for="username">用户名</label>
|
||||
<input
|
||||
id="username"
|
||||
v-model="username"
|
||||
type="text"
|
||||
placeholder="请输入用户名"
|
||||
autocomplete="username"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password">密码</label>
|
||||
<input
|
||||
id="password"
|
||||
v-model="password"
|
||||
type="password"
|
||||
placeholder="请输入密码"
|
||||
autocomplete="current-password"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="error" class="error-message">
|
||||
{{ error }}
|
||||
</div>
|
||||
|
||||
<button type="submit" class="login-btn" :disabled="authStore.loading">
|
||||
{{ authStore.loading ? '登录中...' : '登录' }}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.login-page {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.login-card {
|
||||
background: var(--bg-primary);
|
||||
border-radius: var(--border-radius-lg);
|
||||
padding: var(--spacing-xl);
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
box-shadow: var(--shadow-lg);
|
||||
}
|
||||
|
||||
.logo {
|
||||
text-align: center;
|
||||
margin-bottom: var(--spacing-xl);
|
||||
}
|
||||
|
||||
.logo h1 {
|
||||
font-size: var(--font-size-xl);
|
||||
color: var(--primary-color);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.logo p {
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.form-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
font-size: var(--font-size-sm);
|
||||
font-weight: 500;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.form-group input {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-md);
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.form-group input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary-color);
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: var(--error-color);
|
||||
font-size: var(--font-size-sm);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-btn {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--border-radius);
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.login-btn:hover:not(:disabled) {
|
||||
background: var(--primary-dark);
|
||||
}
|
||||
|
||||
.login-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
</style>
|
||||
25
frontend/tsconfig.json
Normal file
25
frontend/tsconfig.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "preserve",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
|
||||
"references": [{ "path": "./tsconfig.node.json" }]
|
||||
}
|
||||
11
frontend/tsconfig.node.json
Normal file
11
frontend/tsconfig.node.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"skipLibCheck": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
25
frontend/vite.config.ts
Normal file
25
frontend/vite.config.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import { resolve } from 'path'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': resolve(__dirname, 'src')
|
||||
}
|
||||
},
|
||||
server: {
|
||||
port: 3000,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:9000',
|
||||
changeOrigin: true
|
||||
},
|
||||
'/socket.io': {
|
||||
target: 'http://localhost:9000',
|
||||
ws: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user