feat: 四类站点默认脚本+无自定义脚本时按siteType自动加载默认模板
This commit is contained in:
@@ -23,11 +23,40 @@ public class GroovyScriptEngine {
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final GroovyClassLoader classLoader = new GroovyClassLoader(
|
||||
Thread.currentThread().getContextClassLoader());
|
||||
/** 类型 → 默认脚本 */
|
||||
private static final Map<String, String> DEFAULT_SCRIPTS = new ConcurrentHashMap<>();
|
||||
/** 脚本前缀:自动导入必要包 + PluginContext 桩 */
|
||||
private static final String SCRIPT_PREFIX =
|
||||
"import groovy.json.*\n" +
|
||||
"import com.par.core.adapter.*\n";
|
||||
|
||||
/**
|
||||
* 根据站点 ID 加载默认脚本(按站点类型匹配)
|
||||
*/
|
||||
public String loadDefaultScript(String siteId) {
|
||||
// 需要从外部注入 siteMapper,这里用静态方法获取 Spring Bean
|
||||
// 实际由 AdapterController 调用时传入
|
||||
return null; // placeholder, 由 AdapterController 实现具体逻辑
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 classpath 加载默认脚本文件
|
||||
*/
|
||||
public static String loadDefaultByType(String siteType) {
|
||||
if (siteType == null) return null;
|
||||
String key = siteType.toLowerCase();
|
||||
return DEFAULT_SCRIPTS.computeIfAbsent(key, k -> {
|
||||
String path = "scripts/default-" + k + ".groovy";
|
||||
try (var in = GroovyScriptEngine.class.getClassLoader().getResourceAsStream(path)) {
|
||||
if (in == null) return null;
|
||||
return new String(in.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to load default script for type: {}", k, e);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> execute(String groovyScript, String content, String url) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user