From df117304d739b4322ea380bf5eb8bad389002bda Mon Sep 17 00:00:00 2001 From: jinsir <874871581@qq.com> Date: Mon, 17 Nov 2025 19:13:23 +0800 Subject: [PATCH] =?UTF-8?q?api=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/weatherApi.ts | 40 +++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/app/services/weatherApi.ts b/app/services/weatherApi.ts index bd7b4d5..d5b9477 100644 --- a/app/services/weatherApi.ts +++ b/app/services/weatherApi.ts @@ -1,6 +1,7 @@ import axios,{type AxiosInstance,AxiosError} from 'axios'; import { type WeatherData } from '@/store/weatherStore'; + const API_KEY = '0c4679ac2decfe6a756aa09e61f42dc1'; const BASE_URL = 'http://api.weatherstack.com'; @@ -40,7 +41,40 @@ apiClient.interceptors.response.use( return response; }, (error :AxiosError)=>{ - } - -); \ No newline at end of file + console.error('Response Error:',error.message); + if (error.response) { + const status = error.response.status; + if (status === 404) { + throw new Error('未找到该城市的天气信息'); + } else if (status === 401) { + throw new Error('API认证失败,请检查API'); + } else if (status === 403) { + throw new Error('请检查API密钥是否正确'); + } + } + else if (error.request) { + throw new Error('网络连接失败'); + } + else { + return (('配置错误')) + } + } + + +); + +export class WeatherApi { +static async getCurrentWeather(city: string): Promise { + try { + const response = await apiClient.get('/current',{ + params:{query :city} , + }); + return response.data; +}catch (error){ + throw error; +} +} + +} +export default WeatherApi; \ No newline at end of file