import React, { useCallback } from 'react'; import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons'; import { Avatar, Menu, Spin } from 'antd'; import { history, useModel } from 'umi'; import { stringify } from 'querystring'; import HeaderDropdown from '../HeaderDropdown'; import styles from './index.less'; import { outLogin } from '@/services/ant-design-pro/api'; /** * 退出登录,并且将当前的 url 保存 */ const loginOut = async () => { await outLogin(); const { query = {}, search, pathname } = history.location; const { redirect } = query; // Note: There may be security issues, please note if (window.location.pathname !== '/user/login' && !redirect) { history.replace({ pathname: '/user/login', search: stringify({ redirect: pathname + search, }), }); } }; const AvatarDropdown = ({ menu }) => { const { initialState, setInitialState } = useModel('@@initialState'); const onMenuClick = useCallback( (event) => { const { key } = event; if (key === 'logout') { setInitialState((s) => ({ ...s, currentUser: undefined })); loginOut(); return; } history.push(`/account/${key}`); }, [setInitialState], ); const loading = ( ); if (!initialState) { return loading; } const { currentUser } = initialState; if (!currentUser || !currentUser.name) { return loading; } const menuHeaderDropdown = ( {menu && ( 个人中心 )} {menu && ( 个人设置 )} {menu && } 退出登录 ); return ( {currentUser.name} ); }; export default AvatarDropdown;