import React, { useState, useMemo, useEffect } from "react"; import { useLocation } from "react-router-dom"; import { Spin } from "antd"; import HCserviceV3 from "../tmpl/hcservice-v3"; import HCDataSource from "../tmpl/hc-data-source"; import ProgramConfig from "../units"; import { getMenuConfig } from "../layout2/page"; interface Props { serverKey?: string; } const Index = (props: Props) => { const { serverKey } = props; const [loading, setLoading] = useState(true); const { search } = useLocation(); const params = useMemo(() => { const temParams = new URLSearchParams(search); const username = temParams.get("username"); const ssoToken = temParams.get("ssoToken"); const programCode = temParams.get("programCode"); const token = temParams.get("token"); const sourceId = temParams.get("sourceId"); const applicationCode = temParams.get("applicationCode"); return { username, ssoToken, programCode, token, sourceId, applicationCode }; }, [search]); const ssoTokenLogin = async () => { setLoading(true); const { username, ssoToken, programCode, sourceId, applicationCode } = params; const res = await HCserviceV3.ssoTokenLogin({ username, ssoToken }, programCode, applicationCode, serverKey); const { defaultStyleTheme } = await ProgramConfig.getThemeConfig() || {}; if (res.status === "success") { HCDataSource.clear(); ProgramConfig.setHydrocarbonToken(res.token); if (sourceId) { const temData = await getMenuConfig(defaultStyleTheme || "v2"); const menu = ProgramConfig.findItemByIdIterative(temData.menu, sourceId); window.location.hash = menu?.url || `/${defaultStyleTheme || "v2"}/home`; } else { window.location.hash = `/${defaultStyleTheme || "v2"}/home`; } } else if (res.status === "error") { const allThemes = ProgramConfig.getAllThemes(); window.location.hash = `/${allThemes[defaultStyleTheme || "v2"].loginPath}`; } setLoading(false); }; const tokenLogin = async () => { const { token, sourceId } = params; const { defaultStyleTheme } = await ProgramConfig.getThemeConfig() || {}; HCDataSource.clear(); ProgramConfig.setHydrocarbonToken(token); if (sourceId) { const temData = await getMenuConfig(defaultStyleTheme || "v2"); const menu = ProgramConfig.findItemByIdIterative(temData.menu, sourceId); window.location.hash = `/${menu?.url}` || `/${defaultStyleTheme || "v2"}/home`; } else { window.location.hash = `/${defaultStyleTheme || "v2"}/home`; } } useEffect(() => { if (params) { params.token ? tokenLogin() : ssoTokenLogin(); } }, [params]) return ; } export default Index;