344 lines
6.9 KiB
Vue
344 lines
6.9 KiB
Vue
<template>
|
|
<div class="weather-app">
|
|
<div class="weather-header">
|
|
<i class="fas fa-cloud-sun"></i>
|
|
<span class="weather-title">锋天气</span>
|
|
</div>
|
|
|
|
<div class="weather-grid">
|
|
<div v-for="city in cities" :key="city.prefix" class="weather-container">
|
|
<div class="weather-info">
|
|
<div class="primary-info">
|
|
<div class="weather-icon">
|
|
<i :class="getWeatherIcon(city.weatherData.condition)"></i>
|
|
</div>
|
|
<div class="temp-location">
|
|
<div class="temperature">{{ city.weatherData.temperature }}°</div>
|
|
<div class="location-group">
|
|
<div class="location">{{ city.weatherData.location }}</div>
|
|
<div class="condition">{{ city.weatherData.condition }}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="secondary-info">
|
|
<div class="info-item">
|
|
<i class="fas fa-map-marker-alt"></i>
|
|
<span>{{ city.weatherData.country }}</span>
|
|
</div>
|
|
<div class="info-item">
|
|
<i class="fas fa-clock"></i>
|
|
<span>更新于 {{ city.weatherData.lastUpdate }}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, onMounted } from 'vue'
|
|
import axios from 'axios'
|
|
|
|
const CACHE_DURATION = 15 * 60 * 1000 // 15 minutes in milliseconds
|
|
|
|
const cities = ref([
|
|
{
|
|
prefix: 'ningbo',
|
|
url: 'https://api.seniverse.com/v3/weather/now.json?key=SMaPuMVF4IicVWOpa&location=WTQ3VZQP879N&language=zh-Hans&unit=c',
|
|
weatherData: {}
|
|
},
|
|
{
|
|
prefix: 'shenzhen',
|
|
url: 'https://api.seniverse.com/v3/weather/now.json?key=SMaPuMVF4IicVWOpa&location=WS10730EM8EV&language=zh-Hans&unit=c',
|
|
weatherData: {}
|
|
},
|
|
{
|
|
prefix: 'shaoxing',
|
|
url: 'https://api.seniverse.com/v3/weather/now.json?key=SMaPuMVF4IicVWOpa&location=WTMDXTK6STK4&language=zh-Hans&unit=c',
|
|
weatherData: {}
|
|
},
|
|
{
|
|
prefix: 'hangzhou',
|
|
url: 'https://api.seniverse.com/v3/weather/now.json?key=SMaPuMVF4IicVWOpa&location=WTMKQ069CCJ7&language=zh-Hans&unit=c',
|
|
weatherData: {}
|
|
},
|
|
|
|
{
|
|
prefix: 'jinhua',
|
|
url: 'https://api.seniverse.com/v3/weather/now.json?key=SMaPuMVF4IicVWOpa&location=WTJJ6TY7R0FK&language=zh-Hans&unit=c',
|
|
weatherData: {}
|
|
}
|
|
|
|
])
|
|
|
|
const fetchWeather = async (city) => {
|
|
try {
|
|
const response = await axios.get(city.url)
|
|
const weather = response.data.results[0]
|
|
const weatherData = {
|
|
location: weather.location.name,
|
|
country: weather.location.country,
|
|
condition: weather.now.text,
|
|
temperature: weather.now.temperature,
|
|
lastUpdate: new Date(weather.last_update).toLocaleString(),
|
|
timestamp: Date.now()
|
|
}
|
|
localStorage.setItem(city.prefix, JSON.stringify(weatherData))
|
|
city.weatherData = weatherData
|
|
} catch (error) {
|
|
console.error(`Error fetching ${city.prefix} weather data:`, error)
|
|
}
|
|
}
|
|
|
|
const fetchWeatherWithDelay = () => {
|
|
cities.value.forEach((city, index) => {
|
|
const cachedData = localStorage.getItem(city.prefix)
|
|
if (cachedData) {
|
|
const weatherData = JSON.parse(cachedData)
|
|
if (Date.now() - weatherData.timestamp < CACHE_DURATION) {
|
|
city.weatherData = weatherData
|
|
return
|
|
}
|
|
}
|
|
setTimeout(() => {
|
|
fetchWeather(city)
|
|
}, index * 1000)
|
|
})
|
|
}
|
|
|
|
const getWeatherIcon = (condition) => {
|
|
const iconMap = {
|
|
'晴': 'fas fa-sun',
|
|
'多云': 'fas fa-cloud-sun',
|
|
'阴': 'fas fa-cloud',
|
|
'雨': 'fas fa-cloud-rain',
|
|
'雪': 'fas fa-snowflake',
|
|
'雷': 'fas fa-bolt',
|
|
// 可以根据需要添加更多天气状况映射
|
|
}
|
|
return iconMap[condition] || 'fas fa-cloud'
|
|
}
|
|
|
|
onMounted(() => {
|
|
fetchWeatherWithDelay()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');
|
|
|
|
.weather-header {
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
gap: 12px;
|
|
margin-bottom: 30px;
|
|
padding-top: 20px;
|
|
}
|
|
|
|
.weather-header i {
|
|
font-size: 2rem;
|
|
color: #2c3e50;
|
|
}
|
|
|
|
.weather-title {
|
|
font-size: 2.2rem;
|
|
font-weight: 600;
|
|
color: #2c3e50;
|
|
letter-spacing: 2px;
|
|
}
|
|
|
|
.weather-grid {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
display: grid;
|
|
grid-template-columns: 1fr;
|
|
gap: 20px;
|
|
padding: 16px;
|
|
}
|
|
|
|
.weather-container {
|
|
background: rgba(255, 255, 255, 0.95);
|
|
border-radius: 16px;
|
|
padding: 24px;
|
|
transition: all 0.3s ease;
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
|
|
min-width: 0;
|
|
}
|
|
|
|
.weather-container:hover {
|
|
transform: translateY(-5px);
|
|
box-shadow: 0 12px 25px rgba(0, 0, 0, 0.12);
|
|
}
|
|
|
|
.primary-info {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 20px;
|
|
margin-bottom: 16px;
|
|
padding-left: 60px;
|
|
}
|
|
|
|
.weather-icon {
|
|
font-size: 2.5rem;
|
|
color: #4a90e2;
|
|
min-width: 60px;
|
|
width: 60px;
|
|
height: 60px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.temp-location {
|
|
flex: 1;
|
|
min-width: 0;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
margin-left: 60px;
|
|
}
|
|
|
|
.temperature {
|
|
font-size: 2rem;
|
|
font-weight: 400;
|
|
color: #2c3e50;
|
|
line-height: 1.2;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.location-group {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 4px;
|
|
}
|
|
|
|
.location {
|
|
font-size: 1.1rem;
|
|
font-weight: 500;
|
|
color: #2c3e50;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.condition {
|
|
font-size: 0.9rem;
|
|
color: #5d7290;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.secondary-info {
|
|
padding-top: 16px;
|
|
border-top: 1px solid rgba(0, 0, 0, 0.08);
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 8px;
|
|
}
|
|
|
|
.info-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
color: #7f8c8d;
|
|
font-size: 0.85rem;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.info-item > span {
|
|
font-size: 20px;
|
|
}
|
|
|
|
.info-item i {
|
|
min-width: 16px;
|
|
text-align: center;
|
|
color: #95a5a6;
|
|
}
|
|
|
|
@media (max-width: 992px) {
|
|
.weather-grid {
|
|
max-width: 900px;
|
|
padding: 12px;
|
|
gap: 16px;
|
|
}
|
|
|
|
.weather-container {
|
|
padding: 20px;
|
|
}
|
|
|
|
.weather-icon {
|
|
min-width: 50px;
|
|
width: 50px;
|
|
height: 50px;
|
|
}
|
|
|
|
.primary-info {
|
|
gap: 16px;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.weather-grid {
|
|
grid-template-columns: 1fr;
|
|
max-width: 600px;
|
|
}
|
|
|
|
.weather-container {
|
|
padding: 16px;
|
|
}
|
|
|
|
.weather-icon {
|
|
min-width: 45px;
|
|
width: 45px;
|
|
height: 45px;
|
|
font-size: 2rem;
|
|
}
|
|
|
|
.temperature {
|
|
font-size: 1.6rem;
|
|
margin-bottom: 6px;
|
|
}
|
|
|
|
.location {
|
|
font-size: 1rem;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 480px) {
|
|
.weather-grid {
|
|
padding: 10px;
|
|
gap: 12px;
|
|
}
|
|
|
|
.weather-container {
|
|
padding: 14px;
|
|
}
|
|
|
|
.primary-info {
|
|
gap: 12px;
|
|
}
|
|
|
|
.weather-icon {
|
|
min-width: 40px;
|
|
width: 40px;
|
|
height: 40px;
|
|
font-size: 1.8rem;
|
|
}
|
|
|
|
.temperature {
|
|
font-size: 1.4rem;
|
|
margin-bottom: 4px;
|
|
}
|
|
|
|
.location {
|
|
font-size: 0.95rem;
|
|
}
|
|
|
|
.condition {
|
|
font-size: 0.85rem;
|
|
}
|
|
}
|
|
</style> |