feat: implement ScanTerminalDashboard component and associated CSS module for PolyWeather map interface

This commit is contained in:
2569718930@qq.com
2026-04-26 01:55:30 +08:00
parent fbacd24f33
commit e6dbef1ece
7 changed files with 546 additions and 22 deletions
@@ -0,0 +1,77 @@
import { NextRequest, NextResponse } from "next/server";
import {
applyAuthResponseCookies,
buildBackendRequestHeaders,
} from "@/lib/backend-auth";
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
const SCAN_AI_PROXY_TIMEOUT_MS = Math.max(
35_000,
Number(process.env.POLYWEATHER_SCAN_AI_PROXY_TIMEOUT_MS || "45000") || 45_000,
);
export const dynamic = "force-dynamic";
export const maxDuration = 60;
export async function POST(req: NextRequest) {
if (!API_BASE) {
return NextResponse.json(
{ error: "POLYWEATHER_API_BASE_URL is not configured" },
{ status: 500 },
);
}
let body: unknown = {};
try {
body = await req.json();
} catch {
body = {};
}
let auth: Awaited<ReturnType<typeof buildBackendRequestHeaders>> | null = null;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), SCAN_AI_PROXY_TIMEOUT_MS);
try {
auth = await buildBackendRequestHeaders(req);
const headers = new Headers(auth.headers);
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
const res = await fetch(`${API_BASE}/api/scan/terminal/ai-city`, {
method: "POST",
headers,
cache: "no-store",
signal: controller.signal,
body: JSON.stringify(body || {}),
});
if (!res.ok) {
const raw = await res.text();
const response = NextResponse.json(
{ error: `Backend returned ${res.status}`, detail: raw.slice(0, 300) },
{ status: res.status === 402 || res.status === 403 ? res.status : 502 },
);
return applyAuthResponseCookies(response, auth.response);
}
const data = await res.json();
const response = NextResponse.json(data, {
headers: {
"Cache-Control": "no-store",
},
});
return applyAuthResponseCookies(response, auth.response);
} catch (error) {
const timedOut = controller.signal.aborted;
const response = NextResponse.json(
{
error: timedOut
? "City AI request timed out"
: "Failed to fetch city AI data",
detail: String(error),
},
{ status: timedOut ? 504 : 500 },
);
return auth ? applyAuthResponseCookies(response, auth.response) : response;
} finally {
clearTimeout(timeoutId);
}
}
@@ -11235,6 +11235,42 @@
line-height: 1.6;
}
.root :global(.scan-ai-weather-summary) {
color: #e6edf3;
font-weight: 850;
}
.root :global(.scan-ai-weather-bullets) {
display: grid;
gap: 8px;
margin: 10px 0 0;
padding-left: 18px;
color: #9fb2c7;
font-size: 13px;
font-weight: 700;
line-height: 1.6;
}
.root :global(.scan-ai-weather-bullets li::marker) {
color: #4da3ff;
}
.root :global(.scan-ai-raw-metar) {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid rgba(77, 163, 255, 0.12);
color: #6b7a90;
font-family:
ui-monospace,
SFMono-Regular,
Menlo,
Monaco,
Consolas,
"Liberation Mono",
monospace;
font-size: 12px;
}
.root :global(.scan-ai-city-chart) {
height: 260px;
}
@@ -12948,7 +12984,8 @@
.root :global(.scan-terminal.light .scan-ai-city-hero-side > strong),
.root :global(.scan-terminal.light .scan-ai-decision-band strong),
.root :global(.scan-terminal.light .scan-ai-decision-metrics b),
.root :global(.scan-terminal.light .scan-ai-market-bucket strong) {
.root :global(.scan-terminal.light .scan-ai-market-bucket strong),
.root :global(.scan-terminal.light .scan-ai-weather-summary) {
color: #0f172a;
}
@@ -12960,10 +12997,16 @@
.root :global(.scan-terminal.light .scan-ai-market-bucket span),
.root :global(.scan-terminal.light .scan-ai-city-muted),
.root :global(.scan-terminal.light .scan-ai-city-loading),
.root :global(.scan-terminal.light .scan-ai-city-chart-legend) {
.root :global(.scan-terminal.light .scan-ai-city-chart-legend),
.root :global(.scan-terminal.light .scan-ai-weather-bullets) {
color: #475569;
}
.root :global(.scan-terminal.light .scan-ai-raw-metar) {
border-top-color: #e2e8f0;
color: #64748b;
}
.root :global(.scan-terminal.light .scan-ai-log-panel) {
border-color: rgba(35, 72, 118, 0.12);
background: linear-gradient(180deg, rgba(255, 255, 255, 0.8), rgba(246, 250, 255, 0.9));
@@ -60,6 +60,34 @@ type AiPinnedCity = {
displayName?: string | null;
addedAt: number;
};
type AiCityForecastPayload = {
status?: string | null;
reason?: string | null;
model?: string | null;
provider?: string | null;
city_forecast?: {
predicted_max?: number | string | null;
range_low?: number | string | null;
range_high?: number | string | null;
unit?: string | null;
confidence?: string | null;
final_judgment_zh?: string | null;
final_judgment_en?: string | null;
metar_read_zh?: string | null;
metar_read_en?: string | null;
reasoning_zh?: string | null;
reasoning_en?: string | null;
risks_zh?: string[] | null;
risks_en?: string[] | null;
model_cluster_note_zh?: string | null;
model_cluster_note_en?: string | null;
} | null;
};
type AiCityForecastState = {
status: "idle" | "loading" | "ready" | "failed";
payload?: AiCityForecastPayload | null;
error?: string | null;
};
function formatShortDate(value?: string | null, locale = "zh-CN") {
const text = String(value || "").trim();
@@ -583,11 +611,85 @@ function AiPinnedCityCard({
? "Waiting for intraday observations to compare against the DEB path."
: "等待更多日内实测,用来对照 DEB 预测路径。");
const report = detail?.current?.raw_metar || detail?.airport_current?.raw_metar || "";
const weatherLine =
detail?.current?.wx_desc ||
detail?.airport_current?.wx_desc ||
detail?.airport_primary?.wx_desc ||
const airportStation =
detail?.risk?.icao ||
detail?.current?.station_code ||
detail?.airport_current?.station_code ||
detail?.airport_primary?.station_code ||
"";
const [aiForecast, setAiForecast] = useState<AiCityForecastState>({
status: "idle",
});
const detailCityName = detail?.name || item.cityName;
const aiForecastKey = detail
? `${normalizeCityKey(detailCityName)}:${detail.local_date || ""}:${report || ""}`
: "";
useEffect(() => {
if (!aiForecastKey || collapsed) return;
let cancelled = false;
setAiForecast({ status: "loading" });
fetch("/api/scan/terminal/ai-city", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
cache: "no-store",
body: JSON.stringify({
city: detailCityName,
force_refresh: false,
}),
})
.then(async (response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json() as Promise<AiCityForecastPayload>;
})
.then((payload) => {
if (!cancelled) {
setAiForecast({ payload, status: "ready" });
}
})
.catch((error) => {
if (!cancelled) {
setAiForecast({ error: String(error), status: "failed" });
}
});
return () => {
cancelled = true;
};
}, [aiForecastKey, collapsed, detailCityName]);
const aiCityForecast = aiForecast.payload?.city_forecast || null;
const localizedFinalJudgment =
(isEn ? aiCityForecast?.final_judgment_en : aiCityForecast?.final_judgment_zh) ||
(isEn ? aiCityForecast?.reasoning_en : aiCityForecast?.reasoning_zh) ||
"";
const localizedMetarRead =
(isEn ? aiCityForecast?.metar_read_en : aiCityForecast?.metar_read_zh) ||
"";
const localizedReasoning =
(isEn ? aiCityForecast?.reasoning_en : aiCityForecast?.reasoning_zh) ||
"";
const localizedModelNote =
(isEn
? aiCityForecast?.model_cluster_note_en
: aiCityForecast?.model_cluster_note_zh) || "";
const localizedRisksRaw =
(isEn ? aiCityForecast?.risks_en : aiCityForecast?.risks_zh) || [];
const localizedRisks = Array.isArray(localizedRisksRaw)
? localizedRisksRaw
: localizedRisksRaw
? [String(localizedRisksRaw)]
: [];
const aiBullets = [
localizedMetarRead,
localizedReasoning !== localizedFinalJudgment ? localizedReasoning : "",
localizedModelNote,
...localizedRisks,
].filter((line) => String(line || "").trim());
const collapseId = `ai-city-body-${normalizeCityKey(item.cityName) || item.addedAt}`;
@@ -687,17 +789,54 @@ function AiPinnedCityCard({
<AiCityTemperatureChart detail={detail} />
<section className="scan-ai-city-section">
<div className="scan-ai-city-section-title">
{isEn ? "Airport / climate read" : "机场报文与天气解读"}
{isEn ? "AI airport weather read" : "AI 机场报文解读"}
</div>
<p>{weatherLine || (isEn ? "No weather text decoded yet." : "暂无天气文本解读。")}</p>
<p>
{report
? `${detail.risk?.icao || detail.current?.station_code || ""} ${report}`.trim()
: isEn
? "No raw METAR available."
: "暂无原始 METAR 报文。"}
</p>
<p>{paceView?.kicker || ""}</p>
{aiForecast.status === "loading" ? (
<p>
{isEn
? "Deepseek V4 flash is reading the latest airport bulletin..."
: "Deepseek V4 flash 正在解读最新机场报文..."}
</p>
) : aiForecast.status === "ready" && aiCityForecast ? (
<>
<p className="scan-ai-weather-summary">
{localizedFinalJudgment ||
(isEn ? "AI read returned without a final sentence." : "AI 已返回,但缺少最终判断。")}
</p>
<ul className="scan-ai-weather-bullets">
{aiBullets.map((line, index) => (
<li key={`${line}-${index}`}>{line}</li>
))}
</ul>
<p className="scan-ai-raw-metar">
{report
? `${isEn ? "Raw METAR" : "原始 METAR"}${`${airportStation} ${report}`.trim()}`
: isEn
? "Raw METAR: unavailable."
: "原始 METAR:暂无。"}
</p>
</>
) : aiForecast.status === "ready" ? (
<p>
{aiForecast.payload?.reason ||
(isEn
? "AI read is unavailable for this city right now."
: "该城市暂时没有可用的 AI 解读。")}
</p>
) : aiForecast.status === "failed" ? (
<p>
{isEn
? "AI read failed. The raw METAR remains below as fallback context."
: "AI 解读失败。下方仅保留原始 METAR 作为兜底上下文。"}
{aiForecast.error ? ` ${aiForecast.error}` : ""}
</p>
) : (
<p>
{isEn
? "Waiting for AI to read the latest airport bulletin."
: "等待 AI 解读最新机场报文。"}
</p>
)}
</section>
</div>
@@ -56,13 +56,13 @@ type UnlockProOverlayProps = {
const FEATURES = {
"zh-CN": [
"市场扫描台 + V4-Flash 深度复核",
"市场扫描台 + V4-Pro 深度复核",
"今日日内机场报文规则分析(含高温时段)",
"历史对账 + 未来日期分析",
"全平台智能气象推送",
],
"en-US": [
"Market Scan Terminal + V4-Flash review",
"Market Scan Terminal + V4-Pro review",
"Intraday METAR rule-based analysis with peak-time window",
"Historical reconciliation + future-date analysis",
"Cross-platform alerts",