From e5acfa5cf31e4ad2cb55702e918a08d9ffc62c52 Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Tue, 1 Apr 2025 14:45:21 +0900 Subject: [PATCH] =?UTF-8?q?=EB=82=A0=EC=94=A8=20api=20=EB=B0=B1=EC=97=94?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/TheTop.vue | 60 ++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/layouts/TheTop.vue b/src/layouts/TheTop.vue index bae527d..2cf92eb 100644 --- a/src/layouts/TheTop.vue +++ b/src/layouts/TheTop.vue @@ -269,6 +269,7 @@ import { useRouter } from 'vue-router'; import { useThemeStore } from '@s/darkmode'; import { computed, onMounted, ref, watch } from 'vue'; + import axios from '@api'; const baseUrl = import.meta.env.VITE_SERVER; @@ -353,41 +354,42 @@ const weatherKorean = computed(() => weather.value.description || '날씨 정보 없음'); onMounted(() => { - navigator.geolocation.getCurrentPosition(async (position) => { - const lat = position.coords.latitude; - const lon = position.coords.longitude; - const apiKey = '3505b3500aacf34c4497bf449996ea09'; - const url = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric&lang=kr`; + navigator.geolocation.getCurrentPosition(async (position) => { + const lat = position.coords.latitude; + const lon = position.coords.longitude; - try { - const res = await fetch(url); - const data = await res.json(); + try { + const res = await axios.get(`/weather`, { + params: { lat, lon }, + withCredentials: true, + }); - const now = new Date(); + const raw = res.data; + const data = JSON.parse(raw.data); - // 가장 가까운 데이터 - const closest = data.list.reduce((prev, curr) => { - const prevTime = new Date(prev.dt_txt).getTime(); - const currTime = new Date(curr.dt_txt).getTime(); - const nowTime = now.getTime(); + if (!data || !Array.isArray(data.list) || data.list.length === 0) { + console.error('날씨 데이터 형식 오류 또는 없음:', data); + return; + } - const prevDiff = Math.abs(prevTime - nowTime); - const currDiff = Math.abs(currTime - nowTime); + const now = new Date(); + const closest = data.list.reduce((prev, curr) => { + const prevTime = new Date(prev.dt_txt).getTime(); + const currTime = new Date(curr.dt_txt).getTime(); + const nowTime = now.getTime(); + const prevDiff = Math.abs(prevTime - nowTime); + const currDiff = Math.abs(currTime - nowTime); + return prevDiff < currDiff ? prev : curr; + }); - if (prevDiff < currDiff) return prev; - if (prevDiff > currDiff) return curr; - - return prevTime < currTime ? prev : curr; + weather.value.icon = closest.weather[0].icon.replace(/n$/, 'd'); + weather.value.description = closest.weather[0].description; + weather.value.tempMin = Math.round(closest.main.temp_min); + weather.value.tempMax = Math.round(closest.main.temp_max); + } catch (e) { + console.error('날씨 정보 가져오기 실패:', e); + } }); - - weather.value.icon = closest.weather[0].icon.replace(/n$/, 'd'); - weather.value.description = closest.weather[0].description; - weather.value.tempMin = Math.round(closest.main.temp_min); - weather.value.tempMax = Math.round(closest.main.temp_max); - } catch (e) { - console.error('날씨 정보 가져오기 실패:', e); - } - }); }); const handleLogout = async () => {