index.html 22 KB

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