|
@@ -1,30 +1,41 @@
|
|
|
<template>
|
|
|
- <div class="weatherBox" v-if="cityName">
|
|
|
+ <div class="weatherBox" v-if="weatherInfo.cityName">
|
|
|
<div class="tqBox">
|
|
|
- <img :src="getAssetsFile(iconPath)" alt="" />
|
|
|
+ <img :src="getAssetsFile(weatherInfo.iconPath)" alt="" />
|
|
|
<div>
|
|
|
- <div class="day">{{ temperature }}</div>
|
|
|
+ <div class="day">{{ weatherInfo.temperature }}</div>
|
|
|
<div>
|
|
|
<p>℃</p>
|
|
|
- <p>{{ weather }}</p>
|
|
|
+ <p>{{ weatherInfo.weather }}</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<div class="posBox">
|
|
|
- <span class="quality">{{ winddirection }} {{ windpower }}</span>
|
|
|
- <span class="location"><Icon icon="ep:location" />{{ cityName }}</span>
|
|
|
+ <span class="quality">{{ weatherInfo.winddirection }} {{ weatherInfo.windpower }}</span>
|
|
|
+ <span class="location"><Icon icon="ep:location" />{{ weatherInfo.cityName }}</span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</template>
|
|
|
<script setup lang="ts">
|
|
|
import axios from 'axios'
|
|
|
+import { setWithExpire, getWithExpire } from '@/utils'
|
|
|
|
|
|
-const cityName = ref<string>()
|
|
|
-const weather = ref<string>()
|
|
|
-const temperature = ref<string>()
|
|
|
-const winddirection = ref<string>()
|
|
|
-const windpower = ref<string>()
|
|
|
-const iconPath = ref<string | undefined>()
|
|
|
+interface WeatherType {
|
|
|
+ cityName: string
|
|
|
+ weather: string
|
|
|
+ temperature: string
|
|
|
+ winddirection: string
|
|
|
+ windpower: string
|
|
|
+ iconPath: string | undefined
|
|
|
+}
|
|
|
+const weatherInfo = reactive<WeatherType>({
|
|
|
+ cityName: '',
|
|
|
+ weather: '',
|
|
|
+ temperature: '',
|
|
|
+ winddirection: '',
|
|
|
+ windpower: '',
|
|
|
+ iconPath: ''
|
|
|
+})
|
|
|
|
|
|
const getAssetsFile = (url: string | undefined): string => {
|
|
|
if (!url) return ''
|
|
@@ -36,42 +47,51 @@ const getCurrentWeatherInfo = async (city: string): Promise<void> => {
|
|
|
const result = await axios({ url: urlApi, method: 'GET', data: sendData })
|
|
|
const resultData = result['data']
|
|
|
const weatherTxt = resultData['lives'][0]['weather']
|
|
|
- weather.value = weatherTxt
|
|
|
- temperature.value = resultData['lives'][0]['temperature']
|
|
|
- winddirection.value = resultData['lives'][0]['winddirection']
|
|
|
- windpower.value = resultData['lives'][0]['windpower']
|
|
|
+ weatherInfo.weather = weatherTxt
|
|
|
+ weatherInfo.temperature = resultData['lives'][0]['temperature']
|
|
|
+ weatherInfo.winddirection = resultData['lives'][0]['winddirection']
|
|
|
+ weatherInfo.windpower = resultData['lives'][0]['windpower']
|
|
|
if (weatherTxt.indexOf('大雨') !== -1) {
|
|
|
- iconPath.value = 'rain_big.png'
|
|
|
+ weatherInfo.iconPath = 'rain_big.png'
|
|
|
} else if (weatherTxt.indexOf('雷阵雨') !== -1) {
|
|
|
- iconPath.value = 'rain_thunder.png'
|
|
|
+ weatherInfo.iconPath = 'rain_thunder.png'
|
|
|
} else if (weatherTxt.indexOf('雨') !== -1) {
|
|
|
- iconPath.value = 'rain.png'
|
|
|
+ weatherInfo.iconPath = 'rain.png'
|
|
|
} else if (weatherTxt.indexOf('大雪') !== -1) {
|
|
|
- iconPath.value = 'snow_big.png'
|
|
|
+ weatherInfo.iconPath = 'snow_big.png'
|
|
|
} else if (weatherTxt.indexOf('雪') !== -1) {
|
|
|
- iconPath.value = 'snow.png'
|
|
|
+ weatherInfo.iconPath = 'snow.png'
|
|
|
} else if (weatherTxt.indexOf('多云') !== -1) {
|
|
|
- iconPath.value = 'cloud.png'
|
|
|
+ weatherInfo.iconPath = 'cloud.png'
|
|
|
} else if (weatherTxt.indexOf('阴') !== -1) {
|
|
|
- iconPath.value = 'sky_overcast.png'
|
|
|
+ weatherInfo.iconPath = 'sky_overcast.png'
|
|
|
} else if (weatherTxt.indexOf('晴') !== -1) {
|
|
|
- iconPath.value = 'clear_day.png'
|
|
|
+ weatherInfo.iconPath = 'clear_day.png'
|
|
|
} else if (weatherTxt.indexOf('雾') !== -1) {
|
|
|
- iconPath.value = 'fog.png'
|
|
|
+ weatherInfo.iconPath = 'fog.png'
|
|
|
} else if (weatherTxt.indexOf('霾') !== -1) {
|
|
|
- iconPath.value = 'haze.png'
|
|
|
+ weatherInfo.iconPath = 'haze.png'
|
|
|
}
|
|
|
+ setWithExpire('_weathers', weatherInfo)
|
|
|
}
|
|
|
const getLocationByIp = async (): Promise<void> => {
|
|
|
const urlApi = `https://restapi.amap.com/v3/ip?key=10e346e2f3bdcb88fab43e0c950014f5`
|
|
|
const sendData = {}
|
|
|
const result = await axios({ url: urlApi, method: 'GET', data: sendData })
|
|
|
const resultData = result['data']
|
|
|
- cityName.value = resultData['city']
|
|
|
+ weatherInfo.cityName = resultData['city']
|
|
|
getCurrentWeatherInfo(resultData['adcode'])
|
|
|
}
|
|
|
onMounted(() => {
|
|
|
- getLocationByIp()
|
|
|
+ const weathers = getWithExpire('_weathers')
|
|
|
+ if (!weathers) {
|
|
|
+ getLocationByIp()
|
|
|
+ } else {
|
|
|
+ const keys = Object.keys(weathers)
|
|
|
+ keys.forEach((key) => {
|
|
|
+ weatherInfo[key] = weathers[key]
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
</script>
|
|
|
|