index.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>天气查询</title>
  7. <style>
  8. /* 全局样式 - 从风格参考继承 */
  9. * {
  10. color: var(--secondary-font-color);
  11. font-family: "Courier New", Courier, monospace;
  12. box-sizing: border-box;
  13. transition: all 0.2s ease;
  14. }
  15. ::selection {
  16. background: var(--accent-bg-color);
  17. color: var(--accent-font-color);
  18. }
  19. :root,
  20. body {
  21. height: 100%;
  22. margin: 0;
  23. background: var(--content-bg-color);
  24. padding: var(--margin-padding);
  25. --accent-color: #64ffda; /* 强调色:边框 */
  26. --secondary-color: #00695c; /* 次要色:未激活边框 */
  27. --accent-bg-color: #009688;
  28. --secondary-bg-color: #616161;
  29. --content-bg-color: #424242;
  30. --accent-font-color: #f5f5f5;
  31. --secondary-font-color: #eeeeee;
  32. --margin-padding: 0.5rem;
  33. --border-thickness: 0.2rem;
  34. --border-radius: 0.5rem;
  35. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  36. }
  37. body {
  38. max-width: 800px;
  39. margin: 0 auto;
  40. padding: 20px;
  41. }
  42. .container {
  43. display: flex;
  44. flex-direction: column;
  45. gap: 20px;
  46. padding-bottom: var(--margin-padding);
  47. }
  48. .search-box {
  49. display: flex;
  50. gap: 10px;
  51. flex-wrap: wrap;
  52. }
  53. input {
  54. padding: 8px;
  55. flex-grow: 1;
  56. background: var(--secondary-bg-color);
  57. border: var(--border-thickness) solid var(--secondary-color);
  58. border-radius: var(--border-radius);
  59. color: var(--secondary-font-color);
  60. font-size: 1rem;
  61. outline: none;
  62. }
  63. input:focus {
  64. border-color: var(--accent-color);
  65. }
  66. button {
  67. padding: 8px 16px;
  68. background: var(--accent-bg-color);
  69. color: var(--accent-font-color);
  70. border: none;
  71. border-radius: var(--border-radius);
  72. cursor: pointer;
  73. font-weight: bold;
  74. }
  75. button:hover {
  76. opacity: 0.9;
  77. transform: translateY(-2px);
  78. box-shadow: var(--shadow);
  79. }
  80. .heng {
  81. display: flex;
  82. gap: 20px;
  83. flex-wrap: wrap;
  84. }
  85. .weather-info {
  86. background: var(--secondary-bg-color);
  87. border-radius: var(--border-radius);
  88. padding: 20px;
  89. box-shadow: var(--shadow);
  90. overflow: hidden;
  91. }
  92. #graph {
  93. flex: 1;
  94. }
  95. .weather-terminal-info {
  96. flex: 1;
  97. min-height: 512px;
  98. background: black;
  99. border-radius: var(--border-radius);
  100. box-shadow: var(--shadow);
  101. overflow: hidden;
  102. iframe {
  103. height: 100%;
  104. width: 100%;
  105. border: none;
  106. }
  107. }
  108. .weather-detail {
  109. margin-bottom: 10px;
  110. padding-bottom: 8px;
  111. border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  112. }
  113. .weather-detail:last-child {
  114. border-bottom: none;
  115. margin-bottom: 0;
  116. }
  117. h1,
  118. h2 {
  119. color: var(--accent-color);
  120. border-bottom: var(--border-thickness) solid var(--accent-color);
  121. padding-bottom: 0.3rem;
  122. }
  123. /* 翻译按钮相关 */
  124. #translate {
  125. position: relative;
  126. }
  127. .translateSelectLanguage {
  128. background: inherit;
  129. color: inherit;
  130. opacity: 0;
  131. cursor: pointer;
  132. position: absolute;
  133. top: 0;
  134. right: 0;
  135. bottom: 0;
  136. left: 0;
  137. }
  138. </style>
  139. <script
  140. src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
  141. type="text/javascript"
  142. ></script>
  143. <script
  144. src="https://lib.baomitu.com/Chart.js/3.7.1/chart.min.js"
  145. type="application/javascript"
  146. ></script>
  147. <link rel="icon" type="image/svg+xml" href="favicon.svg" />
  148. </head>
  149. <body>
  150. <div class="container">
  151. <h1>天气查询</h1>
  152. <form id="searchForm" class="search-box">
  153. <input type="text" id="cityInput" placeholder="输入城市名称" />
  154. <button type="submit" title="获取天气">
  155. <!-- Copyright (c) 2018-2025 Tabler -->
  156. <svg
  157. xmlns="http://www.w3.org/2000/svg"
  158. width="24"
  159. height="24"
  160. viewBox="0 0 24 24"
  161. fill="none"
  162. stroke="currentColor"
  163. stroke-width="2"
  164. stroke-linecap="round"
  165. stroke-linejoin="round"
  166. class="icon icon-tabler icons-tabler-outline icon-tabler-search"
  167. >
  168. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  169. <path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0" />
  170. <path d="M21 21l-6 -6" />
  171. </svg>
  172. </button>
  173. <button
  174. onclick="window.open('https://github.com/swaybien/pageos-weather', '_blank')"
  175. title="仓库"
  176. >
  177. <!-- Copyright (c) 2018-2025 Tabler -->
  178. <svg
  179. xmlns="http://www.w3.org/2000/svg"
  180. width="24"
  181. height="24"
  182. viewBox="0 0 24 24"
  183. fill="none"
  184. stroke="currentColor"
  185. stroke-width="2"
  186. stroke-linecap="round"
  187. stroke-linejoin="round"
  188. class="icon icon-tabler icons-tabler-outline icon-tabler-brand-git"
  189. >
  190. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  191. <path d="M16 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  192. <path d="M12 8m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  193. <path d="M12 16m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
  194. <path d="M12 15v-6" />
  195. <path d="M15 11l-2 -2" />
  196. <path d="M11 7l-1.9 -1.9" />
  197. <path
  198. d="M13.446 2.6l7.955 7.954a2.045 2.045 0 0 1 0 2.892l-7.955 7.955a2.045 2.045 0 0 1 -2.892 0l-7.955 -7.955a2.045 2.045 0 0 1 0 -2.892l7.955 -7.955a2.045 2.045 0 0 1 2.892 0z"
  199. />
  200. </svg>
  201. </button>
  202. <button id="translate" title="语言">
  203. <!-- Copyright (c) 2018-2025 Tabler -->
  204. <svg
  205. xmlns="http://www.w3.org/2000/svg"
  206. width="24"
  207. height="24"
  208. viewBox="0 0 24 24"
  209. fill="none"
  210. stroke="currentColor"
  211. stroke-width="2"
  212. stroke-linecap="round"
  213. stroke-linejoin="round"
  214. class="icon icon-tabler icons-tabler-outline icon-tabler-world"
  215. >
  216. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  217. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  218. <path d="M3.6 9h16.8" />
  219. <path d="M3.6 15h16.8" />
  220. <path d="M11.5 3a17 17 0 0 0 0 18" />
  221. <path d="M12.5 3a17 17 0 0 1 0 18" />
  222. </svg>
  223. </button>
  224. <button type="button" onclick="clearCache()" title="清除缓存">
  225. <!-- Copyright (c) 2018-2025 Tabler -->
  226. <svg
  227. xmlns="http://www.w3.org/2000/svg"
  228. width="24"
  229. height="24"
  230. viewBox="0 0 24 24"
  231. fill="none"
  232. stroke="currentColor"
  233. stroke-width="2"
  234. stroke-linecap="round"
  235. stroke-linejoin="round"
  236. class="icon icon-tabler icons-tabler-outline icon-tabler-trash-x"
  237. >
  238. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  239. <path d="M4 7h16" />
  240. <path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12" />
  241. <path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3" />
  242. <path d="M10 12l4 4m0 -4l-4 4" />
  243. </svg>
  244. </button>
  245. </form>
  246. <div class="heng">
  247. <div id="weatherResult" class="weather-info">
  248. <p>正在获取当前位置天气……</p>
  249. </div>
  250. <div id="graph" class="weather-info">
  251. <p>正在绘制当前位置天气趋势表……</p>
  252. </div>
  253. </div>
  254. <div class="weather-terminal-info">
  255. <iframe src="https://v2d.wttr.in" id="weatherRich"></iframe>
  256. </div>
  257. </div>
  258. <script>
  259. let dbPromise = null;
  260. function openDB() {
  261. if (!dbPromise) {
  262. dbPromise = new Promise((resolve, reject) => {
  263. const request = indexedDB.open("weatherCache", 1);
  264. request.onupgradeneeded = function (event) {
  265. const db = event.target.result;
  266. if (!db.objectStoreNames.contains("cache")) {
  267. db.createObjectStore("cache", { keyPath: "city" });
  268. }
  269. };
  270. request.onsuccess = function (event) {
  271. resolve(event.target.result);
  272. };
  273. request.onerror = function (event) {
  274. reject(event.target.error);
  275. };
  276. });
  277. }
  278. return dbPromise;
  279. }
  280. function getCache(city) {
  281. return openDB().then((db) => {
  282. return new Promise((resolve, reject) => {
  283. const transaction = db.transaction("cache", "readonly");
  284. const store = transaction.objectStore("cache");
  285. const request = store.get(city);
  286. request.onsuccess = function (event) {
  287. const data = event.target.result;
  288. if (data) {
  289. const now = Date.now();
  290. // 缓存有效期1小时(3600000毫秒)
  291. if (now - data.timestamp < 3600000) {
  292. resolve(data.data);
  293. } else {
  294. reject(new Error("Cache expired"));
  295. }
  296. } else {
  297. reject(new Error("Cache not found"));
  298. }
  299. };
  300. request.onerror = function (event) {
  301. reject(event.target.error);
  302. };
  303. });
  304. });
  305. }
  306. function setCache(city, data) {
  307. return openDB().then((db) => {
  308. return new Promise((resolve, reject) => {
  309. const transaction = db.transaction("cache", "readwrite");
  310. const store = transaction.objectStore("cache");
  311. const request = store.put({
  312. city: city,
  313. data: data,
  314. timestamp: Date.now(),
  315. });
  316. request.onsuccess = resolve;
  317. request.onerror = function (event) {
  318. reject(event.target.error);
  319. };
  320. });
  321. });
  322. }
  323. function clearCache() {
  324. openDB()
  325. .then((db) => {
  326. return new Promise((resolve, reject) => {
  327. const transaction = db.transaction("cache", "readwrite");
  328. const store = transaction.objectStore("cache");
  329. const request = store.clear();
  330. request.onsuccess = resolve;
  331. request.onerror = (event) => reject(event.target.error);
  332. });
  333. })
  334. .then(() => {
  335. alert("缓存已清除");
  336. })
  337. .catch((error) => {
  338. console.error("清除缓存失败:", error);
  339. alert("清除缓存失败");
  340. });
  341. }
  342. function firstFetch() {
  343. getCache("current_location")
  344. .then((data) => {
  345. updateGraph(data);
  346. const current = data.current_condition[0];
  347. const area = data.nearest_area[0];
  348. updateWeatherUI(current, area);
  349. })
  350. .catch(() => {
  351. fetch("https://wttr.in/?format=j1")
  352. .then((response) => response.json())
  353. .then((data) => {
  354. setCache("current_location", data);
  355. updateGraph(data);
  356. const current = data.current_condition[0];
  357. const area = data.nearest_area[0];
  358. updateWeatherUI(current, area);
  359. })
  360. .catch((error) => {
  361. const resultDiv = document.getElementById("weatherResult");
  362. resultDiv.innerHTML = `
  363. <p>无法获取当前位置天气</p>
  364. <p>请输入城市名称手动查询</p>
  365. `;
  366. });
  367. });
  368. }
  369. function updateWeatherUI(current, area) {
  370. let html = `
  371. <h2>${area.areaName[0].value}天气</h2>
  372. <div class="weather-detail">
  373. <strong>当前天气:</strong> ${current.weatherDesc[0].value}
  374. </div>
  375. <div class="weather-detail">
  376. <strong>温度:</strong> ${current.temp_C}°C (体感 ${current.FeelsLikeC}°C)
  377. </div>
  378. <div class="weather-detail">
  379. <strong>湿度:</strong> ${current.humidity}%
  380. </div>
  381. <div class="weather-detail">
  382. <strong>风速:</strong> ${current.windspeedKmph} km/h (${current.winddir16Point})
  383. </div>
  384. <div class="weather-detail">
  385. <strong>降水量:</strong> ${current.precipMM} mm
  386. </div>
  387. <div class="weather-detail">
  388. <strong>能见度:</strong> ${current.visibility} km
  389. </div>
  390. <div class="weather-detail">
  391. <strong>气压:</strong> ${current.pressure} hPa
  392. </div>
  393. <div class="weather-detail">
  394. <strong>更新时间:</strong> ${current.observation_time}
  395. </div>
  396. `;
  397. const res = document.getElementById("weatherResult");
  398. res.innerHTML = html;
  399. translate.execute([res]); // 翻译元素及内容
  400. refreshTranslate();
  401. }
  402. function updateGraph(data) {
  403. const graph = document.getElementById("graph");
  404. try {
  405. graph.innerHTML =
  406. '<canvas id="weatherChart0"></canvas><canvas id="weatherChart1"></canvas>';
  407. const ctx0 = document
  408. .getElementById("weatherChart0")
  409. .getContext("2d");
  410. const ctx1 = document
  411. .getElementById("weatherChart1")
  412. .getContext("2d");
  413. // 准备数据
  414. const labels = [];
  415. const datasets0 = [
  416. {
  417. label: "空气温度 (°C)",
  418. data: [],
  419. borderColor: "rgb(255, 99, 132)",
  420. backgroundColor: "rgba(255, 99, 132, 0.5)",
  421. },
  422. {
  423. label: "体感温度 (°C)",
  424. data: [],
  425. borderColor: "rgb(54, 162, 235)",
  426. backgroundColor: "rgba(54, 162, 235, 0.5)",
  427. },
  428. ];
  429. const datasets1 = [
  430. {
  431. label: "阳光率 (%)",
  432. data: [],
  433. borderColor: "rgb(255, 128, 128)",
  434. backgroundColor: "rgba(255, 128, 128, 0.5)",
  435. },
  436. {
  437. label: "下雨率 (%)",
  438. data: [],
  439. borderColor: "rgb(75, 192, 192)",
  440. backgroundColor: "rgba(75, 192, 192, 0.5)",
  441. },
  442. {
  443. label: "下雪率 (%)",
  444. data: [],
  445. borderColor: "rgb(153, 102, 255)",
  446. backgroundColor: "rgba(153, 102, 255, 0.5)",
  447. },
  448. {
  449. label: "打雷率 (%)",
  450. data: [],
  451. borderColor: "rgb(255, 159, 64)",
  452. backgroundColor: "rgba(255, 159, 64, 0.5)",
  453. },
  454. {
  455. label: "刮风率 (%)",
  456. data: [],
  457. borderColor: "rgb(123, 159, 321)",
  458. backgroundColor: "rgba(123, 159, 321, 0.5)",
  459. },
  460. {
  461. label: "雾水率 (%)",
  462. data: [],
  463. borderColor: "rgb(88, 192, 123)",
  464. backgroundColor: "rgba(88, 192, 123, 0.5)",
  465. },
  466. ];
  467. // 提取数据点
  468. data.weather.forEach((day) => {
  469. day.hourly.forEach((hourData) => {
  470. const hourNum = parseInt(hourData.time);
  471. const hours = Math.floor(hourNum / 100);
  472. const minutes = hourNum % 100;
  473. labels.push(
  474. `${day.date.slice(5)} ${hours
  475. .toString()
  476. .padStart(2, "0")}:${minutes.toString().padStart(2, "0")}`
  477. );
  478. datasets0[0].data.push(parseFloat(hourData.tempC));
  479. datasets0[1].data.push(parseFloat(hourData.FeelsLikeC));
  480. datasets1[0].data.push(parseFloat(hourData.chanceofsunshine));
  481. datasets1[1].data.push(parseFloat(hourData.chanceofrain));
  482. datasets1[2].data.push(parseFloat(hourData.chanceofsnow));
  483. datasets1[3].data.push(parseFloat(hourData.chanceofthunder));
  484. datasets1[4].data.push(parseFloat(hourData.chanceofwindy));
  485. datasets1[5].data.push(parseFloat(hourData.chanceoffog));
  486. });
  487. });
  488. // 创建图表
  489. new Chart(ctx0, {
  490. type: "line",
  491. data: {
  492. labels: labels,
  493. datasets: datasets0,
  494. },
  495. options: {
  496. responsive: true,
  497. plugins: {
  498. title: {
  499. display: true,
  500. text: "气温趋势",
  501. color: "#f5f5f5",
  502. },
  503. legend: {
  504. labels: {
  505. color: "#eeeeee",
  506. },
  507. },
  508. },
  509. scales: {
  510. x: {
  511. ticks: {
  512. color: "#eeeeee",
  513. maxRotation: 45,
  514. minRotation: 30,
  515. },
  516. },
  517. y: {
  518. ticks: {
  519. color: "#eeeeee",
  520. },
  521. title: {
  522. display: true,
  523. text: "数值",
  524. color: "#eeeeee",
  525. },
  526. },
  527. },
  528. },
  529. });
  530. new Chart(ctx1, {
  531. type: "line",
  532. data: {
  533. labels: labels,
  534. datasets: datasets1,
  535. },
  536. options: {
  537. responsive: true,
  538. plugins: {
  539. title: {
  540. display: true,
  541. text: "气象趋势",
  542. color: "#f5f5f5",
  543. },
  544. legend: {
  545. labels: {
  546. color: "#eeeeee",
  547. },
  548. },
  549. },
  550. scales: {
  551. x: {
  552. ticks: {
  553. color: "#eeeeee",
  554. maxRotation: 45,
  555. minRotation: 30,
  556. },
  557. },
  558. y: {
  559. ticks: {
  560. color: "#eeeeee",
  561. },
  562. title: {
  563. display: true,
  564. text: "数值",
  565. color: "#eeeeee",
  566. },
  567. },
  568. },
  569. },
  570. });
  571. } catch (error) {
  572. console.error(error);
  573. }
  574. }
  575. function updateIframe(city) {
  576. const iframe = document.getElementById("weatherRich");
  577. iframe.src = `https://v2d.wttr.in/${city}`;
  578. }
  579. function getWeather() {
  580. const city = document.getElementById("cityInput").value.trim();
  581. updateIframe(city);
  582. const resultDiv = document.getElementById("weatherResult");
  583. const graph = document.getElementById("graph");
  584. getCache(city)
  585. .then((data) => {
  586. updateGraph(data);
  587. const current = data.current_condition[0];
  588. const area = data.nearest_area[0];
  589. updateWeatherUI(current, area);
  590. })
  591. .catch(() => {
  592. resultDiv.innerHTML = "<p>加载中……</p>";
  593. graph.innerHTML = "<p>加载中……</p>";
  594. fetch(`https://wttr.in/${city}?format=j1`)
  595. .then((response) => response.json())
  596. .then((data) => {
  597. setCache(city, data);
  598. updateGraph(data);
  599. const current = data.current_condition[0];
  600. const area = data.nearest_area[0];
  601. updateWeatherUI(current, area);
  602. })
  603. .catch((error) => {
  604. resultDiv.innerHTML = `<p>获取天气信息失败: ${error.message}</p>`;
  605. document.querySelector(
  606. "#graph tbody"
  607. ).innerHTML = `<tr><td colspan="4" style="text-align: center; padding: 20px;">数据加载失败</td></tr>`;
  608. });
  609. });
  610. }
  611. // translate-js 翻译系统
  612. //// 初始化翻译系统
  613. function initTranslate() {
  614. try {
  615. translate.service.use("client.edge");
  616. translate.request.listener.start();
  617. translate.setAutoDiscriminateLocalLanguage();
  618. translate.language.setUrlParamControl();
  619. translate.language.translateLocal = true;
  620. translate.ignore.class.push("notTranslate");
  621. translate.execute();
  622. } catch (e) {
  623. console.error("翻译系统出错:" + e);
  624. }
  625. }
  626. //// 刷新翻译
  627. function refreshTranslate() {
  628. try {
  629. translate.selectLanguageTag.refreshRender();
  630. } catch (e) {
  631. console.error("刷新翻译出错:" + e);
  632. }
  633. }
  634. // 初始化
  635. function initialize() {
  636. initTranslate(); //// 初始化页面翻译
  637. firstFetch();
  638. // 添加表单提交事件监听
  639. const form = document.getElementById("searchForm");
  640. if (form) {
  641. form.addEventListener("submit", function (event) {
  642. event.preventDefault();
  643. getWeather();
  644. });
  645. }
  646. }
  647. // 触发器
  648. //// 网页加载完毕后触发初始化
  649. window.addEventListener("DOMContentLoaded", () => initialize());
  650. </script>
  651. </body>
  652. </html>