前端初始化
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import axios from 'axios';
|
||||
import {Message, Loading} from 'element-ui';
|
||||
import store from '../store';
|
||||
import router from "../router";
|
||||
import {BASE_URL} from "../utils/http";
|
||||
|
||||
// 记录请求个数、加载
|
||||
let count = 0, loading = null;
|
||||
|
||||
// axios实例化
|
||||
const instance = axios.create({
|
||||
baseURL: '/library',
|
||||
// timeout: 60000,
|
||||
headers: {
|
||||
'Cache-Control': 'no-cache',
|
||||
'Pragma': 'no-cache'
|
||||
}
|
||||
});
|
||||
// 添加请求拦截器
|
||||
instance.interceptors.request.use(function (config) {
|
||||
// 在发送请求之前做些什么
|
||||
loading = Loading.service(null);
|
||||
count++;
|
||||
store.commit('saveIsLoading', true);
|
||||
return config;
|
||||
}, function (error) {
|
||||
// 对请求错误做些什么
|
||||
count--;
|
||||
if (count < 1) {
|
||||
loading.close();
|
||||
store.commit('saveIsLoading', false);
|
||||
}
|
||||
Message.error(error);
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
// 添加响应拦截器
|
||||
instance.interceptors.response.use(function (response) {
|
||||
// 对响应数据做点什么
|
||||
count--;
|
||||
if (count < 1) {
|
||||
loading.close();
|
||||
store.commit('saveIsLoading', false);
|
||||
}
|
||||
|
||||
// 登录超时
|
||||
if (response.data.respCode === '401') {
|
||||
store.commit('saveUserInfo', null);
|
||||
router.push('/login');
|
||||
Message.error('登录超时过期');
|
||||
// var url = 'http://192.168.144.22:9966/login?loginType=1'
|
||||
// // var url = 'http://10.64.23.30:7766/home'
|
||||
//
|
||||
// return window.open('http://192.168.144.29:9090/cas/login?service='+url, '_self');
|
||||
// return window.open('https://caddmuat.changan.com.cn/cas/login?service='+url, '_self');
|
||||
// window.open(BASE_URL.BASE_CHANGAN + "/logout?service=" + BASE_URL.BASE_LOCAL + "/login?loginType=1", '_self');
|
||||
}
|
||||
|
||||
// 请求成功
|
||||
if (response.status === 200) {
|
||||
if (response.data.respCode === "0") {
|
||||
return response.data;
|
||||
} else if (response.data.respCode === "-1") {
|
||||
Message.error(response.data.message);
|
||||
return Promise.reject(response);
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
} else { // 请求失败
|
||||
Message.error(response.data.message);
|
||||
return Promise.reject(response);
|
||||
}
|
||||
|
||||
}, function (error) {
|
||||
// 对响应错误做点什么
|
||||
count--;
|
||||
if (count < 1) {
|
||||
loading.close();
|
||||
store.commit('saveIsLoading', false);
|
||||
}
|
||||
|
||||
// 登录过期
|
||||
if (error.response.status === 401) {
|
||||
store.commit('saveUserInfo', null);
|
||||
router.push('/login');
|
||||
Message.error('登录超时过期');
|
||||
// window.open(BASE_URL.BASE_CHANGAN + "/logout?service=" + BASE_URL.BASE_LOCAL + "/login?loginType=1", '_self');
|
||||
// var url = 'http://192.168.144.22:9966/login?loginType=1'
|
||||
// // var url = 'http://10.64.23.30:7766/home'
|
||||
//
|
||||
// return window.open('http://192.168.144.29:9090/cas/login?service='+url, '_self');
|
||||
// // return window.open('https://caddmuat.changan.com.cn/cas/login?service='+url, '_self');
|
||||
}
|
||||
|
||||
// 判断超时
|
||||
if (error.code === "ECONNABORTED" && error.message.indexOf('timeout') !== -1 && !error.config._retry) {
|
||||
Message.error(error.message);
|
||||
} else {
|
||||
Message.error(error.message);
|
||||
}
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
export {instance, loading};
|
||||
Reference in New Issue
Block a user