AvatarDropdown.tsx 3.1 KB

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