2023-12-16 18:10:04 +08:00
|
|
|
/**
|
|
|
|
|
* axios全局配置
|
|
|
|
|
*/
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
axios.defaults.timeout = 30000
|
|
|
|
|
|
|
|
|
|
// 默认配置
|
2024-08-26 20:53:34 +08:00
|
|
|
axios.defaults.baseURL = '';
|
2023-12-16 18:10:04 +08:00
|
|
|
|
|
|
|
|
axios.interceptors.request.use(
|
|
|
|
|
config => {
|
|
|
|
|
let token = localStorage.getItem('token')
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.accessToken = `${token}`;
|
|
|
|
|
config.headers.Authorization = `${token}`;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 添加一个请求拦截器
|
|
|
|
|
axios.interceptors.response.use(
|
|
|
|
|
response => {
|
|
|
|
|
if (response.data) {
|
|
|
|
|
if (response.data.code === 401) {
|
|
|
|
|
} else if (response.data.code === 200) {
|
|
|
|
|
return Promise.resolve(response.data)
|
|
|
|
|
} else {
|
|
|
|
|
return response.data
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Promise.reject(response.data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1)
|
|
|
|
|
console.log('请求超时!')
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 通用POST方法
|
|
|
|
|
export const POST = (url, params) => {
|
|
|
|
|
return axios.post(`${url}`, params).then(response => response);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 通用POST_JSON方法
|
|
|
|
|
export const POST_JSON = (url, json, params) => {
|
|
|
|
|
return axios.post(`${url}`, json, {
|
|
|
|
|
params: params
|
|
|
|
|
}).then(response => response);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 通用GET方法
|
|
|
|
|
export const GET = (url, params) => {
|
|
|
|
|
return axios.get(`${url}`, {
|
|
|
|
|
params: params
|
|
|
|
|
}).then(response => response);
|
|
|
|
|
};
|