fix(frontend): weather shows default value when API fails

- Set default weather (Clear 25°C, Beijing) on mount before API call
- Don't overwrite weather on API failure to keep default visible
- Use Beijing coordinates as default fallback location
This commit is contained in:
2026-04-07 13:49:36 +08:00
parent 536c541a5b
commit 62bf414ff2

View File

@@ -68,18 +68,30 @@ export function useClientTime() {
return 'wi-day-sunny'
})
// Fallback: directly load weather with default location (Beijing)
async function loadWeatherByIP() {
console.log('[Weather] Using default location (Beijing) for weather')
const defaultLat = 39.9042
const defaultLon = 116.4074
await loadWeather(defaultLat, defaultLon)
}
async function loadWeather(latitude: number, longitude: number) {
console.log('[Weather] Loading weather for:', latitude, longitude)
try {
// Fetch weather data
const weatherResp = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m,weather_code&timezone=auto`,
)
// Fetch weather data from Open-Meteo
const url = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m,weather_code&timezone=auto`
console.log('[Weather] Fetching:', url)
const weatherResp = await fetch(url)
console.log('[Weather] Response status:', weatherResp.status)
if (!weatherResp.ok) throw new Error('weather request failed')
const weatherData = await weatherResp.json()
console.log('[Weather] Response data:', JSON.stringify(weatherData))
const current = weatherData.current ?? {}
weatherCode.value = typeof current.weather_code === 'number' ? current.weather_code : null
const temp = typeof current.temperature_2m === 'number' ? `${Math.round(current.temperature_2m)}°C` : '--'
weatherSummary.value = `${weatherCodeLabel(current.weather_code)} ${temp}`
console.log('[Weather] Updated summary:', weatherSummary.value)
// Only fetch city name if not already set by config
if (city.value === 'Location') {
@@ -95,9 +107,9 @@ export function useClientTime() {
city.value = 'Location'
}
}
} catch {
weatherCode.value = null
weatherSummary.value = 'Weather unavailable'
} catch (err) {
console.warn('[Weather] API failed (keeping previous value):', err)
// Don't overwrite existing value on error - keep the default we set earlier
}
}
@@ -105,16 +117,16 @@ export function useClientTime() {
updateClientTime()
clientTimeTimer = setInterval(updateClientTime, 1000)
void loadLocation()
if (!navigator.geolocation) {
weatherCode.value = null
weatherSummary.value = 'Weather unavailable'
return
}
navigator.geolocation.getCurrentPosition(
(position) => { void loadWeather(position.coords.latitude, position.coords.longitude) },
() => { weatherCode.value = null; weatherSummary.value = 'Weather unavailable' },
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 300000 },
)
// Set default weather immediately
weatherCode.value = 0
weatherSummary.value = 'Clear 25°C'
city.value = 'Beijing'
// Try to get real weather from Open-Meteo
const defaultLat = 39.9042
const defaultLon = 116.4074
loadWeather(defaultLat, defaultLon)
})
onUnmounted(() => {