import { outLogin } from '@/services/ant-design-pro/api'; import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons'; import { Avatar, Menu, Spin } from 'antd'; import type { ItemType } from 'antd/lib/menu/hooks/useItems'; import { stringify } from 'querystring'; import type { MenuInfo } from 'rc-menu/lib/interface'; import React, { useCallback } from 'react'; import { history, useModel } from 'umi'; import HeaderDropdown from '../HeaderDropdown'; import styles from './index.less'; export type GlobalHeaderRightProps = { menu?: boolean; }; /** * 退出登录,并且将当前的 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: React.FC = ({ menu }) => { const { initialState, setInitialState } = useModel('@@initialState'); const onMenuClick = useCallback( (event: MenuInfo) => { const { key } = event; if (key === 'logout') { setInitialState((s) => ({ ...s, currentUser: undefined })); loginOut(); return; } else if (key === 'center') { history.push('/system/myInfo.html'); } }, [setInitialState], ); const loading = ( ); if (!initialState) { return loading; } const { currentUser } = initialState; if (!currentUser || !currentUser.nickname) { return loading; } const menuItems: ItemType[] = [ ...(menu ? [ { key: 'center', icon: , label: '个人中心', }, { key: 'settings', icon: , label: '个人设置', }, { type: 'divider' as const, }, ] : []), { key: 'logout', icon: , label: '退出登录', }, ]; const menuHeaderDropdown = ( ); return ( {currentUser.nickname} ); }; export default AvatarDropdown;