| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import ModalCenter from '@/pages/Account/Center/ModalCenter';
- import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
- import { useEmotionCss } from '@ant-design/use-emotion-css';
- import { history, useModel } from '@umijs/max';
- import { Spin } from 'antd';
- import { stringify } from 'querystring';
- import type { MenuInfo } from 'rc-menu/lib/interface';
- import React, { useCallback, useState } 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">{currentUser?.userName}</span>;
- };
- export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, children }) => {
- /**
- * 退出登录,并且将当前的 url 保存
- */
- const loginOut = async () => {
- localStorage.removeItem('Admin-Token');
- const { search, pathname } = window.location;
- const urlParams = new URL(window.location.href).searchParams;
- /** 此方法会跳转到 redirect 参数所在的位置 */
- const redirect = urlParams.get('redirect');
- // 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 actionClassName = useEmotionCss(({ token }) => {
- return {
- display: 'flex',
- height: '48px',
- marginLeft: 'auto',
- overflow: 'hidden',
- alignItems: 'center',
- padding: '0 8px',
- cursor: 'pointer',
- borderRadius: token.borderRadius,
- '&:hover': {
- backgroundColor: token.colorBgTextHover,
- },
- };
- });
- const { initialState, setInitialState } = useModel('@@initialState');
- const [visible, setVisible] = useState<boolean>(false);
- const onMenuClick = useCallback(
- (event: MenuInfo) => {
- const { key } = event;
- if (key === 'logout') {
- flushSync(() => {
- setInitialState((s) => ({ ...s, currentUser: undefined }));
- });
- loginOut();
- return;
- }
- if (key === 'center') {
- setVisible(true);
- return;
- }
- history.push(`/account/${key}`);
- },
- [setInitialState],
- );
- const loading = (
- <span className={actionClassName}>
- <Spin
- size="small"
- style={{
- marginLeft: 8,
- marginRight: 8,
- }}
- />
- </span>
- );
- if (!initialState) {
- return loading;
- }
- const { currentUser } = initialState;
- if (!currentUser || !currentUser.mobile) {
- return loading;
- }
- const menuItems = [
- ...(menu
- ? [
- {
- key: 'center',
- icon: <UserOutlined />,
- label: '个人中心',
- },
- {
- key: 'settings',
- icon: <SettingOutlined />,
- label: '个人设置',
- },
- {
- type: 'divider' as const,
- },
- ]
- : []),
- {
- key: 'center',
- icon: <UserOutlined />,
- label: '个人中心',
- },
- {
- key: 'logout',
- icon: <LogoutOutlined />,
- label: '退出登录',
- },
- ];
- return (
- <>
- {visible && <ModalCenter visible={visible} onClose={() => setVisible(false)} />}
- <HeaderDropdown
- menu={{
- selectedKeys: [],
- onClick: onMenuClick,
- items: menuItems,
- }}
- >
- {children}
- </HeaderDropdown>
- </>
- );
- };
|