AvatarDropdown.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
  2. import { CloseOutlined, LogoutOutlined, SwapOutlined, SwapRightOutlined, UserOutlined } from '@ant-design/icons';
  3. import { Avatar, MenuProps } from 'antd';
  4. import globaStore from '../store/index'
  5. import HeaderDropdown from '../components/HeaderDropdown';
  6. import styles from './index.less';
  7. import { toJS } from 'mobx';
  8. import { useLocalObservable, observer } from 'mobx-react'
  9. import useNewToken from '@/Hook/useNewToken';
  10. export interface GlobalHeaderRightProps {
  11. config: any,
  12. }
  13. const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ config }) => {
  14. const store = useLocalObservable(() => globaStore)
  15. const [dialogVisible, setDialogVisible] = useState<boolean>(false)
  16. const [dOpen, setDopen] = useState(false)
  17. const ref1 = useRef(null);
  18. const [openTour, setOpenTour] = useState<boolean>(false);
  19. const { token } = useNewToken()
  20. const userInfo: any = useMemo(() => {
  21. return toJS(store.data.userInfo)
  22. }, [store.data.userInfo])
  23. const onMenuClick = useCallback(
  24. (event: any) => {
  25. const { key } = event;
  26. if (key === 'logout') {
  27. store.loginOut();
  28. return;
  29. }
  30. if (key === 'company') {
  31. setDialogVisible(() => true)
  32. return
  33. }
  34. },
  35. [],
  36. );
  37. const items: MenuProps['items'] = [
  38. {
  39. icon: <SwapOutlined />,
  40. key: 'company',
  41. label: '切换公司'
  42. },
  43. {
  44. icon: <LogoutOutlined />,
  45. key: 'logout',
  46. label: '退出登录'
  47. }]
  48. //是否首次打开平台
  49. useEffect(() => {
  50. let isOneOpen = localStorage.getItem('isOneOpen')
  51. let isPhone = sessionStorage.getItem('isPhone')
  52. if (!isOneOpen && !openTour && !isPhone) {
  53. setOpenTour(true)
  54. }
  55. }, [])
  56. //新增字体样式
  57. function addStyle() {
  58. const styleElement = document.createElement('style');
  59. styleElement.textContent = '* { font-family: cursive !important; }';
  60. styleElement.id = 'fontfamilystyle'
  61. document.head.appendChild(styleElement);
  62. }
  63. //删除字体样式
  64. function removeStyle() {
  65. const styleElement = document.getElementById('fontfamilystyle');
  66. if (styleElement) {
  67. document.head.removeChild(styleElement);
  68. }
  69. }
  70. useEffect(() => {
  71. console.log("config.fontfamilystyle===>", config.fontfamilystyle)
  72. if (config.fontfamilystyle) {
  73. addStyle()
  74. } else {
  75. removeStyle()
  76. }
  77. }, [config.fontfamilystyle])
  78. return (
  79. <>
  80. <HeaderDropdown menu={{ items, onClick: onMenuClick }} overlayStyle={{ background: token.colortRansparency, borderRadius: token.borderRadius }} onOpenChange={(open) => {
  81. setDopen(open)
  82. }} open={dOpen} >
  83. <span className={`${styles.action} ${styles.account}`} ref={ref1}>
  84. <Avatar className={styles.avatar} src={userInfo?.dingTalkUserList?.[0]?.avatar || config?.myBgUrl || config?.bgUrl[config.acPrimary]} style={{ backgroundColor: '#fde3cf', color: '#f56a00' }} icon={<UserOutlined />} alt="avatar" />
  85. <span className={`${styles.name} anticon`} style={localStorage.getItem("showGx") != '1' ? { color: '#fff' } : { color: '#fff' }}>{userInfo?.nickname}</span>
  86. </span>
  87. </HeaderDropdown>
  88. <div className={styles.dialog} style={{ display: dialogVisible ? 'block' : 'none' }}>
  89. <div className={styles.companyAccount}>
  90. <div className={styles.close} onClick={() => { setDialogVisible(false) }}><CloseOutlined style={{ color: '#000' }} /></div>
  91. <h3>切换公司账户</h3>
  92. <div className={styles.chooseTableBlock}>
  93. {toJS(store.data.companyList)?.map((item: any) => {
  94. if (item.companyId != store.data.userInfo?.onlineCompanyId) {
  95. return <div className={`${styles.acTableLineContent} ${styles.acTableLine}`} key={item?.companyId} onClick={() => { store.selectCompany(item?.companyId) }}>
  96. <div className={styles.actname}>{item?.companyInfo?.companyName}</div>
  97. <div className={styles.right}> <div className={styles.actcha}>{item?.powerLevel === 999 ? '超级管理员' : item?.powerLevel === 100 ? '系统管理员' : item?.powerLevel === 99 ? '管理员' : '普通用户'}</div> <SwapRightOutlined className={styles.iconRight} /></div>
  98. </div>
  99. } else {
  100. return <div className={`${styles.acTableLineContent} ${styles.acNone}`} key={item?.companyId}>
  101. <div className={styles.actname}>{item?.companyInfo?.companyName}</div>
  102. <div className={styles.right}> <div className={styles.actcha}>{item?.powerLevel === 999 ? '超级管理员' : item?.powerLevel === 100 ? '系统管理员' : item?.powerLevel === 99 ? '管理员' : '普通用户'}</div> <SwapRightOutlined className={styles.iconRight} /></div>
  103. </div>
  104. }
  105. })}
  106. </div>
  107. </div>
  108. </div>
  109. </ >
  110. );
  111. };
  112. export default observer(AvatarDropdown);