123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { outLogin } from '@/services/ant-design-pro/api';
- import { LockOutlined, 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, useEffect, useState } from 'react';
- import { history, useModel } from 'umi';
- import HeaderDropdown from '../HeaderDropdown';
- import { currentUserInfo } from '@/services/setting';
- import styles from './index.less';
- import UpdatePassword from '@/components/RightContent/updatePassword';
- 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<GlobalHeaderRightProps> = ({ menu }) => {
- const { initialState, setInitialState } = useModel('@@initialState');
- const [avatar, setAvatar] = useState('');
- const [visible, setVisible] = useState(false);
- useEffect(() => {
- const token = localStorage.getItem('token');
- currentUserInfo(token).then((res: any) => {
- if (res.code === 0) {
- setAvatar(res?.data?.photo);
- }
- });
- }, []);
- const onMenuClick = useCallback(
- (event: MenuInfo) => {
- const { key } = event;
- if (key === 'logout') {
- setInitialState((s) => ({ ...s, currentUser: undefined }));
- loginOut();
- return;
- }
- if (key === 'updatePassword') {
- setVisible(true);
- return;
- }
- history.push(`/account/${key}`);
- },
- [setInitialState],
- );
- const loading = (
- <span className={`${styles.action} ${styles.account}`}>
- <Spin
- size="small"
- style={{
- marginLeft: 8,
- marginRight: 8,
- }}
- />
- </span>
- );
- if (!initialState) {
- return loading;
- }
- const { currentUser } = initialState;
- if (!currentUser || !currentUser.name) {
- return loading;
- }
- const menuItems: ItemType[] = [
- ...(menu
- ? [
- {
- key: 'center',
- icon: <UserOutlined />,
- label: '个人中心',
- },
- {
- key: 'settings',
- icon: <SettingOutlined />,
- label: '个人设置',
- },
- {
- type: 'divider' as const,
- },
- ]
- : []),
- {
- key: 'updatePassword',
- icon: <LockOutlined />,
- label: '修改密码',
- },
- {
- key: 'logout',
- icon: <LogoutOutlined />,
- label: '退出登录',
- },
- ];
- const menuHeaderDropdown = (
- <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick} items={menuItems} />
- );
- return (
- <>
- <HeaderDropdown overlay={menuHeaderDropdown}>
- <span className={`${styles.action} ${styles.account}`}>
- <Avatar size="small" className={styles.avatar} src={avatar} alt="avatar" />
- <span className={`${styles.name} anticon`}>{currentUser.name}</span>
- </span>
- </HeaderDropdown>
- {visible && (
- <UpdatePassword
- visible={visible}
- onCancel={() => {
- setVisible(false);
- }}
- />
- )}
- </>
- );
- };
- export default AvatarDropdown;
|