refactor: parse(content,pageKey)替代URL路由,新增resolvePageKey
This commit is contained in:
@@ -27,6 +27,7 @@ public class AdapterController {
|
||||
private final AccessResultMapper accessResultMapper;
|
||||
private final CachedStorageService storageService;
|
||||
private final com.par.core.mapper.SiteMapper siteMapper;
|
||||
private final com.par.core.service.SitePagesService sitePagesService;
|
||||
|
||||
/**
|
||||
* 用指定适配配置解析指定访问结果
|
||||
@@ -61,7 +62,8 @@ public class AdapterController {
|
||||
catch (Exception e) { return ApiResponse.error(500, "读取内容失败: " + e.getMessage()); }
|
||||
if (content == null) return ApiResponse.error(404, "内容为空");
|
||||
|
||||
return ApiResponse.success(groovyEngine.execute(script, content, accessResult.getUrl()));
|
||||
String pageKey = resolvePageKey(siteConfig.getSiteId(), accessResult.getUrl());
|
||||
return ApiResponse.success(groovyEngine.execute(script, content, accessResult.getUrl(), pageKey));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,7 +137,9 @@ public class AdapterController {
|
||||
if (content == null) {
|
||||
item.put("parsed", Map.of("matched", false, "message", "内容为空"));
|
||||
} else {
|
||||
item.put("parsed", groovyEngine.execute(script, content, url));
|
||||
String pageKey = resolvePageKey(siteConfig.getSiteId(), url);
|
||||
item.put("pageKey", pageKey);
|
||||
item.put("parsed", groovyEngine.execute(script, content, url, pageKey));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -156,4 +160,29 @@ public class AdapterController {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 根据页面配置的 URL 模板反查出 pageKey(如 sync/signIn/search) */
|
||||
@SuppressWarnings("unchecked")
|
||||
private String resolvePageKey(String siteId, String url) {
|
||||
if (url == null || url.isBlank()) return null;
|
||||
try {
|
||||
Map<String, Object> pages = sitePagesService.getPages(siteId);
|
||||
Object entries = pages != null ? pages.get("entries") : null;
|
||||
if (entries instanceof List) {
|
||||
String path = url.replaceAll("^https?://[^/]+", "").replaceAll("\\?.*", "");
|
||||
for (Object e : (List<?>) entries) {
|
||||
if (e instanceof Map) {
|
||||
Map<String, Object> entry = (Map<String, Object>) e;
|
||||
String entryUrl = (String) entry.get("url");
|
||||
if (entryUrl == null) continue;
|
||||
String entryPath = entryUrl.replaceAll("\\?.*", "").replaceAll("\\{.*?\\}", "");
|
||||
if (path.trim().equals(entryPath.trim()) || path.contains(entryPath)) {
|
||||
return (String) entry.get("pageKey");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ public class SitePagesController {
|
||||
|
||||
// 3. 用 Groovy 脚本解析内容
|
||||
try {
|
||||
Map<String, Object> parsed = groovyEngine.execute(script, content, url);
|
||||
Map<String, Object> parsed = groovyEngine.execute(script, content, url, pageKey);
|
||||
boolean matched = Boolean.TRUE.equals(parsed.get("matched"));
|
||||
result.put("parsed", parsed);
|
||||
result.put("matched", matched);
|
||||
|
||||
Reference in New Issue
Block a user