2025-08-11 11:51:38 +08:00
|
|
|
|
// src/utils/http.js
|
|
|
|
|
|
import axios from 'axios';
|
2025-09-02 16:28:18 +08:00
|
|
|
|
import config from './config.js';
|
2025-08-11 11:51:38 +08:00
|
|
|
|
// 创建axios实例
|
|
|
|
|
|
const http = axios.create({
|
2025-09-02 16:28:18 +08:00
|
|
|
|
baseURL: config.BASE_URL, // API的基础路径
|
2025-09-22 16:24:41 +08:00
|
|
|
|
timeout: 5000
|
2025-08-11 11:51:38 +08:00
|
|
|
|
});
|
2025-09-02 16:28:18 +08:00
|
|
|
|
|
2025-08-11 11:51:38 +08:00
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
http.interceptors.request.use(
|
2025-09-02 16:28:18 +08:00
|
|
|
|
config => {
|
|
|
|
|
|
// 在发送请求之前做些什么,例如添加token等
|
|
|
|
|
|
// config.headers['token'] = `${uni.getStorageSync('token')?? ''}`;
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
// 对请求错误做些什么
|
2025-08-14 11:26:34 +08:00
|
|
|
|
|
2025-09-02 16:28:18 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2025-08-11 11:51:38 +08:00
|
|
|
|
);
|
2025-09-02 16:28:18 +08:00
|
|
|
|
|
2025-08-11 11:51:38 +08:00
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
http.interceptors.response.use(
|
2025-09-02 16:28:18 +08:00
|
|
|
|
response => {
|
|
|
|
|
|
// 对响应数据做点什么
|
|
|
|
|
|
return response.data; // 根据实际情况可能需要返回response.data或其他处理后的数据
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
// 对响应错误做点什么
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2025-08-11 11:51:38 +08:00
|
|
|
|
);
|
2025-09-02 16:28:18 +08:00
|
|
|
|
|
2025-08-11 11:51:38 +08:00
|
|
|
|
export default http;
|