diff --git a/dashboard/src/App.vue b/dashboard/src/App.vue index 91c8deb..3b91f0f 100644 --- a/dashboard/src/App.vue +++ b/dashboard/src/App.vue @@ -1,469 +1,26 @@ diff --git a/dashboard/src/components/AppSidebar.vue b/dashboard/src/components/AppSidebar.vue new file mode 100644 index 0000000..2424ea5 --- /dev/null +++ b/dashboard/src/components/AppSidebar.vue @@ -0,0 +1,118 @@ + + + + + diff --git a/dashboard/src/components/BaseButton.vue b/dashboard/src/components/BaseButton.vue new file mode 100644 index 0000000..dd5020f --- /dev/null +++ b/dashboard/src/components/BaseButton.vue @@ -0,0 +1,50 @@ + + + + + diff --git a/dashboard/src/composables/index.js b/dashboard/src/composables/index.js new file mode 100644 index 0000000..b883f31 --- /dev/null +++ b/dashboard/src/composables/index.js @@ -0,0 +1,5 @@ +// 导出所有组合式函数 +export { useAuth } from './useAuth.js' +export { useApi, usePagination, useForm } from './useApi.js' +export { formatDate, formatNumber, truncate } from './useFormatters.js' +export { debounce, throttle, storage, copyToClipboard } from './useUtils.js' diff --git a/dashboard/src/composables/useAuth.js b/dashboard/src/composables/useAuth.js new file mode 100644 index 0000000..040810e --- /dev/null +++ b/dashboard/src/composables/useAuth.js @@ -0,0 +1,77 @@ +/** + * 认证状态管理组合式函数 + */ +import { ref, onMounted, onUnmounted } from 'vue' + +export function useAuth() { + const isLoggedIn = ref(false) + const user = ref(null) + const loading = ref(true) + + // 检查登录状态 + const checkAuth = () => { + isLoggedIn.value = !!localStorage.getItem('access_token') + if (isLoggedIn.value) { + const userData = localStorage.getItem('user') + if (userData) { + try { + user.value = JSON.parse(userData) + } catch { + user.value = null + } + } + } else { + user.value = null + } + } + + // 登录 + const login = (token, userData) => { + localStorage.setItem('access_token', token) + localStorage.setItem('user', JSON.stringify(userData)) + isLoggedIn.value = true + user.value = userData + window.dispatchEvent(new CustomEvent('auth-change')) + } + + // 登出 + const logout = async (api) => { + try { + if (api) await api.logout() + } catch (e) { + console.error('登出失败:', e) + } + localStorage.removeItem('access_token') + localStorage.removeItem('user') + isLoggedIn.value = false + user.value = null + window.dispatchEvent(new CustomEvent('auth-change')) + } + + // 获取 Token + const getToken = () => localStorage.getItem('access_token') + + // 监听认证变化 + const handleAuthChange = () => { + checkAuth() + } + + onMounted(() => { + checkAuth() + window.addEventListener('auth-change', handleAuthChange) + }) + + onUnmounted(() => { + window.removeEventListener('auth-change', handleAuthChange) + }) + + return { + isLoggedIn, + user, + loading, + checkAuth, + login, + logout, + getToken + } +} diff --git a/dashboard/src/views/AuthView.vue b/dashboard/src/views/AuthView.vue index 13151ba..7bcda4b 100644 --- a/dashboard/src/views/AuthView.vue +++ b/dashboard/src/views/AuthView.vue @@ -1,335 +1,94 @@ diff --git a/dashboard/src/views/ConversationsView.vue b/dashboard/src/views/ConversationsView.vue index bf526c4..ceda75e 100644 --- a/dashboard/src/views/ConversationsView.vue +++ b/dashboard/src/views/ConversationsView.vue @@ -1,201 +1,38 @@