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.
This commit is contained in:
@@ -47,11 +47,13 @@
|
|||||||
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { useUIStore } from '@/stores/ui'
|
import { useUIStore } from '@/stores/ui'
|
||||||
|
import { useAuthStore } from '@/stores/auth'
|
||||||
import { MessagePlugin } from 'tdesign-vue-next'
|
import { MessagePlugin } from 'tdesign-vue-next'
|
||||||
import { getCurrentUser } from '@/api/auth'
|
import { getCurrentUser, logout as logoutApi } from '@/api/auth'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const uiStore = useUIStore()
|
const uiStore = useUIStore()
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
const menuRef = ref<HTMLElement>()
|
const menuRef = ref<HTMLElement>()
|
||||||
const menuVisible = ref(false)
|
const menuVisible = ref(false)
|
||||||
@@ -99,11 +101,19 @@ const handleSettings = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注销
|
// 注销
|
||||||
const handleLogout = () => {
|
const handleLogout = async () => {
|
||||||
menuVisible.value = false
|
menuVisible.value = false
|
||||||
|
|
||||||
// 清理本地存储
|
try {
|
||||||
localStorage.removeItem('WeKnora_token')
|
// 调用后端API注销
|
||||||
|
await logoutApi()
|
||||||
|
} catch (error) {
|
||||||
|
// 即使API调用失败,也继续执行本地清理
|
||||||
|
console.error('注销API调用失败:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理所有状态和本地存储
|
||||||
|
authStore.logout()
|
||||||
|
|
||||||
MessagePlugin.success('已退出登录')
|
MessagePlugin.success('已退出登录')
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
@click="gotopage(subitem.path)">
|
@click="gotopage(subitem.path)">
|
||||||
<i v-if="currentSecondpath == subitem.path" class="dot"></i>
|
<i v-if="currentSecondpath == subitem.path" class="dot"></i>
|
||||||
<span class="submenu_title"
|
<span class="submenu_title"
|
||||||
:style="currentSecondpath == subitem.path ? 'margin-left:14px;max-width:160px;' : 'margin-left:18px;max-width:173px;'">
|
:style="currentSecondpath == subitem.path ? 'margin-left:14px;max-width:160px;' : 'margin-left:18px;max-width:185px;'">
|
||||||
{{ subitem.title }}
|
{{ subitem.title }}
|
||||||
</span>
|
</span>
|
||||||
<t-dropdown
|
<t-dropdown
|
||||||
@@ -93,6 +93,7 @@ import { onMounted, watch, computed, ref, reactive, nextTick } from 'vue';
|
|||||||
import { useRoute, useRouter } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
import { getSessionsList, delSession } from "@/api/chat/index";
|
import { getSessionsList, delSession } from "@/api/chat/index";
|
||||||
import { getKnowledgeBaseById, listKnowledgeBases, uploadKnowledgeFile } from '@/api/knowledge-base';
|
import { getKnowledgeBaseById, listKnowledgeBases, uploadKnowledgeFile } from '@/api/knowledge-base';
|
||||||
|
import { logout as logoutApi } from '@/api/auth';
|
||||||
import { kbFileTypeVerification } from '@/utils/index';
|
import { kbFileTypeVerification } from '@/utils/index';
|
||||||
import { useMenuStore } from '@/stores/menu';
|
import { useMenuStore } from '@/stores/menu';
|
||||||
import { useAuthStore } from '@/stores/auth';
|
import { useAuthStore } from '@/stores/auth';
|
||||||
@@ -550,7 +551,16 @@ const gotopage = async (path: string) => {
|
|||||||
pathPrefix.value = path;
|
pathPrefix.value = path;
|
||||||
// 处理退出登录
|
// 处理退出登录
|
||||||
if (path === 'logout') {
|
if (path === 'logout') {
|
||||||
|
try {
|
||||||
|
// 调用后端API注销
|
||||||
|
await logoutApi();
|
||||||
|
} catch (error) {
|
||||||
|
// 即使API调用失败,也继续执行本地清理
|
||||||
|
console.error('注销API调用失败:', error);
|
||||||
|
}
|
||||||
|
// 清理所有状态和本地存储
|
||||||
authStore.logout();
|
authStore.logout();
|
||||||
|
MessagePlugin.success('已退出登录');
|
||||||
router.push('/login');
|
router.push('/login');
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
@@ -830,7 +840,7 @@ watch(() => route.params.kbId, () => {
|
|||||||
|
|
||||||
.submenu_item_p {
|
.submenu_item_p {
|
||||||
height: 44px;
|
height: 44px;
|
||||||
padding: 4px 8px 4px 0px;
|
padding: 4px 0px 4px 0px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user