import React, { ReactNode, useEffect, useState } from "react"; import { CaretDownOutlined, ClearOutlined, LoginOutlined, LogoutOutlined, MenuFoldOutlined, MenuUnfoldOutlined, SettingOutlined, UnlockOutlined, UserOutlined,ToolOutlined, CompressOutlined, GlobalOutlined, DownloadOutlined } from "@ant-design/icons"; import { Button, ButtonProps, Dropdown, GetProp, MenuProps, Popconfirm } from "antd"; import HcserviceV3 from "../../tmpl/hcservice-v3"; import { UserInfo, ThemeConfig } from "../../tmpl/interface"; import UserInfoBar from "../../layout/menu/userinfo-bar"; import { useLocale } from "../../locale/useLocale"; import { NavLink, useNavigate } from "react-router-dom"; import ResetPassword from "../../layout/menu/reset-password"; import HCDataSource from "../../tmpl/hc-data-source"; import { useSessionStorageState } from 'ahooks'; import { KeepAliveTab } from "../../hooks/use-tabs"; import Units from "../../units"; import ThemeButton from "./theme-button"; import LocaleButton from "../../locale/LocaleButton"; import AliIcon from "../../icon/aliIcon"; type DropDownMenuType = { label: string | number | ReactNode; key: string; icon?: ReactNode; show?: () => boolean; }; interface Props extends ButtonProps { logoutUrl?: string; customRender?: ReactNode; layoutRootPath?: string; themeConfig?: ThemeConfig; } const UserButton: React.FC = (props) => { const { logoutUrl = "/login2", customRender, layoutRootPath = "v2", themeConfig, ...otherProps } = props; // translate const { translate, changeSiderCollapsed, getSiderCollapsed, compactTheme, changeCompactTheme } = useLocale(); const [keepAliveTabs, setKeepAliveTabs] = useSessionStorageState('keepAliveTabs', { defaultValue: [], }); // navigation const navigate = useNavigate(); // 用户配置信息 const [userConfig, setUserConfig] = useState({ id: undefined, name: undefined, }); const initUserConfig = async () => { const userInfo: UserInfo = await Units.getUserInfo(); setUserConfig(userInfo); }; // 初始化侧边栏菜单显隐 const initSiderCollapsed = (hideLeftMenuTheme: string) => { if (!hideLeftMenuTheme || !layoutRootPath) { return; } const temThemes = hideLeftMenuTheme.split(","); const isCollapsed = getSiderCollapsed(); if ((temThemes.includes(layoutRootPath) && !isCollapsed) || (!temThemes.includes(layoutRootPath) && isCollapsed)) { changeSiderCollapsed(); } } // 上下文配置信息 const [globalContext, setGlobalContext] = useState(); const initContext = async () => { let context = await HcserviceV3.getContextSourceValue(); setGlobalContext(context); }; useEffect(() => { initContext(); initUserConfig(); }, []); useEffect(() => { if (themeConfig) { initSiderCollapsed(themeConfig.hideLeftMenuTheme); } }, [themeConfig]); // 用户是否登录 const [isLogin, setIsLogin] = useState(false); useEffect(() => { if (userConfig?.name !== undefined && userConfig?.name !== null) setIsLogin(true); else setIsLogin(false); }, [userConfig]); // logout const logout = async () => { setKeepAliveTabs([]); await HcserviceV3.logout2(); Units.logout(); navigate(logoutUrl); }; // dropdown menu const onClick: MenuProps["onClick"] = ({ key }) => { }; const dropdownMenuList: DropDownMenuType[] = [ { label: {translate("${登录}")}, key: "login", icon: , show: () => !isLogin, }, { label: ( ), key: "userInfo", icon: , show: () => { return isLogin && userConfig?.viewPersonalInfoable; }, }, { label: userConfig?.name, key: "userInfo", icon: , show: () => isLogin && !!customRender, }, { label: {translate("${全局配置}")}, key: "globalContext", icon: , show: () => { return ( isLogin && Array.isArray(globalContext) && globalContext.length > 0 ); }, }, { label: , key: "resetPwd", icon: , show: () => { return isLogin && userConfig.changePasswordable; }, }, { label: ( HCDataSource.reload([])}> {translate("${清空缓存}")} ), key: "cleanCache", icon: , show: () => { return isLogin && userConfig.cleanCachable; }, }, { label: ( e.stopPropagation()}> Units.getPageConfigDefault()} okText={translate("${确定}")} cancelText={translate("${取消}")} okType="danger" > {translate("${我的配置}")} ), key: "userPageConfig", icon: , show: () => isLogin && themeConfig?.allowUserConfig, }, { label: changeCompactTheme()}>{translate(compactTheme ? "${默认主题}" : "${紧凑主题}")}, key: "compactTheme", icon: , show: () => isLogin, }, { label:
{translate("${主题切换}")}
, key: "changeTheme", icon: , show: () => isLogin, }, { label:
{translate("${语言}")}
, key: "changeLocale", icon: , show: () => isLogin, }, { label: ( changeSiderCollapsed()}> {getSiderCollapsed() ? translate("${显示侧边栏}") : translate("${ 隐藏侧边栏 }")} ), key: "collapseSider", icon: {getSiderCollapsed() ? : }, show: () => isLogin, }, { label:{translate("${系统运维}")}, key: "devops", icon: , show: () => isLogin && userConfig?.devops, }, { label:
{translate("${退出}")}
, key: "logout", icon: , show: () => isLogin, }, ]; const cleanDropdownMenuList: GetProp = dropdownMenuList .filter((item) => { if (!item.show) return true; return item.show(); }) .map(({ label, key, icon }) => { return { label, key, icon, }; }); return ( {customRender && isLogin ? customRender : } ); }; export default UserButton;