| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import { fetchLogout } from '@/services/login';
- import { removeToken } from '@/utils/authUtils';
- import { toLoginPage } from '@/utils/routerUtils';
- import { LogoutOutlined, UserOutlined } from '@ant-design/icons';
- import { history, Icon, useIntl, useModel } from '@umijs/max';
- import { Spin } from 'antd';
- import { createStyles } from 'antd-style';
- import React, { useCallback } from 'react';
- import { flushSync } from 'react-dom';
- import HeaderDropdown from '../HeaderDropdown';
- export type GlobalHeaderRightProps = {
- menu?: boolean;
- children?: React.ReactNode;
- };
- export const AvatarName = () => {
- const { initialState } = useModel('@@initialState');
- const { currentUser } = initialState || {};
- return <span className="anticon leading-6">{currentUser?.username}</span>;
- };
- const useStyles = createStyles(({ token }) => {
- return {
- action: {
- display: 'flex',
- height: '48px',
- marginLeft: 'auto',
- overflow: 'hidden',
- alignItems: 'center',
- padding: '0 8px',
- cursor: 'pointer',
- borderRadius: token.borderRadius,
- '&:hover': {
- backgroundColor: token.colorBgTextHover,
- },
- },
- };
- });
- export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, children }) => {
- const intl = useIntl();
- const { initialState, setInitialState } = useModel('@@initialState');
- const loginOut = async () => {
- await fetchLogout();
- toLoginPage();
- removeToken();
- };
- const { styles } = useStyles();
- const onMenuClick = useCallback(
- (event: any) => {
- event?.domEvent?.stopPropagation();
- const { key } = event;
- if (key === 'logout') {
- flushSync(() => {
- setInitialState((s) => ({ ...s, currentUser: undefined }));
- });
- loginOut();
- return;
- }
- history.push(`/sys/${key}`);
- },
- [setInitialState],
- );
- const loading = (
- <span className={styles.action}>
- <Spin
- size="small"
- style={{
- marginLeft: 8,
- marginRight: 8,
- }}
- />
- </span>
- );
- if (!initialState) {
- return loading;
- }
- const { currentUser } = initialState;
- if (!currentUser || !currentUser.username) {
- return loading;
- }
- const menuItems = [
- ...(menu
- ? [
- {
- key: 'userInfo',
- icon: <UserOutlined />,
- label: intl.formatMessage({ id: 'layout.user.userInfo' }),
- },
- {
- key: 'modifyPassword',
- icon: <Icon icon="ri:key-line" className="mt-2" />,
- label: intl.formatMessage({ id: 'layout.user.modifyPassword' }),
- },
- {
- type: 'divider' as const,
- },
- ]
- : []),
- {
- key: 'logout',
- icon: <LogoutOutlined />,
- label: intl.formatMessage({ id: 'layout.user.logout' }),
- },
- ];
- return (
- <HeaderDropdown
- menu={{
- selectedKeys: [],
- onClick: onMenuClick,
- items: menuItems,
- }}
- >
- {children}
- </HeaderDropdown>
- );
- };
|