AvatarDropdown.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { outLogin } from '@/services/ant-design-pro/api';
  2. import { LockOutlined, LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons';
  3. import { Avatar, Menu, Spin } from 'antd';
  4. import type { ItemType } from 'antd/lib/menu/hooks/useItems';
  5. import { stringify } from 'querystring';
  6. import type { MenuInfo } from 'rc-menu/lib/interface';
  7. import React, { useCallback, useEffect, useState } from 'react';
  8. import { history, useModel } from 'umi';
  9. import HeaderDropdown from '../HeaderDropdown';
  10. import { currentUserInfo } from '@/services/setting';
  11. import styles from './index.less';
  12. import UpdatePassword from '@/components/RightContent/updatePassword';
  13. export type GlobalHeaderRightProps = {
  14. menu?: boolean;
  15. };
  16. /**
  17. * 退出登录,并且将当前的 url 保存
  18. */
  19. const loginOut = async () => {
  20. await outLogin();
  21. const { query = {}, search, pathname } = history.location;
  22. const { redirect } = query;
  23. // Note: There may be security issues, please note
  24. if (window.location.pathname !== '/user/login' && !redirect) {
  25. history.replace({
  26. pathname: '/user/login',
  27. search: stringify({
  28. redirect: pathname + search,
  29. }),
  30. });
  31. }
  32. };
  33. const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
  34. const { initialState, setInitialState } = useModel('@@initialState');
  35. const [avatar, setAvatar] = useState('');
  36. const [visible, setVisible] = useState(false);
  37. useEffect(() => {
  38. const token = localStorage.getItem('token');
  39. currentUserInfo(token).then((res: any) => {
  40. if (res.code === 0) {
  41. setAvatar(res?.data?.photo);
  42. }
  43. });
  44. }, []);
  45. const onMenuClick = useCallback(
  46. (event: MenuInfo) => {
  47. const { key } = event;
  48. if (key === 'logout') {
  49. setInitialState((s) => ({ ...s, currentUser: undefined }));
  50. loginOut();
  51. return;
  52. }
  53. if (key === 'updatePassword') {
  54. setVisible(true);
  55. return;
  56. }
  57. history.push(`/account/${key}`);
  58. },
  59. [setInitialState],
  60. );
  61. const loading = (
  62. <span className={`${styles.action} ${styles.account}`}>
  63. <Spin
  64. size="small"
  65. style={{
  66. marginLeft: 8,
  67. marginRight: 8,
  68. }}
  69. />
  70. </span>
  71. );
  72. if (!initialState) {
  73. return loading;
  74. }
  75. const { currentUser } = initialState;
  76. if (!currentUser || !currentUser.name) {
  77. return loading;
  78. }
  79. const menuItems: ItemType[] = [
  80. ...(menu
  81. ? [
  82. {
  83. key: 'center',
  84. icon: <UserOutlined />,
  85. label: '个人中心',
  86. },
  87. {
  88. key: 'settings',
  89. icon: <SettingOutlined />,
  90. label: '个人设置',
  91. },
  92. {
  93. type: 'divider' as const,
  94. },
  95. ]
  96. : []),
  97. {
  98. key: 'updatePassword',
  99. icon: <LockOutlined />,
  100. label: '修改密码',
  101. },
  102. {
  103. key: 'logout',
  104. icon: <LogoutOutlined />,
  105. label: '退出登录',
  106. },
  107. ];
  108. const menuHeaderDropdown = (
  109. <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick} items={menuItems} />
  110. );
  111. return (
  112. <>
  113. <HeaderDropdown overlay={menuHeaderDropdown}>
  114. <span className={`${styles.action} ${styles.account}`}>
  115. <Avatar size="small" className={styles.avatar} src={avatar} alt="avatar" />
  116. <span className={`${styles.name} anticon`}>{currentUser.name}</span>
  117. </span>
  118. </HeaderDropdown>
  119. {visible && (
  120. <UpdatePassword
  121. visible={visible}
  122. onCancel={() => {
  123. setVisible(false);
  124. }}
  125. />
  126. )}
  127. </>
  128. );
  129. };
  130. export default AvatarDropdown;