-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
33 lines (29 loc) · 1.18 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class WeatherWidget {
constructor() {
this.weatherElement = document.querySelector('.weather');
this.API_KEY = 'your_api_key'; // 替换为你的和风天气API密钥
this.city = '广州'; // 默认城市
this.init();
}
async init() {
try {
// 先获取城市ID
const cityUrl = `https://geoapi.qweather.com/v2/city/lookup?location=${this.city}&key=${this.API_KEY}`;
const cityResponse = await fetch(cityUrl);
const cityData = await cityResponse.json();
const locationId = cityData.location[0].id;
// 获取天气信息
const weatherUrl = `https://devapi.qweather.com/v7/weather/now?location=${locationId}&key=${this.API_KEY}`;
const weatherResponse = await fetch(weatherUrl);
const weatherData = await weatherResponse.json();
this.updateWeather(weatherData.now);
} catch (error) {
console.error('获取天气信息失败:', error);
this.weatherElement.textContent = '天气信息获取失败';
}
}
updateWeather(weather) {
this.weatherElement.textContent =
`${this.city} ${weather.text} ${weather.temp}°C ${weather.windDir} ${weather.windScale}级`;
}
}