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