index.html 22 KB

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