From 62bf414ff25c58d14fe446de051c75cafffad4d2 Mon Sep 17 00:00:00 2001 From: "WIN-JHFT4D3SIVT\\caoxiaozhu" Date: Tue, 7 Apr 2026 13:49:36 +0800 Subject: [PATCH] fix(frontend): weather shows default value when API fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../pages/chat/composables/useClientTime.ts | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/frontend/src/pages/chat/composables/useClientTime.ts b/frontend/src/pages/chat/composables/useClientTime.ts index 673761d..fcdf371 100644 --- a/frontend/src/pages/chat/composables/useClientTime.ts +++ b/frontend/src/pages/chat/composables/useClientTime.ts @@ -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}¤t=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}¤t=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(() => {