diff --git a/packages/core/client/src/application/Application.tsx b/packages/core/client/src/application/Application.tsx index 685d5c5aa3..64996d45dc 100644 --- a/packages/core/client/src/application/Application.tsx +++ b/packages/core/client/src/application/Application.tsx @@ -100,6 +100,7 @@ export class Application { public dataSourceManager: DataSourceManager; public name: string; public globalVars: Record = {}; + public globalVarsCtx: Record = {}; loading = true; maintained = false; @@ -484,6 +485,9 @@ export class Application { addGlobalVar(key: string, value: any) { set(this.globalVars, key, value); } + setGlobalVarCtx(key: string, value: any) { + set(this.globalVarsCtx, key, value); + } getGlobalVar(key) { return get(this.globalVars, key); @@ -491,4 +495,7 @@ export class Application { getGlobalVars() { return this.globalVars; } + getGlobalVarsCtx() { + return this.globalVarsCtx; + } } diff --git a/packages/core/client/src/application/hooks/useGlobalVariable.ts b/packages/core/client/src/application/hooks/useGlobalVariable.ts index 37f281f13c..f09779a602 100644 --- a/packages/core/client/src/application/hooks/useGlobalVariable.ts +++ b/packages/core/client/src/application/hooks/useGlobalVariable.ts @@ -10,6 +10,7 @@ import { isFunction } from 'lodash'; import { useMemo } from 'react'; import { useApp } from './'; +import { VariableOption } from '../../variables/types'; export const useGlobalVariable = (key: string) => { const app = useApp(); @@ -55,3 +56,19 @@ export const useGlobalVariables = () => { return result as any; }; + +//获取全局变量的值 +export const useGlobalVariablesCtx = () => { + const app = useApp(); + const variablesCtx = app.getGlobalVarsCtx(); + const uniqueValues = new Set(); + Object.entries(variablesCtx).forEach(([key, value]) => { + if (!value) return; + uniqueValues.add({ + name: key, + ctx: value, + }); + }); + + return [...uniqueValues] as VariableOption[]; +}; diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index 2c13c69de5..eb3a98936e 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -223,15 +223,18 @@ const VariablesProvider = ({ children, filterVariables }: any) => { [setCtx], ); - const getVariable = useCallback((variableName: string): VariableOption => { - if (!ctxRef.current[variableName]) { - return null; - } + const getVariable = useCallback( + (variableName: string): VariableOption => { + if (!ctxRef.current[variableName] && !builtinVariables.find((v) => v.name === variableName)) { + return null; + } - return { - ...variablesStore[variableName], - }; - }, []); + return { + ...variablesStore[variableName], + }; + }, + [builtinVariables], + ); const removeVariable = useCallback( (variableName: string) => { @@ -338,7 +341,6 @@ const VariablesProvider = ({ children, filterVariables }: any) => { }) as VariablesContextType, [getCollectionField, getVariable, parseVariable, registerVariable, removeVariable, setCtx], ); - return {children}; }; diff --git a/packages/core/client/src/variables/hooks/useBuiltinVariables.ts b/packages/core/client/src/variables/hooks/useBuiltinVariables.ts index c7d59b61ae..d36d01e372 100644 --- a/packages/core/client/src/variables/hooks/useBuiltinVariables.ts +++ b/packages/core/client/src/variables/hooks/useBuiltinVariables.ts @@ -15,6 +15,7 @@ import { useAPITokenVariable } from '../../schema-settings/VariableInput/hooks/u import { useCurrentRoleVariable } from '../../schema-settings/VariableInput/hooks/useRoleVariable'; import { useURLSearchParamsVariable } from '../../schema-settings/VariableInput/hooks/useURLSearchParamsVariable'; import { VariableOption } from '../types'; +import { useGlobalVariablesCtx } from '../../application/hooks/useGlobalVariable'; /** * 相当于全局的变量 @@ -26,6 +27,7 @@ const useBuiltInVariables = () => { const { apiTokenCtx } = useAPITokenVariable(); const { datetimeCtx } = useDatetimeVariable(); const { urlSearchParamsCtx, name: urlSearchParamsName, defaultValue } = useURLSearchParamsVariable(); + const globalVariablesCtx = useGlobalVariablesCtx(); const builtinVariables: VariableOption[] = useMemo(() => { return [ { @@ -88,9 +90,17 @@ const useBuiltInVariables = () => { ctx: urlSearchParamsCtx, defaultValue, }, + ...globalVariablesCtx, ]; - }, [currentRoleCtx, currentUserCtx, datetimeCtx, defaultValue, urlSearchParamsCtx, urlSearchParamsName]); - + }, [ + currentRoleCtx, + currentUserCtx, + datetimeCtx, + defaultValue, + urlSearchParamsCtx, + urlSearchParamsName, + globalVariablesCtx, + ]); return { builtinVariables }; };