AvatarDropdown.tsx 3.1 KB

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