fix: 修复详情页空白和海报图片错误
- DetailViewModel 添加 @MainActor 确保状态更新在主线程 - DetailView 消除空白初始状态,ProgressView 作为默认兜底 - 取消时保留加载状态避免页面闪回空白 - 使用 .task(id:) 确保切换条目时任务重新触发 - 海报优先从 JSON-LD image 字段获取,HTML fallback 改用正确选择器 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -93,10 +93,6 @@ enum HTMLParser {
|
||||
let h1 = try doc.select("h1").first()
|
||||
let fullTitle = try h1?.text().trimmingCharacters(in: .whitespacesAndNewlines) ?? "未知标题"
|
||||
|
||||
// 海报
|
||||
let imgSrc = try doc.select("img.w-full.h-full.object-cover").first()?.attr("src") ?? ""
|
||||
let posterURL = URL(string: imgSrc)
|
||||
|
||||
// === 优先从 JSON-LD 提取结构化数据 ===
|
||||
var year = 0
|
||||
var rating: Double?
|
||||
@@ -105,12 +101,17 @@ enum HTMLParser {
|
||||
var genres: [String] = []
|
||||
var description = ""
|
||||
var region = ""
|
||||
var posterURL: URL?
|
||||
|
||||
if let jsonLDScript = try doc.select("script[type=application/ld+json]").first() {
|
||||
let jsonText = try jsonLDScript.data()
|
||||
if let data = jsonText.data(using: .utf8),
|
||||
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
|
||||
|
||||
if let imageStr = json["image"] as? String {
|
||||
posterURL = URL(string: imageStr)
|
||||
}
|
||||
|
||||
if let published = json["datePublished"] as? String {
|
||||
year = Int(published) ?? 0
|
||||
}
|
||||
@@ -140,6 +141,12 @@ enum HTMLParser {
|
||||
|
||||
// === 从 HTML 补充缺失数据 ===
|
||||
|
||||
// 海报 fallback: 主海报在 .flex-shrink-0 容器中
|
||||
if posterURL == nil {
|
||||
let imgSrc = try doc.select(".flex-shrink-0 img.object-cover").first()?.attr("src") ?? ""
|
||||
posterURL = URL(string: imgSrc)
|
||||
}
|
||||
|
||||
// 评分 fallback
|
||||
if rating == nil {
|
||||
let ratingText = try doc.select(".rating-display").text()
|
||||
|
||||
Reference in New Issue
Block a user