diff --git a/src/api/login/index.ts b/src/api/login/index.ts index a5f9cb5..aff4797 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -10,3 +10,10 @@ export const login = (params: { phone: string; password: string }) => { data: LoginResult; }>; }; + +export const logout = () => { + return post(`/op/logout`) as Promise<{ + code: number | string; + msg: string; + }>; +}; diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 8a81d87..093b576 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -7,6 +7,7 @@ import { usePermissionStore } from '@/stores/permissionStore'; import { useTabsStore } from '@/stores/tabsStore'; import { useUserStore } from '@/stores/userStore'; import { clearAuthDataCache } from '@/api/authCache'; +import { logout as logoutApi } from '@/api/login'; import router from '@/router'; const TOKEN_KEY = 'MY_APP_AUTH_TOKEN'; @@ -66,9 +67,14 @@ function createAuth() { }; /** - * 退出:清除 token → 清除菜单+权限 → 清除动态路由 → 跳转登录页 + * 退出:调用登出接口 → 清除 token → 清除菜单+权限 → 清除动态路由 → 跳转登录页 */ - const logout = () => { + const logout = async () => { + try { + await logoutApi(); + } catch { + // 即使接口失败也要继续清除本地会话 + } clearSession(); router.push('/login'); }; diff --git a/src/layouts/BasicLayout.tsx b/src/layouts/BasicLayout.tsx index bcbd633..ba05f1e 100644 --- a/src/layouts/BasicLayout.tsx +++ b/src/layouts/BasicLayout.tsx @@ -113,8 +113,6 @@ export default defineComponent({ okText: '确认', cancelText: '取消', onOk: () => { - const { clearUser } = useUserStore(); - clearUser(); auth.logout(); }, });