diff --git a/par-api/src/main/java/com/par/api/controller/ConfigController.java b/par-api/src/main/java/com/par/api/controller/ConfigController.java index 21b4073..cebc39d 100644 --- a/par-api/src/main/java/com/par/api/controller/ConfigController.java +++ b/par-api/src/main/java/com/par/api/controller/ConfigController.java @@ -154,8 +154,11 @@ public class ConfigController { String schemaVersion = (String) payload.get("schemaVersion"); String groovyScript = (String) payload.get("script"); - if (version == null || configJson == null) { - return ApiResponse.error(400, "version and config are required"); + if (version == null) { + return ApiResponse.error(400, "version is required"); + } + if ((configJson == null || configJson.isBlank()) && (groovyScript == null || groovyScript.isBlank())) { + return ApiResponse.error(400, "config 或 script 至少需要一个"); } Long submitterId = (Long) request.getAttribute("accountId"); diff --git a/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java b/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java index d9a349a..aa3e9be 100644 --- a/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java +++ b/par-core/src/main/java/com/par/core/service/impl/ConfigServiceImpl.java @@ -56,8 +56,10 @@ public class ConfigServiceImpl implements ConfigService { @Override public SiteConfig submitConfig(String siteId, String version, String configJson, String schemaVersion, String groovyScript, Long submitterId) { - String configHash = HmacUtil.sha256(configJson); - String storagePath = String.format("configs/%s/pending/%s.json", siteId, configHash.substring(0, 8)); + boolean hasConfig = configJson != null && !configJson.isBlank(); + boolean hasScript = groovyScript != null && !groovyScript.isBlank(); + String configHash = hasConfig ? HmacUtil.sha256(configJson) : HmacUtil.sha256(groovyScript); + String idPrefix = hasConfig ? configHash.substring(0, 8) : Long.toHexString(System.currentTimeMillis()); // 相同 hash 的提交 → 真正重复,跳过 SiteConfig sameHash = siteConfigMapper.selectOne( @@ -70,18 +72,21 @@ public class ConfigServiceImpl implements ConfigService { return sameHash; } - // 存储配置 - try { - storageService.store(storagePath, configJson); - } catch (Exception e) { - log.error("存储配置文件失败 siteId={} version={}: {}", siteId, version, e.getMessage()); - throw new RuntimeException("存储配置文件失败: " + e.getMessage(), e); + // 存储 JSON 配置(可选) + String storagePath = null; + if (hasConfig) { + storagePath = String.format("configs/%s/pending/%s.json", siteId, idPrefix); + try { storageService.store(storagePath, configJson); } + catch (Exception e) { + log.error("存储配置文件失败 siteId={} version={}: {}", siteId, version, e.getMessage()); + throw new RuntimeException("存储配置文件失败: " + e.getMessage(), e); + } } - // 存储 Groovy 脚本(可选) + // 存储 Groovy 脚本 String scriptPath = null; - if (groovyScript != null && !groovyScript.isBlank()) { - scriptPath = String.format("configs/%s/pending/%s.groovy", siteId, configHash.substring(0, 8)); + if (hasScript) { + scriptPath = String.format("configs/%s/pending/%s.groovy", siteId, idPrefix); storageService.store(scriptPath, groovyScript); }