235 lines
7.7 KiB
HTML
235 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>AI 天气工作流</title>
|
|
<style>
|
|
:root {
|
|
--bg: #f4f6fb;
|
|
--primary: #3b6ef6;
|
|
--text: #1f2937;
|
|
--muted: #6b7280;
|
|
--border: #e5e9f0;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
|
background: linear-gradient(160deg, #eef2ff, #f4f6fb 45%, #e6f7f5);
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 24px;
|
|
color: var(--text);
|
|
}
|
|
.card {
|
|
width: 100%;
|
|
max-width: 600px;
|
|
background: #fff;
|
|
border-radius: 20px;
|
|
box-shadow: 0 20px 60px rgba(31, 45, 90, .12);
|
|
padding: 36px 32px;
|
|
}
|
|
.badge {
|
|
display: inline-block;
|
|
font-size: 12px;
|
|
color: var(--primary);
|
|
background: #eef2ff;
|
|
border: 1px solid #dbe3ff;
|
|
padding: 4px 10px;
|
|
border-radius: 999px;
|
|
margin-bottom: 14px;
|
|
}
|
|
h1 { font-size: 23px; }
|
|
.subtitle { color: var(--muted); font-size: 14px; margin-top: 8px; line-height: 1.6; }
|
|
.form { display: flex; gap: 10px; margin-top: 22px; }
|
|
.form input {
|
|
flex: 1;
|
|
height: 48px;
|
|
border: 1px solid var(--border);
|
|
border-radius: 12px;
|
|
padding: 0 16px;
|
|
font-size: 15px;
|
|
outline: none;
|
|
}
|
|
.form input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59, 110, 246, .15); }
|
|
.form button {
|
|
height: 48px;
|
|
padding: 0 22px;
|
|
border: none;
|
|
border-radius: 12px;
|
|
background: var(--primary);
|
|
color: #fff;
|
|
font-size: 15px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
}
|
|
.form button:disabled { opacity: .6; cursor: not-allowed; }
|
|
.result { margin-top: 24px; display: none; }
|
|
.result.show { display: block; }
|
|
.days { display: flex; gap: 10px; margin-top: 14px; }
|
|
.day {
|
|
flex: 1;
|
|
background: #f8fafc;
|
|
border: 1px solid var(--border);
|
|
border-radius: 12px;
|
|
padding: 14px 12px;
|
|
text-align: center;
|
|
}
|
|
.day .w { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
|
|
.day .ico { font-size: 22px; margin-bottom: 6px; }
|
|
.day .tmp { font-size: 15px; font-weight: 700; }
|
|
.day .range { font-size: 12px; color: var(--muted); margin-top: 4px; }
|
|
.link {
|
|
margin-top: 18px;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
color: #fff;
|
|
background: #10b981;
|
|
text-decoration: none;
|
|
padding: 12px 20px;
|
|
border-radius: 12px;
|
|
font-weight: 600;
|
|
font-size: 15px;
|
|
}
|
|
.link:hover { background: #059669; }
|
|
.err {
|
|
margin-top: 18px;
|
|
color: #c0392b;
|
|
background: #fdf2f2;
|
|
border: 1px solid #f5c6c6;
|
|
border-radius: 12px;
|
|
padding: 14px;
|
|
font-size: 14px;
|
|
display: none;
|
|
}
|
|
.load { margin-top: 18px; color: var(--muted); font-size: 14px; display: none; }
|
|
.spin {
|
|
display: inline-block;
|
|
width: 14px;
|
|
height: 14px;
|
|
border: 2px solid #c7d2fe;
|
|
border-top-color: var(--primary);
|
|
border-radius: 50%;
|
|
animation: s .7s linear infinite;
|
|
vertical-align: -2px;
|
|
margin-right: 8px;
|
|
}
|
|
@keyframes s { to { transform: rotate(360deg); } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main class="card">
|
|
<span class="badge">工作流:查天气 → 生成曲线图</span>
|
|
<h1>未来3天天气曲线</h1>
|
|
<p class="subtitle">输入城市提交后,系统自动查询未来3天天气并生成温度变化曲线图。</p>
|
|
<form class="form" id="form">
|
|
<input id="city" placeholder="例如:北京 / 上海 / 深圳" autocomplete="off">
|
|
<button id="btn" type="submit">提交</button>
|
|
</form>
|
|
<div class="load" id="load"><span class="spin"></span>正在查询并生成曲线图…</div>
|
|
<div class="err" id="err"></div>
|
|
<div class="result" id="result"></div>
|
|
</main>
|
|
<script>
|
|
const ICO = {
|
|
晴: '☀️', 大致晴朗: '🌤️', 多云: '⛅', 阴: '☁️',
|
|
雾: '🌫️', 雾凇: '🌫️', 小毛毛雨: '🌦️', 毛毛雨: '🌦️',
|
|
强毛毛雨: '🌧️', 小雨: '🌧️', 中雨: '🌧️', 大雨: '🌧️',
|
|
冻雨: '🌧️', 小雪: '🌨️', 中雪: '🌨️', 大雪: '❄️',
|
|
小阵雨: '🌦️', 中阵雨: '🌧️', 强阵雨: '⛈️', 小阵雪: '🌨️',
|
|
大阵雪: '❄️', 雷雨: '⛈️', 雷雨伴冰雹: '⛈️', 强雷雨伴冰雹: '⛈️',
|
|
未知: '🌡️'
|
|
};
|
|
|
|
const form = document.getElementById('form');
|
|
const city = document.getElementById('city');
|
|
const btn = document.getElementById('btn');
|
|
const load = document.getElementById('load');
|
|
const err = document.getElementById('err');
|
|
const result = document.getElementById('result');
|
|
|
|
let summary = null;
|
|
let chartUrl = null;
|
|
|
|
function render() {
|
|
if (!summary && !chartUrl) return;
|
|
let h = '';
|
|
if (summary) {
|
|
h += '<div class="days">' + summary.summary.map(s =>
|
|
`<div class="day"><div class="w">${s.week} ${s.date.slice(5)}</div><div class="ico">${ICO[s.weather] || '🌡️'}</div><div class="tmp">${s.weather}</div><div class="range">${s.min}° ~ ${s.max}°</div></div>`
|
|
).join('') + '</div>';
|
|
}
|
|
if (chartUrl) {
|
|
h += '<a class="link" href="' + chartUrl + '" target="_blank">查看温度变化曲线图 →</a>';
|
|
}
|
|
result.innerHTML = h;
|
|
result.classList.add('show');
|
|
}
|
|
|
|
form.addEventListener('submit', async e => {
|
|
e.preventDefault();
|
|
const c = city.value.trim();
|
|
if (!c) {
|
|
err.textContent = '请输入城市名称';
|
|
err.style.display = 'block';
|
|
return;
|
|
}
|
|
btn.disabled = true;
|
|
err.style.display = 'none';
|
|
result.classList.remove('show');
|
|
result.innerHTML = '';
|
|
summary = null;
|
|
chartUrl = null;
|
|
load.style.display = 'block';
|
|
|
|
try {
|
|
const r = await fetch('/api/weather', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ city: c })
|
|
});
|
|
if (!r.ok) throw new Error('请求失败 HTTP ' + r.status);
|
|
|
|
const reader = r.body.getReader();
|
|
const dec = new TextDecoder();
|
|
let buf = '';
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
buf += dec.decode(value, { stream: true });
|
|
let idx;
|
|
while ((idx = buf.indexOf('\n\n')) >= 0) {
|
|
const block = buf.slice(0, idx);
|
|
buf = buf.slice(idx + 2);
|
|
let ev = 'message';
|
|
let data = '';
|
|
for (const line of block.split('\n')) {
|
|
if (line.startsWith('event:')) ev = line.slice(6).trim();
|
|
else if (line.startsWith('data:')) data += line.slice(5).trim();
|
|
}
|
|
if (ev === 'meta') {
|
|
try { load.innerHTML = '<span class="spin"></span>' + JSON.parse(data).message; } catch {}
|
|
} else if (ev === 'summary') {
|
|
try { summary = JSON.parse(data); render(); } catch {}
|
|
} else if (ev === 'chart') {
|
|
try { chartUrl = JSON.parse(data).chartUrl; render(); } catch {}
|
|
} else if (ev === 'error') {
|
|
try { err.textContent = '出错了:' + JSON.parse(data).message; err.style.display = 'block'; } catch {}
|
|
}
|
|
}
|
|
}
|
|
} catch (e2) {
|
|
err.textContent = '出错了:' + e2.message;
|
|
err.style.display = 'block';
|
|
} finally {
|
|
btn.disabled = false;
|
|
load.style.display = 'none';
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|