119 lines
3.8 KiB
JavaScript
119 lines
3.8 KiB
JavaScript
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; let 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.status === 200) {
|
|
if (response.data.respCode === '0') {
|
|
return response.data
|
|
} else if (response.data.respCode === '-1') {
|
|
// Message.error(response.data.message)
|
|
// if (response.data.status == 401) {
|
|
store.commit('savePathUrl', null)
|
|
store.commit('setHandleProcess', null)
|
|
// router.push('/login')
|
|
Message.error(response.data.message)
|
|
// 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');
|
|
// }
|
|
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)
|
|
}
|
|
|
|
// 登录过期
|
|
console.log( error.response.data.status,"error.response")
|
|
if (error.response.data.status == 401) {
|
|
store.commit('savePathUrl', null)
|
|
store.commit('setHandleProcess', null)
|
|
store.commit('removeDicts', 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.response.status === 500) {
|
|
store.commit('savePathUrl', null)
|
|
store.commit('setHandleProcess', null)
|
|
store.commit('removeDicts', 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');
|
|
}
|
|
console.log(error,"302 Found")
|
|
|
|
// 判断超时
|
|
if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1 && !error.config._retry) {
|
|
Message.error(error.message)
|
|
}
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
export { instance, loading }
|