2023-01-15 05:22:30 +00:00
|
|
|
<template>
|
2023-07-26 06:51:16 +00:00
|
|
|
<div class="weather" v-if="weatherData.adCode.city && weatherData.weather.weather">
|
2023-01-15 05:22:30 +00:00
|
|
|
<span>{{ weatherData.adCode.city }} </span>
|
|
|
|
<span>{{ weatherData.weather.weather }} </span>
|
|
|
|
<span>{{ weatherData.weather.temperature }}℃</span>
|
2023-07-03 09:51:58 +00:00
|
|
|
<span class="sm-hidden">
|
|
|
|
{{
|
2023-08-23 05:38:17 +00:00
|
|
|
weatherData.weather.winddirection?.endsWith('风')
|
|
|
|
? weatherData.weather.winddirection
|
|
|
|
: weatherData.weather.winddirection + '风'
|
2023-07-03 09:51:58 +00:00
|
|
|
}}
|
|
|
|
</span>
|
2023-01-15 05:22:30 +00:00
|
|
|
<span class="sm-hidden">{{ weatherData.weather.windpower }} 级</span>
|
|
|
|
</div>
|
|
|
|
<div class="weather" v-else>
|
|
|
|
<span>天气数据获取失败</span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2023-08-23 05:38:17 +00:00
|
|
|
import { getAdcode, getWeather, getOtherWeather } from '@/api';
|
|
|
|
import { Error } from '@icon-park/vue-next';
|
2023-01-15 05:22:30 +00:00
|
|
|
|
|
|
|
// 高德开发者 Key
|
2023-04-21 08:37:08 +00:00
|
|
|
const mainKey = import.meta.env.VITE_WEATHER_KEY;
|
2023-01-15 05:22:30 +00:00
|
|
|
|
|
|
|
// 天气数据
|
2023-04-21 08:37:08 +00:00
|
|
|
const weatherData = reactive({
|
2023-01-15 05:22:30 +00:00
|
|
|
adCode: {
|
|
|
|
city: null, // 城市
|
|
|
|
adcode: null, // 城市编码
|
|
|
|
},
|
|
|
|
weather: {
|
|
|
|
weather: null, // 天气现象
|
|
|
|
temperature: null, // 实时气温
|
|
|
|
winddirection: null, // 风向描述
|
|
|
|
windpower: null, // 风力级别
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// 获取天气数据
|
|
|
|
const getWeatherData = () => {
|
|
|
|
// 获取地理位置信息
|
2023-07-03 09:51:58 +00:00
|
|
|
if (!mainKey) {
|
|
|
|
getOtherWeather()
|
|
|
|
.then((res) => {
|
|
|
|
console.log(res);
|
|
|
|
const data = res.result;
|
|
|
|
weatherData.adCode = {
|
|
|
|
city: data.city.name,
|
|
|
|
adcode: data.city.cityId,
|
|
|
|
};
|
|
|
|
weatherData.weather = {
|
|
|
|
weather: data.condition.condition,
|
|
|
|
temperature: data.condition.temp,
|
|
|
|
winddirection: data.condition.windDir,
|
|
|
|
windpower: data.condition.windLevel,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-08-23 05:38:17 +00:00
|
|
|
console.error('天气信息获取失败:' + err);
|
|
|
|
onError('天气信息获取失败');
|
2023-07-03 09:51:58 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
getAdcode(mainKey)
|
|
|
|
.then((res) => {
|
2023-01-15 05:22:30 +00:00
|
|
|
weatherData.adCode = {
|
|
|
|
city: res.city,
|
|
|
|
adcode: res.adcode,
|
|
|
|
};
|
|
|
|
// 获取天气信息
|
|
|
|
getWeather(mainKey, weatherData.adCode.adcode)
|
|
|
|
.then((res) => {
|
2023-07-03 09:51:58 +00:00
|
|
|
weatherData.weather = {
|
|
|
|
weather: res.lives[0].weather,
|
|
|
|
temperature: res.lives[0].temperature,
|
|
|
|
winddirection: res.lives[0].winddirection,
|
|
|
|
windpower: res.lives[0].windpower,
|
|
|
|
};
|
2023-01-15 05:22:30 +00:00
|
|
|
})
|
2023-07-03 09:51:58 +00:00
|
|
|
.catch((err) => {
|
2023-08-23 05:38:17 +00:00
|
|
|
console.error('天气信息获取失败:' + err);
|
|
|
|
onError('天气信息获取失败');
|
2023-01-15 05:22:30 +00:00
|
|
|
});
|
2023-07-03 09:51:58 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-08-23 05:38:17 +00:00
|
|
|
console.error('地理位置获取失败:' + err);
|
|
|
|
onError('地理位置获取失败');
|
2023-07-03 09:51:58 +00:00
|
|
|
});
|
|
|
|
}
|
2023-01-15 05:22:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 报错信息
|
|
|
|
const onError = (message) => {
|
|
|
|
ElMessage({
|
2023-07-03 09:51:58 +00:00
|
|
|
message,
|
2023-01-15 05:22:30 +00:00
|
|
|
icon: h(Error, {
|
2023-08-23 05:38:17 +00:00
|
|
|
theme: 'filled',
|
|
|
|
fill: '#efefef',
|
2023-01-15 05:22:30 +00:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
console.error(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
// 调用获取天气
|
|
|
|
getWeatherData();
|
|
|
|
});
|
2023-07-03 09:51:58 +00:00
|
|
|
</script>
|