增加调用函数的方法。

This commit is contained in:
mxd
2021-08-30 22:37:08 +08:00
parent 401033f74b
commit 0d65ae93fd
3 changed files with 27 additions and 2 deletions

View File

@@ -136,6 +136,10 @@ public class MagicFunctionManager {
return mappings.values().stream().distinct().collect(Collectors.toList());
}
public FunctionInfo getFunctionInfo(String path) {
return mappings.get(path);
}
private boolean hasConflict(TreeNode<Group> group, String newPath) {
// 获取要移动的接口
List<FunctionInfo> infos = mappings.values().stream()

View File

@@ -19,7 +19,7 @@ public interface MagicAPIService extends MagicModule {
*
* @param method 请求方法
* @param path 请求路径
* @param context 请求上下文,主要给脚本中使用
* @param context 变量信息
*/
Object execute(String method, String path, Map<String, Object> context);
@@ -28,10 +28,18 @@ public interface MagicAPIService extends MagicModule {
*
* @param method 请求方法
* @param path 请求路径
* @param context 请求上下文,主要给脚本中使用
* @param context 变量信息
*/
Object call(String method, String path, Map<String, Object> context);
/**
* 执行MagicAPI中的函数
*
* @param path 函数路径
* @param context 变量信息
*/
Object invoke(String path, Map<String, Object> context);
/**
* 保存接口
*

View File

@@ -180,6 +180,19 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
}
}
@Override
public Object invoke(String path, Map<String, Object> context) {
FunctionInfo functionInfo = magicFunctionManager.getFunctionInfo(path);
if (functionInfo == null) {
throw new MagicServiceException(String.format("找不到对应函数 [%s]", path));
}
MagicScriptContext scriptContext = new MagicScriptContext(context);
scriptContext.putMapIntoContext(context);
SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
simpleScriptContext.setAttribute(MagicScript.CONTEXT_ROOT, scriptContext, ScriptContext.ENGINE_SCOPE);
return ((MagicScript) ScriptManager.compile("MagicScript", functionInfo.getScript())).eval(simpleScriptContext);
}
@Override
public String saveApi(ApiInfo info) {
// 非空验证