api更新
This commit is contained in:
parent
109e838573
commit
df117304d7
|
|
@ -1,6 +1,7 @@
|
||||||
import axios,{type AxiosInstance,AxiosError} from 'axios';
|
import axios,{type AxiosInstance,AxiosError} from 'axios';
|
||||||
import { type WeatherData } from '@/store/weatherStore';
|
import { type WeatherData } from '@/store/weatherStore';
|
||||||
|
|
||||||
|
|
||||||
const API_KEY = '0c4679ac2decfe6a756aa09e61f42dc1';
|
const API_KEY = '0c4679ac2decfe6a756aa09e61f42dc1';
|
||||||
const BASE_URL = 'http://api.weatherstack.com';
|
const BASE_URL = 'http://api.weatherstack.com';
|
||||||
|
|
||||||
|
|
@ -40,7 +41,40 @@ apiClient.interceptors.response.use(
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
(error :AxiosError)=>{
|
(error :AxiosError)=>{
|
||||||
}
|
|
||||||
|
|
||||||
|
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<WeatherData> {
|
||||||
|
try {
|
||||||
|
const response = await apiClient.get<WeatherData>('/current',{
|
||||||
|
params:{query :city} ,
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
}catch (error){
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
export default WeatherApi;
|
||||||
Loading…
Reference in New Issue