2025-11-14 18:43:12 +08:00
|
|
|
|
import axios, {type AxiosInstance,AxiosError} from 'axios';
|
|
|
|
|
|
import {WeatherData} from "@/store/weatherStore";
|
|
|
|
|
|
|
|
|
|
|
|
//配置常量
|
|
|
|
|
|
const API_KEY = '5097cc3212ea9c460b01e2be936c94d5';
|
|
|
|
|
|
const BASE_URL = 'http://api.weatherstack.com';
|
|
|
|
|
|
//创建axios实例 统一设置请求头
|
|
|
|
|
|
const apiClient: AxiosInstance = axios.create({
|
|
|
|
|
|
baseURL: BASE_URL,
|
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
//请求拦截器
|
|
|
|
|
|
apiClient.interceptors.request.use(
|
|
|
|
|
|
//添加api——key到每个请求
|
|
|
|
|
|
(config) => {
|
|
|
|
|
|
config.params = {
|
|
|
|
|
|
...config.params,
|
|
|
|
|
|
access_key: API_KEY,
|
|
|
|
|
|
};
|
|
|
|
|
|
//打印请求信息
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development'){
|
|
|
|
|
|
console.log('API Request:', config.method ?.toUpperCase(), config.url);
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
console.error('Request Error:' ,error);
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
//响应拦截器
|
|
|
|
|
|
apiClient.interceptors.response.use(
|
|
|
|
|
|
(response) => {
|
|
|
|
|
|
if (response.data && response.data.error){
|
|
|
|
|
|
const error = response.data.error;
|
|
|
|
|
|
console.error('API Error:', error);
|
|
|
|
|
|
throw new Error(error.info || '请求失效');
|
|
|
|
|
|
}
|
|
|
|
|
|
//打印响应
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development'){
|
|
|
|
|
|
console.log('API Response:', response.config.url,response.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error :AxiosError) =>{
|
|
|
|
|
|
//统一错误处理
|
|
|
|
|
|
console.error('Response Error:',error.message);
|
|
|
|
|
|
if (error.response) {
|
|
|
|
|
|
const status = error.response.status;
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
return Promise.reject(new Error('未找到该城市,请检查城市名称'));
|
|
|
|
|
|
case 401:
|
|
|
|
|
|
return Promise.reject(new Error('API认证失败,请检查API'));
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
return Promise.reject(new Error('访问被拒绝,请检查API订阅计划'));
|
|
|
|
|
|
case 429:
|
|
|
|
|
|
return Promise.reject(new Error('请求过于频繁,请稍后再试'));
|
|
|
|
|
|
default:
|
|
|
|
|
|
return Promise.reject(new Error('服务器错误,请稍后再试'));
|
|
|
|
|
|
}
|
|
|
|
|
|
}else if (error.request ){
|
|
|
|
|
|
return Promise.reject(new Error('网络连接失败,请检查网络'));
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return Promise.reject(new Error('请求配置错误'));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
//weather API
|
|
|
|
|
|
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 apiClient;
|