AvatarDropdown.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
  2. import { history, useModel } from '@umijs/max';
  3. import { Space, Spin } from 'antd';
  4. import { createStyles } from 'antd-style';
  5. import type { MenuInfo } from 'rc-menu/lib/interface';
  6. import React, { useCallback } from 'react';
  7. import { flushSync } from 'react-dom';
  8. import HeaderDropdown from '../HeaderDropdown';
  9. import { logOut } from '@/services/login';
  10. export type GlobalHeaderRightProps = {
  11. menu?: boolean;
  12. children?: React.ReactNode;
  13. };
  14. export const AvatarName = () => {
  15. const { initialState } = useModel('@@initialState');
  16. const { currentUser } = initialState || {};
  17. return <span className="anticon">{currentUser?.nickname}</span>;
  18. };
  19. const useStyles = createStyles(({ token }) => {
  20. return {
  21. action: {
  22. display: 'flex',
  23. height: '48px',
  24. marginLeft: 'auto',
  25. overflow: 'hidden',
  26. alignItems: 'center',
  27. padding: '0 8px',
  28. cursor: 'pointer',
  29. borderRadius: token.borderRadius,
  30. '&:hover': {
  31. backgroundColor: token.colorBgTextHover,
  32. },
  33. },
  34. };
  35. });
  36. export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, children }) => {
  37. /**
  38. * 退出登录,并且将当前的 url 保存
  39. */
  40. const loginOut = async () => {
  41. // await outLogin();
  42. let { search, pathname, origin, href } = window.location;
  43. const urlParams = new URL(window.location.href).searchParams;
  44. /** 此方法会跳转到 redirect 参数所在的位置 */
  45. // await logOut()
  46. localStorage.removeItem("Token")
  47. sessionStorage.removeItem("menuType");
  48. sessionStorage.removeItem("selectApp");
  49. // history.push('/user/login')
  50. window.location.href = origin
  51. };
  52. const { styles } = useStyles();
  53. const { initialState, setInitialState } = useModel('@@initialState');
  54. const onMenuClick = useCallback(
  55. (event: MenuInfo) => {
  56. const { key } = event;
  57. if (key === 'logout') {
  58. flushSync(() => {
  59. setInitialState((s) => ({ ...s, menuType: s?.menuType || 'distributor', currentUser: undefined, token: "" }));
  60. });
  61. loginOut();
  62. return;
  63. }
  64. history.push(`/account/${key}`);
  65. },
  66. [setInitialState],
  67. );
  68. const loading = (
  69. <span className={styles.action}>
  70. <Spin
  71. size="small"
  72. style={{
  73. marginLeft: 8,
  74. marginRight: 8,
  75. }}
  76. />
  77. </span>
  78. );
  79. if (!initialState) {
  80. return loading;
  81. }
  82. const { currentUser } = initialState;
  83. if (!currentUser || !currentUser.nickname) {
  84. return loading;
  85. }
  86. const menuItems = [
  87. ...(menu
  88. ? [
  89. {
  90. key: 'center',
  91. icon: <UserOutlined />,
  92. label: '个人中心',
  93. },
  94. {
  95. key: 'settings',
  96. icon: <SettingOutlined />,
  97. label: '个人设置',
  98. },
  99. {
  100. type: 'divider' as const,
  101. },
  102. ]
  103. : []),
  104. {
  105. key: 'logout',
  106. icon: <LogoutOutlined />,
  107. label: '退出登录',
  108. },
  109. ];
  110. return (
  111. <Space>
  112. <HeaderDropdown
  113. menu={{
  114. selectedKeys: [],
  115. onClick: onMenuClick,
  116. items: menuItems,
  117. }}
  118. >
  119. {children}
  120. </HeaderDropdown>
  121. </Space>
  122. );
  123. };