From 30a711ea2f48532db69d2322637cef90d3204039 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 6 Nov 2025 21:36:38 +0800 Subject: [PATCH] feat: Implement logout functionality with API integration - Added logout API call in both menu and user menu components. - Enhanced error handling for logout process to ensure local state is cleared even if API fails. - Updated styles for submenu items for better UI consistency. --- frontend/src/components/UserMenu.vue | 18 ++++++++++++++---- frontend/src/components/menu.vue | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/UserMenu.vue b/frontend/src/components/UserMenu.vue index 97e626b8..913b27c7 100644 --- a/frontend/src/components/UserMenu.vue +++ b/frontend/src/components/UserMenu.vue @@ -47,11 +47,13 @@ import { ref, computed, onMounted, onUnmounted } from 'vue' import { useRouter } from 'vue-router' import { useUIStore } from '@/stores/ui' +import { useAuthStore } from '@/stores/auth' import { MessagePlugin } from 'tdesign-vue-next' -import { getCurrentUser } from '@/api/auth' +import { getCurrentUser, logout as logoutApi } from '@/api/auth' const router = useRouter() const uiStore = useUIStore() +const authStore = useAuthStore() const menuRef = ref() const menuVisible = ref(false) @@ -99,11 +101,19 @@ const handleSettings = () => { } // 注销 -const handleLogout = () => { +const handleLogout = async () => { menuVisible.value = false - // 清理本地存储 - localStorage.removeItem('WeKnora_token') + try { + // 调用后端API注销 + await logoutApi() + } catch (error) { + // 即使API调用失败,也继续执行本地清理 + console.error('注销API调用失败:', error) + } + + // 清理所有状态和本地存储 + authStore.logout() MessagePlugin.success('已退出登录') diff --git a/frontend/src/components/menu.vue b/frontend/src/components/menu.vue index 4fb84d81..4cd9a29a 100644 --- a/frontend/src/components/menu.vue +++ b/frontend/src/components/menu.vue @@ -57,7 +57,7 @@ @click="gotopage(subitem.path)"> + :style="currentSecondpath == subitem.path ? 'margin-left:14px;max-width:160px;' : 'margin-left:18px;max-width:185px;'"> {{ subitem.title }} { pathPrefix.value = path; // 处理退出登录 if (path === 'logout') { + try { + // 调用后端API注销 + await logoutApi(); + } catch (error) { + // 即使API调用失败,也继续执行本地清理 + console.error('注销API调用失败:', error); + } + // 清理所有状态和本地存储 authStore.logout(); + MessagePlugin.success('已退出登录'); router.push('/login'); return; } else { @@ -830,7 +840,7 @@ watch(() => route.params.kbId, () => { .submenu_item_p { height: 44px; - padding: 4px 8px 4px 0px; + padding: 4px 0px 4px 0px; box-sizing: border-box; }