AvatarDropdown.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* eslint-disable */
  2. import React, { useCallback } from 'react'
  3. import { InfoCircleFilled, LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons'
  4. import { Avatar, Menu, Spin, Modal } from 'antd'
  5. import { createSearchParams, history, useModel } from '@umijs/max'
  6. import HeaderDropdown from '../HeaderDropdown'
  7. import styles from './index.less'
  8. import { clearAuthCache, removeToken } from '@/utils/auth'
  9. import ws from '@/utils/ws'
  10. import { isDevMode } from '@/utils/env'
  11. import consts from '@/consts'
  12. export type GlobalHeaderRightProps = {
  13. menu?: boolean
  14. }
  15. /**
  16. * 退出登录,并且将当前的 url 保存
  17. */
  18. const loginOut = async () => {
  19. // Note: There may be security issues, please note
  20. if (window.location.pathname !== consts.LOGIN_PATH) {
  21. // 断开socket连接
  22. ws.disconnect()
  23. history.replace({
  24. pathname: '/user/login',
  25. search: createSearchParams({ redirect: window.location.pathname }).toString()
  26. })
  27. }
  28. }
  29. const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
  30. const { initialState, setInitialState } = useModel('@@initialState')
  31. const onMenuClick = useCallback(
  32. (event: {
  33. key: React.Key
  34. keyPath: React.Key[]
  35. item: React.ReactInstance
  36. domEvent: React.MouseEvent<HTMLElement>
  37. }) => {
  38. const { key } = event
  39. if (key === 'logout' && initialState) {
  40. return Modal.confirm({
  41. icon: <InfoCircleFilled />,
  42. title: '温馨提示',
  43. content: '是否确认退出系统?',
  44. autoFocusButton: null,
  45. onOk: () => {
  46. // 清除AuthCache
  47. removeToken()
  48. setInitialState({ ...initialState, currentUser: undefined, roles: [], permData: {} })
  49. loginOut()
  50. return
  51. }
  52. })
  53. }
  54. return history.push(`/account/${key}`)
  55. },
  56. [initialState, setInitialState]
  57. )
  58. const loading = (
  59. <span className={`${styles.action} ${styles.account}`}>
  60. <Spin
  61. size="small"
  62. style={{
  63. marginLeft: 8,
  64. marginRight: 8
  65. }}
  66. />
  67. </span>
  68. )
  69. if (!initialState) {
  70. return loading
  71. }
  72. const { currentUser } = initialState
  73. if (!currentUser || !currentUser.staffId) {
  74. return loading
  75. }
  76. const menuItems = [
  77. { icon: <SettingOutlined />, label: '个人设置', key: 'settings' },
  78. { icon: <LogoutOutlined />, label: '退出登录', key: 'logout' }
  79. ]
  80. const menuHeaderDropdown = (
  81. <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick} items={menuItems} />
  82. )
  83. return (
  84. <HeaderDropdown overlay={menuHeaderDropdown} destroyPopupOnHide>
  85. <span className={`${styles.action} ${styles.account}`}>
  86. <Avatar
  87. size="small"
  88. className={styles.avatar}
  89. src={
  90. (isDevMode() ? 'http://cld2qa.com' : 'window.location.origin') +
  91. (currentUser?.avatar ?? consts.DEFAULT_AVATAR)
  92. }
  93. alt="avatar"
  94. />
  95. <span className={`${styles.name} anticon`}>{currentUser.username}</span>
  96. </span>
  97. </HeaderDropdown>
  98. )
  99. }
  100. export default AvatarDropdown