You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
866 B
TypeScript

import { Message } from '@arco-design/web-react';
import { logout } from '@/api/user';
import { clearToken } from '@/utils/auth';
interface UseUserReturnType {
logoutHooks: () => Promise<void>;
}
export default function useUser(): UseUserReturnType {
const logoutHooks = async () => {
try {
// 调用后端登出接口
await logout();
} catch (error) {
// 即使后端登出失败,也要清除本地状态
console.error('Logout error:', error);
} finally {
// 清除本地存储的用户状态
localStorage.removeItem('userStatus');
clearToken()
// 显示成功消息
Message.success('退出成功');
// 重定向到登录页面
const { protocol, host } = window.location;
window.location.href = `${protocol}//${host}/login`;
}
};
return {
logoutHooks
};
}