123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
- import { history, useModel } from '@umijs/max';
- import { Space, Spin } from 'antd';
- import { createStyles } from 'antd-style';
- import type { MenuInfo } from 'rc-menu/lib/interface';
- import React, { useCallback } from 'react';
- import { flushSync } from 'react-dom';
- import HeaderDropdown from '../HeaderDropdown';
- import { logOut } from '@/services/login';
- export type GlobalHeaderRightProps = {
- menu?: boolean;
- children?: React.ReactNode;
- };
- export const AvatarName = () => {
- const { initialState } = useModel('@@initialState');
- const { currentUser } = initialState || {};
- return <span className="anticon">{currentUser?.nickname}</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 }) => {
- /**
- * 退出登录,并且将当前的 url 保存
- */
- const loginOut = async () => {
- // await outLogin();
- let { search, pathname, origin, href } = window.location;
- const urlParams = new URL(window.location.href).searchParams;
- /** 此方法会跳转到 redirect 参数所在的位置 */
- // await logOut()
- localStorage.removeItem("Token")
- sessionStorage.removeItem("menuType");
- sessionStorage.removeItem("selectApp");
- // history.push('/user/login')
- window.location.href = origin
- };
- const { styles } = useStyles();
- const { initialState, setInitialState } = useModel('@@initialState');
- const onMenuClick = useCallback(
- (event: MenuInfo) => {
- const { key } = event;
- if (key === 'logout') {
- flushSync(() => {
- setInitialState((s) => ({ ...s, menuType: s?.menuType || 'distributor', currentUser: undefined, token: "" }));
- });
- loginOut();
- return;
- }
- history.push(`/account/${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.nickname) {
- return loading;
- }
- const menuItems = [
- ...(menu
- ? [
- {
- key: 'center',
- icon: <UserOutlined />,
- label: '个人中心',
- },
- {
- key: 'settings',
- icon: <SettingOutlined />,
- label: '个人设置',
- },
- {
- type: 'divider' as const,
- },
- ]
- : []),
- {
- key: 'logout',
- icon: <LogoutOutlined />,
- label: '退出登录',
- },
- ];
- return (
- <Space>
- <HeaderDropdown
- menu={{
- selectedKeys: [],
- onClick: onMenuClick,
- items: menuItems,
- }}
- >
- {children}
- </HeaderDropdown>
- </Space>
- );
- };
|