|
|
|
|
import axios from 'axios';
|
|
|
|
|
// import { Loading } from 'element-ui'; //项目已经全局引入element的话可以不单独引入
|
|
|
|
|
// let loading //定义loading变量
|
|
|
|
|
// function startLoading() { //使用Element loading-start 方法
|
|
|
|
|
// loading = Loading.service({
|
|
|
|
|
// lock: true,
|
|
|
|
|
// text: '加载中……',
|
|
|
|
|
// background: 'rgba(255, 255, 255, 0.7)'
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// function endLoading() { //使用Element loading-close 方法
|
|
|
|
|
// loading.close()
|
|
|
|
|
// }
|
|
|
|
|
// //那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
|
|
|
|
|
// //声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
|
|
|
|
|
// //调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
|
|
|
|
|
// let needLoadingRequestCount = 0
|
|
|
|
|
// export function showFullScreenLoading() {
|
|
|
|
|
// if (needLoadingRequestCount === 0) {
|
|
|
|
|
// startLoading()
|
|
|
|
|
// }
|
|
|
|
|
// needLoadingRequestCount++
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// export function tryHideFullScreenLoading() {
|
|
|
|
|
// if (needLoadingRequestCount <= 0) return
|
|
|
|
|
// needLoadingRequestCount--
|
|
|
|
|
// if (needLoadingRequestCount === 0) {
|
|
|
|
|
// endLoading()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
// process.env.NODE_ENV === 'development' 来判断是否开发环境
|
|
|
|
|
// easy-mock服务挂了,暂时不使用了
|
|
|
|
|
// baseURL: '',
|
|
|
|
|
// timeout: 5000
|
|
|
|
|
baseURL: 'api',//把原来的项目地址,改成api,解决跨域问题
|
|
|
|
|
timeout: 3000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
config => {
|
|
|
|
|
// showFullScreenLoading()
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
response => {
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
// tryHideFullScreenLoading()
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
Promise.reject();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default service;
|