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