import { LogoutOutlined, UserOutlined } from '@ant-design/icons'; import { Avatar, Dropdown, Menu, Spin } from 'antd'; import type { ItemType } from 'antd/lib/menu/hooks/useItems'; import type { MenuInfo } from 'rc-menu/lib/interface'; import React, { useCallback } from 'react'; import { history, request, useIntl, useModel } from 'umi'; import styles from './index.less'; import { removeToken } from '../utils/token'; export type GlobalHeaderRightProps = { menu?: boolean; }; /** * 退出登录,并且将当前的 url 保存 */ const loginOut = async () => { const { data } = await request('/goapi/logout', { method: 'POST', }); removeToken(); window.location.href = data?.url; }; const AvatarDropdown: React.FC = ({ menu }) => { const { initialState, setInitialState } = useModel('@@initialState'); const { formatMessage } = useIntl(); const onMenuClick = useCallback( (event: MenuInfo) => { 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.id) { return loading; } const menuItems: ItemType[] = [ { key: 'logout', icon: , label: formatMessage({ id: 'component.logout.confirm', defaultMessage: '退出登录' }), }, ]; const menuHeaderDropdown = ( ); return ( } alt="avatar" /> {currentUser?.nameZh || currentUser?.nameEn}({currentUser?.account}) ); }; export default AvatarDropdown;